SQL 2005 - Exclude records from Select

Experts,
I use the following query today
select
	REF,
	DATE,
	LEV1,
	LEV2,
	COALESCE(MAX(FROM_CODE),'UNR')as FROM_CODE,
	COALESCE(SUM(FROM_QTY), (SUM(TO_QTY)*-1))as FROM_QTY,
	COALESCE(MAX(TO_CODE),'UNR')as TO_CODE,
	COALESCE(SUM(TO_QTY), (SUM(FROM_QTY)*-1)) as TO_QTY	
	FROM BVF_947
	GROUP BY REF, DATE, LEV1, LEV2

Open in new window


Before the Group BY, each row will contain a FROM_CODE or a TO_CODE, but rarely both.
After the Group By each summarized record will always have both a FROM_CODE and TO_CODE

What I want to do, is exlcude any rows where the value is the same in both columns.  If we need to specify them there are only 4 values (UNR, QI, BLK, DIS or XX).  Can anyone help me do this?
LVL 1
JDCamAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike EghtebasDatabase and Application DeveloperCommented:
;with D
As
(
select
	REF,
	DATE,
	LEV1,
	LEV2,
	COALESCE(MAX(FROM_CODE),'UNR')as FROM_CODE,
	COALESCE(SUM(FROM_QTY), (SUM(TO_QTY)*-1))as FROM_QTY,
	COALESCE(MAX(TO_CODE),'UNR')as TO_CODE,
	COALESCE(SUM(TO_QTY), (SUM(FROM_QTY)*-1)) as TO_QTY	
	FROM BVF_947
	GROUP BY REF, DATE, LEV1, LEV2
)
Select * From D
Where FROM_CODE <> TO_CODE;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike EghtebasDatabase and Application DeveloperCommented:
To include counts with a separate query:

;with D
As
(
select
	REF,
	DATE,
	LEV1,
	LEV2,
	COALESCE(MAX(FROM_CODE),'UNR')as FROM_CODE,
	COALESCE(SUM(FROM_QTY), (SUM(TO_QTY)*-1))as FROM_QTY,
	COALESCE(MAX(TO_CODE),'UNR')as TO_CODE,
	COALESCE(SUM(TO_QTY), (SUM(FROM_QTY)*-1)) as TO_QTY	
	FROM BVF_947
	GROUP BY REF, DATE, LEV1, LEV2
)
Select *, Count(*) From_To_Matches From D
Where FROM_CODE = TO_CODE

Open in new window

0
JDCamAuthor Commented:
Excellent!!  Thanks
0
Mike EghtebasDatabase and Application DeveloperCommented:
If you like to take a look at different types of subqueries other than Common Table Expression (CTE) used here, please see this link.
Subqueries
0
Mike EghtebasDatabase and Application DeveloperCommented:
correction on the count solution:
;with D
As
(
select
	REF,
	DATE,
	LEV1,
	LEV2,
	COALESCE(MAX(FROM_CODE),'UNR')as FROM_CODE,
	COALESCE(SUM(FROM_QTY), (SUM(TO_QTY)*-1))as FROM_QTY,
	COALESCE(MAX(TO_CODE),'UNR')as TO_CODE,
	COALESCE(SUM(TO_QTY), (SUM(FROM_QTY)*-1)) as TO_QTY	
	FROM BVF_947
	GROUP BY REF, DATE, LEV1, LEV2
)
Select REF, Count(REF) From_To_Matches From D
Where FROM_CODE = TO_CODE
Group By REF;

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.