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 |
masond
Constraint Violating Yak Guru
447 Posts |
Posted - 2013-09-23 : 06:30:48
|
Hi all Hope your wellI have to produce a report, but not sure of the best way to get the required resultsAim - to count how many [FDMSAccountNo] there are per given [Month_end_date], and then do a case when on the[Retail_amount] For eg 10 Fdmsaccountno in JanThose 10 Fdmsaccountno vary in [Retail_amount] I have 5 fdmsaccountno which are between %0 & £52fdmaccounno beterrn £6& £103 fdmsaccountno £10>My query is SELECT [FDMSAccountNo] ,[Month_end_date] ,[Retail_amount] FROM [FDMS].[dbo].[Fact_Fee_History] where [Fee_Sequence_Number] = '32r' and Month_end_date between '2013-01-01' and '2013-12-01' |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-09-23 : 08:34:43
|
quote: Originally posted by masond Hi all Hope your wellI have to produce a report, but not sure of the best way to get the required resultsAim - to count how many [FDMSAccountNo] there are per given [Month_end_date], and then do a case when on the[Retail_amount] For eg 10 Fdmsaccountno in JanThose 10 Fdmsaccountno vary in [Retail_amount] I have 5 fdmsaccountno which are between %0 & £52fdmaccounno beterrn £6& £103 fdmsaccountno £10>My query is SELECT [FDMSAccountNo] ,[Month_end_date] ,[Retail_amount] FROM [FDMS].[dbo].[Fact_Fee_History] where [Fee_Sequence_Number] = '32r' and Month_end_date between '2013-01-01' and '2013-12-01'
Are you looking for something like this?SELECT [Month_end_date] , CASE WHEN [Retail_amount] <= 5 THEN 1 ELSE 0 END AS LessThan5 , CASE WHEN [Retail_amount] > 5 AND [Retail_amount] <= 10 THEN 1 ELSE 0 END AS LessThan10 , CASE WHEN [Retail_amount] > 10 THEN 1 ELSE 0 END AS GreaterThan10FROM [FDMS].[dbo].[Fact_Fee_History]WHERE [Fee_Sequence_Number] = '32r' AND Month_end_date BETWEEN '2013-01-01' AND '2013-12-01'GROUP BY [Month_end_date] |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-09-23 : 11:43:07
|
Should be this i guessSELECT [Month_end_date] , [blue]SUM(CASE WHEN [Retail_amount] <= 5 THEN 1 ELSE 0 END) AS LessThan5 , SUM(CASE WHEN [Retail_amount] > 5 AND [Retail_amount] <= 10 THEN 1 ELSE 0 END) AS LessThan10 , SUM(CASE WHEN [Retail_amount] > 10 THEN 1 ELSE 0 END) AS GreaterThan10, COUNT(FDMSAccountNo) AS AccntCountFROM [FDMS].[dbo].[Fact_Fee_History]WHERE [Fee_Sequence_Number] = '32r' AND Month_end_date BETWEEN '2013-01-01' AND '2013-12-01'GROUP BY [Month_end_date] ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
masond
Constraint Violating Yak Guru
447 Posts |
Posted - 2013-09-23 : 11:50:49
|
HI Visakh16 My end query ended up liek this SELECTFdmsaccountno ,Case when [Retail_amount] between 0 and 4.99 then 1 else 0 end as '£0 to £4.99',Case when [Retail_amount] between 5 and 6.99 then 1 else 0 end as '£5 to £6.99',Case when [Retail_amount] between 7 and 7.49 then 1 else 0 end as '£7 to £7.49',Case when [Retail_amount] between 7.50 and 7.99 then 1 else 0 end as '£7.50 to £7.99',Case when [Retail_amount] between 8 and 9.99 then 1 else 0 end as '£8 to £9.99',Case when [Retail_amount] between 10 and 11.99 then 1 else 0 end as '£10 to 11.99',Case when [Retail_amount] between 12 and 14.99 then 1 else 0 end as '£12 to 14.99',Case when [Retail_amount] between 15 and 19.99 then 1 else 0 end as '£15 to 19.99',Case when [Retail_amount] > 20 then 1 else 0 end as '£20+'FROM [FDMS].[dbo].[Fact_Fee_History]WHERE [Fee_Sequence_Number] = '32r'AND Month_end_date BETWEEN '2013-01-01' AND '2013-12-01'GROUP BY FDMSAccountNo,[Retail_amount] |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-09-23 : 11:54:57
|
Don't you need the aggregate function (SUM) wrapped around each of those case expressions as Visakh pointed out? If you don't have that then the group by retail amount and FDMSAccountNo is equivalent to using a DISTINCT clause, which doesn't seem like the right thing to do here. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-09-23 : 12:06:07
|
quote: Originally posted by masond HI Visakh16 My end query ended up liek this SELECTFdmsaccountno ,Case when [Retail_amount] between 0 and 4.99 then 1 else 0 end as '£0 to £4.99',Case when [Retail_amount] between 5 and 6.99 then 1 else 0 end as '£5 to £6.99',Case when [Retail_amount] between 7 and 7.49 then 1 else 0 end as '£7 to £7.49',Case when [Retail_amount] between 7.50 and 7.99 then 1 else 0 end as '£7.50 to £7.99',Case when [Retail_amount] between 8 and 9.99 then 1 else 0 end as '£8 to £9.99',Case when [Retail_amount] between 10 and 11.99 then 1 else 0 end as '£10 to 11.99',Case when [Retail_amount] between 12 and 14.99 then 1 else 0 end as '£12 to 14.99',Case when [Retail_amount] between 15 and 19.99 then 1 else 0 end as '£15 to 19.99',Case when [Retail_amount] > 20 then 1 else 0 end as '£20+'FROM [FDMS].[dbo].[Fact_Fee_History]WHERE [Fee_Sequence_Number] = '32r'AND Month_end_date BETWEEN '2013-01-01' AND '2013-12-01'GROUP BY FDMSAccountNo,[Retail_amount]
I dont see any need of GROUP BY if you're trying to list out detail data itself (Retail_Amount in GROUP BY)------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|