Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
sz1
Aged Yak Warrior
555 Posts |
Posted - 2014-01-20 : 10:30:41
|
HiIm trying to add up all the rows for each category which Ive done below but then I want to only show in the CTE the total of all categories and not all the counts, so just the max total for each one.The CTE is showing more than one category per max count on the Max(MyRank). Is there a way to keep the CTE but get it to show me only the max total for each single category?ThanksCode to work out totalsSELECT [TypeOfIncident] ,[Category] ,ROW_NUMBER()OVER (PARTITION BY [Category] order by [Category]) 'MyRank' --,[SubCategory] FROM [dbo].[Incident]WHERE [CreatedDateTime] BETWEEN '2013-01-20 00:00:00' AND '2014-01-20 23:59:59'and [TypeOfIncident] = 'Failure'Code to work out MaxUSE TestgoWITH CTE_MyRank as (SELECT [TypeOfIncident] ,[Category] ,ROW_NUMBER()OVER (PARTITION BY [Category] order by [Category]) 'MyRank' --,[SubCategory] FROM [dbo].[Incident]WHERE [CreatedDateTime] BETWEEN '2013-01-20 00:00:00' AND '2014-01-20 23:59:59'and [TypeOfIncident] = 'Failure')SELECT TypeOfIncident, (select distinct[Category]), Max(MyRank) as MaxTotal FROM CTE_MyRankGROUP BY [TypeOfIncident], [Category], [MyRank])SZ1Please help me to enable me to help others! |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2014-01-20 : 22:29:04
|
can you post some sample data and the expected result ? KH[spoiler]Time is always against us[/spoiler] |
|
|
sz1
Aged Yak Warrior
555 Posts |
Posted - 2014-01-21 : 05:04:54
|
Sure I can get the max for each by using a count of and group by but wanted to know if I can do the same on a max Row_Number as shown below, currently the MaxTotal is returning a 1 instead of the actual max per category.Id like to see the MaxTotal return the same as Category Total?TaUSE TestgoWITH CTE_MyRank as (SELECT [TypeOfIncident] ,Count([Category]) 'Category' ,ROW_NUMBER()OVER (PARTITION BY [Category] order by [Category]) 'MyRank' --,[SubCategory] FROM [dbo].[Incident]WHERE [CreatedDateTime] BETWEEN '2013-01-20 00:00:00' AND '2014-01-20 23:59:59'and [TypeOfIncident] = 'Failure'Group By [TypeOfIncident], [Category])SELECT TypeOfIncident, [Category], max(MyRank) as MaxTotal FROM CTE_MyRankGROUP BY [TypeOfIncident], [Category], [MyRank]--ORDER BY MyRank;TypeOfIncident Category MaxTotalFailure 1 1Failure 2 1Failure 14 1Failure 26 1Failure 347 1Failure 748 1Failure 1261 1Failure 1289 1Failure 1623 1Failure 2877 1Failure 3303 1Failure 3394 1SZ1Please help me to enable me to help others! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2014-01-21 : 07:15:02
|
so according to your rules what should you get as values for MaxTotal field?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
sz1
Aged Yak Warrior
555 Posts |
Posted - 2014-01-21 : 08:54:00
|
The same figure to the left of the 1 values, when you run the query without the CTE the rows will add numbers tallying to the amount per category, so I thought I could return the Max of each row number max output. SO if there are 10 oranges then I would see 1,2,3,4,5,6,7,8,9,10 and if 3 apples I would see 1,2,3, what I want to see in the column would be 10 and 3. This works with the count and group by as seen on the output but the right column with values of 1 I want the same output if this is possible. Its really for future use as it works with the count...thanksSZ1Please help me to enable me to help others! |
|
|
|
|
|
|
|