Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Multiple values in some columns single value in another Expand / Collapse
Author
Message
Posted Friday, September 14, 2012 9:20 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, April 17, 2014 2:30 AM
Points: 5, Visits: 45
Hi,
I'm a first time poster long time reader of SSC (so perhaps I should already know this!). But...

I'm having trouble isolating some rows in a table. Its an audit table that can have the same values repeated in some fields. See the table below. I'm only interested in selecting the row where EMG ID is 1267202 as it has a Backscan value of 1 AND 0 instead of only 1 or only 0. (The real life audit table has many more rows but these are the key ones I need for my query)

Any idea how I can isolate these two rows only?

DECLARE @t TABLE (EMGID Varchar(7), Reference Varchar(8), Backscan Int)

INSERT INTO @t
SELECT '1267202', '32655185', 1
UNION ALL SELECT '1267202', '32655185', 0
UNION ALL SELECT '1239667', '33264522', 1
UNION ALL SELECT '1239667', '33264522', 1
UNION ALL SELECT '1234000', '33674689', 0
UNION ALL SELECT '1234000', '33674689', 0
UNION ALL SELECT '1235670', '33674689', 1
UNION ALL SELECT '1237830', '33674689', 0
select * from @t

I should also state that I'm not interested in EMGID rows '1235670' or '1237830' as these are single entries.

Many thanks,
Eamonn
Post #1359407
Posted Friday, September 14, 2012 9:30 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 7:54 PM
Points: 23,227, Visits: 31,921
How about this:


DECLARE @t TABLE (EMGID Varchar(7), Reference Varchar(8), Backscan Int);

INSERT INTO @t
SELECT '1267202', '32655185', 1
UNION ALL SELECT '1267202', '32655185', 0
UNION ALL SELECT '1239667', '33264522', 1
UNION ALL SELECT '1239667', '33264522', 1
UNION ALL SELECT '1234000', '33674689', 0
UNION ALL SELECT '1234000', '33674689', 0;

select * from @t t
where
exists(select 1 from @t t1 where t1.EMGID = t.EMGID and Backscan = 0) and
exists(select 1 from @t t1 where t1.EMGID = t.EMGID and Backscan = 1);





Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1359423
Posted Friday, September 14, 2012 9:34 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, April 17, 2014 2:30 AM
Points: 5, Visits: 45
That looks like it could do the trick, thanks Lynn. I'll use that method for my production tables and see how I get on
Post #1359430
Posted Friday, September 14, 2012 9:41 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, April 17, 2014 2:30 AM
Points: 5, Visits: 45
Yep that definately works. It took you 5 seconds to fix what had me scratching my head for a couple of hours.

Cheers!
Post #1359439
Posted Friday, September 14, 2012 9:47 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 7:54 PM
Points: 23,227, Visits: 31,921
Glad I could help.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1359456
Posted Saturday, September 15, 2012 9:34 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 7:49 PM
Points: 1,945, Visits: 2,894
>> I'm having trouble isolating some rows in a table. Its an audit table that can have the same values repeated in some fields [sic: columns are not fields]. See the table below. I'm only interested in selecting the row where EMG ID is 1267202 as it has a something_backscan value of 1 AND 0 instead of only 1 or only 0. (The real life audit table has many more rows but these are the key ones I need for my query) <<

It would really help if we has DDL, a key, constraints and proper data element names. This is a form of relational division.

CREATE TABLE Foobar
(emg_id VARCHAR(7) NOT NULL
CHECK (emg_id LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9]'),
vague_reference VARCHAR(8) NOT NULL
CHECK (vague_reference
LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'),
something_backscan SMALLINT DEFAULT 0 NOT NULL
CHECK (something_backscan IN (0,1));

INSERT INTO Foobar
VALUES
('1267202', '32655185', 1), ('1267202', '32655185', 0),
('1239667', '33264522', 1), ('1239667', '33264522', 1),
('1234000', '33674689', 0), ('1234000', '33674689', 0),
('1235670', '33674689', 1),
('1237830', '33674689', 0);

SELECT emg_id, vague_reference
FROM Foobar
GROUP BY emg_id, vague_reference
HAVING MIN(something_backscan) = 0
AND MAX(something_backscan) = 1;



Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Post #1359816
Posted Saturday, September 15, 2012 8:59 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 7:54 PM
Points: 23,227, Visits: 31,921
CELKO (9/15/2012)
>> I'm having trouble isolating some rows in a table. Its an audit table that can have the same values repeated in some fields [sic: columns are not fields]. See the table below. I'm only interested in selecting the row where EMG ID is 1267202 as it has a something_backscan value of 1 AND 0 instead of only 1 or only 0. (The real life audit table has many more rows but these are the key ones I need for my query) <<

It would really help if we has DDL, a key, constraints and proper data element names. This is a form of relational division.

CREATE TABLE Foobar
(emg_id VARCHAR(7) NOT NULL
CHECK (emg_id LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9]'),
vague_reference VARCHAR(8) NOT NULL
CHECK (vague_reference
LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'),
something_backscan SMALLINT DEFAULT 0 NOT NULL
CHECK (something_backscan IN (0,1));

INSERT INTO Foobar
VALUES
('1267202', '32655185', 1), ('1267202', '32655185', 0),
('1239667', '33264522', 1), ('1239667', '33264522', 1),
('1234000', '33674689', 0), ('1234000', '33674689', 0),
('1235670', '33674689', 1),
('1237830', '33674689', 0);

SELECT emg_id, vague_reference
FROM Foobar
GROUP BY emg_id, vague_reference
HAVING MIN(something_backscan) = 0
AND MAX(something_backscan) = 1;




Mr. Celko,

It would be nice if you would test your code before you post, especially since you are the worlds formost SQL expert. The following code errors in SQL Server 2008 R2 and I really don't feel like figuring out why. That is going to have to be up to you.


CREATE TABLE Foobar
(emg_id VARCHAR(7) NOT NULL
CHECK (emg_id LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9]'),
vague_reference VARCHAR(8) NOT NULL
CHECK (vague_reference
LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'),
something_backscan SMALLINT DEFAULT 0 NOT NULL
CHECK (something_backscan IN (0,1));


Here is the error message:
Msg 102, Level 15, State 1, Line 8
Incorrect syntax near ';'.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1359862
Posted Saturday, September 15, 2012 9:04 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 7:54 PM
Points: 23,227, Visits: 31,921
Also, after modifying your code to use the table variable provided by the OP, it only returned one row, not both rows (one with backscan = 0 and one with backscan = 1).



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1359863
Posted Sunday, September 16, 2012 5:54 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 9:50 PM
Points: 36,951, Visits: 31,458
eamonn.byrne (9/14/2012)
Yep that definately works. It took you 5 seconds to fix what had me scratching my head for a couple of hours.

Cheers!


Before you use it, Joe is right.

1. What is the key here? Is it just EmgID or is it EmgID and Reference together.

2. Regardless of what the answer to 1 is, is there anything special you want done for an EmgID with multiple Reference values?


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1359932
Posted Monday, September 17, 2012 1:23 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, April 17, 2014 2:30 AM
Points: 5, Visits: 45
Jeff Moden (9/16/2012)
eamonn.byrne (9/14/2012)
Yep that definately works. It took you 5 seconds to fix what had me scratching my head for a couple of hours.

Cheers!


Before you use it, Joe is right.

1. What is the key here? Is it just EmgID or is it EmgID and Reference together.

2. Regardless of what the answer to 1 is, is there anything special you want done for an EmgID with multiple Reference values?


Thanks for the further replies gus and apologies if I wasn't clear. Yes the key is the combined EMGID and Reference fields. Lynn was on the money though and I was able to work out the rest of what I needed from what she provided.

Cheers
Post #1360006
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse