﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / SQL Server Newbies  / Multiple values in some columns single value in another / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Fri, 24 May 2013 18:03:55 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Multiple values in some columns single value in another</title><link>http://www.sqlservercentral.com/Forums/Topic1359407-1292-1.aspx</link><description>Oops. Sorry! :blush:</description><pubDate>Mon, 17 Sep 2012 05:39:26 GMT</pubDate><dc:creator>eamonn.byrne</dc:creator></item><item><title>RE: Multiple values in some columns single value in another</title><link>http://www.sqlservercentral.com/Forums/Topic1359407-1292-1.aspx</link><description>[quote][b]eamonn.byrne (9/17/2012)[/b][hr][quote][b]Jeff Moden (9/16/2012)[/b][hr][quote][b]eamonn.byrne (9/14/2012)[/b][hr]Yep that definately works. It took you 5 seconds to fix what had me scratching my head for a couple of hours. :ermm:Cheers![/quote]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?[/quote]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[/quote]Glad I could help. One small thing, I am not a she, I am a he.</description><pubDate>Mon, 17 Sep 2012 04:58:11 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: Multiple values in some columns single value in another</title><link>http://www.sqlservercentral.com/Forums/Topic1359407-1292-1.aspx</link><description>[quote][b]Jeff Moden (9/16/2012)[/b][hr][quote][b]eamonn.byrne (9/14/2012)[/b][hr]Yep that definately works. It took you 5 seconds to fix what had me scratching my head for a couple of hours. :ermm:Cheers![/quote]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?[/quote]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</description><pubDate>Mon, 17 Sep 2012 01:23:58 GMT</pubDate><dc:creator>eamonn.byrne</dc:creator></item><item><title>RE: Multiple values in some columns single value in another</title><link>http://www.sqlservercentral.com/Forums/Topic1359407-1292-1.aspx</link><description>[quote][b]eamonn.byrne (9/14/2012)[/b][hr]Yep that definately works. It took you 5 seconds to fix what had me scratching my head for a couple of hours. :ermm:Cheers![/quote]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?</description><pubDate>Sun, 16 Sep 2012 17:54:08 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Multiple values in some columns single value in another</title><link>http://www.sqlservercentral.com/Forums/Topic1359407-1292-1.aspx</link><description>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).</description><pubDate>Sat, 15 Sep 2012 21:04:42 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: Multiple values in some columns single value in another</title><link>http://www.sqlservercentral.com/Forums/Topic1359407-1292-1.aspx</link><description>[quote][b]CELKO (9/15/2012)[/b][hr]&amp;gt;&amp;gt; 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) &amp;lt;&amp;lt;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 FoobarVALUES ('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_referenceHAVING MIN(something_backscan) = 0   AND MAX(something_backscan) = 1;    [/quote]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.[code="sql"]CREATE TABLE Foobar(emg_id VARCHAR(7) NOT NULLCHECK (emg_id LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9]'), vague_reference VARCHAR(8) NOT NULLCHECK (vague_referenceLIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'), something_backscan SMALLINT DEFAULT 0 NOT NULLCHECK (something_backscan IN (0,1));[/code]Here is the error message:[code="plain"]Msg 102, Level 15, State 1, Line 8Incorrect syntax near ';'.[/code]</description><pubDate>Sat, 15 Sep 2012 20:59:58 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: Multiple values in some columns single value in another</title><link>http://www.sqlservercentral.com/Forums/Topic1359407-1292-1.aspx</link><description>&amp;gt;&amp;gt; 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) &amp;lt;&amp;lt;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 FoobarVALUES ('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_referenceHAVING MIN(something_backscan) = 0   AND MAX(something_backscan) = 1;    </description><pubDate>Sat, 15 Sep 2012 09:34:07 GMT</pubDate><dc:creator>CELKO</dc:creator></item><item><title>RE: Multiple values in some columns single value in another</title><link>http://www.sqlservercentral.com/Forums/Topic1359407-1292-1.aspx</link><description>Glad I could help.</description><pubDate>Fri, 14 Sep 2012 09:47:21 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: Multiple values in some columns single value in another</title><link>http://www.sqlservercentral.com/Forums/Topic1359407-1292-1.aspx</link><description>Yep that definately works. It took you 5 seconds to fix what had me scratching my head for a couple of hours. :ermm:Cheers!</description><pubDate>Fri, 14 Sep 2012 09:41:39 GMT</pubDate><dc:creator>eamonn.byrne</dc:creator></item><item><title>RE: Multiple values in some columns single value in another</title><link>http://www.sqlservercentral.com/Forums/Topic1359407-1292-1.aspx</link><description>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</description><pubDate>Fri, 14 Sep 2012 09:34:09 GMT</pubDate><dc:creator>eamonn.byrne</dc:creator></item><item><title>RE: Multiple values in some columns single value in another</title><link>http://www.sqlservercentral.com/Forums/Topic1359407-1292-1.aspx</link><description>How about this:[code="sql"]DECLARE @t TABLE (EMGID Varchar(7), Reference Varchar(8), Backscan Int);INSERT INTO @tSELECT '1267202', '32655185', 1UNION ALL SELECT '1267202', '32655185', 0UNION ALL SELECT '1239667', '33264522', 1UNION ALL SELECT '1239667', '33264522', 1UNION ALL SELECT '1234000', '33674689', 0UNION ALL SELECT '1234000', '33674689', 0;select * from @t twhere    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);[/code]</description><pubDate>Fri, 14 Sep 2012 09:30:57 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>Multiple values in some columns single value in another</title><link>http://www.sqlservercentral.com/Forums/Topic1359407-1292-1.aspx</link><description>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 @tSELECT '1267202', '32655185', 1UNION ALL SELECT '1267202', '32655185', 0UNION ALL SELECT '1239667', '33264522', 1UNION ALL SELECT '1239667', '33264522', 1UNION ALL SELECT '1234000', '33674689', 0UNION ALL SELECT '1234000', '33674689', 0UNION ALL SELECT '1235670', '33674689', 1UNION ALL SELECT '1237830', '33674689', 0select * from @tI should also state that I'm not interested in EMGID rows '1235670' or '1237830' as these are single entries.Many thanks,Eamonn</description><pubDate>Fri, 14 Sep 2012 09:20:29 GMT</pubDate><dc:creator>eamonn.byrne</dc:creator></item></channel></rss>