So I am aware I can get the count of one table by using select count(*) from table1;
I have tried
select(select count() from table1) table1,
(select count() from table2) table2
from dual;
However it does not work.
Two possible solutions. Cross join and Union all + aggregation
Cross join:
select t1.cnt as table1_count,
t2.cnt as table2_count
from
(select count(*) cnt from table1) t1
cross join
(select count(*) cnt from table2) t2
Union all + max aggregation:
select max(t1_cnt) table1_count, max(t2_cnt) table2_count
from
(
select count(*) t1_cnt, 0 t2_cnt from table1
union all
select 0 t1_cnty, count(*) t2_cnt from table2
) s
Related
i want to count sum of this after minus please help me
SELECT t1.*
FROM table1 t1
MINUS
SELECT t2.*
FROM table2 t1
JOIN customers c ON t1.number = t2.number;
Another way is using CTE
With cte as (SELECT t1.*
FROM table1 t1
MINUS
SELECT t2.*
FROM table2 t1
JOIN customers c ON t1.number = t2.number)
Select count(*) from cte;
one way is :
select count(*) from (
SELECT *
FROM table1 t1
MINUS
SELECT *
FROM table2 t1
JOIN customers c ON t1.number = t2.number
) t
I suspect that you really want:
select count(*)
from (select number
from table1
minus
select number
from customers
) t;
It seems really odd that you would have two tables (table1 and table2 in your question) with exactly the same columns.
In addition, this does something useful, which is to count the number of numbers in table1 that are not customers.
I have tables with the same name in 2 different schemas. What I want to do is get a count comparison in the 2 tables in the format
TableName : Count1 : Count2
How can I achieve this via Hive query?
Use UNION ALL:
select 'db1.table_name' table_name, count(col1) count1, count(col2) count2 from db1.table_name
UNION ALL
select 'db2.table_name' table_name, count(col1) count1, count(col2) count2 from db2.table_name
You can do a cross join of the count queries.
select t1.count1,t2.count2
from (select count(*) as count1 from tbl1) t1
cross join (select count(*) as count2 from tbl2) t2
Try full outer join
select tt1.cn,tt2.cn from
(select count(1) as cn from db1.table) tt1
full outer join
(select count(1) as cn from db2.table ) tt2
on tt1.cn=tt2.cn;
I need to query three values from two tables. the first two values are queried as follows:
SELECT
(SELECT COUNT(*) FROM table1) AS count,
(SELECT COUNT(*) FROM table2 WHERE config IS NULL) AS upCount,
the third value downCount should be count - upCount. can this operation be done by psql and returned as downCount?
One option would be to simply repeat the subqueries:
SELECT
(SELECT COUNT(*) FROM table1) AS count,
(SELECT COUNT(*) FROM table2 WHERE config IS NULL) AS upCount,
(SELECT COUNT(*) FROM table1) -
(SELECT COUNT(*) FROM table2 WHERE config IS NULL) AS downCount;
You could also use a CTE to compute the original two subqueries first:
WITH cte AS (
(SELECT COUNT(*) FROM table1) AS count,
(SELECT COUNT(*) FROM table2 WHERE config IS NULL) AS upCount
)
SELECT
count,
upCount,
count - upCount AS downCount
FROM cte;
You can use a subquery or move the subqueries to the FROM clause. I would suggest the latter:
SELECT t1.cnt, t2.upcount, (t1.cnt - t2.upcount) as downcount
FROM (SELECT COUNT(*) as cnt FROM table1) t1 CROSS JOIN
(SELECT COUNT(*) as upcount FROM table2 WHERE config IS NULL) t2;
I have a table:
My select:
select regexp_split_to_table(t3."Id"::character varying,'') as s
from (select t1."Id" from table1 t1
union all
select t2."Id"from table2 t2) t3
order by s
Or also I can get a string '22173345566179111134546175622323811' with this:
select string_agg(t3."Id"::character varying,'') as s
from (select t1."Id" from table1 t1
union all
select t2."Id"from table2 t2) t3
I need to get a table with number|count data, I mean for any number to get a count of repetitions in the select, for example:
1 | 9
2 | 5
3 | 5
and so on..
PostgreSQL DBMS
Does this do what you want?
select id, count(*)
from (select t1."Id" from table1 t1
union all
select t2."Id" from table2 t2
) t3
group by id
order by id;
If I understand you right, you want a list of all digits, that exist in a set of IDs from two tables and the count of each digit, how often it appears in all these IDs. If so, you just need to GROUP BY a digit and use count().
SELECT s.d,
count(*) count
FROM (SELECT t1."Id"
FROM table1 t1
UNION ALL
SELECT t2."Id"
FROM table2 t2) t3
CROSS JOIN LATERAL regexp_split_to_table(t3."Id"::character varying, '') s(d)
GROUP BY s.d
ORDER BY s.d;
easiest way
select regexp_split_to_table(t3."Id"::character varying,'') s, count(*) count
from (select t1."Id" from table1 t1 union all select t2."Id"from table2 t2) t3
group by s
So I know MS-Access does not allow SELECT COUNT(DISTINCT....) FROM ..., but I am trying to find a more viable alternative to the usual standard of
SELECT COUNT(*) FROM (SELECT DISTINCT Name FROM table1)
My problem is I am trying to do three separate Count functions and group them on ID. If I use the method above, it is giving me the total unique value count for the whole table instead of the total count for only the value of ID. I tried doing
(SELECT COUNT(*) FROM (SELECT DISTINCT Name FROM table1 as T2
WHERE T2.ColumnA = T1.ColumnA)) As MyVal
FROM table1 as T1
but it tells me I need to specify a value for T1.ColumnA.
The SQL query I am trying to accomplish is this:
SELECT ID
COUNT(DISTINCT ColumnA) as CA,
COUNT(DISTINCT ColumnB) as CB,
COUNT(DISTINCT ColumnC) as CC
FROM table1
GROUP BY ID
Any ideas?
You can use subqueries. Assuming you have a table where each id occurs once:
select (select count(*)
from (select columnA
from table1 t1
where t1.id = t.id
group by columnA
) as a
) as num_a,
(select count(*)
from (select columnB
from table1 t1
where t1.id = t.id
group by columnB
) as b
) as num_b,
(select count(*)
from (select columnC
from table1 t1
where t1.id = t.id
group by columnC
) as c
) as num_c
from <table with ids> as t;
I'm not sure if you'll think this is "viable".
EDIT:
This makes it even more complicated . . . it suggests that MS Access doesn't support correlation clauses more than one level deep (might you consider switching to another database?).
In any case, the brute force way:
select a.id, a.numA, b.numB, c.numC
from ((select id, count(*) as numA
from (select id, columnA
from table1 t1
group by id, columnA
) as a
) as a inner join
(select id, count(*) as numB
from (select id, columnB
from table1 t1
group by id, columnB
) as b
) as b
on a.id = b.id
) inner join
(select id, count(*) as numC
from (select id, columnC
from table1 t1
group by id, columnC
) as c
) c
on c.id = a.id;