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 |
|
Codesearcher
Starting Member
8 Posts |
Posted - 2011-12-07 : 00:00:52
|
| I have two QueriesQuery A returnsOPERPROP WELLCOMPL WELLNAME RESNO 6521 2162911 ST205 B-02 40756521 2355711 ST206 B-03 40756521 2106801 ST205 B-01 40771234 2222222 SOB-2509-01 7122Query B returnsWELLCOMPL RESName 1234567 A2222222 BLogic neededCompare the WellCompl no in Query A with Well compl no from Query Bfor OperProp 6521 i dont have any matching record so final query should return the last record of this groupwhen it reads wellCompl no 2222222 it has a match so the final query should return its matching record fro query BFinal result should be OPERPROP WELLCOMPL WELLNAME RESNO ResName6521 2106801 ST205 B-01 4077 NULL1234 2222222 SOB-2509-01 7122 BHow to implement this logic in SQL query? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-07 : 00:08:12
|
| [code]SELECT OPERPROP, WELLCOMPL, WELLNAME, RESNO, ResNameFROM(SELECT ROW_NUMBER() OVER (PARTITION BY a.OPERPROP,b.ResName ORDER BY a.WELLCOMPL DESC) AS Rn,a.OPERPROP, a.WELLCOMPL, a.WELLNAME, a.RESNO, b.ResNameFROM (QueryA)aLEFT JOIN (QueryB)bON b.WELLCOMPL = a.WELLCOMPL )tWHERE Rn=1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-12-07 : 00:19:34
|
how do you match the result of Query A to result of Query B ? using which column of A to match with which column of B ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|