[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

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

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
groovymonkey
Asked:
groovymonkey
  • 3
  • 3
  • 2
1 Solution
 
SharathData EngineerCommented:
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
 
groovymonkeyAuthor Commented:
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
 
SharathData EngineerCommented:
If you run my query, you will get what you are looking for.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
groovymonkeyAuthor Commented:
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
 
Nick67Commented:
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
 
Nick67Commented:
Oh, I forgot you wanted the difference too!
add
(Sum(temptable.T1Count) - Sum(temptable.T2Count)) AS CountDifference
0
 
SharathData EngineerCommented:
I dont see any error in my query. Post your "syntax" error.
0
 
Nick67Commented:
@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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now