SUM two SELECT COUNT Queries with GROUP BY - sql

I have two queries in a linked database (linking tables from two different project databases). I need to combine the queries to get a total count that is grouped by Interaction_Type1. The SQL code is as follows:
Query#1:
SELECT Sum(Temp.cnt) AS SumOfcnt, Temp.Interaction_Type1
FROM (SELECT COUNT(*) as cnt, Interaction_Type1 from AMERILOCKMasterConversionTable
GROUP BY Interaction_Type1
UNION ALL
SELECT COUNT(*), Interaction_Type2 from AMERILOCKMasterConversionTable
GROUP BY Interaction_Type2
UNION ALL
SELECT COUNT(*), Interaction_Type3 from AMERILOCKMasterConversionTable
GROUP BY Interaction_Type3
) AS Temp
GROUP BY Temp.Interaction_Type1;
and Query#2
SELECT Sum(Temp.cnt) AS SumOfcnt, Temp.Interaction_Type1
FROM (SELECT COUNT(*) as cnt, Interaction_Type1 from MARKETMasterConversionTable
GROUP BY Interaction_Type1
UNION ALL
SELECT COUNT(*), Interaction_Type2 from MARKETMasterConversionTable
GROUP BY Interaction_Type2
UNION ALL
SELECT COUNT(*), Interaction_Type3 from MARKETMasterConversionTable
GROUP BY Interaction_Type3
) AS Temp
GROUP BY Temp.Interaction_Type1;
I would like the query to yeild the following results:
Interaction_Type1 Total
Left_Message 23
Made_Contact 16
Bad_Phone_Number 8
No_Answer 12
I am brand new to SQL and have researched all of this online and have had no luck in combining these two queries to produce the desired results.
Any help would be GREATLY appreciated!!
Thanks!
Red

You should have something like
SELECT Integration_Type, SUM(*)
FROM ( SELECT Integration_Type, SumOfcnt FROM VIEW1
UNION ALL
SELECT Integration_Type, SumOfcnt FROM VIEW2)
GROUP BY Integration_Type
but before create views for queries you have provided here, or just gather all your queries in single view.

Related

How to combine SQL queries with count(*)?

I need to combine three below SQL queries and the result should be displayed with three columns(firstname,schoolname,locationname).
select count(*) as firstname from StudentRecord
select count(*) as schoolname from SchoolRecord
select count(*) as locationname from LocationRecord
Could you please clarify.
Thanks in advance.
select
(select count(*) from StudentRecord) as firstname,
(select count(*) from SchoolRecord) as schoolname,
(select count(*) from LocationRecord) as locationname
from dual
As already noticed, a bit add column names. And table names...

Union two tables with different column names

code snippet
As you can see from the results table, the union hasn't worked properly as there is more than one row for each make of car e.g. Toyota is listed for NG and KE in row 1 and for SA in row 3. Does anyone know how to join these tables more successfully?
Thank you!
How about making your last query another CTE, then grouping and summing the cnt on the new CTE?
WITH combined AS (
select title FROM autochek_ng
union all
select title FROM autocheck_kenya)
, brands AS (
SELECT LEFT(title, CHARINDEX(' ' , title)) as brand
FROM combined )
, aggregate as (
select brand, count(brand) as cnt
from brands
group by brand
UNION ALL
select make as brand, count(make) as cnt
from south_africa5000
group by make)
select brand, sum(cnt) cnt
from aggregate
group by brand

COUNT of GROUP of two fields in SQL Query -- Postgres

I have a table in postgres with 2 fields: they are columns of ids of users who have looked at some data, under two conditions:
viewee viewer
------ ------
93024 66994
93156 93151
93163 113671
137340 93161
92992 93161
93161 93135
93156 93024
And I want to group them by both viewee and viewer field, and count the number of occurrences, and return that count
from high to low:
id count
------ -----
93161 3
93156 2
93024 2
137340 1
66994 1
92992 1
93135 1
93151 1
93163 1
I have been running two queries, one for each column, and then combining the results in my JavaScript application code. My query for one field is...
SELECT "viewer",
COUNT("viewer")
FROM "public"."friend_currentfriend"
GROUP BY "viewer"
ORDER BY count DESC;
How would I rewrite this query to handle both fields at once?
You can combine to columns from the table into a single one by using union all then use group by as below:
select id ,count(*) Count from (
select viewee id from vv
union all
select viewer id from vv) t
group by id
order by count(*) desc
Results:
This is a good place to use a lateral join:
select v.viewx, count(*)
from t cross join lateral
(values (t.viewee), (t.viewer)) v(viewx)
group by v.viewx
order by count(*) desc;
You can try this :
SELECT a.ID,
SUM(a.Total) as Total
FROM (SELECT t.Viewee AS ID,
COUNT(t.Viewee) AS Total
FROM #Temp t
GROUP BY t.Viewee
UNION
SELECT t.Viewer AS ID,
COUNT(t.Viewer) AS Total
FROM #Temp t
GROUP BY t.Viewer
) a
GROUP BY a.ID
ORDER BY SUM(a.Total) DESC

How to count the number of records in an sql database

I am using the below query to get distinct records from 4 specific columns in an sql DB.
SELECT DISTINCT customer,
product,
category,
sector
FROM data_table
I need to add the count of products in this query. Any ideas?
are you find something below
select count(*) from
(SELECT DISTINCT customer, product, category, sector
FROM data_table
) a
or do you need window function count() if your dbms support
SELECT DISTINCT customer, product, category, sector,
count(*) over() as cnt
FROM data_table

Selecting Sum of the Count From Tables in a DB

I have the following query:
SELECT SUM(count)
FROM (SELECT 'artist' AS table_name, COUNT(*) as count FROM artist
UNION
SELECT 'persons' AS table_name, COUNT(*) as count FROM persons
UNION
SELECT 'track' AS table_name, COUNT(*) as count FROM track)
It works as expected and returns the proper count. But now when I do the following query I get the incorrect count:
SELECT SUM(count)
FROM (SELECT COUNT(*) as count FROM artist
UNION
SELECT COUNT(*) as count FROM persons
UNION
SELECT COUNT(*) as count FROM track)
Why is it the first query gets the proper count and the second one does not?
The UNION eliminates duplicate values, so if two counts happen to be the same, one is eliminated and only one is summed. Try using UNION ALL instead.
SELECT sum(count) FROM
(SELECT COUNT(*) as count FROM artist
UNION ALL
SELECT COUNT(*) as count FROM persons
UNION ALL
SELECT COUNT(*) as count FROM track)
If you can live with approximate, just count all tables in one go
SELECT
Total_Rows= SUM(st.row_count)
FROM
sys.dm_db_partition_stats st
WHERE
(index_id < 2) --cover both heaps and clustered indexes
AND
OBJECT_SCHEMA_NAME (object_id) <> 'sys' --ignore system stuff
This will run in a flash
Adopting your original, you can get count per table and overall in one go..
SELECT
*,
SUM(count) OVER () AS GrandTotal
FROM (SELECT 'artist' AS table_name, COUNT(*) as count FROM artist
UNION
SELECT 'persons' AS table_name, COUNT(*) as count FROM persons
UNION
SELECT 'track' AS table_name, COUNT(*) as count FROM track)
How is it inaccurate? One way might be because, in the second query, you hvae UNION, and two or more rows contain the same value--because UNION removes duplicate values. Try it with UNION ALL, which returns all rows, not just unique ones.