Home All Groups Group Topic Archive Search About

Re: Advice on SQL statement please.

Author
29 Jun 2006 8:17 PM
Tracy McKibben
Tracy McKibben wrote:
Show quote
> Russell Verdun wrote:
>> I have a query that generates the dataset below, based on the year being
>> filtered I get the sum of an amount Group By the type. What I would
>> like to
>> do is use the exact qry using a differnet date, to generate a third
>> column
>> called Prior12Mnths. How would I use my qry to accomplish this task.
>> I appreciate the help.
>>
>> Here's my qry:
>>
>> Select GroupType, Sum(SumRevAmt) as Last12Mnths
>> from MyQRY
>> Where Period = '200006'
>> Group by GroupType
>>
>>
>>
>>
>>
>> Type                    Last12Mnths_200006       Prior12Mnths_199906
>>
>> Airlines                  1234.50                                   
>> ??????
>> Concessions          73854.00                                   ??????
>> etc........
>>
>>
>>
>
> It's not clear from your example what this "other date" would be, so
> I'll use a different example.  Say I have a table containing
> transactions, and each transaction consists of an account, a transaction
> date, and an amount:
>
> SELECT
>    account,
>    SUM(CASE WHEN DATEDIFF(m, transdate, GETDATE()) <= 12 THEN amount
> ELSE 0) AS Last12Months,
>    SUM(CASE WHEN DATEDIFF(m, transdate, GETDATE()) BETWEEN 13 AND 24
> THEN amount ELSE 0) AS Prev12Months
> FROM table
> GROUP BY account
>
> Is that enough to get you started?
>

Sorry, those CASE statements are missing ENDs...

AddThis Social Bookmark Button