Solved

SQL lookup

Posted on 2012-03-30
16
337 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Display Date and Time 7 48
Report 8 27
*** Windows Server 2012 Websites Set Up *** 17 28
SQL Query 20 19
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

732 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