Solved

SQL lookup

Posted on 2012-03-30
16
334 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

813 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now