Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Lifetime value analysis
Message
 
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
00682635
Message ID:
00682836
Views:
11
You got me. <g>
Here's Sql Server version.
-- Get first timers for 1997 
SELECT AcctNum, SUM(Amount) AS Amount, 1997 AS giftyear 
  FROM mytable 
  WHERE GiftDate BETWEEN '1997/01/01' AND '1997/12/31 23:59:59' 
	  AND AcctNum NOT IN ( 
        SELECT AcctNum FROM mytable WHERE GiftDate < '1997/01/01' )
  GROUP BY AcctNum 

-- The same group's contributions after 1997
SELECT AcctNum, SUM(Amount) AS Amount, DATEPART(Year, GiftDate) AS giftyear 
  FROM mytable 
  WHERE GiftDate > '1997/12/31 23:59:59' 
   AND AcctNum IN ( 
		SELECT AcctNum  FROM mytable 
	  		WHERE GiftDate BETWEEN '1997/01/01' AND '1997/12/31 23:59:59' )
	AND AcctNum NOT IN ( 
        		SELECT AcctNum FROM mytable WHERE GiftDate < '1997/01/01' )
  GROUP BY AcctNum, DATEPART(Year, GiftDate)
>Cool VFP code <bg>
>
>The queries will perform better if you change the WHERE YEAR(giftDate) < 1997 to WHERE giveDate < '19970101'. This way, SQL Server won't do a table scan.
>
>>Something like
SELECT AcctNum, SUM(Amount) AS Amount, 1997 AS year ;
>>  FROM mytable ;
>>  WHERE Year(GiftDate) = 1997 ;
>>   AND AcctNum NOT IN ( ;
>>      SELECT AcctNum FROM mytable ;
>>          WHERE Year(GiftDate) < 1997 ;
>>  GROUP BY AcctNum ;
>>  INTO CURSOR crsGift1997 NOFILTER
>>
>>SELECT AcctNum, SUM(Amount) AS Amount, Year(GiftDate) AS year ;
>>  FROM mytable ;
>>  WHERE Year(GiftDate) > 1997 ;
>>   AND AcctNum IN ( ;
>>      SELECT AcctNum FROM crsGift1997 );
>>  GROUP BY AcctNum, Year ;
>>  INTO CURSOR crsGiftAfter NOFILTER
>>
>>
< snip >
--sb--
Previous
Reply
Map
View

Click here to load this message in the networking platform