Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
duanecwilson
Constraint Violating Yak Guru
273 Posts |
Posted - 2014-05-15 : 12:17:37
|
How would I convert this query to use a JOIN in the subquery:SELECT ProcDate , MAX(b.DayKey) DayKey FROM dbo.MyTable a , ( SELECT MAX(DayKey) DayKey FROM dbo.MyTable WHERE ExtractType = 'D' ) bWHERE ( ExtractType = 'D' ) AND a.Daykey = b.DayKey GROUP BY ProcDate Thanks.Duane |
|
duanecwilson
Constraint Violating Yak Guru
273 Posts |
Posted - 2014-05-15 : 12:50:06
|
I think I have answered my own question. Anyone let me know if it seems like this is incorrect or I am missing anything. It is supposed to yield a single row:I Do think I have found the answer to my own question. It does work:SELECT a.ProcDate, MAX(b.DayKey) DayKey FROM dbo.MyTable aINNER JOIN ( SELECT MAX(DayKey) DayKey FROM dbo.MyTable WHERE ExtractType = 'D' ) bON a.DayKey = b.DayKeyWHERE ( a.ExtractType = 'D' )GROUP BY a.ProcDate Thank you.Duane |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2014-05-15 : 14:22:51
|
[code];WITH b AS( SELECT MAX(DayKey) AS DayKey FROM dbo.MyTable a WHERE ExtractType = 'D')SELECT MAX(ProcDate) AS ProcDate, a.DayKeyFROM MyTable aJOIN b ON a.DayKey = b.DayKey AND a.ExtractType = 'D'GROUP BY a.DayKey[/code]Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
ScottPletcher
Aged Yak Warrior
550 Posts |
Posted - 2014-05-15 : 18:00:49
|
I think the code below is functionally equivalent, but less overhead and much clearer as to what is going on:SELECT DISTINCT ProcDate, DayKeyFROM dbo.MyTable aWHERE ExtractType = 'D' AND DayKey = ( SELECT MAX(DayKey) FROM dbo.MyTable WHERE ExtractType = 'D' ) |
|
|
duanecwilson
Constraint Violating Yak Guru
273 Posts |
Posted - 2014-05-16 : 14:32:00
|
Thank you for both of these. I am going to try these and put them into my inventory of possible solutions.Duane |
|
|
|
|
|