Combining 3 Tables and Finding the Count for Fruit in SQL - sql

I need to combine three tables and find the total count for fruit after combining the three tables using SQL. The name of the fruit columns in the three tables are pr16pnk.fruit, pr16puf.fruit, and pr16yag.fruit. 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. I also need help putting it in descending order.
SELECT pr16pnk.fruit, COUNT(*)
FROM pr16pnk
GROUP BY pr16pnk.fruit
UNION ALL
SELECT pr16puf.fruit, COUNT(*)
FROM pr16puf
GROUP BY pr16puf.fruit
UNION ALL
SELECT pr16yag.fruit, COUNT(*)
FROM pr16yag
GROUP BY pr16yag.fruit

Use aggregators and ORDER BY, query is as simple as below
SELECT sum(A.fruit ,B.fruit, C.fruit)
FROM pr16pnk A
pr16puf B'
pr16yag C
ORDER BY fruit desc;

Just use a subquery:
SELECT fruit, SUM(cnt)
FROM ((SELECT pr16pnk.fruit, COUNT(*) as cnt
FROM pr16pnk
GROUP BY pr16pnk.fruit
) UNION ALL
SELECT pr16puf.fruit, COUNT(*) as cnt
FROM pr16puf
GROUP BY pr16puf.fruit
) UNION ALL
(SELECT pr16yag.fruit, COUNT(*) as cnt
FROM pr16yag
GROUP BY pr16yag.fruit
)
) f
GROUP BY fruit

Try this,
select f.fruit, count(*) from
(
SELECT pr16pnk.fruit from pr16pnk
union
SELECT pr16puf.fruit from pr16puf
union
SELECT pr16yag.fruit from pr16yag
) f
group by f.fruit
order by f.fruit desc

Related

SQL - Find Record count for multiple tables at a time in snowflake

I want to see counts have Tables at 1 time, instead of Running each.
For EX:
select COUNT(*) from "Fact_MASTER ";
select COUNT(*) from "Dim_MASTER ";
select COUNT(*) from "Fact2 ";
select COUNT(*) from "Dim2";
select COUNT(*) from "Fact3";
select COUNT(*) from "Dim3"
Is there any way we can write a CTE to pull as Record count for each in a temp table or so like below:
You can use union all:
select 'Fact_MASTER', COUNT(*) from "Fact_MASTER " union all
select 'Dim_MASTER', COUNT(*) from "Dim_MASTER " union all
select 'Fact2', COUNT(*) from "Fact2 " union all
select 'Dim2', COUNT(*) from "Dim2" union all
select 'Fact3', COUNT(*) from "Fact3" union all
select 'Dim3', COUNT(*) from "Dim3"
It looks like you want each count in a separate column. If so, you can turn each query to a separate subquery, and select them:
select
(select count(*) from "Fact_MASTER") as fact_master,
(select count(*) from "Dim_MASTER ") as dim_master,
(select count(*) from "Fact2") as fact2,
(select count(*) from "Dim2") as dim2,
(select count(*) from "Fact3") as fact3
(select count(*) from "Dim3") as dim3
Have you tried simply running:
SHOW TABLES;
If you then want to use that information for something else, you can then follow-up with something like:
select "rows" as cnt
from table(result_scan(last_query_id()))
where "name" in (...);
If you have a set list of tables that you want to PIVOT you can also use the result_scan() function to pivot the data:
https://docs.snowflake.com/en/sql-reference/constructs/pivot.html

Select a third column based on two distant rows within the same table

I want to select a third column based on two distant columns within the same table.
I could only think of this:
select tl.thirdcolumn
from table1 t1
WHERE
EXISTS
(
Select distinct tl.firstcolumn , t1.secondcolumn
From t1
)
This:
select distinct tl.thirdcolumn
from table t1
won't work as I don't want the distinct thirdrow. I want the thirdrow to be based on the first two rows being distinct.
I guess its a kind of nested sql statment with a select top 1... idk
CATEGORY NAME Query
---------------------------------------------------
STUDENTS NUMBER_OF_CHAPTERS QueryA
STUDENTS NUMBER_OF_STUDENT_MEMBERS QueryB
STUDENTS NUMBER_OF_STUDENT_MEMBERS QueryB
MEMBERS NUMBER_OF_MEMBERS_WORLDWIDE QueryC
MEMBERS NUMBER_OF_MEMBERS_WORLDWIDE QueryC
Your question is rather hard to follow, but I think you might simply want group by:
select tl.firstcolumn , t1.secondcolumn, max(tl.thirdcolumn)
from table1 t1
group by tl.firstcolumn , t1.secondcolumn;
If you want rows where the pair of values only appears once, then add having count(*) = 1:
select tl.firstcolumn , t1.secondcolumn, max(tl.thirdcolumn)
from table1 t1
group by tl.firstcolumn , t1.secondcolumn
having count(*) = 1;
Query -
SELECT
CATEGORY,NAME,QUERY
FROM
(
WITH TAB AS (
SELECT
'STUDENTS' AS CATEGORY,
'NUMBER_OF_CHAPTERS' AS NAME,
'QUERYA' AS QUERY
FROM
DUAL
UNION ALL
SELECT
'STUDENTS' AS CATEGORY,
'NUMBER_OF_STUDENT_MEMBERS' AS NAME,
'QUERYB' AS QUERY
FROM
DUAL
UNION ALL
SELECT
'STUDENTS' AS CATEGORY,
'NUMBER_OF_STUDENT_MEMBERS' AS NAME,
'QUERYB' AS QUERY
FROM
DUAL
UNION ALL
SELECT
'MEMBERS' AS CATEGORY,
'NUMBER_OF_MEMBERS_WORLDWIDE' AS NAME,
'QUERYC' AS QUERY
FROM
DUAL
UNION ALL
SELECT
'MEMBERS' AS CATEGORY,
'NUMBER_OF_MEMBERS_WORLDWIDE' AS NAME,
'QUERYC' AS QUERY
FROM
DUAL
) SELECT
CATEGORY,
NAME,
QUERY,
COUNT(*) OVER(PARTITION BY
CATEGORY,
NAME
ORDER BY
CATEGORY,
NAME,
QUERY
) AS RNK
FROM
TAB
)
WHERE
RNK = 1;
Output -
"CATEGORY","NAME","QUERY"
"STUDENTS","NUMBER_OF_CHAPTERS","QueryA"

Join Tables and totals

How would I join the following tables to give a total count from all the tables.
select (BatchIdentifier),
Count(distinct BatchIdentifier) as ERTBatchCheckIdentifier
from ERTBatchChecks
Group By BatchIdentifier
select (ERTBatchNumber),
Count(distinct ERTBatchNumber) as ERTBatchNumber
from ERTClaims
Group By ERTBatchNumber
select (BatchIdentifier),
Count(distinct BatchIdentifier) as ERTBatchesIdentifier
from ERTBatches
Group By BatchIdentifier
If you simply want a SUM() then use a derived table and union all:
SELECT Batch, SUM(Cnt) AS TotalCount
FROM (
select (BatchIdentifier) AS Batch,
Count(distinct BatchIdentifier) as Cnt
from ERTBatchChecks
Group By BatchIdentifier
union all
select (ERTBatchNumber),
Count(distinct ERTBatchNumber)
from ERTClaims
Group By ERTBatchNumber
union all
select (BatchIdentifier),
Count(distinct BatchIdentifier)
from ERTBatches
Group By BatchIdentifier
) SubCounts
GROUP BY Batch
If you want a total count of the distinct 'batches' then use union and COUNT():
SELECT Count(Batch) AS TotalDistinctBatches
FROM (
select distinct BatchIdentifier as Batch
from ERTBatchChecks
union
select distinct ERTBatchNumber
from ERTClaims
union
select distinct BatchIdentifier
from ERTBatches
) DistinctBatches
Note: Union all will keep duplicates, Union will not. Hence for adding the counts, we want to use Union all in case two of the counts are the same. But for counting the distinct batches, we want Union so we don't count the same batch more than once.

multiple select in one query [Teradata]

I'm trying to do multiple select from diff tables and just have a result in one column.
SELECT COUNT(*) FROM tb1 union
SELECT COUNT(*) FROM tb2 union
SELECT COUNT(*) FROM tb3;
output should be like:
593643
18103600
0
Problem with this is that the result is being arranged on desc order.
Like below:
0
593643
18103600
I would want the result to be as I put the select statement.
Please advise. Btw, I'm using teradata.
Thank you.
SQL result sets are inherently unordered, unless you explicitly specify an order by clause. You can do this with a subquery:
select cnt
from ((SELECT COUNT(*) as cnt, 1 as ord FROM tb1)
union all
(SELECT COUNT(*), 2 FROM tb2)
union all
(SELECT COUNT(*), 3 FROM tb3)
) t
order by ord
If you want specific order, add ORDER BY clause. It would also be good to use UNION ALL so you always get 3 rows, even with duplicate results (two tables having the same number of rows):
SELECT 'tbl1' AS tablename, COUNT(*) AS cnt, 1 AS ord FROM tb1 UNION ALL
SELECT 'tbl2', COUNT(*), 2 FROM tb2 UNION ALL
SELECT 'tbl3', COUNT(*), 3 FROM tb3
ORDER BY ord ;

Finding duplicates with two similar columns and one distinct

I am in a situation where I need to select rows that have the same content in two specific columns, AND distinct content in a third one. So far I got this for the two similar columns:
SELECT id, Title,
COUNT(*) AS NumOccurrences
FROM Table
GROUP BY id, Title
HAVING ( COUNT(*) > 1 )
I now need to specify a third distinct column in this query. Let's call it Ralph. This obviously does not work:
SELECT id, Title, DISTINCT Ralph,
COUNT(*) AS NumOccurrences
FROM Table
GROUP BY id, Title
HAVING ( COUNT(*) > 1 )
So what will?
select * from (
SELECT id, Title, COUNT(*) AS NumOccurrences
FROM Table t
GROUP BY id, Title
HAVING ( COUNT(*) > 1 )
) t
cross apply (
select distinct Ralph
from Table
where id = t.id and Title = t.Title
) t2
You can use COUNT(*) with OVER() clause
;WITH cte AS
(
SELECT id, Title, Ralph, COUNT(*) OVER (PARTITION BY id, Title) AS cnt
FROM dbo.test11
GROUP BY id, Title, Ralph
)
SELECT *
FROM cte
WHERE cnt > 1
Demo on SQLFiddle