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 |
|
jai2808
Starting Member
27 Posts |
Posted - 2009-05-26 : 09:36:51
|
| Hi,I need help in implementing the logic.I have data in a table as shown belowRecID Fname LName Address SID1 x Y abc 122 x Y xyz 12I need to match address or SID and get the results set as in this case SID is same but different address, still i need to get these records.In case if address is same and SID is different, i need both these records.I need to check if address or sID is same and get the results wrt to fname and lname.can some one help on this query. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-05-26 : 09:58:54
|
SELECT DISTINCT RecID Fname LName Address SID FROM Table1 E 12°55'05.63"N 56°04'39.26" |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2009-05-26 : 10:27:35
|
| Not sure what you want.Looks like rows with the same SID but different addresses and same address but different SID.Not sure what you mean by the rest but here's a startselect *from tblwhere SID in(select SIDfrom tblgroup by SIDhaving count(distinct address) > 1)or address in(select addressfrom tblgroup by addresshaving count(distinct SID) > 1)==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
jai2808
Starting Member
27 Posts |
Posted - 2009-05-29 : 05:06:09
|
| Sorry for not giving the exact queryhere is my requirement.I have a table along with the following dataFname Sname Address LegalIDA B xxx 0A B xxx 2A B yyy 2I need to write a query where F name and S Name are same and addess or legalid should be matching.The output should show all the 3 rows because, 1 and 2 record have same address and 2 and 3 record has legal id.I just want the fname and sname. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-05-29 : 06:22:42
|
[code]-- Prepare sample dataDECLARE @Source TABLE ( Fname VARCHAR(20), Sname VARCHAR(20), Address VARCHAR(20), LegalID INT )INSERT @Source SELECT 'A', 'B', 'xxx', 0 UNION ALLSELECT 'A', 'B', 'xxx', 2 UNION ALLSELECT 'A', 'B', 'yyy', 2-- Prepare staging areaDECLARE @Stage TABLE ( Fname VARCHAR(20), Sname VARCHAR(20), Address VARCHAR(20), LegalID INT, Match INT )insert @stage (fname, sname, [address], legalid)SELECT fname, sname, [address], legalidFROM @sourceUPDATE sSET s.Match = 1FROM @Stage AS sINNER JOIN ( SELECT Fname, Sname, [Address] FROM @Stage GROUP BY Fname, Sname, [Address] HAVING COUNT(*) > 1 ) AS d ON d.Fname = s.Fname AND d.Sname = s.Sname AND d.[Address] = s.[Address]UPDATE sSET s.Match = 2FROM @Stage AS sINNER JOIN ( SELECT Fname, Sname, LegalID FROM @Stage GROUP BY Fname, Sname, LegalID HAVING COUNT(*) > 1 ) AS d ON d.Fname = s.Fname AND d.Sname = s.Sname AND d.LegalID = s.LegalIDselect * from @stagewhere match is not null[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
|
|
|
|
|