SQL Recipes
A FREE cookbook for SQL queries and examples
Register FAQ Search Today's Posts Mark Forums Read

SQL - Questions and Answers Have a SQL question? Post it here. First do a search to see if someone hasn't already answered it.

Go Back   SQL Recipes a FREE cookbook of SQL queries and examples > SQL queries and examples > SQL - Questions and Answers

Reply
 
LinkBack Thread Tools Search this Thread
  #1 (permalink)  
Old 06-27-2008, 09:45 PM
Unregistered
 
Posts: n/a
Exclamation ANY dialect question:

multiple enteries for one id number


I have a table with multiple entries for patient id numbers. Basically a patient transitions from one program to another keeping the same id number. I need to know how many patients have transitioned and I am having trouble writing the code. I have the following columns: patid and patgroup. So the data is like this:
Code:
PatId            PatGroup
1                 A
1                 B
I want to do something like:

select count (patid)
from
where patgroup <this is where I need help> where A became B
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

ANSWER(S):

  #2 (permalink)  
Old 06-28-2008, 07:32 AM
Dimitar
 
Posts: n/a
Default ANY answer. Re: multiple enteries for one id number

SELECT patid, count(patid)
FROM patients
GROUP BY patid;


to extract number of participations in groups for each id number

SELECT patid, count(patid)
FROM patients
GROUP BY patid
HAVING count(patid) > 1;


to show only those that transitioned at least once.

I suppose there are no duplicates in your data, e.g.
id group
1 A
1 A

Hope this helps,
Dimitar
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-30-2008, 07:49 PM
Unregistered
 
Posts: n/a
Exclamation ANY answer. Re: multiple enteries for one id number

Hi Dimitar,

That is great, but now I am stuck again. Your code gave me the patid number and count for those patid that showed up more than once in my table. But here is the question, how many patid went from A or C to B only?

PatId PatGroup
1 A
1 B
2 C
2 B
3 C
3 C

Using the example above the answer is 2, PatId 1 and 2 transitioned to B. How do I write that in SQL?

Thanks so much!
LB
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 06-30-2008, 10:08 PM
Dimitar
 
Posts: n/a
Default ANY answer. Re: multiple enteries for one id number

Hi,

I will think about it tomorrow.
But here is a question:

id group
1 A
1 B

How do you know that the patient transited from A to B, and not from B to A?
Tables are unordered sets, unless there is a dedicated column that specifies which row is "after" or "before" another.

Also, what DB engine are you using?

Dimitar
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 07-01-2008, 02:06 PM
Unregistered
 
Posts: n/a
Smile ANY answer. Re: multiple enteries for one id number

Dimitar,

I have a third column which shows date, so I know participants when participants transitioned from A to B. I work in SQL server and am using SQL Query Analyzer version 8.00.194

Thanks,
LB
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 07-01-2008, 06:32 PM
Dimitar
 
Posts: n/a
Default ANY answer. Re: multiple enteries for one id number

LB,

Here is what came to mind first:

SELECT p1.PatId,p1.PatGroup FROM patients AS p1 WHERE p1.PatGroup='B'
AND
 p1.PatId IN (
            SELECT p2.PatId FROM patients AS p2 WHERE (p2.PatGroup='A' OR p2.PatGroup='C') AND (p2.DateEntered < p1.DateEntered)
)
;


Did it work?

Dimitar
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 07-01-2008, 06:37 PM
Dimitar
 
Posts: n/a
Default ANY answer. Re: multiple enteries for one id number

SELECT count(*) FROM (
SELECT p1.PatId,p1.PatGroup FROM patients AS p1 WHERE p1.PatGroup='B'
AND
 p1.PatId IN (
            SELECT p2.PatId FROM patients AS p2 WHERE (p2.PatGroup='A' OR p2.PatGroup='C') AND (p2.DateEntered < p1.DateEntered)
)) AS dt
;


to show the count.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 07-01-2008, 09:07 PM
Unregistered
 
Posts: n/a
Smile ANY answer. Re: multiple enteries for one id number

Hi Dimitar...YEAH!

I think it worked. I had to change part of it but you were so much help. I am new to SQL, like I just learned it two weeks ago. I'm not in IT so the hard stuff, like subqueries, are soo hard for me. You are a great resource and I would love to get an email address for you. This is the query that I used:

USE xx
SELECT p1.patid, p1.patgroup
FROM patients AS p1
WHERE p1.patgroup = 'B' AND p1.patid IN
(SELECT p2.patid
FROM patients AS p2
WHERE p2.billprogramid IN ('A', 'C')


Thank you!
LB
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 10:11 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
Copyright (c) 2006-2008 SQL Recipes

1 2 3 4 5 6 7 8