Select the first instance of a record











up vote
8
down vote

favorite
7












I have a table, myTable that has two fields in it ID and patientID. The same patientID can be in the table more than once with a different ID. How can I make sure that I get only ONE instance of every patientID.?



EDIT: I know this isn't perfect design, but I need to get some info out of the database and today and then fix it later.










share|improve this question






















  • Are there any criteria to decide which record to return or do you want to return any one at random?
    – Panagiotis Kanavos
    Jun 16 '12 at 14:57












  • top 1 only returns one record. I need exactly one instance of every patient ID. It doesn't matter which tblClaims.id is left out.
    – wootscootinboogie
    Jun 16 '12 at 15:00






  • 1




    Then solution using a CTE is the way to go
    – Panagiotis Kanavos
    Jun 16 '12 at 15:05















up vote
8
down vote

favorite
7












I have a table, myTable that has two fields in it ID and patientID. The same patientID can be in the table more than once with a different ID. How can I make sure that I get only ONE instance of every patientID.?



EDIT: I know this isn't perfect design, but I need to get some info out of the database and today and then fix it later.










share|improve this question






















  • Are there any criteria to decide which record to return or do you want to return any one at random?
    – Panagiotis Kanavos
    Jun 16 '12 at 14:57












  • top 1 only returns one record. I need exactly one instance of every patient ID. It doesn't matter which tblClaims.id is left out.
    – wootscootinboogie
    Jun 16 '12 at 15:00






  • 1




    Then solution using a CTE is the way to go
    – Panagiotis Kanavos
    Jun 16 '12 at 15:05













up vote
8
down vote

favorite
7









up vote
8
down vote

favorite
7






7





I have a table, myTable that has two fields in it ID and patientID. The same patientID can be in the table more than once with a different ID. How can I make sure that I get only ONE instance of every patientID.?



EDIT: I know this isn't perfect design, but I need to get some info out of the database and today and then fix it later.










share|improve this question













I have a table, myTable that has two fields in it ID and patientID. The same patientID can be in the table more than once with a different ID. How can I make sure that I get only ONE instance of every patientID.?



EDIT: I know this isn't perfect design, but I need to get some info out of the database and today and then fix it later.







sql sql-server-2008






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jun 16 '12 at 14:54









wootscootinboogie

3,3272781156




3,3272781156












  • Are there any criteria to decide which record to return or do you want to return any one at random?
    – Panagiotis Kanavos
    Jun 16 '12 at 14:57












  • top 1 only returns one record. I need exactly one instance of every patient ID. It doesn't matter which tblClaims.id is left out.
    – wootscootinboogie
    Jun 16 '12 at 15:00






  • 1




    Then solution using a CTE is the way to go
    – Panagiotis Kanavos
    Jun 16 '12 at 15:05


















  • Are there any criteria to decide which record to return or do you want to return any one at random?
    – Panagiotis Kanavos
    Jun 16 '12 at 14:57












  • top 1 only returns one record. I need exactly one instance of every patient ID. It doesn't matter which tblClaims.id is left out.
    – wootscootinboogie
    Jun 16 '12 at 15:00






  • 1




    Then solution using a CTE is the way to go
    – Panagiotis Kanavos
    Jun 16 '12 at 15:05
















Are there any criteria to decide which record to return or do you want to return any one at random?
– Panagiotis Kanavos
Jun 16 '12 at 14:57






Are there any criteria to decide which record to return or do you want to return any one at random?
– Panagiotis Kanavos
Jun 16 '12 at 14:57














top 1 only returns one record. I need exactly one instance of every patient ID. It doesn't matter which tblClaims.id is left out.
– wootscootinboogie
Jun 16 '12 at 15:00




top 1 only returns one record. I need exactly one instance of every patient ID. It doesn't matter which tblClaims.id is left out.
– wootscootinboogie
Jun 16 '12 at 15:00




1




1




Then solution using a CTE is the way to go
– Panagiotis Kanavos
Jun 16 '12 at 15:05




Then solution using a CTE is the way to go
– Panagiotis Kanavos
Jun 16 '12 at 15:05












3 Answers
3






active

oldest

votes

















up vote
23
down vote



accepted










You could use a CTE with ROW_NUMBER function:



WITH CTE AS(
SELECT myTable.*
, RN = ROW_NUMBER()OVER(PARTITION BY patientID ORDER BY ID)
FROM myTable
)
SELECT * FROM CTE
WHERE RN = 1





share|improve this answer

















  • 4




    I would upvote this answer ten times if it was possible.
    – AFract
    Oct 29 '15 at 9:34










  • Good answer @TimSchmelter. The same output can also be achieved using a sub-query: select * from ( SELECT myTable.* , RN = ROW_NUMBER()OVER(PARTITION BY patientID ORDER BY ID) FROM myTable ) as myPatient where RN = 1
    – Syed
    Nov 1 at 0:12




















up vote
3
down vote













It sounds like you're looking for DISTINCT:



SELECT DISTINCT patientID FROM myTable


you can get the same "effect" with GROUP BY:



SELECT patientID FROM myTable GROUP BY patientID





share|improve this answer





















  • Not exactly. The patient ID can show up in the table more than once with a diffierent tblclaims.id. So if i select distinct, that makes that I get that patientID returned more than once
    – wootscootinboogie
    Jun 16 '12 at 14:58










  • @wootscootinboogie only if you add more fields to the projections you're selecting. DISTINCT of only patientID should give you what you need. if you do DISTINCT patiendID, ID then yes, you will get distinct combinations thus more than one patientID
    – Pavel Veller
    Jun 16 '12 at 15:00


















up vote
0
down vote













The simple way would be to add LIMIT 1 to the end of your query. This will ensure only a single row is returned in the result set.






share|improve this answer

















  • 1




    LIMIT is MySql, isn't it?
    – Rango
    Jun 16 '12 at 14:59










  • yes, limit isn't in sql server
    – wootscootinboogie
    Jun 16 '12 at 14:59










  • it's SELECT TOP 1 ... in MSSQL
    – Pavel Veller
    Jun 16 '12 at 15:01






  • 2




    @PavelVeller: But i assume that TOP 1 is not what OP is looking for. He want just one record for every different patientID.
    – Rango
    Jun 16 '12 at 15:02










  • Okay, so LIMIT is available in MySQL and Drizzle, but doesn't SQL Server have something to similar effect? In fact it does, in a roundabout way. You can set up your query in a subquery using ROW_NUMBER(), and then limit the selection using the generated row numbers. Source: blogs.msdn.com/b/sqlserver/archive/2006/10/25/…
    – Lady Serena Kitty
    Jun 16 '12 at 15:05













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11064473%2fselect-the-first-instance-of-a-record%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
23
down vote



accepted










You could use a CTE with ROW_NUMBER function:



WITH CTE AS(
SELECT myTable.*
, RN = ROW_NUMBER()OVER(PARTITION BY patientID ORDER BY ID)
FROM myTable
)
SELECT * FROM CTE
WHERE RN = 1





share|improve this answer

















  • 4




    I would upvote this answer ten times if it was possible.
    – AFract
    Oct 29 '15 at 9:34










  • Good answer @TimSchmelter. The same output can also be achieved using a sub-query: select * from ( SELECT myTable.* , RN = ROW_NUMBER()OVER(PARTITION BY patientID ORDER BY ID) FROM myTable ) as myPatient where RN = 1
    – Syed
    Nov 1 at 0:12

















up vote
23
down vote



accepted










You could use a CTE with ROW_NUMBER function:



WITH CTE AS(
SELECT myTable.*
, RN = ROW_NUMBER()OVER(PARTITION BY patientID ORDER BY ID)
FROM myTable
)
SELECT * FROM CTE
WHERE RN = 1





share|improve this answer

















  • 4




    I would upvote this answer ten times if it was possible.
    – AFract
    Oct 29 '15 at 9:34










  • Good answer @TimSchmelter. The same output can also be achieved using a sub-query: select * from ( SELECT myTable.* , RN = ROW_NUMBER()OVER(PARTITION BY patientID ORDER BY ID) FROM myTable ) as myPatient where RN = 1
    – Syed
    Nov 1 at 0:12















up vote
23
down vote



accepted







up vote
23
down vote



accepted






You could use a CTE with ROW_NUMBER function:



WITH CTE AS(
SELECT myTable.*
, RN = ROW_NUMBER()OVER(PARTITION BY patientID ORDER BY ID)
FROM myTable
)
SELECT * FROM CTE
WHERE RN = 1





share|improve this answer












You could use a CTE with ROW_NUMBER function:



WITH CTE AS(
SELECT myTable.*
, RN = ROW_NUMBER()OVER(PARTITION BY patientID ORDER BY ID)
FROM myTable
)
SELECT * FROM CTE
WHERE RN = 1






share|improve this answer












share|improve this answer



share|improve this answer










answered Jun 16 '12 at 14:58









Rango

359k45451712




359k45451712








  • 4




    I would upvote this answer ten times if it was possible.
    – AFract
    Oct 29 '15 at 9:34










  • Good answer @TimSchmelter. The same output can also be achieved using a sub-query: select * from ( SELECT myTable.* , RN = ROW_NUMBER()OVER(PARTITION BY patientID ORDER BY ID) FROM myTable ) as myPatient where RN = 1
    – Syed
    Nov 1 at 0:12
















  • 4




    I would upvote this answer ten times if it was possible.
    – AFract
    Oct 29 '15 at 9:34










  • Good answer @TimSchmelter. The same output can also be achieved using a sub-query: select * from ( SELECT myTable.* , RN = ROW_NUMBER()OVER(PARTITION BY patientID ORDER BY ID) FROM myTable ) as myPatient where RN = 1
    – Syed
    Nov 1 at 0:12










4




4




I would upvote this answer ten times if it was possible.
– AFract
Oct 29 '15 at 9:34




I would upvote this answer ten times if it was possible.
– AFract
Oct 29 '15 at 9:34












Good answer @TimSchmelter. The same output can also be achieved using a sub-query: select * from ( SELECT myTable.* , RN = ROW_NUMBER()OVER(PARTITION BY patientID ORDER BY ID) FROM myTable ) as myPatient where RN = 1
– Syed
Nov 1 at 0:12






Good answer @TimSchmelter. The same output can also be achieved using a sub-query: select * from ( SELECT myTable.* , RN = ROW_NUMBER()OVER(PARTITION BY patientID ORDER BY ID) FROM myTable ) as myPatient where RN = 1
– Syed
Nov 1 at 0:12














up vote
3
down vote













It sounds like you're looking for DISTINCT:



SELECT DISTINCT patientID FROM myTable


you can get the same "effect" with GROUP BY:



SELECT patientID FROM myTable GROUP BY patientID





share|improve this answer





















  • Not exactly. The patient ID can show up in the table more than once with a diffierent tblclaims.id. So if i select distinct, that makes that I get that patientID returned more than once
    – wootscootinboogie
    Jun 16 '12 at 14:58










  • @wootscootinboogie only if you add more fields to the projections you're selecting. DISTINCT of only patientID should give you what you need. if you do DISTINCT patiendID, ID then yes, you will get distinct combinations thus more than one patientID
    – Pavel Veller
    Jun 16 '12 at 15:00















up vote
3
down vote













It sounds like you're looking for DISTINCT:



SELECT DISTINCT patientID FROM myTable


you can get the same "effect" with GROUP BY:



SELECT patientID FROM myTable GROUP BY patientID





share|improve this answer





















  • Not exactly. The patient ID can show up in the table more than once with a diffierent tblclaims.id. So if i select distinct, that makes that I get that patientID returned more than once
    – wootscootinboogie
    Jun 16 '12 at 14:58










  • @wootscootinboogie only if you add more fields to the projections you're selecting. DISTINCT of only patientID should give you what you need. if you do DISTINCT patiendID, ID then yes, you will get distinct combinations thus more than one patientID
    – Pavel Veller
    Jun 16 '12 at 15:00













up vote
3
down vote










up vote
3
down vote









It sounds like you're looking for DISTINCT:



SELECT DISTINCT patientID FROM myTable


you can get the same "effect" with GROUP BY:



SELECT patientID FROM myTable GROUP BY patientID





share|improve this answer












It sounds like you're looking for DISTINCT:



SELECT DISTINCT patientID FROM myTable


you can get the same "effect" with GROUP BY:



SELECT patientID FROM myTable GROUP BY patientID






share|improve this answer












share|improve this answer



share|improve this answer










answered Jun 16 '12 at 14:56









Pavel Veller

5,28012023




5,28012023












  • Not exactly. The patient ID can show up in the table more than once with a diffierent tblclaims.id. So if i select distinct, that makes that I get that patientID returned more than once
    – wootscootinboogie
    Jun 16 '12 at 14:58










  • @wootscootinboogie only if you add more fields to the projections you're selecting. DISTINCT of only patientID should give you what you need. if you do DISTINCT patiendID, ID then yes, you will get distinct combinations thus more than one patientID
    – Pavel Veller
    Jun 16 '12 at 15:00


















  • Not exactly. The patient ID can show up in the table more than once with a diffierent tblclaims.id. So if i select distinct, that makes that I get that patientID returned more than once
    – wootscootinboogie
    Jun 16 '12 at 14:58










  • @wootscootinboogie only if you add more fields to the projections you're selecting. DISTINCT of only patientID should give you what you need. if you do DISTINCT patiendID, ID then yes, you will get distinct combinations thus more than one patientID
    – Pavel Veller
    Jun 16 '12 at 15:00
















Not exactly. The patient ID can show up in the table more than once with a diffierent tblclaims.id. So if i select distinct, that makes that I get that patientID returned more than once
– wootscootinboogie
Jun 16 '12 at 14:58




Not exactly. The patient ID can show up in the table more than once with a diffierent tblclaims.id. So if i select distinct, that makes that I get that patientID returned more than once
– wootscootinboogie
Jun 16 '12 at 14:58












@wootscootinboogie only if you add more fields to the projections you're selecting. DISTINCT of only patientID should give you what you need. if you do DISTINCT patiendID, ID then yes, you will get distinct combinations thus more than one patientID
– Pavel Veller
Jun 16 '12 at 15:00




@wootscootinboogie only if you add more fields to the projections you're selecting. DISTINCT of only patientID should give you what you need. if you do DISTINCT patiendID, ID then yes, you will get distinct combinations thus more than one patientID
– Pavel Veller
Jun 16 '12 at 15:00










up vote
0
down vote













The simple way would be to add LIMIT 1 to the end of your query. This will ensure only a single row is returned in the result set.






share|improve this answer

















  • 1




    LIMIT is MySql, isn't it?
    – Rango
    Jun 16 '12 at 14:59










  • yes, limit isn't in sql server
    – wootscootinboogie
    Jun 16 '12 at 14:59










  • it's SELECT TOP 1 ... in MSSQL
    – Pavel Veller
    Jun 16 '12 at 15:01






  • 2




    @PavelVeller: But i assume that TOP 1 is not what OP is looking for. He want just one record for every different patientID.
    – Rango
    Jun 16 '12 at 15:02










  • Okay, so LIMIT is available in MySQL and Drizzle, but doesn't SQL Server have something to similar effect? In fact it does, in a roundabout way. You can set up your query in a subquery using ROW_NUMBER(), and then limit the selection using the generated row numbers. Source: blogs.msdn.com/b/sqlserver/archive/2006/10/25/…
    – Lady Serena Kitty
    Jun 16 '12 at 15:05

















up vote
0
down vote













The simple way would be to add LIMIT 1 to the end of your query. This will ensure only a single row is returned in the result set.






share|improve this answer

















  • 1




    LIMIT is MySql, isn't it?
    – Rango
    Jun 16 '12 at 14:59










  • yes, limit isn't in sql server
    – wootscootinboogie
    Jun 16 '12 at 14:59










  • it's SELECT TOP 1 ... in MSSQL
    – Pavel Veller
    Jun 16 '12 at 15:01






  • 2




    @PavelVeller: But i assume that TOP 1 is not what OP is looking for. He want just one record for every different patientID.
    – Rango
    Jun 16 '12 at 15:02










  • Okay, so LIMIT is available in MySQL and Drizzle, but doesn't SQL Server have something to similar effect? In fact it does, in a roundabout way. You can set up your query in a subquery using ROW_NUMBER(), and then limit the selection using the generated row numbers. Source: blogs.msdn.com/b/sqlserver/archive/2006/10/25/…
    – Lady Serena Kitty
    Jun 16 '12 at 15:05















up vote
0
down vote










up vote
0
down vote









The simple way would be to add LIMIT 1 to the end of your query. This will ensure only a single row is returned in the result set.






share|improve this answer












The simple way would be to add LIMIT 1 to the end of your query. This will ensure only a single row is returned in the result set.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jun 16 '12 at 14:55









Lady Serena Kitty

418




418








  • 1




    LIMIT is MySql, isn't it?
    – Rango
    Jun 16 '12 at 14:59










  • yes, limit isn't in sql server
    – wootscootinboogie
    Jun 16 '12 at 14:59










  • it's SELECT TOP 1 ... in MSSQL
    – Pavel Veller
    Jun 16 '12 at 15:01






  • 2




    @PavelVeller: But i assume that TOP 1 is not what OP is looking for. He want just one record for every different patientID.
    – Rango
    Jun 16 '12 at 15:02










  • Okay, so LIMIT is available in MySQL and Drizzle, but doesn't SQL Server have something to similar effect? In fact it does, in a roundabout way. You can set up your query in a subquery using ROW_NUMBER(), and then limit the selection using the generated row numbers. Source: blogs.msdn.com/b/sqlserver/archive/2006/10/25/…
    – Lady Serena Kitty
    Jun 16 '12 at 15:05
















  • 1




    LIMIT is MySql, isn't it?
    – Rango
    Jun 16 '12 at 14:59










  • yes, limit isn't in sql server
    – wootscootinboogie
    Jun 16 '12 at 14:59










  • it's SELECT TOP 1 ... in MSSQL
    – Pavel Veller
    Jun 16 '12 at 15:01






  • 2




    @PavelVeller: But i assume that TOP 1 is not what OP is looking for. He want just one record for every different patientID.
    – Rango
    Jun 16 '12 at 15:02










  • Okay, so LIMIT is available in MySQL and Drizzle, but doesn't SQL Server have something to similar effect? In fact it does, in a roundabout way. You can set up your query in a subquery using ROW_NUMBER(), and then limit the selection using the generated row numbers. Source: blogs.msdn.com/b/sqlserver/archive/2006/10/25/…
    – Lady Serena Kitty
    Jun 16 '12 at 15:05










1




1




LIMIT is MySql, isn't it?
– Rango
Jun 16 '12 at 14:59




LIMIT is MySql, isn't it?
– Rango
Jun 16 '12 at 14:59












yes, limit isn't in sql server
– wootscootinboogie
Jun 16 '12 at 14:59




yes, limit isn't in sql server
– wootscootinboogie
Jun 16 '12 at 14:59












it's SELECT TOP 1 ... in MSSQL
– Pavel Veller
Jun 16 '12 at 15:01




it's SELECT TOP 1 ... in MSSQL
– Pavel Veller
Jun 16 '12 at 15:01




2




2




@PavelVeller: But i assume that TOP 1 is not what OP is looking for. He want just one record for every different patientID.
– Rango
Jun 16 '12 at 15:02




@PavelVeller: But i assume that TOP 1 is not what OP is looking for. He want just one record for every different patientID.
– Rango
Jun 16 '12 at 15:02












Okay, so LIMIT is available in MySQL and Drizzle, but doesn't SQL Server have something to similar effect? In fact it does, in a roundabout way. You can set up your query in a subquery using ROW_NUMBER(), and then limit the selection using the generated row numbers. Source: blogs.msdn.com/b/sqlserver/archive/2006/10/25/…
– Lady Serena Kitty
Jun 16 '12 at 15:05






Okay, so LIMIT is available in MySQL and Drizzle, but doesn't SQL Server have something to similar effect? In fact it does, in a roundabout way. You can set up your query in a subquery using ROW_NUMBER(), and then limit the selection using the generated row numbers. Source: blogs.msdn.com/b/sqlserver/archive/2006/10/25/…
– Lady Serena Kitty
Jun 16 '12 at 15:05




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11064473%2fselect-the-first-instance-of-a-record%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo