Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Count First Instance of a Value

Posted on 2014-02-18
4
Medium Priority
?
697 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

604 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