performing math inside PostgreSQL query - sql

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;

Related

How do I add two counts in HIVE hql?

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

Fastest Query to find records which have equal values and specific different value

I have a table transactions with the following columns:
transactionId, systemId, subId and type
I need to find all transactionIds that have
subId and type as equals but different systemId
I tried the following query but I am not sure that it is the fastest query to use:
SELECT DISTINCT transactionId, T1.systemId system1,
T2.systemId system2, T1.subId
FROM transactions T1
INNER JOIN transactions T2
WHERE T1.subId = T2.subId
AND T1.type = T2.type
AND T1.systemId != T2.systemId
Simply use a GROUP BY with a HAVING clause:
SELECT DISTINCT transactionId
FROM t
GROUP BY transactionId, subId, type
HAVING COUNT(DISTINCT systemId) > 1
There are many factors that affect execution efficiency, and you can write all query for comparison.
such as
select *
from (
select t1.*,
count(distinct t1.systemId) over(partition by t1.type, t1.subId) cot
from transactions t1
) t1
where t1.cot > 1
;
select *
from transactions t1
where exists(
select *
from (
select v1.type, v1.subId
from transactions v1
group by v1.type, v1.subId
having count(distinct v1.systemId) > 1
) vv1
where vv1.type = t1.type
and vv1.subId = t1.subId
)
;
Depending on the dbms you are using, there will be more different query ways

How do I count three different distinct values and group on an ID in MS-Access?

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;

Max & Distinct based on several field

Based on the raw data below and expected result, I need help on how to come up with the correct query.
Basically, I need that data based on max(ID), however, do note that RATING and TYPE field could be different hence Group By wouldn't work.
Thank you.
You seem to want the highest id for each NumId. You can do this using row_number():
select t.*
from (select t.*, row_number() over (partition by NumId order by id desc) as seqnum
from t
) t
where t.seqnum = 1;
Use NOT EXISTS to return a row if no other row has the same NumTitle but a higher ID value:
select t1.*
from tablename t1
where not exists (select 1 from tablename t2
where t2.NumTitle = t1.NumTitle
and t2.ID > t1.ID)
Or, JOIN version:
select t1.*
from tablename t1
join (select NumTitle, MAX(ID) from tablename group by NumTitle) t2
on t2.NumTitle = t1.NumTitle and t2.ID = t1.ID

Compare table and find elements with not matching count

I've two tables
Table1
And Table2
Now I want those RequestId and the count of those RequestId from Table1 which differ from that of Table2, For Example, Output should be
I can get individual count and RequestId from both the tables by the following query:
select RequestId, Count(RequestId) AS [Count] from Table1 group by RequestId
But how to compare both tables in a single query, Any help will be good and it would be good if no looping is performed until its the only way of doing it, as there are many records in both the tables, images shared here are just for understanding.
You'd use your queries as subqueries in FROM. Outer join table2, because there ain't rows for all request IDs in it:
select t1.requestid, t1.cnt - coalesce(t2.cnt, 0) as diff
from (select requestid, count(*) as cnt from table1 group by requestid) t1
left join (select requestid, count(*) as cnt from table2 group by requestid) t2
on t2.requestid = t1.requestid
where (t1.cnt - coalesce(t2.cnt, 0)) > 0;
If i didn't get you wrong then you can just use your query to check the count of RequestId in each tables and then join them by the count not even. and do the simple math:
select t1.RequestId, (t1.Count - t2.Count) as count
from
(select RequestId, Count(RequestId) AS [Count] from Table1 group by RequestId)t1
left join(select RequestId, Count(RequestId) AS [Count] from Table2 group by RequestId)t2
on t1.RequestId = t2.RequestId
where t1.Count <> t2.Count