MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Comparison query

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
showkath
Beginner


Joined: 23 Mar 2005
Posts: 17
Topics: 7

PostPosted: Mon Sep 26, 2005 7:34 am    Post subject: Comparison query Reply with quote

Hi,

Could you help me in writing a query for the below requirement
I need to find out the difference between two tables as follows

Table A:

Fld1, Fld2

Table B:

Fld1, Fld2

I want to count number of records in both the tables
for fld1=x and fld2=y (fox all x and y)
_________________
Regards,
Showkath
-----------------------------------------------------
Our opinions do not really blossom into fruition until we have expressed them to someone else - Mark Twain
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12375
Topics: 75
Location: San Jose

PostPosted: Mon Sep 26, 2005 7:52 am    Post subject: Reply with quote

Showkath,

Try this

Code:

SELECT (CASE SUM(A.CNT)                     
             WHEN 0 THEN 'THE COUNTS MATCH' 
             ELSE  'THE COUNTS ARE OFF'     
        END)                                 
  FROM (SELECT COUNT(*) AS CNT               
          FROM TABLE1
         WHERE FLD1 = X
           AND FLD2 = Y
         UNION ALL                           
        SELECT COUNT(*) * -1 AS CNT         
          FROM TABLE2
         WHERE FLD1 = X
           AND FLD2 = Y) A                 
 ;                                           


Hope this helps...

Cheers

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
vkphani
Intermediate


Joined: 05 Sep 2003
Posts: 483
Topics: 48

PostPosted: Mon Sep 26, 2005 8:03 am    Post subject: Reply with quote

SELECT COUNT(*) AS COUNT1 FROM TABLEA WHERE FLD1.TABLEA = 'X';

SELECT COUNT(*) AS COUNT2 FROM TABLEA WHERE FLD2.TABLEA = 'Y';

SELECT COUNT(*) AS COUNT3 FROM TABLEB WHERE FLD1.TABLEB = 'X';

SELECT COUNT(*) AS COUNT4 FROM TABLEB WHERE FLD2.TABLEB = 'Y';
Back to top
View user's profile Send private message Send e-mail
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12375
Topics: 75
Location: San Jose

PostPosted: Mon Sep 26, 2005 8:13 am    Post subject: Reply with quote

vkphani,

If you ever decide to answer a question make sure that you understand the question. Please do not give answers which will not serve any purpose except for confusing the poster. read showkath's question once again carefully. He needs to find the difference in counts between the 2 tables. Your queries just gives 4 counts which does not help showkath in any way.

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
vkphani
Intermediate


Joined: 05 Sep 2003
Posts: 483
Topics: 48

PostPosted: Mon Sep 26, 2005 8:24 am    Post subject: Reply with quote

Kolusu,

COUNT1 - COUNT3 and COUNT2 - COUNT4 will give the desire result after finding the counts right.
I was suppose to mention this in my last post. By mistake I have submitted without these details.
Back to top
View user's profile Send private message Send e-mail
showkath
Beginner


Joined: 23 Mar 2005
Posts: 17
Topics: 7

PostPosted: Mon Sep 26, 2005 10:27 pm    Post subject: Reply with quote

Thanks guys for your excellent suppport.
_________________
Regards,
Showkath
-----------------------------------------------------
Our opinions do not really blossom into fruition until we have expressed them to someone else - Mark Twain
Back to top
View user's profile Send private message
showkath
Beginner


Joined: 23 Mar 2005
Posts: 17
Topics: 7

PostPosted: Thu Sep 29, 2005 5:16 am    Post subject: Comparison query Reply with quote

Hi Kolusu,

Query suggested by you works fine. With that I have tried the below query
to display the number of records if the count does not match from both the
tables, for particular value of fld1 and fld2.


SELECT Y.LOT2,Y.CNT2,Z.LOT3,Z.CNT3
FROM

(SELECT A.fld1 AS LOT2,COUNT(*) AS CNT2
FROM table1 A
WHERE A.fld2 = 1 GROUP BY A.fld1 ) Y,

(SELECT B.LOT AS LOT3,COUNT(*) AS CNT3
FROM table2 B
WHERE B.fld2 = 1 GROUP BY B.fld1 ) Z

But it ends up in correlated sub-query.

i.e the count and fld1 is displaying correctly for y.lot2 and y.cnt2, but the first value of Z.lot3 and z.cnt3 is repeating.

Could you pls help me to write the appropriate query.

to summarize I want to display all the fld1 and the count wherever
teh corresponding recs does not match
_________________
Regards,
Showkath
-----------------------------------------------------
Our opinions do not really blossom into fruition until we have expressed them to someone else - Mark Twain
Back to top
View user's profile Send private message
Phantom
Data Mgmt Moderator
Data Mgmt Moderator


Joined: 07 Jan 2003
Posts: 1056
Topics: 91
Location: The Blue Planet

PostPosted: Thu Sep 29, 2005 5:41 am    Post subject: Reply with quote

showkath,

Code:

(SELECT B.LOT AS LOT3,COUNT(*) AS CNT3
FROM table2 B
WHERE B.fld2 = 1 GROUP BY B.fld1 ) Z


Check this piece of query. You have used B.LOT in the select statement. Shouldn't this be B.FLD1 ?

Quote:

to summarize I want to display all the fld1 and the count wherever
teh corresponding recs does not match


You don't seem to compare the count returned from Table 1 and Table 2 anywhere, So you will end up displaying Field 1 from Table 1 followed by its count, then Field 1 from table 2 followed by its count irrespective of whether the count matches or not.

Thanks,
Phantom
Back to top
View user's profile Send private message
showkath
Beginner


Joined: 23 Mar 2005
Posts: 17
Topics: 7

PostPosted: Thu Sep 29, 2005 10:41 pm    Post subject: Reply with quote

Phantom,

You are right. It should be B.FLD1 instead of B.LOT.


I want to select the four columns and compare them side by side for the corresponding Fld1. That's why i am not comparing the counts in the query.

If I run the two queries separately, I am getting the expected result. But I want to combine the query and see the difference.
_________________
Regards,
Showkath
-----------------------------------------------------
Our opinions do not really blossom into fruition until we have expressed them to someone else - Mark Twain
Back to top
View user's profile Send private message
Phantom
Data Mgmt Moderator
Data Mgmt Moderator


Joined: 07 Jan 2003
Posts: 1056
Topics: 91
Location: The Blue Planet

PostPosted: Fri Sep 30, 2005 12:35 am    Post subject: Reply with quote

showkath,

You are missing one more thing in your query. Your query will produce A x B output since you did not use any join b/w the two tables.

For example assume that you inner query 1 returns 3 rows

Code:

(SELECT
      A.FLD1   AS LOT2,
      COUNT(*) AS CNT2
 FROM
      TABLE1 A
 WHERE
      A.FLD2 = 1 GROUP BY A.FLD1 ) Y,

Output:
LOT2     CNT2
----     ----
F1          3
F2          5
F3          2


Also, let assume that your inner query 2, returns 2 rows.
Code:

(SELECT
      B.FLD1   AS LOT3,
      COUNT(*) AS CNT3
 FROM
      TABLE2 B
 WHERE
      B.FLD2 = 1 GROUP BY B.FLD1 ) Z

Output:
LOT3     CNT3
----     ----
F1          1
F2          9


Since there is no join you will end up producing 3 x 2 = 6 rows as shown below.

Code:

LOT2     CNT2     LOT3     CNT3
----     ----     ----     ----
F1       3        F1          1
F2       5        F1          1
F3       2        F1          1
F1       3        F2          9
F2       5        F2          9
F3       2        F2          9


You already noticed this -
Quote:
but the first value of Z.lot3 and z.cnt3 is repeating
. If you come down, you will find the subsequent records of Z repeating.

I'm not good in SQL but try this.
Code:

SELECT
     Y.LOT2,
     Y.CNT2,
     Z.LOT3,
     Z.CNT3
FROM
     (SELECT
           A.FLD1   AS LOT2,
           COUNT(*) AS CNT2
      FROM
           TABLE1 A
      WHERE
           A.FLD2 = 1 GROUP BY A.FLD1 ) Y,
     (SELECT
           B.FLD1   AS LOT3,
           COUNT(*) AS CNT3
      FROM
           TABLE2 B
      WHERE
           B.FLD2 = 1 GROUP BY B.FLD1 ) Z
WHERE
     Y.LOT2 = Z.LOT3


Thanks,
Phantom
Back to top
View user's profile Send private message
Phantom
Data Mgmt Moderator
Data Mgmt Moderator


Joined: 07 Jan 2003
Posts: 1056
Topics: 91
Location: The Blue Planet

PostPosted: Fri Sep 30, 2005 12:36 am    Post subject: Reply with quote

Nothing much... Just included the Where condition.

Code:

WHERE
     Y.LOT2 = Z.LOT3


Does this solve your problem ?

Thanks,
Phantom
Back to top
View user's profile Send private message
showkath
Beginner


Joined: 23 Mar 2005
Posts: 17
Topics: 7

PostPosted: Fri Sep 30, 2005 3:19 am    Post subject: Reply with quote

Phantom,

Thanks for that. It really solved my problem.
_________________
Regards,
Showkath
-----------------------------------------------------
Our opinions do not really blossom into fruition until we have expressed them to someone else - Mark Twain
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group