Solved

SQL lookup

Posted on 2012-03-30
16
338 Views
Last Modified: 2012-06-27
I have a table with columns:
id, userid, atype, address

atype can only be 1 or 0

I need to do an SQL lookup to SELECT userid WHERE atype = 0 but ... here's the important bit... where there is ONLY one entry in the table for that userid

I tried and failed with this:

SELECT userid, atype
FROM tbladdress
GROUP BY userid, atype
HAVING count(userid)=1 and atype=0

Please help

/ Tobzzz
0
Comment
Question by:tobzzz
[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
  • 6
  • 5
  • 4
  • +1
16 Comments
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37788709
SELECT userid, atype
FROM tbladdress
where atype =0
GROUP BY userid, atype
HAVING count(userid)=1
0
 
LVL 11

Author Comment

by:tobzzz
ID: 37788746
thanks - testing this i get 2 or more rows in some cases (where userid has an entry atype=1 once or more than once in addition to 1 instance of atype=0. I need it where there's only 1 single entry in the table for a userid, and that it's atype=0
0
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37788766
SELECT userid, atype
FROM tbladdress
where count(userid)=1 and atype =0
GROUP BY userid, atype
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 37788818
>> where there is ONLY one entry in the table for that userid <<

Hmm, you mean only one entry with atype = 0, and other rows possible:


SELECT userid, atype
FROM tbladdress
GROUP BY userid, atype
HAVING SUM(CASE WHEN atype = 0 THEN 1 ELSE 0 END) = 1
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 37788824
>> need it where there's only 1 single entry in the table for a userid, and that it's atype=0 <<


SELECT userid, atype
FROM tbladdress
GROUP BY userid, atype
HAVING SUM(CASE WHEN atype = 0 THEN 1 ELSE 0 END) = 1
     AND SUM(CASE WHEN COALESCE(atype, 1) <> 0 THEN 1 ELSE 0 END) = 0
0
 
LVL 11

Expert Comment

by:SANDY_SK
ID: 37788827
try this

select userid, atype from (
select count(userid) , atype , userid as cnt from tbladdress GROUP BY userid, atype
) as tab where cnt = 1;
0
 
LVL 11

Author Comment

by:tobzzz
ID: 37788876
Hi Scott,

Yep, it;s:
>> need it where there's only 1 single entry in the table for a userid, and that it's atype=0 <<
but your query returned userids that have more than 1 row in the database. In fact the first it returned has 2 rows, one with atype=0 and another with atype=1, there must be only one row in the database for a userid and atype=0. Thanks!
0
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37788908
SELECT userid, atype
FROM tbladdress
where count(userid)=1 and atype =0
GROUP BY userid, atype

what is the result you are getting from above sql statement?
0
 
LVL 11

Author Comment

by:tobzzz
ID: 37788917
Group By in a statement requires Having clause too so it will throw an error
0
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37788944
SELECT userid, atype
FROM tbladdress
GROUP BY userid, atype
HAVING (count(userid)=1) and atype=0
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 37788979
I'll re-post my very last post, which I'm pretty sure does what you need:


SELECT userid, atype
FROM tbladdress
GROUP BY userid, atype
HAVING SUM(CASE WHEN atype = 0 THEN 1 ELSE 0 END) = 1
     AND SUM(CASE WHEN COALESCE(atype, 1) <> 0 THEN 1 ELSE 0 END) = 0
0
 
LVL 11

Author Comment

by:tobzzz
ID: 37789074
Scott and Anuroopsundd,

Your queries bring back the exact same 1,262 results and as I said, unfortunately it's still picking up userid thats have 2 or more rows

so if I had this table:

id    |    userid    |    atype    |
1    |    33    |    0    |
2    |    42    |    0    |
3    |    33    |    1    |
4    |    67    |    1    |
5    |    33    |    1    |
6    |    88    |    0    |
7    |    91    |    0    |
8    |    91    |    1    |

I want this result:

userid    |    atype    |
      42    |     0       |
      88    |     0       |

Thanks
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 37789212
Are you using the LAST version of my query?  The one I re-posted above?
0
 
LVL 11

Author Comment

by:tobzzz
ID: 37789289
Yes sir, word-for-word except for adding 'ORDER BY userid' as a final line so i can see the results easily
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 200 total points
ID: 37789293
D'OH, (somehow) just noticed "atype" was in the SELECT and GROUP BY, D'OH D'OH:


SELECT userid
FROM tbladdress
GROUP BY userid
HAVING SUM(CASE WHEN atype = 0 THEN 1 ELSE 0 END) = 1
     AND SUM(CASE WHEN COALESCE(atype, 1) <> 0 THEN 1 ELSE 0 END) = 0
ORDER BY userid
0
 
LVL 11

Author Comment

by:tobzzz
ID: 37789314
That's the stuff! Great job, Scott, thanks. Many thanks to all others for their time.
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

726 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