SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to query same table with 2 WHERE clause onto 1 row


How to query same table with 2 WHERE clause onto 1 row

Author
Message
brett.y
brett.y
SSC Veteran
SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)

Group: General Forum Members
Points: 264 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 @t
WHERE value = (SELECT min(value) FROM @t)

SELECT min(value), min(t_stamp) from @t
WHERE value = (SELECT max(value) FROM @t)

John Mitchell-245523
John Mitchell-245523
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: General Forum Members
Points: 35251 Visits: 16683
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
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42176 Visits: 20012
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
brett.y
brett.y
SSC Veteran
SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)

Group: General Forum Members
Points: 264 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.:-)
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42176 Visits: 20012
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
Dscheypie
Dscheypie
SSC-Addicted
SSC-Addicted (498 reputation)SSC-Addicted (498 reputation)SSC-Addicted (498 reputation)SSC-Addicted (498 reputation)SSC-Addicted (498 reputation)SSC-Addicted (498 reputation)SSC-Addicted (498 reputation)SSC-Addicted (498 reputation)

Group: General Forum Members
Points: 498 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.
dwain.c
dwain.c
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18123 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Mr. Kapsicum
Mr. Kapsicum
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2019 Visits: 1037
So many replies,,,,,, so much of learning.! :-)
brett.y
brett.y
SSC Veteran
SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)

Group: General Forum Members
Points: 264 Visits: 166
OMG so many ways!!! This community is great :-)
sumith1andonly1
sumith1andonly1
SSC-Addicted
SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)SSC-Addicted (468 reputation)

Group: General Forum Members
Points: 468 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;
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search