473,408 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

How to get multiple row values having distinct column value

Hi,

I have table 'patientdata' consisting of patient data in three columns fname, lname and loc.

I want to have a query that shows which of these patients were diagnosed in more than one location.

Here is sample table:

fname lname loc
tom hanks mi
tom hanks ca
jon trevolta nj
jon trevolta nj
tom hanks mi
jim kerry md

Answer should be:

tom hanks mi
tom hanks ca


Thanks in advance

Chintan
Aug 2 '11 #1
10 11230
Rabbit
12,516 Expert Mod 8TB
You could use a count distinct query that groups by the first name and last name. Anything having a count greater than 1 would be the results you're looking for. If you absolutely have to have the locations as well, then you could subquery it in the from clause.
Aug 2 '11 #2
Is there any way I can accomplish this in one single query
Aug 2 '11 #3
And here is the thing I dont want distinct fname or lname but I want distinct loc for same names
Aug 2 '11 #4
Rabbit
12,516 Expert Mod 8TB
Yes you can do it in a single query, my post lays out how to do that.

Yes, I know. That's why you do a count distinct on loc.
Aug 2 '11 #5
I added one more column dob. When I use the below command... it result as

Expand|Select|Wrap|Line Numbers
  1. tom    hanks            mi    10    2
  2. jon    trevolta    nj    13    2

and not

Expand|Select|Wrap|Line Numbers
  1. tom    hanks            mi    10    
  2. tom     hanks           md      10
  3. jon    trevolta    nj    13    
  4. jon    trevolta    nj    13    
Can you suggest me how to get this.. And thanks for your help so far



Expand|Select|Wrap|Line Numbers
  1. SELECT *, 
  2. COUNT(dob) AS NumOccurrences
  3. FROM patientdata
  4. GROUP BY dob
  5. HAVING COUNT(dob) > 1
Aug 2 '11 #6
Rabbit
12,516 Expert Mod 8TB
That query can't possibly work. It should error out seeing as how you have a last name field and first name field that is not in an aggregate function nor in the group by clause.

Assuming dob is date of birth, why are you counting date of birth? I thought you wanted to count distinct locations.

And as I said earlier, if you want to return the location as well, you would need to subquery the aggregate query. Either into the from clause so you can join by it, or into the where clause so you can filter by it.
Aug 2 '11 #7
I wanted to do this is in single query which is a priority.
Also I found out that two person can have same name and so filtering through dob is much better option... So what change I should do to query as I don't want to use subquery...

Again thanks
Aug 2 '11 #8
Hey I got it.. Without using subquery. As if ur using count its expensive with large # of data instead use this.

Expand|Select|Wrap|Line Numbers
  1. SELECT  *
  2. FROM    patientdata mto
  3. WHERE   EXISTS
  4.         (
  5.         SELECT  1
  6.         FROM    patientdata mti
  7.         WHERE   mti.dob = mto.dob
  8.         LIMIT 1, 1
  9.         )
Aug 2 '11 #9
Rabbit
12,516 Expert Mod 8TB
You just used a subquery. Plus I don't see how that is going to return the results you need. That subquery is going to return exist for every record so you're just going to get everything back.
Aug 3 '11 #10
skrypt
1
can this be a solution

Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT pd1.Firstname, pd1.Lastname, pd1.Diagnosis_location, Medical_record_number
  2. FROM patientdata pd1 JOIN patientdata pd2
  3. USING(Medical_record_number)
  4. WHERE pd1.Firstname = pd2.Firstname
  5.     AND pd1.Lastname = pd2.Lastname
  6.     AND pd1.Date_of_Birth = pd2.Date_of_Birth
  7.     AND pd1.Diagnosis_location != pd2.Diagnosis_location
  8. ORDER BY pd1.Lastname;
Dec 29 '11 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Olivier | last post by:
I'm trying to write a query which allows that multiple values from one column are placed in one record. ex: table Nr Letters 1 A 2 A 2 B 2 C
2
by: graycam | last post by:
E.G. I want to record sales. Three tables - TblProduct TblCust & TblSales TblCust fields = custID custName TblProducts fields = prodID prodName TblSales fields = salesID custID prodID If...
2
by: jvb | last post by:
I am trying to find a way, other than iterating through every row or requerring the database, to get the distinct values from a column in a datatable. Any help would be appreciated. Thanks.
1
by: sverdlov | last post by:
Hi all, I have, let's say, 1 column called Payment_type. In the column, there are 6 types of values: 1, 2, 3, 4, 5, 6, like in --------------------------------- Payment_type |...
5
by: mctime | last post by:
Hello, I am attempting to split a raw data table into a new table that has split out a specific field in the raw data and created a new record for each split but I have come to an impasse due to...
5
by: unknown418 | last post by:
I am confused what is the efficient way to read in a file with multiple values per line. Each value is separated by a space character. The values could be characters or real values. I am using...
7
nev
by: nev | last post by:
i declared 2 datatables: dim dtt1 as datatable = ds.datatable1 dim dtt2 as datatable = ds.datatable2 dtt1 has firstname, lastname columns dtt2 has firstname column when i filter dtt1 by...
0
by: rajesh | last post by:
Hi , i want to select the first minimum date in the column d for each distinct column a value. how i can do that in a sql query. xyztable Column a Column b Column c ...
3
by: questionit | last post by:
Hi How do i write multiple values in a single column, using INSERT INTO I have a listbox and i want all the selected values from that listbox inserted into a table column INSERT INTO...
1
by: kgandu | last post by:
Is there anyway that we can get the distinct values of the column on which there exist a partition. eg : for my fact table the date is partitioned for each date. if i select the distinct values of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.