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 |
hey001us
Posting Yak Master
185 Posts |
Posted - 2007-07-02 : 16:08:33
|
Is there any best way to find tune this:I am comparing some value with around five servers, but takes long time.I need to show the exception if the values are not matching SELECT customerid, s1.status s1, s2.status s2, s3.status s3, s4.status s4, s5.status s5FROM (select customerid, name, status from server1) s1JOIN (select customerid, name, status from server2) s2 on s1.customerid = s2.customeridJOIN (select customerid, name, status from server3) s3 on s1.customerid = s3.customeridLEFT JOIN (select customerid, name, status from server4) s4 on s1.customerid = s4.customeridLEFT (select customerid, name, status from server5) s5 on s1.customerid = s5.customeridwhere s1.status <> s2.statusand s1.status <> s3.statusand s1.status <> s4.statusand s1.status <> s5.statushey |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-02 : 16:22:07
|
What is yor objective?With your query as of now, you will get customers with same status from server3 and server4, becuase they are both inequal to customer from server1.Example: Customer #100 from Server1 has status 'a', customer #100 from server3 has status 'b' and customer #100 from server4 has status 'b', is a valid combination.Peter LarssonHelsingborg, Sweden |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-02 : 16:24:09
|
this might do the same thingselect customerid from (SELECT customerid, status from server1 union allSELECT customerid, status from server2 union allSELECT customerid, status from server3 union allSELECT customerid, status from server4 union allSELECT customerid, status from server5) as dgroup by customerid having min(status) <> max(status) unless you provide better explanation, or some sample data and expected output, your problem may be unsolved.Peter LarssonHelsingborg, Sweden |
 |
|
hey001us
Posting Yak Master
185 Posts |
Posted - 2007-07-02 : 16:25:18
|
yes its a valid combination. i need to display the customer #100 only.thanks Peterhey |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-02 : 16:35:24
|
What is performance with suggestion posted 07/02/2007 : 16:24:09 ?Peter LarssonHelsingborg, Sweden |
 |
|
hey001us
Posting Yak Master
185 Posts |
Posted - 2007-07-02 : 16:40:45
|
I have given below the sample date.select 'c1' customerid, 'a' status into #server1union allselect 'c2' customerid, 'b' statusunion allselect 'c3' customerid, 'z' statusunion allselect 'c4' customerid, 'a' statusselect 'c1' customerid, 'a' status into #server2union allselect 'c2' customerid, 'b' statusunion allselect 'c3' customerid, 'z' statusunion allselect 'c4' customerid, 'a' statusselect 'c1' customerid, 'x' status into #server3union allselect 'c2' customerid, 'b' statusunion allselect 'c3' customerid, 'c' statusunion allselect 'c4' customerid, 'a' statusselect 'c1' customerid, 'x' status into #server4union allselect 'c2' customerid, 'b' statusunion allselect 'c3' customerid, 'c' statusunion allselect 'c4' customerid, 'a' statusunion allselect 'c5' customerid, 'a' statusunion allselect 'c6' customerid, 'a' statusselect 'c1' customerid, 'x' status into #server5union allselect 'c2' customerid, 'j' statusunion allselect 'c3' customerid, 'c' statusunion allselect 'c4' customerid, 'a' statusunion allselect 'c5' customerid, 'a' statusSELECT s1.customerid, s1.status s1, s2.status s2, s3.status s3, s4.status s4, s5.status s5FROM (select customerid, status from #server1) s1JOIN (select customerid, status from #server2) s2 on s1.customerid = s2.customeridJOIN (select customerid, status from #server3) s3 on s1.customerid = s3.customeridLEFT JOIN (select customerid, status from #server4) s4 on s1.customerid = s4.customeridLEFT JOIn (select customerid, status from #server5) s5 on s1.customerid = s5.customeridwhere s1.status <> s2.statusor s1.status <> s3.statusor s1.status <> s4.statusor s1.status <> s5.statushey |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-02 : 16:44:49
|
Is server1 some kind of "master" server?Because this sample data is also validinsert #server1 select 'c0', 'd'insert #server2 select 'c0', 'g'insert #server3 select 'c0', 'g'insert #server4 select 'c0', 'g'insert #server5 select 'c0', 'g' Peter LarssonHelsingborg, Sweden |
 |
|
hey001us
Posting Yak Master
185 Posts |
Posted - 2007-07-02 : 16:46:00
|
yes Peterhey |
 |
|
hey001us
Posting Yak Master
185 Posts |
Posted - 2007-07-02 : 16:52:12
|
then the out put i need as follows:c0 d g g g gc1 a a x x xc2 b b b b jc3 z z c c chey |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-02 : 16:52:19
|
Are you only interested in the CustomerID's? Where the status on Server2 to Server5 is different from the status on Server1?Try thisSELECT DISTINCT s1.CustomerIDFROM #Server1 AS s1LEFT JOIN ( SELECT CustomerID, Status FROM #Server2 UNION ALL SELECT CustomerID, Status FROM #Server3 UNION ALL SELECT CustomerID, Status FROM #Server4 UNION ALL SELECT CustomerID, Status FROM #Server5 ) AS s ON s.CustomerID = s1.CustomerID AND s.Status <> s1.Status Peter LarssonHelsingborg, Sweden |
 |
|
hey001us
Posting Yak Master
185 Posts |
Posted - 2007-07-02 : 16:53:31
|
nope, we need which server the status is wrong as well.hey |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-02 : 16:55:24
|
Your "JOIN" should be edited to "LEFT JOIN", as the other servers.Peter LarssonHelsingborg, Sweden |
 |
|
hey001us
Posting Yak Master
185 Posts |
Posted - 2007-07-02 : 16:59:28
|
thanks much, i am working on it now.hey |
 |
|
hey001us
Posting Yak Master
185 Posts |
Posted - 2007-07-02 : 17:00:44
|
only server 4 and 5 is left joinhey |
 |
|
hey001us
Posting Yak Master
185 Posts |
Posted - 2007-07-02 : 17:10:08
|
I got some idea based on you SQL. Cheershey |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-02 : 17:14:33
|
[code]SELECT s1.customerid, s1.status AS s1, s2.status AS s2, s3.status AS s3, s4.status AS s4, s5.status AS s5FROM #server1 AS s1INNER JOIN #server2 AS s2 on s2.customerid = s1.customeridINNER JOIN #server3 AS s3 on s3.customerid = s1.customeridLEFT JOIN #server4 AS s4 on s4.customerid = s1.customeridLEFT JOIN #server5 AS s5 on s5.customerid = s1.customeridwhere s1.status <> s2.status or s1.status <> s3.status or s1.status <> s4.status or s1.status <> s5.status[/code]Peter LarssonHelsingborg, Sweden |
 |
|
hey001us
Posting Yak Master
185 Posts |
Posted - 2007-07-02 : 17:19:55
|
Thanks much.hey |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-07-02 : 17:25:01
|
Here is a query that runs twice as fast (20 reads instead of 44).select customerid, max(s1) as s1, max(s2) as s2, max(s3) as s3, max(s4) as s4, max(s5) as s5from ( select customerid, status as s1, '' as s2, '' as s3, '' as s4, '' as s5 from #server1 union all select customerid, null, status, '', '', '' from #server2 union all select customerid, null, '', status, '', '' from #server3 union all select customerid, null, '', '', status, '' from #server4 union all select customerid, null, '', '', '', status from #server5 ) as dgroup by customeridhaving max(s1) <> max(s2) or max(s1) <> max(s3) or max(s1) <> max(s4) or max(s1) <> max(s5) Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|
|