![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hello everybody. I need help with my stats report.I have following query . DECLARE @DateFrom DATETIME; DECLARE @DateTo DATETIME; DECLARE @Provider INT; SET @DateFrom='2009-12-01 00:00:00.0'; SET @DateTo='2009-12-01 23:55:00.0'; SET @Provider=6; SELECT SUM(case when AUDIT_CHANGES LIKE '%A%' then 1 else 0 end) AS [A], SUM(case when AUDIT_CHANGES LIKE '%B1%' then 1 else 0 end) AS [B1], SUM(case when AUDIT_CHANGES LIKE '%B2%' then 1 else 0 end) AS [B2], SUM(case when AUDIT_CHANGES LIKE '%B3%' then 1 else 0 end) AS [B3], SUM(case when AUDIT_CHANGES LIKE '%B4%' then 1 else 0 end) AS [B4], SUM(case when AUDIT_CHANGES LIKE '%B5%' then 1 else 0 end) AS [B5], FROM AUDIT_TRAIL WHERE PROVIDER_ID = @Provider AND TIME_STAMP BETWEEN @DateFrom AND @DateTo AND AUDIT_CHANGES IS NOT NULL Provider A B1 B2 B3 B4 B5 XXX 0 23 7 10 8 8 This report gives me stats for one day(12.01.09).I need to get results for from 11.01.09 thru 11.30.09 for each weekday. I have SQL Server 2005 . Thank you |
#3
| |||
| |||
|
|
On Tue, 8 Dec 2009 12:18:30 -0800 (PST), hayko98 wrote: Hello everybody. I need help with my stats report.I have following query . DECLARE @DateFrom DATETIME; DECLARE @DateTo DATETIME; DECLARE @Provider INT; SET @DateFrom='2009-12-01 00:00:00.0'; SET @DateTo='2009-12-01 23:55:00.0'; SET @Provider=6; SELECT SUM(case when AUDIT_CHANGES LIKE '%A%' * then 1 else 0 end) AS [A], SUM(case when AUDIT_CHANGES LIKE '%B1%' *then 1 else 0 end) AS [B1], SUM(case when AUDIT_CHANGES LIKE '%B2%' *then 1 else 0 end) AS [B2], SUM(case when AUDIT_CHANGES LIKE '%B3%' *then 1 else 0 end) AS [B3], SUM(case when AUDIT_CHANGES LIKE '%B4%' *then 1 else 0 end) AS [B4], SUM(case when AUDIT_CHANGES LIKE '%B5%' *then 1 else 0 end) AS [B5], FROM *AUDIT_TRAIL WHERE *PROVIDER_ID = @Provider AND * * * * * * * * * * * * * *TIME_STAMP BETWEEN @DateFrom AND @DateTo AND * * AUDIT_CHANGES IS NOT NULL Provider * *A * * * B1 * * *B2 * * *B3 * * *B4 * * *B5 XXX 0 * * * 23 * * *7 * * * 10 * * *8 * * * 8 This report gives me stats for one day(12.01.09).I need to get results for from 11.01.09 thru 11.30.09 for each weekday. I have SQL Server 2005 . Thank you Hi Hayko, Something like this, I guess: SELECT * DATEADD(day, * * * * * * * * *DATEDIFF(day, '20000101', TIME_STAMP), * * * * * * * * '20000101') AS Day, * * * * *PROVIDER_ID AS Provider, * * * * *SUM(case when AUDIT_CHANGES LIKE '%A%' * * * * * * * * * then 1 else 0 end) AS [A], * * * * *SUM(case when AUDIT_CHANGES LIKE '%B1%' * * * * * * * * * then 1 else 0 end) AS [B1], * * * * *SUM(case when AUDIT_CHANGES LIKE '%B2%' * * * * * * * * * then 1 else 0 end) AS [B2], * * * * *SUM(case when AUDIT_CHANGES LIKE '%B3%' * * * * * * * * * then 1 else 0 end) AS [B3], * * * * *SUM(case when AUDIT_CHANGES LIKE '%B4%' * * * * * * * * * then 1 else 0 end) AS [B4], * * * * *SUM(case when AUDIT_CHANGES LIKE '%B5%' * * * * * * * * * then 1 else 0 end) AS [B5] FROM * * AUDIT_TRAIL WHERE * *TIME_STAMP >= '20091101' AND * * *TIME_STAMP < *'20091201' AND * * *AUDIT_CHANGES IS NOT NULL GROUP BY DATEDIFF(day, '20000101', TIME_STAMP), PROVIDER_ID; -- Hugo Kornelis, SQL Server MVP My SQL Server blog:http://sqlblog.com/blogs/hugo_kornelis- Hide quoted text - - Show quoted text - |
#4
| |||
| |||
|
|
Hi Hugo. After running your query i am getting a lot of duplicate dates: * * * 11/1/2009 1 0 0 0 0 0 0 11/1/2009 1 0 0 0 0 0 0 11/1/2009 1 0 0 0 1 0 0 11/1/2009 1 0 0 0 0 0 0 11/1/2009 1 0 0 0 0 0 0 11/1/2009 1 0 0 0 0 0 0 11/1/2009 1 0 0 0 0 1 0 11/1/2009 1 0 0 0 0 0 0 11/1/2009 1 0 0 0 0 0 0 * * * * Is there any way to group them by date?Also if you can explain what does DATEADD(day, DATEDIFF(day, '20000101', TIME_STAMP), '20000101') AS Day do and why 20000101? |
#5
| |||
| |||
|
|
Hi Hugo. After running your query i am getting a lot of duplicate dates: |
|
Also if you can explain what does DATEADD(day, DATEDIFF(day, '20000101', TIME_STAMP), '20000101') AS Day do and why 20000101? |
#6
| |||
| |||
|
|
On Tue, 8 Dec 2009 14:32:09 -0800 (PST), hayko98 wrote: Hi Hugo. After running your query i am getting a lot of duplicate dates: Hi Hayko, Did you include the PROVIDER_ID column in the SELECT list? And did you include the GROUP BY clause? (Note that if you select a part of the statement in Management Studio, only that part will be executed). If both questions are answered with "yes", I'll need more information. And most of all, I'll need a way to reproduce the behaviour on my test machine. Please seewww.aspfaq.com/5006for an overview of the information I'll need to debug the problem. Also if you can explain what does DATEADD(day, * * * * * * * * DATEDIFF(day, '20000101', TIME_STAMP), * * * * * * * *'20000101') AS Day do and why 20000101? Well, the "DATEDIFF(day, '20000101', TIME_STAMP)" calculates the number of day boundaries between the date in the TIME_STAMP column and the reference date (1/1/2000 in this case). This is used to group stats per date, since the result will be the same for all stats of the same day, regardless how late they were. That's why I included this expression in the GROUP BY. And "DATEADD(day, (above expression), '200010101') adds that number of days to the reference date, resulting in the day of the TIME_STAMP but without the time (or rather, since a datetime always has a time part, with the time part set to midnight). So this displays the date of the event, but not the time. The choice of 20000101 as a reference date is just because I needed a date. Any other date would have done as well. But do remember that the same reference date has to be used in the DATEDIFF and the DATEADD! -- Hugo Kornelis, SQL Server MVP My SQL Server blog:http://sqlblog.com/blogs/hugo_kornelis |
#7
| |||
| |||
|
|
Hugo: Yes to both of your questions and i could not connect to the link that you attached (The requested URL /www.aspfaq.com/5006 was not found on this server. ) |
![]() |
| Thread Tools | |
| Display Modes | |
| |