Solved

Count First Instance of a Value

Posted on 2014-02-18
4
578 Views
Last Modified: 2014-02-18
I have a result set that dumps data as such:

ID      DATE/TIME
AAA1077	2013-01-28 21:50:00.000
AAA1077	2013-01-28 21:50:00.000
AAA1077	2013-01-28 21:50:00.000
AAA1077	2013-01-28 21:50:00.000
AAAA055	2013-01-03 09:05:00.000
AAAA055	2013-01-03 09:05:00.000
AAAA055	2013-01-03 09:05:00.000
AAAA055	2013-01-03 09:05:00.000
AAAB405	2013-01-03 22:17:00.000
AAAB405	2013-01-03 22:17:00.000
AAAB405	2013-01-03 22:17:00.000
AAAD127	2013-01-05 23:02:00.000
AAAD127	2013-01-05 23:02:00.000
AAAD127	2013-01-05 23:02:00.000
AAAD127	2013-01-05 23:02:00.000
AAAE160	2013-01-06 20:37:00.000
AAAE160	2013-01-06 20:37:00.000
AAAE160	2013-01-06 20:37:00.000

Open in new window


How can I create a 3rd column and have it flag a 1 for this first occurrence of each Id and then 0's for and duplicates?

I know I can COUNT DISTINCT, but I need this format mentioned for the presentation layer.
0
Comment
Question by:LCNW
  • 2
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39868702
check this out

declare @T TABLE 
(
ID VARCHAR(30), dt DATETIME
)
INSERT INTO @T VALUES 
('AAA1077','2013-01-28 21:50:00.000')
,('AAA1077','2013-01-28 21:50:00.000')
,('AAA1077','2013-01-28 21:50:00.000')
,('AAA1077','2013-01-28 21:50:00.000')
,('AAAA055','2013-01-03 09:05:00.000')
,('AAAA055','2013-01-03 09:05:00.000')
,('AAAA055','2013-01-03 09:05:00.000')
,('AAAA055','2013-01-03 09:05:00.000')
,('AAAB405','2013-01-03 22:17:00.000')
,('AAAB405','2013-01-03 22:17:00.000')
,('AAAB405','2013-01-03 22:17:00.000')
,('AAAD127','2013-01-05 23:02:00.000')
,('AAAD127','2013-01-05 23:02:00.000')
,('AAAD127','2013-01-05 23:02:00.000')
,('AAAD127','2013-01-05 23:02:00.000')
,('AAAE160','2013-01-06 20:37:00.000')
,('AAAE160','2013-01-06 20:37:00.000')
,('AAAE160','2013-01-06 20:37:00.000')

;WITH C AS
(
SELECT *,ROW_NUMBER() OVER(partition by ID order by dt) rn FROM @T
)
SELECT id,dt,CASE RN WHEN 1 THEN 1 ELSE 0 END AS RN FROM C

Open in new window

0
 
LVL 1

Author Comment

by:LCNW
ID: 39868718
Thanks. I tweaked it to fit and it worked.

CASE WHEN ROW_NUMBER() OVER(partition by Id order by DateTime) = 1 THEN 1 ELSE 0 END AS [Flagged] 

Open in new window

0
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39868728
Cool.
0
 
LVL 1

Author Closing Comment

by:LCNW
ID: 39868730
Thanks.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now