SQL Basics

 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me



Go Back   Tutorialized ForumsDatabasesSQL Basics

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Tutorialized Forums Sponsor:
  #1  
Old February 24th, 2013, 04:52 PM
SouthAustin SouthAustin is offline
Registered User
Tutorialized Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 2 SouthAustin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 47 sec
Reputation Power: 0
"Never equal to" in SQL

Hello, SQL newbie question. If i have a table like this:
col1 col2
foo 1
foo 2
foo 3
bar 2
bar 3

How do a get all the distinct values in col1 for rows that are never equal to 1 in col2. In this case the query should return bar only because no row exists with bar and 1 together . if I filter on col2 <> 1 I get both foo and bar back. the pseudo code I need is
select distinct col1 where col2 is never equal to 1
thanks in advance

Reply With Quote
  #2  
Old August 11th, 2013, 12:08 AM
jethrow jethrow is offline
Registered User
Tutorialized Newbie (0 - 499 posts)
 
Join Date: Aug 2013
Posts: 83 jethrow User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 h 59 m 36 sec
Reputation Power: 1
sql Code:
Original - sql Code
  1. DECLARE @t TABLE (col1 varchar(10), col2 int);
  2. INSERT INTO @t VALUES
  3.     ('foo', 1),
  4.     ('foo', 2),
  5.     ('foo', 3),
  6.     ('bar', 2),
  7.     ('bar', 3);
  8.  
  9. SELECT  col1 FROM (
  10.     SELECT    p1.col1, (
  11.                 SELECT  ','+CAST(col2 AS varchar(10))+','
  12.                 FROM    @t AS p2
  13.                 WHERE   (p1.col1 = p2.col1)
  14.                 FOR  XML PATH('') ) col2
  15.     FROM        @t AS p1
  16.     GROUP BY    col1    ) n
  17. WHERE   col2 NOT LIKE '%,1,%'


... or here's a much cleaner example:
sql Code:
Original - sql Code
  1. DECLARE @t TABLE (col1 varchar(10), col2 int);
  2. INSERT INTO @t VALUES
  3.     ('foo', 1),
  4.     ('foo', 2),
  5.     ('foo', 3),
  6.     ('bar', 2),
  7.     ('bar', 3);
  8.  
  9. SELECT col1 FROM @t
  10. GROUP BY col1;
  11.  
  12. EXCEPT
  13.  
  14. SELECT DISTINCT col1 FROM @t
  15. WHERE col2=1;

Last edited by jethrow : August 11th, 2013 at 04:37 PM.

Reply With Quote
Reply

Viewing: Tutorialized ForumsDatabasesSQL Basics > "Never equal to" in SQL


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap