Can someone point me in the right direction: How do I say: "Display all
rows from MYTABLE where column x is the same but column y is different"?
Thanks much.If I understand your question correctly then this will do it
select * from table where colX = someValue
and colY <> someValue
example
select * from employees where FirstName= 'John'
and Zipcode<> '10028'
http://sqlservercode.blogspot.com/|||SELECT a.*
FROM MYTABLE as a, MYTABLE as b
WHERE a.x = b.x AND a.y <> b.y
"Rick Charnes" wrote:
> Can someone point me in the right direction: How do I say: "Display all
> rows from MYTABLE where column x is the same but column y is different"?
> Thanks much.
>|||select distinct
x
,y
from table
?
DDL, sample data and expected results would help a lot.
ML
http://milambda.blogspot.com/|||I'm not sure what you mean.
Column X is the same as what?
Column Y is different than what?
Are you comparing rows against specific values?
Or are you comparing the rows against the rows from another table.
Can you give us a simple example?
--
Ryan Powers
Clarity Consulting
http://www.claritycon.com
"Rick Charnes" wrote:
> Can someone point me in the right direction: How do I say: "Display all
> rows from MYTABLE where column x is the same but column y is different"?
> Thanks much.
>|||Sorry; my fault: I don't know the values that I'm comparing. I want to
say: give me all rows in the table where col X is the same but within
those matched rows col Y is different. But I don't have any specific
values of X or Y that I'm comparing.
I think I need to use:
GROUP BY x
HAVING count(*) > 1
...and something with column Y <> column Y...?
somehow, but I'm not sure how.
In article <1136564368.957582.7870@.o13g2000cwo.googlegroups.com>,
denis.gobo@.gmail.com says...
> If I understand your question correctly then this will do it
> select * from table where colX = someValue
> and colY <> someValue
> example
> select * from employees where FirstName= 'John'
> and Zipcode<> '10028'
> http://sqlservercode.blogspot.com/
>|||Rick Charnes wrote:
> Sorry; my fault: I don't know the values that I'm comparing. I want to
> say: give me all rows in the table where col X is the same but within
> those matched rows col Y is different. But I don't have any specific
> values of X or Y that I'm comparing.
> I think I need to use:
> GROUP BY x
> HAVING count(*) > 1
> ...and something with column Y <> column Y...?
> somehow, but I'm not sure how.
> In article <1136564368.957582.7870@.o13g2000cwo.googlegroups.com>,
> denis.gobo@.gmail.com says...
Try:
SELECT T1.* -- List the column(s) don't use *
FROM your_table AS T1
WHERE EXISTS
(SELECT *
FROM your_table AS T2
WHERE T1.x = T2.x
AND T1.y <> T2.y);
Or maybe:
SELECT x
FROM your_table
GROUP BY x
HAVING MIN(y)<MAX(y);
depending on what column(s) you want to output.
David Portas
SQL Server MVP
--|||Here is one way
create table mark (value varchar(50),id int)
insert into mark
select 'AA',1 union all
select 'AA',1 union all
select 'AB',2 union all
select 'AB',1
select distinct m.* from mark m join(
select value from mark
group by id,value
having count(*) =1) m2 on m.value =m2.value
http://sqlservercode.blogspot.com/|||Post DDL, sample data and expected results, then.
ML
http://milambda.blogspot.com/|||This does it. Thanks very much.
In article <C8F7E55C-707D-43FA-B6ED-BC2697B8D64F@.microsoft.com>,
Lee@.discussions.microsoft.com says...
> SELECT a.*
> FROM MYTABLE as a, MYTABLE as b
> WHERE a.x = b.x AND a.y <> b.y
> "Rick Charnes" wrote:
>
>
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment