Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 How to query same table with 2 WHERE clause onto 1 row Rate Topic Display Mode Topic Options
Author
 Message
 Posted Wednesday, August 28, 2013 1:16 AM
 SSC Rookie Group: General Forum Members Last Login: Thursday, January 22, 2015 1:55 AM Points: 42, Visits: 166
 I have the following two queries but how do I combine them to return the results into one row as one result set.The result I'm Looking for would be 1 2013/08/23 00:00:00 5 2013/08/28 00:00:00`DECLARE @t TABLE ( [ndx] [int] IDENTITY(1,1) NOT NULL, [value] [int] NULL, [t_stamp] [datetime] NULL);INSERT INTO @t VALUES (1,'20130823')INSERT INTO @t VALUES (1,'20130824')INSERT INTO @t VALUES (2,'20130825')INSERT INTO @t VALUES (3,'20130826')INSERT INTO @t VALUES (4,'20130827')INSERT INTO @t VALUES (5,'20130828')INSERT INTO @t VALUES (5,'20130829')SELECT min(value), min(t_stamp) from @tWHERE value = (SELECT min(value) FROM @t)SELECT min(value), min(t_stamp) from @tWHERE value = (SELECT max(value) FROM @t)`
Post #1489036
 Posted Wednesday, August 28, 2013 1:26 AM
 SSCertifiable Group: General Forum Members Last Login: Yesterday @ 8:49 AM Points: 7,030, Visits: 14,716
 Probably several ways of doing that, but this one will work:`WITH a (aval, astamp) AS ( SELECT min(value), min(t_stamp) from @t WHERE value = (SELECT min(value) FROM @t) ), b (bval, bstamp) AS ( SELECT min(value), min(t_stamp) from @t WHERE value = (SELECT max(value) FROM @t) )SELECT a.aval, a.astamp, b.bval, b.bstampFROM aCROSS JOIN b`John
Post #1489041
 Posted Wednesday, August 28, 2013 1:28 AM
 SSCrazy Eights Group: General Forum Members Last Login: 2 days ago @ 10:11 AM Points: 8,587, Visits: 18,753
 John's correct, here's another way:`SELECT col1 = MIN(CASE WHEN t.value = x.min_value THEN value ELSE NULL END), col2 = MIN(CASE WHEN t.value = x.min_value THEN t_stamp ELSE NULL END), col3 = MIN(CASE WHEN t.value = x.max_value THEN value ELSE NULL END), col4 = MIN(CASE WHEN t.value = x.max_value THEN t_stamp ELSE NULL END) FROM @t tCROSS APPLY ( SELECT min_value = MIN(value), max_value = MAX(value) FROM @t) xWHERE t.value IN (x.min_value, x.max_value)` “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail ShawFor fast, accurate and documented assistance in answering your questions, please read this article.Understanding and using APPLY, (I) and (II) Paul White Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff ModenExploring Recursive CTEs by Example Dwain Camps
Post #1489042
 Posted Friday, August 30, 2013 3:28 AM
 SSC Rookie Group: General Forum Members Last Login: Thursday, January 22, 2015 1:55 AM Points: 42, Visits: 166
 Thanks for that. That turned out way more complex than I ever imagined for what appears (in theory) to be a simple retrieval of a couple of rows of data. Gonna have to study the solutions to try to understand them better.
Post #1490040
 Posted Friday, August 30, 2013 3:45 AM
 SSCrazy Eights Group: General Forum Members Last Login: 2 days ago @ 10:11 AM Points: 8,587, Visits: 18,753
 Here's another way which you might find a little more intuitive:`;WITH q1 (min_value, min_t_stamp) AS ( SELECT min(value), min(t_stamp) FROM @t WHERE value = (SELECT min(value) FROM @t)),q2 (min_value, min_t_stamp) AS ( SELECT min(value), min(t_stamp) FROM @t WHERE value = (SELECT max(value) FROM @t))SELECT * FROM q1, q2` “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail ShawFor fast, accurate and documented assistance in answering your questions, please read this article.Understanding and using APPLY, (I) and (II) Paul White Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff ModenExploring Recursive CTEs by Example Dwain Camps
Post #1490049
 Posted Tuesday, September 17, 2013 4:12 AM
 SSC-Enthusiastic Group: General Forum Members Last Login: Friday, November 18, 2016 5:59 AM Points: 174, Visits: 271
 Old fashioned:`SELECT value, MIN(t_stamp) FROM @t WHERE value IN ( SELECT MIN(Value) Value FROM @t UNION ALL SELECT MAX(Value) FROM @t ) GROUP BY value` ________________________________________________________If you set out to do something, something else must be done first.
Post #1495430
 Posted Wednesday, September 18, 2013 11:45 PM
 Hall of Fame Group: General Forum Members Last Login: Wednesday, February 24, 2016 6:28 AM Points: 3,977, Visits: 6,431
 ChrisM@Work (8/28/2013)John's correct, here's another way:`SELECT col1 = MIN(CASE WHEN t.value = x.min_value THEN value ELSE NULL END), col2 = MIN(CASE WHEN t.value = x.min_value THEN t_stamp ELSE NULL END), col3 = MIN(CASE WHEN t.value = x.max_value THEN value ELSE NULL END), col4 = MIN(CASE WHEN t.value = x.max_value THEN t_stamp ELSE NULL END) FROM @t tCROSS APPLY ( SELECT min_value = MIN(value), max_value = MAX(value) FROM @t) xWHERE t.value IN (x.min_value, x.max_value)`+1 for a bizarre but wonderful query!It inspired me to come up with this:`SELECT col1 = MIN(CASE WHEN a.t_stamp = mv1 THEN value ELSE NULL END), col2 = MIN(CASE WHEN a.t_stamp = mv1 THEN t_stamp ELSE NULL END), col3 = MIN(CASE WHEN a.t_stamp = mv2 THEN value ELSE NULL END), col4 = MIN(CASE WHEN a.t_stamp = mv2 THEN t_stamp ELSE NULL END) FROM (SELECT value, t_stamp ,mv1=MIN(t_stamp) OVER (PARTITION BY (SELECT NULL)) ,mv2=MAX(t_stamp) OVER (PARTITION BY (SELECT NULL))FROM @t) aWHERE t_stamp = mv1 OR t_stamp = mv2` My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!My thought question: Have you ever been told that your query runs too fast?My advice:INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.Need to UNPIVOT? Why not CROSS APPLY VALUES instead?Since random numbers are too important to be left to chance, let's generate some!Learn to understand recursive CTEs by example.Splitting strings based on patterns can be fast!My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Post #1496218
 Posted Thursday, September 19, 2013 12:42 AM
 Say Hey Kid Group: General Forum Members Last Login: Wednesday, November 30, 2016 11:13 PM Points: 698, Visits: 935
 So many replies,,,,,, so much of learning.!
Post #1496233
 Posted Thursday, September 19, 2013 1:53 AM
 SSC Rookie Group: General Forum Members Last Login: Thursday, January 22, 2015 1:55 AM Points: 42, Visits: 166
 OMG so many ways!!! This community is great
Post #1496248
 Posted Wednesday, September 25, 2013 1:03 AM
 SSC Journeyman Group: General Forum Members Last Login: Thursday, August 14, 2014 4:48 AM Points: 76, Visits: 346
 hi, check this query..; withRECORD_SET1 (MIN_VALUE, MIN_TSTAMP) AS (SELECT min(VALUE), min(T_STAMP) from @t WHERE VALUE = (SELECT min(VALUE) FROM @t)),RECORD_SET2 (MAX_VALUE, MAX_TSTAMP) AS (SELECT min(VALUE), min(T_STAMP) from @t WHERE VALUE = (SELECT max(VALUE) FROM @t))SELECT * FROM RECORD_SET1 ,RECORD_SET2;
Post #1498146

 Permissions