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 Thursday, September 26, 2013 3:52 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, September 3, 2014 2:36 PM
Points: 32, Visits: 403
SELECT
a.mins, a.stamp, b.mins, b.stamp
FROM
(SELECT min(value) as mins, min(t_stamp) as stamp
from @t
WHERE value = (SELECT min(value) FROM @t)) a,
(SELECT min(value) as mins, min(t_stamp) as stamp
from @t
WHERE value = (SELECT max(value) FROM @t)) b
Post #1499092
Posted Friday, September 27, 2013 1:13 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
Gotta say I like that last one ccavaco. Very easy to understand. (in fact the ONLY one I understand )
Thanks!
Post #1499222
Posted Friday, September 27, 2013 1:18 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:50 AM
Points: 5,369, Visits: 9,913
brett.y (9/27/2013)
in fact the ONLY one I understand

It's one of four very similar solutions that have been posted. The only ways they differ are as follows:
(1) Some use common table expressions (CTEs); some use subqueries
(2) Some use a comma to join the two tables; some use the CROSS JOIN operator

John
Post #1499223
Posted Friday, September 27, 2013 1:25 AM


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: Today @ 3:25 AM
Points: 3,648, Visits: 5,328
I'd bet money though that ChrisM's first solution, bizarre as it seems, is probably the fastest one suggested.

I had to dissect it myself before I figured out what he was doing. As you probably should. It is an excellent learning opportunity.



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 #1499228
Posted Friday, September 27, 2013 1:56 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
As a NooB to SQL I do tend to like what I actually understand (it was just nice to see a solution I understood before I ran it etc) .......................but your 100% correct, every answer I work through brings me on in SQL. Thanks again for sharing your expertise.
Post #1499238
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse