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 |
|
FData
Starting Member
24 Posts |
Posted - 2012-08-31 : 04:21:46
|
| Hey guys This is an other stupid question, but one thats puzzling me This is my query SELECT FDMSAccountNo,[Month_end_date] as hst_date_processed,Case when Fee_Code IN ('42B','42C','42D','42E') Then 'PCI'When Fee_Code = '00Y' Then 'Refund Transaction Charge'When salesMI_Group1 IN ('other', 'N/A') and d.repeat_msc = 'Y' then 'Other repeatable'When salesMI_Group1 IN ('other', 'N/A') and d.repeat_msc = 'N' then 'Other Non repeatable'Else salesMI_Group1 end as description,d.repeat_msc,Sum([Retail_amount]) As AmountInto #MscCountFROM FDMS.dbo.Fact_Fee_History fINNER JOINFDMS.dbo.Dim_Fee_Codes D ON f.Fee_Sequence_Number = D.Fee_Codewhere Month_end_date >= dateadd(mm,datediff(mm,0,getdate())-12,0)and Month_end_date < dateadd(mm,datediff(mm,0,getdate()),0)and FDMSAccountNo ='878000039882'Group by FDMSAccountNo, Month_end_date, d.Fee_Code, d.SalesMI_Group1, d.Repeat_MSCselect * from #MscCount--drop table #MscCount, #mscSELECT [FDMSAccountNo],hst_date_processed,case when description ='MSC' then SUM ([Amount])else 0 end as [MSC_2012],case when description ='PCI' then SUM ([Amount])else 0 end as [PCI_2012],case when description ='Refund Transaction Charge' then SUM ([Amount])else 0 end as [RefundTransactionCharge_2012],case when description ='Other repeatable' then SUM ([Amount])else 0 end as [Other_repeatable2012],case when description ='Other Non repeatable' then SUM ([Amount])else 0 end as [Other_Non_repeatable2012]Into #MscFROM #MscCountwhere FDMSAccountNo = '878000039882'group by [FDMSAccountNo],hst_date_processed,descriptionIts returning the following results FDMSAccountNo hst_date_processed MSC_2012 PCI_2012 878000039882 01/08/2011 31.96 0878000039882 01/08/2011 0 3.99 Basically i need to sum, all the hst_date_processed by month together , so i achieve a one liner. so the new layout would be FDMSAccountNo hst_date_processed MSC_2012 PCI_2012878000039882 01/08/2011 31.96 3.99 any ideas ? |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-08-31 : 04:53:07
|
[code][Month_end_date] as hst_date_processe[/code]is the [Month_end_date] always the date of the last day of the month ? like Mar 31, Apr 30, May 31 etc ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
FData
Starting Member
24 Posts |
Posted - 2012-08-31 : 04:59:50
|
| Hi Khtan Month_end_data is always Beginning of the month Eg 01/08/201101/09/201101/10/201101/11/201101/12/201101/01/201201/02/201201/03/201201/04/201201/05/201201/06/201201/07/2012 |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-08-31 : 05:03:47
|
remove the description from the GROUP BYgroup by [FDMSAccountNo],hst_date_processed, description KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
FData
Starting Member
24 Posts |
Posted - 2012-08-31 : 05:09:14
|
| hi khtan when i remove the description i get an error msg saying Msg 8120, Level 16, State 1, Line 2Column '#MscCount.description' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-08-31 : 05:16:21
|
sorry .. try thisSELECT [FDMSAccountNo],hst_date_processed, SUM ( case when description ='MSC' then ([Amount])else 0 end ) as [MSC_2012], SUM ( case when description ='PCI' then ([Amount])else 0 end ) as [PCI_2012], SUM ( case when description ='Refund Transaction Charge' then ([Amount])else 0 end ) as [RefundTransactionCharge_2012], SUM ( case when description ='Other repeatable' then ([Amount])else 0 end ) as [Other_repeatable2012], SUM ( case when description ='Other Non repeatable' then ([Amount])else 0 end ) as [Other_Non_repeatable2012]Into #MscFROM #MscCountwhere FDMSAccountNo = '878000039882'group by [FDMSAccountNo],hst_date_processed KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
FData
Starting Member
24 Posts |
Posted - 2012-08-31 : 05:20:28
|
| you are a star * thank you very much x |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-08-31 : 05:30:42
|
actually you don't need the intermediate temp table. You can use derived table or CTESELECT [FDMSAccountNo], hst_date_processed, SUM ( case when description ='MSC' then ([Amount])else 0 end ) as [MSC_2012], SUM ( case when description ='PCI' then ([Amount])else 0 end ) as [PCI_2012], SUM ( case when description ='Refund Transaction Charge' then ([Amount])else 0 end ) as [RefundTransactionCharge_2012], SUM ( case when description ='Other repeatable' then ([Amount])else 0 end ) as [Other_repeatable2012], SUM ( case when description ='Other Non repeatable' then ([Amount])else 0 end ) as [Other_Non_repeatable2012]FROM( SELECT FDMSAccountNo, [Month_end_date] as hst_date_processed, Case when Fee_Code IN ('42B','42C','42D','42E') Then 'PCI' When Fee_Code = '00Y' Then 'Refund Transaction Charge' When salesMI_Group1 IN ('other', 'N/A') and d.repeat_msc = 'Y' then 'Other repeatable' When salesMI_Group1 IN ('other', 'N/A') and d.repeat_msc = 'N' then 'Other Non repeatable' Else salesMI_Group1 end as description, d.repeat_msc, Sum([Retail_amount]) As Amount FROM FDMS.dbo.Fact_Fee_History f INNER JOIN FDMS.dbo.Dim_Fee_Codes D ON f.Fee_Sequence_Number = D.Fee_Code where Month_end_date >= dateadd(mm,datediff(mm,0,getdate())-12,0) and Month_end_date < dateadd(mm,datediff(mm,0,getdate()),0) and FDMSAccountNo ='878000039882' Group by FDMSAccountNo, Month_end_date, d.Fee_Code, d.SalesMI_Group1, d.Repeat_MSC) dGROUP BY [FDMSAccountNo], hst_date_processed KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
FData
Starting Member
24 Posts |
Posted - 2012-08-31 : 05:38:12
|
| hi Khtan Thank you for your update, i have created a new topic called inner join confusion, If you can take a glance at that, you will be able to tell me whether your post above would be Applicable In that scenario |
 |
|
|
|
|
|
|
|