combine 2 sql select count statements - sql

I ahave 2 simple count queries:
select count (*) from t_object
select count (*) from t_diagram
How is the simplest way to combine their result (sum)?

Use UNION ALL to get two different count:
select count (*), 't_object count' from t_object
union all
select count (*), 't_diagram count' from t_diagram
To get the sum of the counts, use a derived table:
select sum(dt.cnt) from
(
select count(*) as cnt from t_object
union all
select count(*) as cnt from t_diagram
) dt
Or, use a sub-query:
select count(*) + (select count(*) from t_diagram) from t_object

Depends on what you mean by "combine". To sum them up:
select (select count (*) from t_object) + count(*) as combined_count
from t_diagram

Related

Combining 3 Tables and Finding Average Age in SQL

I need to combine three tables and find the average age after combining the three tables using SQL. The name of the age columns in the three tables are pr16pnk.age, pr16puf.age, and pr16yag.age. I have successfully found the results when the tables are separated, but I am having trouble combining the results. Listed below is the code that I used.
SELECT AVG(pr16pnk.age) AS MeanAge
FROM pr16pnk
UNION ALL
SELECT AVG(pr16puf.age) AS MeanAge
FROM pr16puf
UNION ALL
SELECT AVG(pr16yag.age) AS MeanAge
FROM pr16yag
You can use two levels of aggregation. For the average of averages:
SELECT AVG(MeanAge)
FROM (SELECT AVG(pr16pnk.age) AS MeanAge
FROM pr16pnk
UNION ALL
SELECT AVG(pr16puf.age) AS MeanAge
FROM pr16puf
UNION ALL
SELECT AVG(pr16yag.age) AS MeanAge
FROM pr16yag
) a;
However, what you might really want is the overall average, which would be calculated as:
SELECT AVG(MeanAge),
SUM(sum_age) / SUM(cnt)
FROM (SELECT AVG(pr16pnk.age) AS MeanAge, SUM(age) as sum_age, COUNT(*) as cnt
FROM pr16pnk
UNION ALL
SELECT AVG(pr16puf.age) AS MeanAge, SUM(age) as sum_age, COUNT(*) as cnt
FROM pr16puf
UNION ALL
SELECT AVG(pr16yag.age) AS MeanAge, SUM(age) as sum_age, COUNT(*) as cnt
FROM pr16yag
) a;

The SUM of an aggregate COUNT function

I want to create a query to calculate the percentage sales of the overall policies in my database.
The policies are split under two separate headings
UL
NL
The code i want should display
product name
number of policies sold
policies sold per product as a percentage of the overall number of policies sold.
I have made a few attempts at scripting this code (please see below) but cannot get them to run correctly.
Syntax 1:
SELECT b.PRODUCT_NAME, b.POLICIES_SOLD, 100.00*(b.POLICIES_SOLD/SUM(b.POLICIES_SOLD)) AS'PERC_SALES'
FROM
(
SELECT a.PRODUCT_NAME, COUNT(a.PRODUCT_NAME) AS
'POLICIES_SOLD'
FROM
(SELECT PRODUCT_NAME FROM [ATLANTIS\jjudge].
[ALL_POLICIES_201706_NL]
UNION ALL
SELECT PRODUCT_NAME FROM [ATLANTIS\jjudge].
[ALL_POLICIES_201706_UL])a GROUP BY PRODUCT_NAME)b ;
Syntax 2:
SELECT a.PRODUCT_NAME, a.[POLICIES SOLD], 100.00*(a.[POLICIES SOLD]/SUM(a.[POLICIES SOLD]))
FROM
(SELECT PRODUCT_NAME, COUNT(*) AS 'POLICIES SOLD'FROM
[ATLANTIS\jjudge].[ALL_POLICIES_201706_NL] GROUP BY PRODUCT_NAME
UNION ALL
SELECT PRODUCT_NAME, COUNT(*) AS 'POLICIES SOLD' FROM
[ATLANTIS\jjudge].[ALL_POLICIES_201706_UL] GROUP BY
PRODUCT_NAME)a ;
Syntax 3:
SELECT b.PRODUCT_NAME, COUNT(b.PRODUCT_NAME) AS
'POLICIES_SOLD', 100.00*
(COUNT(b.PRODUCT_NAME)/SUM(SELECT(PRODUCT_NAME))
FROM
(SELECT COUNT(*) AS 'POLICY_COUNT' FROM [ATLANTIS\jjudge].[ALL_POLICIES_201706_NL]
UNION ALL
SELECT COUNT(*) AS 'POLICY_COUNT' FROM [ATLANTIS\jjudge].[ALL_POLICIES_201706_UL])a)) AS 'PERC_SALES'
FROM
(SELECT PRODUCT_NAME FROM [ATLANTIS\jjudge].
[ALL_POLICIES_201706_NL]
UNION ALL
SELECT PRODUCT_NAME FROM [ATLANTIS\jjudge].
[ALL_POLICIES_201706_UL])b GROUP BY PRODUCT_NAME;
I think you want a Window Function. Modify your Syntax 1 first line as:
SELECT
b.PRODUCT_NAME,
b.POLICIES_SOLD,
100.00*b.POLICIES_SOLD/SUM(b.POLICIES_SOLD) OVER () AS 'PERC_SALES'
You can do this using a single aggregation query with window functions:
SELECT p.PRODUCT_NAME, COUNT(*) AS POLICIES_SOLD,
COUNT(*) * 100.0 / SUM(COUNT(*)) OVER () as PERC_SALES
FROM ((SELECT PRODUCT_NAME
FROM [ATLANTIS\jjudge].[ALL_POLICIES_201706_NL]
) UNION ALL
(SELECT PRODUCT_NAME
FROM [ATLANTIS\jjudge].[ALL_POLICIES_201706_UL]
)
) p
GROUP BY PRODUCT_NAME;

How to get a combined count on two tables for shared id

I have two transcript tables, A_Transcript and B_Transcript, and I want a query that will give me all the users who have more than 2 total transcripts (across both tables). Thanks!
Here's the query if I just had one table with data:
select user_Id, count(*) From A_Transcript
group by user_ID
having count(*) > 2;
You need to get the full list of userids from both tables before grouping, and you need UNION ALL to do a non-distinct union:
SELECT user_Id, count(*)
FROM
(
SELECT user_Id
FROM A_Transcript a
UNION ALL
select user_Id
FROM B_Transcript b
) combined
GROUP BY user_Id
HAVING COUNT(*) >2
You can just union the 2 queries together.
SELECT user_Id, count(*) FROM A_Transcript
GROUP BY user_ID
HAVING count(*) > 2
UNION
SELECT user_Id, count(*) FROM B_Transcript
GROUP BY user_ID
HAVING count(*) > 2

Count and divide in select statement

I need to display number of contacts per month.
I have the below query which is giving me incorrect results.
SELECT
a.DATE_YYYYMM,
CONVERT(DECIMAL, COUNT(a.id)) / COUNT(DISTINCT a.id) Average_contacts_per_member_per_month,
COUNT(a.id) Count_Total_Contacts_Per_Month,
a.id
--INTO #temp_Contacts
FROM
(SELECT DISTINCT
*,
ISNULL(CONVERT(NVARCHAR(6), l.SERVICE_DATE, 112),'') AS SERVICE_DATE_YYYYMM
FROM l) a
WHERE
1=1
AND a.IS_V = 1
GROUP BY
id, a.DATE_YYYYMM
My results for average and counts are the same and I know they should differ.
Any thoughts?
You are grouping by a.id, so the count is always going to be 1.
You want something more like:
SELECT a.DATE_YYYYMM,
CONVERT(DECIMAL, COUNT(a.id))/COUNT(DISTINCT a.id) as Average_contacts_per_member_per_month,
COUNT(a.id) as Count_Total_Contacts_Per_Month
FROM (SELECT DISTINCT *,
COALESCE(CONVERT(NVARCHAR(6), l.SERVICE_DATE, 112), '') AS SERVICE_DATE_YYYYMM
FROM [MCS].[JXM1563].[LTIH_SSIS] l
) a
WHERE a.IS_V = 1
GROUP BY a.DATE_YYYYMM

How to add values coming from 2 queries

From the 1st query I am getting some value and from 2nd query I am getting some value. I want the sum of the two values.
Query 1:
select sum(EAmount) from EstimateAmount where pid='3' group by pid
Query 2:
select sum(OPEAmount) from OPEAmount where pid='3' group by pid
select
(select sum(EAmount) from EstimateAmount
where pid='3'
group by pid)
+
(select sum(OPEAmount) from OPEAmount
where pid='3'
group by pid)
Mitch solution is correct, I just want to add a more generic one for the cases when you need the sum for all pids and that can be extended to more aggregates:
with agg_EA as (
select pid, sum(EAmount) as sumEA
from EstimateAmount
group by pid)
, agg_OPEA as (
select pid, sum(OPEAmount) as sumOPE
from OPEAmount
group by pid)
select sumEA+sumOPE
from agg_EA
join agg_OPEA on agg_EA.pid = agg_OPE.pid
You can also use Union All and within a nested table when aggregating sums of sums
select sum(agg.Total) as GrandTotal
from ( select sum(EAmount) as Total from EstimateAmount where pid='3' group by pid
union all
select sum(OPEAmount) as Total from OPEAmount where pid='3' group by pid
) agg
Just join them:
SELECT sum(coalesce(e.EAmount,0) + coalesce(o.OPEAmount,0))
FROM EstimateAmount e
LEFT JOIN OPEAmount o ON o.pid = e.pid
WHERE e.pid = 3
GROUP BY e.pid