Home > database >  The SQL statistics on the same day, this week, this month
The SQL statistics on the same day, this week, this month

Time:11-09

Whether a statement statistics out of the day, week, month of data?
Select name, count (quantity) as the from table 1 month where datediff (month, A.S TARTTIME, getdate ())=0 group by name
Select name, count (quantity) as weeks from table 1 where datediff (week, A.S TARTTIME, getdate ())=0 group by name
Select name, count (number), as the from table 1 where datediff (dd, A.S TARTTIME, getdate ())=0 group by name

Or the above result merged into

On week name
Zhang SAN 100 50 10
Li si 49 12 90

CodePudding user response:

SELECT the name,
The SUM (CASE WHEN datediff (month, A.S TARTTIME, getdate ())=0 THEN 1 ELSE 0 END) AS month,
The SUM (CASE WHEN datediff (week, A.S TARTTIME, getdate ())=0 THEN 1 ELSE 0 END) AS weeks,
The SUM (CASE WHEN datediff (dd, A.S TARTTIME, getdate ())=0 THEN 1 ELSE 0 END) AS day
FROM table 1
Group by name

CodePudding user response:

In order to improve the operation efficiency, easy to write code, increase good month in advance in the database table structure, field
  • Related