I just used it when I was working on a project
User table: User ID, Username, Balance
Flow meter: time, user ID, user name, type (0 recharge, 1 consumption), change amount
Now to check the daily sales and monthly sales of each user, the easiest way is to check all users first, and then select sum(changemoney) from liushui where userid=? in the for loop in the c# code
Now I want to try a SQL statement and find it out. After searching, I get the following SQL statement:
WITH DailySales AS( select [user].id,[user].username,[user].balance, case when sum([liushui].changemoney) is NULL then 0 else sum([liushui].changemoney) end as ri_xse from [user] left join [liushui] on [user].id=[liushui].userid and [liushui].type=1 and [liushui].createtime between '2017-07-11 00:00:00' and '2017-07-11 23:59:59' group by [user].id,[user].username,[user].balance ) ,MonthSales AS( select [user].id,[user].username,[user].balance, case when sum([liushui].changemoney) is NULL then 0 else sum([liushui].changemoney) end as yue_xse from [user] left join [liushui] on [user].id=[liushui].userid and [liushui].type=1 and [liushui].createtime between '2017-07-01 00:00:00' and '2017-07-31 23:59:59' group by [user].id,[user].username,[user].balance ) select d.id,d.username,d.balance,d.ri_xse,m.yue_xse from DailySales D inner join MonthSales M on D.id = M.id
The result is as follows:
After finding out, netizens suggested that it is best to divide the water meter into separate meters, such as one meter per month, otherwise the water meter will be very large in the future. . .
Do this first. .