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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Qurey showing all the rows in table

Author  Topic 

velvettiger
Posting Yak Master

115 Posts

Posted - 2008-06-10 : 18:16:03
Hi guys,

The below query should produce all those customers who are in 'orginalerrorfiles' but not in 'hopefullyworks2'. 3 fields will be used as the check to make sure that the rows, that r identical won't be displayed but those that r different will be displayed.

Instead of doing that it just display everything in the table called 'orginalerrorfiles'.

Note 'orginalerrorfiles' consist of 141455 rows
'hopefullyworks2' consist of 134784 rows


select o.card_no
,o.ref_no
,o.tran_val

from OriginalErrorFiles o

where exists (select h.card_no
,h.ref_no
,h.tran_val
from hopefullyworks2 h
where( h.card_no = o.card_no and
h.ref_no=o.ref_no and
h.tran_val = o.tran_val
))



tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-10 : 18:18:13
...where not exists...

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

velvettiger
Posting Yak Master

115 Posts

Posted - 2008-06-10 : 18:19:49
even with the where not exists it is still giving me all the fields in the origincalerrorfiles table
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-10 : 18:21:17
I don't understand what you mean. Please show us a data example.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

velvettiger
Posting Yak Master

115 Posts

Posted - 2008-06-10 : 18:29:42
Hi guys,

Ok I have included a data sample.

Basically i want those people who are in 'originalerrorfiles' but not in the 'hopefullyworks2 ' tables.

As you can see card_no,ref_no and tran_value are used/combined to see whether or not a row should be included in the final resultset.

e.g Original
card_no ref_no tran_value
12 20 12.00
12 20 2.00
47 01 4.00
52 11 2.00
04 05 13.00
04 20 6.00



e.g Posted
card_no ref_no tran_value
12 20 12.00
12 21 2.00
47 01 4.00
52 25 25.00
04 20 6.00



e.g Result
card_no ref_no tran_value
12 20 2.00
52 11 2.00
04 05 13.00
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-10 : 18:39:38
"where not exists" works fine for me.

Check this sample out using the data you provided:


DECLARE @original table (card_no tinyint, ref_no tinyint, tran_value decimal(4,2))

INSERT INTO @original VALUES(12,20,12.00)
INSERT INTO @original VALUES(12,20,2.00)
INSERT INTO @original VALUES(47,1,4.00)
INSERT INTO @original VALUES(52,11,2.00)
INSERT INTO @original VALUES(4,5,13.00)
INSERT INTO @original VALUES(4,20,6.00)

DECLARE @posted table (card_no tinyint, ref_no tinyint, tran_value decimal(4,2))

INSERT INTO @posted VALUES(12,20,12.00)
INSERT INTO @posted VALUES(12,21,2.00)
INSERT INTO @posted VALUES(47,1,4.00)
INSERT INTO @posted VALUES(52,25,25.00)
INSERT INTO @posted VALUES(4,20,6.00)

SELECT *
FROM @original o
WHERE NOT EXISTS
(SELECT * FROM @posted p WHERE o.card_no = p.card_no AND o.ref_no = p.ref_no AND o.tran_value = p.tran_value)


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -