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»»

How to query same table with 2 WHERE clause onto 1 row Expand / Collapse
Author
Message
Posted Wednesday, August 28, 2013 1:16 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, September 8, 2014 7:19 AM
Points: 42, Visits: 161
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 @t
WHERE value = (SELECT min(value) FROM @t)

SELECT min(value), min(t_stamp) from @t
WHERE value = (SELECT max(value) FROM @t)
Post #1489036
Posted Wednesday, August 28, 2013 1:26 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 3:16 AM
Points: 5,386, Visits: 9,964
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.bstamp
FROM
a
CROSS JOIN b

John
Post #1489041
Posted Wednesday, August 28, 2013 1:28 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 2:44 AM
Points: 6,750, Visits: 13,898
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 t
CROSS APPLY (
SELECT min_value = MIN(value), max_value = MAX(value)
FROM @t
) x
WHERE 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 Shaw

For 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 Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1489042
Posted Friday, August 30, 2013 3:28 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, September 8, 2014 7:19 AM
Points: 42, Visits: 161
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


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 2:44 AM
Points: 6,750, Visits: 13,898
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 Shaw

For 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 Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1490049
Posted Tuesday, September 17, 2013 4:12 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, August 25, 2014 10:53 PM
Points: 160, Visits: 235
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

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 11:32 PM
Points: 3,420, Visits: 5,351
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 t
CROSS APPLY (
SELECT min_value = MIN(value), max_value = MAX(value)
FROM @t
) x
WHERE 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) a
WHERE 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!
Post #1496218
Posted Thursday, September 19, 2013 12:42 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Yesterday @ 2:47 AM
Points: 467, Visits: 615
So many replies,,,,,, so much of learning.!
Post #1496233
Posted Thursday, September 19, 2013 1:53 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, September 8, 2014 7:19 AM
Points: 42, Visits: 161
OMG so many ways!!! This community is great
Post #1496248
Posted Wednesday, September 25, 2013 1:03 AM


SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, August 14, 2014 4:48 AM
Points: 76, Visits: 346
hi, check this query..


; with
RECORD_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
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse