Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TempVar Assistance - Counting Rows

Status
Not open for further replies.

sap1958

Technical User
Joined
Oct 22, 2009
Messages
138
Location
US
Here is my script:
declare @CashGiving table
(
giftid char(10),
gifteffdat datetime,
giftacctdv char(6)
col int
)
insert into @CashGiving
(giftid,gifteffdat,giftacctdv,col)
select giftid,gifteffdat,giftacctdv,row_number() over(partition by giftid order by giftid) as col
from gifts with (nolock)
where gifttype in ('g','b','y')
order by giftid
The query produces data such as
giftid gifteffdat giftacctdv col First
001 7/1/2006 12 1 Y
001 7/15/2006 12 2 N
001 7/15/2006 14 3 Y
003 7/22/2009 17 1 Y
003 7/30/2006 25 2 N
For each giftid, if an Id appears more than once they get a new row number. I want to create a "First" column that sets to a 'Y' whenever
a. If the ID appears for the first time
b. If the ID remains the same but the giftacctdv changes
 
how about

case when row_number() over(partition by giftid order by giftid) = 1 then 'y' else 'n' end as [first]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top