Solved

SQL lookup

Posted on 2012-03-30
16
336 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
  • 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

820 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