Author |
Topic |
sz1
Aged Yak Warrior
555 Posts |
Posted - 2013-01-08 : 08:47:27
|
Hi,I'm trying to add another top 6 case statement to calulate the day before stats for a category field. Can this be included in the case statement?Select Top 6 c.INTI_CATEGORY, count(c.ID) As TotalCat, --actual resultsCase When c.OCCURED_DT = DATEADD("d",-1,GETDATE()) --calulate yesterday resultsThen count(c.ID)End AS TotalCatYesterdayFrom DIM_CALL cWhere TYPE = 'Incident'And c.OPEN_FLAG = 1 and c.ETL_CURRENT =1Group by c.INTI_CATEGORY ,c.OCCURED_DTOrder By TotalCat DescSZ1Learning and development is the driving force in the universe...! |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-01-08 : 23:53:36
|
sorry didnt get that. can you illustrate with sample data?you'll only get 6 rows from above statement so didnt understand need of another top 6 over this!------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
sz1
Aged Yak Warrior
555 Posts |
Posted - 2013-01-09 : 03:09:12
|
HiI need to include another top 6 for previous day too if this is possible.The data I get back is 6 rows by category showing the top highest categories. I also want to be able to see the previous days top 6 categories to be able to compare and see whats gone up and down in the list. So I was thinking if I add to the code below an extra Top 6 case statement for the previous day I could get the results?can this be achieved in the same query? or do I need 2 seperate datasets...Maybe something like this:--todays resultsSelect Top 6 c.INTI_CATEGORY, count(c.ID) As TotalCatToday, --yesterdays resultsCase When c.OCCURED_DT <= DATEADD("d",-1,GETDATE())Then count(c.ID)End AS TotalCatYesterdayFrom DIM_CALL cWhere TYPE = 'Incident'And c.OPEN_FLAG = 1 and c.ETL_CURRENT =1Group by c.INTI_CATEGORY ,c.OCCURED_DTOrder By TotalCat DescResults: TotalCatYesteday is showing same results as TotalCatToday even though the dateadd is <= previous day?any ideas?INTI_CATEGORY TotalCatToday Date TotalCatYesterday-------------------------------- ------------- ----------------------- ----------------- Central 129 2013-01-08 09:16:11.537 129Local 93 2013-01-08 09:16:11.537 93Core Applications 74 2013-01-08 09:16:11.537 74Printers 54 2013-01-08 09:16:11.537 54Mail Services 23 2013-01-08 09:16:11.537 23Thin Client 21 2013-01-08 09:16:11.537 21 (6 row(s) affected)SZ1Learning and development is the driving force in the universe...! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-01-09 : 03:31:18
|
do you mean getting previous ranks for top 6 items today or do you mean getting top 6 items for today and yesterday side by side?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
sz1
Aged Yak Warrior
555 Posts |
Posted - 2013-01-09 : 04:04:10
|
Top 6 rank today and yesterday side by side, is this possible in same query?...been trying to just pull the records for yesterday using the occured date field below to exclude calls less than datetime but I'm struggling a bit to get them side by sideSelect Top 6 c.INTI_CATEGORY, count(c.ID) As TotalCatFrom DIM_CALL cWhere TYPE = 'Incident'And c.OPEN_FLAG = 1 and c.ETL_CURRENT =1And c.OCCURED_DT <= '2013-01-08 23:59:59' /*And c.OCCURED_DT <= '2013-01-08 23:59:59'*/--been trying to just get figure here --DATEADD(d,-1,GetDate())Group by c.INTI_CATEGORYOrder By TotalCat DescSZ1Learning and development is the driving force in the universe...! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-01-09 : 04:23:28
|
[code]SELECT Seq,MAX(CASE WHEN DATEDIFF(dd,0,c.OCCURED_DT)= DATEDIFF(dd,0,GETDATE()) THEN INTI_CATEGORY END) AS TodayCat,MAX(CASE WHEN DATEDIFF(dd,0,c.OCCURED_DT)= DATEDIFF(dd,1,GETDATE()) THEN INTI_CATEGORY END) AS YestCatFROM(Select c.INTI_CATEGORY, count(c.ID) As TotalCat,ROW_NUMBER() OVER (PARTITION BY DATEADD(dd,DATEDIFF(dd,0,c.OCCURED_DT),0) ORDER BY count(c.ID) DESC) AS SeqFrom DIM_CALL cWhere TYPE = 'Incident'And c.OPEN_FLAG = 1 and c.ETL_CURRENT =1And c.OCCURED_DT >= '2013-01-08' And c.OCCURED_DT < '2013-01-10'Group by c.INTI_CATEGORY,DATEADD(dd,DATEDIFF(dd,0,c.OCCURED_DT),0))tWHERE Seq<=6GROUP BY Seq[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
sz1
Aged Yak Warrior
555 Posts |
Posted - 2013-01-09 : 04:35:08
|
I'm getting c.OCCURED_DT cant be bound on the first 2 belowMAX(CASE WHEN DATEDIFF(dd,0,c.OCCURED_DT)= DATEDIFF(dd,0,GETDATE()) THEN INTI_CATEGORY END) AS TodayCat,MAX(CASE WHEN DATEDIFF(dd,0,c.OCCURED_DT)= DATEDIFF(dd,1,GETDATE()) THEN INTI_CATEGORY END) AS YestCatAlso, I see you have entered actual dates, is it possible for the query to recalculate on a daily basis?ThanksSZ1Learning and development is the driving force in the universe...! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-01-09 : 04:37:56
|
[code]SELECT Seq,MAX(CASE WHEN dt = DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0) THEN INTI_CATEGORY END) AS TodayCat,MAX(CASE WHEN dt= DATEADD(dd,DATEDIFF(dd,1,GETDATE()),0) THEN INTI_CATEGORY END) AS YestCatFROM(Select c.INTI_CATEGORY, count(c.ID) As TotalCat,DATEADD(dd,DATEDIFF(dd,0,c.OCCURED_DT),0) AS dt,ROW_NUMBER() OVER (PARTITION BY DATEADD(dd,DATEDIFF(dd,0,c.OCCURED_DT),0) ORDER BY count(c.ID) DESC) AS SeqFrom DIM_CALL cWhere TYPE = 'Incident'And c.OPEN_FLAG = 1 and c.ETL_CURRENT =1And c.OCCURED_DT >= DATEADD(dd,DATEDIFF(dd,0,GETDATE()),-1) And c.OCCURED_DT < DATEADD(dd,DATEDIFF(dd,0,GETDATE()),1)Group by c.INTI_CATEGORY,DATEADD(dd,DATEDIFF(dd,0,c.OCCURED_DT),0))tWHERE Seq<=6GROUP BY Seq[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
sz1
Aged Yak Warrior
555 Posts |
Posted - 2013-01-09 : 04:50:06
|
Its not returning what I would expect to see...this the what I have already, what I would like to see is the same for TotalCatYest, so CategoryYest and TotalCatYest in next 2 columns.Category TotalCatToday Central 129 Local 93 Core Applications 74 Printers 54 Mail Services 23 Thin Client 21 Your result shows this, also strangely you have one different from my one where Domain Services is Printers.1 NULL Central2 NULL Local3 NULL Core Applications4 NULL Domain Services5 NULL Mail Services6 NULL Thin ClientSZ1Learning and development is the driving force in the universe...! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-01-09 : 05:26:57
|
whats the significance of OPEN_FLAG = 1 and c.ETL_CURRENT=1?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
sz1
Aged Yak Warrior
555 Posts |
Posted - 2013-01-09 : 05:37:05
|
OPEN_FLAG = 1? means all calls that are open and c.ETL_CURRENT=1? means using current up to date dataset information, if not it will return all records and we only want the current ones that are open.Basically is just means show me everything that is open and valid.SZ1Learning and development is the driving force in the universe...! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-01-09 : 05:59:25
|
are you sure you'll current day records with these values returned by query?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
sz1
Aged Yak Warrior
555 Posts |
Posted - 2013-01-09 : 06:08:39
|
This returns the top 6 current records, so 2 columns one with the cat name and the other with the cat total. So really just need to include the previous days records side by side or even on its own query for now.--return top 6 onlySelect Top 6 c.INTI_CATEGORY, count(c.ID) As TotalCatFrom DIM_CALL cWhere TYPE = 'Incident'And c.OPEN_FLAG = 1 and c.ETL_CURRENT =1Group by c.INTI_CATEGORYOrder By TotalCat DescSZ1Learning and development is the driving force in the universe...! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-01-09 : 06:28:56
|
how can you guarantee that same categories will come in top 6 in previous day too?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
sz1
Aged Yak Warrior
555 Posts |
Posted - 2013-01-09 : 06:53:14
|
The top 6 count of ID and grouping of the category returns the top highest records, if all the records that are today are left out by only looking at occured date yesterday and backwards from there I would have thought that would have calculated the top 6 previous records only correctly? leaving out the current days selection. Otherwsie the only other way I can think of is to aggregate all the new current records and subtract these from all other open records from the previous days figure.The categories may have changed slightly from previous day too, so the may differ slightly if a change has happened.Does that help?Also, I can check yesterdays results as I have a record, so can I change this query to just look at yesterday results leaving out todays by using the OCCURRED_DT field, if I have to have 2 queries one for today and one for yesterday I can work with that.--return top 6 onlySelect Top 6 c.INTI_CATEGORY, count(c.ID) As TotalCatFrom DIM_CALL cWhere c.OCCURED_DT < DateAdd(dd,0,GETDATE())-1and TYPE = 'Incident'And c.OPEN_FLAG = 1 and c.ETL_CURRENT =1Group by c.INTI_CATEGORYOrder By TotalCat DescAlso, here I've asked for yesterdays date but the date is not doing anything, I need to bind it to the category count?Declare @Date1 DateTimeSet @Date1 = '2013-01-08'--return top 6 onlySelect Top 6 c.INTI_CATEGORY, count(c.ID) As TotalCat,@Date1From DIM_CALL c--Where c.OCCURED_DT <= '2013-01-08' -- < DateAdd(dd,0,GETDATE())-1where TYPE = 'Incident'And c.OPEN_FLAG = 1 and c.ETL_CURRENT =1Group by c.INTI_CATEGORYOrder By TotalCat DescSZ1Learning and development is the driving force in the universe...! |
|
|
|