Solved

How to use the fields in one table to query items in another table to compare the number of occurences

Posted on 2011-03-17
8
190 Views
Last Modified: 2012-05-11
Hello,
I have two tables (table1 and table2) I need to compare fields between the tables
i.e. I need to determine the number of duplicates of table1.lp and the number of duplicates in table2.lp (that are equal to table1.lp values0 and calculate the difference

table1.lp          table2.lp   Diff
______________________________
5                         20           15
 
0
Comment
Question by:groovymonkey
  • 3
  • 3
  • 2
8 Comments
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
Are you looking for this?
select t1.lp,t1.t1_cnt,t2.t2_cnt,t2.t2_cnt - t1.t1_cnt as diff
  from (select lp,count(*) t1_cnt from table1 group by lp) t1
  join (select lp,count(*) t2_cnt from table1 group by lp) t2 
    on t1.lp = t2.lp

Open in new window

0
 

Author Comment

by:groovymonkey
Comment Utility
haha I am not sure...to clarify...I was in  a rush when posting the question....

I have one table that I know is full of duplicate records and I want to do several things....I need to...

1.  do a duplicate count for this table (table1)
2. and then query only the lp values in table 2 that are in table 1
3. and do a duplicate count for these values

for example lets say in table1 I have the following records

lp
ddd123
ddd123
ddd123
eee555
eee555

and in table2 I have the following records

lp
ddd123
ddd123
ddd123
ddd123
ddd123
www222
www222
eee555
eee555
eee555
eee555

I want the following output

lpcommon       table1sum       table2sum

ddd123                 3                    5
eee555                 2                    4

thanks
groovymonkey

0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
If you run my query, you will get what you are looking for.
0
 

Author Comment

by:groovymonkey
Comment Utility
I am running this and I am getting a syntax error message

select t1.lp,t1.t1_cnt,t2.t2_cnt,t2.t2_cnt - t1.t1_cnt as diff
  from (select lp,count(*) t1_cnt from t1 group by lp) t1
  join (select lp,count(*) t2_cnt from t1 group by lp) t2
    on t1.lp = t2.lp

Do you know where the error is?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 26

Expert Comment

by:Nick67
Comment Utility
Hakey Shakey Mang!
You're looking for an ugly little subquery Union All temptable Group by query!
That any askin' for much

First build your conditon.
You want only lp values that are in t1
SELECT lp FROM t1
That will be the subquery

Now you want the values of lp in t2 that are in t1
SELECT lp FROM t2 HAVING t2.lp in (SELECT lp FROM t1)

Now you want counts!
Thats where the union all is going to in

SELECT lp, Count(t1.lp) as T1Count, 0 as T2Count  from t1 GROUP BY t1.lp
UNION ALL
SELECT lp, T1Count as 0, Count(t2.lp) as T2Count FROM t2 GROUP BY t2.lp HAVING t2.lp in (SELECT lp from t1)

Now you have two records for each lp.
Some value and 0
0 and some value.
Group and sum them and you're there.
That's the temptable.

The earlier mess becomes the temptable and you select, sum and group from it

SELECT temptable.lp, Sum(temptable.T1Count) As T1FinalCount, Sum(temptable.T2Count) as T2FinalCount FROM(

SELECT lp, Count(t1.lp) as T1Count, 0 as T2Count  from t1 GROUP BY t1.lp
UNION ALL
SELECT lp, T1Count as 0, Count(t2.lp) as T2Count FROM t2 GROUP BY t2.lp HAVING t2.lp in (SELECT lp from t1)

) as temptable

GROUP BY temptable.lp;

Child's play!

Take the SQL from the code window
Using the Query Editor, one step at a time, you can get there
SELECT temptable.lp, Sum(temptable.T1Count) AS T1FinalCount, Sum(temptable.T2Count) AS T2FinalCount
FROM [SELECT t2.lp, Count(t2.lp) AS T2Count, 0 AS T1Count
FROM t2
GROUP BY t2.lp
HAVING (((t2.lp) In (SELECT t1.lp
FROM t1
)))


Union All

SELECT t1.lp, 0 as  T2Count, Count(t1.lp) AS T1Count
FROM t1
GROUP BY t1.lp]. AS temptable
GROUP BY temptable.lp;

Open in new window

0
 
LVL 26

Expert Comment

by:Nick67
Comment Utility
Oh, I forgot you wanted the difference too!
add
(Sum(temptable.T1Count) - Sum(temptable.T2Count)) AS CountDifference
0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
I dont see any error in my query. Post your "syntax" error.
0
 
LVL 26

Accepted Solution

by:
Nick67 earned 500 total points
Comment Utility
@Sharath_123

Your syntax is fairly bare.  The Access 2003 query editor certainly won't allow the skipping of the AS keyword.
'count(*) t1_cnt'
'count(*) t2_cnt'
If you don't give a calculated column a name in the Query by Example, Access will invent one to use with AS.
But you have to have one
I tried it.

Also the original post asked for a duplicate count for t1.  Period.
You have a join on t1.lp = t2.lp
Not a left or right join.
So he won't get a count or difference for t1.lp values that have no match in t2 in any event.

I've never seen syntax like yours.  Is it coming from the query editor in Access?
The way you use JOIN--I do that in the SQL Server Management Express-- but I didn't know that Access will join two temptables that way.
It won't join them with JOIN
It will with INNER JOIN -- although that doesn't give the OP's requirements.

This would work in the query editor

Select temp2.lp, cnt1, cnt2,  cnt2 - nz(cnt1,0) as diff from
(Select t2.lp, Count(t2.lp) as cnt1 from t2 group by  t2.lp) as temp1
right Join
(Select t1.lp, Count(t1.lp) as cnt2 from t1 group by t1.lp ) as temp2
on temp1.lp =temp2.lp

and is simpler than my original post


Select temp2.lp, cnt1, cnt2,  cnt2 - nz(cnt1,0) as diff from
(Select t2.lp, Count(t2.lp) as cnt1 from t2 group by  t2.lp) as temp1
right Join
(Select t1.lp, Count(t1.lp) as cnt2 from t1 group by t1.lp ) as temp2
on temp1.lp =temp2.lp

Open in new window

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now