SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Need Efficient Way To Highlight Rows With Changes.
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Jawad
Starting Member

Pakistan
17 Posts

Posted - 08/06/2012 :  14:54:28  Show Profile  Reply with Quote
Hi
SQL Server 2005
Data loads in my table every day with a new loaded(last column) as follows.

CREATE TABLE T1
(
colId		int,
col2		varchar(10),
col3		varchar(10),
col4		varchar(10),
colLoadId	int
);

INSERT INTO t1 VALUES (6,'abc','pqr','xyz',1);
INSERT INTO t1 VALUES (7,'abc','pqt','xzy',1);
INSERT INTO t1 VALUES (6,'abc','pqr','xyz',2);
INSERT INTO t1 VALUES (7,'abc','pqr','xyz',2);

SELECT * FROM t1;

colId	col2	col3	col4	colLoadId
6	abc	pqr	xyz	1
7	abc	pqt	xzy	1
6	abc	pqr	xyz	2
7	abc	pqr	xyz	2

On the base columns (ColId and ColLoadId) and I need to find which ColId's date change in this load.

I do it like follows:

SELECT t.Colid
       ,t.Col2
       ,t.Col3
       ,t.Col4 
  FROM t1 t
  JOIN t1 t2
    ON (t.colId=t2.colId)
 WHERE t.colLoadId=1
   AND t2.colLoadId=2
   AND (t.Col2<>t2.Col2
    OR  t.Col3<>t2.Col3
    OR  t.Col4<>t2.Col4)

It serves my purpose but in actual my table got 30+ columns to compare for every record.
I feel adding 30+ 'OR' clauses to compare every row is not the best way to do it.
So can anyone guide me a more efficient way to compare rows on the base of (ColId and ColLoadId)?

Wishes

robvolk
Most Valuable Yak

USA
15636 Posts

Posted - 08/06/2012 :  15:12:04  Show Profile  Visit robvolk's Homepage  Reply with Quote
SELECT Colid,Col2,Col3,Col4 FROM t1 WHERE colLoadId=1
EXCEPT
SELECT Colid,Col2,Col3,Col4 FROM t1 WHERE colLoadId=2
Go to Top of Page

Jawad
Starting Member

Pakistan
17 Posts

Posted - 08/06/2012 :  15:23:46  Show Profile  Reply with Quote
Thanks robvolk
Go to Top of Page

Jawad
Starting Member

Pakistan
17 Posts

Posted - 10/02/2012 :  08:25:05  Show Profile  Reply with Quote
quote:
Originally posted by robvolk

SELECT Colid,Col2,Col3,Col4 FROM t1 WHERE colLoadId=1
EXCEPT
SELECT Colid,Col2,Col3,Col4 FROM t1 WHERE colLoadId=2



Hi robvolk

you solutions is exactly what I want.
Now I wish to go one step farther in performance enhansement and need your help again please.

I am doing it like follows:

SELECT COUNT(*)
  FROM
       (
        SELECT Colid,Col2,Col3,Col4 FROM t1 WHERE colLoadId=1
        EXCEPT
        SELECT Colid,Col2,Col3,Col4 FROM t1 WHERE colLoadId=2
        )


but it compare all rows all the times, I wish that it exit at 1st row with the difference and give me output of 1 instead of all row comparison.

Wishes
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 10/02/2012 :  09:37:08  Show Profile  Reply with Quote
The following may be faster if you have lot of differences. If there are only one or two rows that qualify then they are likely to perform the same.
IF EXISTS
       (
        SELECT Colid,Col2,Col3,Col4 FROM t1 WHERE colLoadId=1
        EXCEPT
        SELECT Colid,Col2,Col3,Col4 FROM t1 WHERE colLoadId=2
       )
	SELECT 1
ELSE 
	SELECT 0;
Go to Top of Page

Jawad
Starting Member

Pakistan
17 Posts

Posted - 10/02/2012 :  09:48:59  Show Profile  Reply with Quote
quote:
Originally posted by sunitabeck

The following may be faster if you have lot of differences. If there are only one or two rows that qualify then they are likely to perform the same.
IF EXISTS
       (
        SELECT Colid,Col2,Col3,Col4 FROM t1 WHERE colLoadId=1
        EXCEPT
        SELECT Colid,Col2,Col3,Col4 FROM t1 WHERE colLoadId=2
       )
	SELECT 1
ELSE 
	SELECT 0;





Thanks a lot sunitabeck
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.62 seconds. Powered By: Snitz Forums 2000