getting different counts from two different tables? - database

I'm having trouble with a query I'm trying to write in oracle.
Basically I want to get the count of two tables that have the same provider list. I can run the query individually but not sure how to do it in one shot.
Here's a example:
SELECT name, COUNT(name)
FROM table1
group by name
This gives me a list of names and a count beside them. If I run the same command but on table2(changing the from statement), it works fine. What I want to do is have a query that takes count from table1 and substracts it from count from table2.
How can I do it? I have tried to do nested selects and so on but can't seem to get it to work.

One option assuming that the name values in both tables are identical would be
SELECT t1.name, t2.cnt - t1.cnt diff
FROM( SELECT name, COUNT(name) cnt
FROM table1
group by name ) t1,
( SELECT name, COUNT(name) cnt
FROM table2
group by name ) t2
WHERE t1.name = t2.name
If you want to handle the case where one table has a name that the other doesn't but you don't know which table has the extra name (and assuming that you want to say that the other table has a cnt of 0 for that name)
SELECT coalesce(t1.name, t2.name) name,
nvl(t1.cnt, 0 ) t1_cnt,
nvl(t2.cnt, 0 ) t2_cnt,
nvl(t2.cnt,0) - nvl(t1.cnt,0) diff
FROM( SELECT name, COUNT(name) AS cnt
FROM table1
group by table1.name ) t1
full outer join
( SELECT name, COUNT(name) AS cnt
FROM table2
group by table2.name ) t2
on( t1.name = t2.name )
which you can see in this SQLFiddle

Related

How to left join onto existing query in SQL?

I am looking to left join another table because there are two columns in that table that I need to add to my query..how can I left join onto my existing query? For example the query I am using is similar to the one below..
select subject, sum(cnt_daily) as cnt,
min(cnt_daily) as min_cnt_daily, max(cnt_daily) as max_cnt_daily
from (
select study_date, subject, count(*) as cnt_daily
from mytable
where study_date >= '2022-01-01'
group by study_date, subject
) t
group by subject
I tried
select *
from mytable
left join table2
on mytable.id= table1.id
order by table1.id;
But i know this isnt right
You may use CTE:
WITH t AS (
select study_date, subject, count(*) as cnt_daily
from mytable
where study_date >= '2022-01-01'
group by study_date, subject
)
select subject, sum(cnt_daily) as cnt,
min(cnt_daily) as min_cnt_daily, max(cnt_daily) as max_cnt_daily
from t
group by subject
This could encourage reuse of the query inside CTE.

Find most recent date for ID with multiple records in MS Access

I have a field, "ID", and it has repeat values. (In the example; A21, B42, and C14). My two other fields in the table are "Date" and "Measurement". I want to create a query that will call the previous date WITH matching ID and display the results of that previous row. My end goal is to have a field in my query that will find the change between the current measurement for the ID and the measurement from the date prior.
I have attached an image of the table I have and what I want the query to display.
Sadly MS Access does not support lag(). This however can be emulated with a self-join and a not exists condition with a correlated subquery:
select
t.id,
tprev.date as previous_date,
tprev.measureement as previous_measurement
from Table1 as t
left join Table1 as tprev
on (tprev.id = t.id)
and (tprev.dat < t.date)
and (not exists (
select 1
from Table1 as t1
where
t1 = t.id
and t1.dat < t.date
and t1.dat > tprev.date
))
This is how to make the described query function:
SELECT t.NUM, t.ID, tprev.Date_ AS previous_date, tprev.Measurement AS previous_measurement
FROM Table1 AS t LEFT JOIN Table1 AS tprev ON (tprev.Date_ < t.Date_) AND (tprev.id = t.id)
WHERE not exists
(select 1
from Table1 AS t1
where
t1.ID = t.ID
and t1.Date_ < t.Date_
and t1.Date_ > tprev.Date_);

SQL Insert into a table FROM 2 unrelated tables and NOT Exists

I'm able to run the next sql script once, to insert some rows using information from 2 unrelated tables :
INSERT INTO CompanyCarType (Name, CompanyId, LastModDateTime, LastModUserId)
SELECT T.Name, C.Id , GETDATE() LastModDateTime, C.LastModUserId
FROM CarType T
CROSS JOIN Company C
But, I need to be able to run this script many times without inserting duplicates, or failing in my case, as the table won't allow duplicated CompanyId
and Name.
So I tried to use WHERE NOT EXISTS, but as there are 2 tables in the SELECT statement, I can't use an alias to refer to it, and the syntax is incorrect.
INSERT INTO CompanyCarType (Name, CompanyId, LastModDateTime, LastModUserId)
SELECT T.Name, C.Id , GETDATE() LastModDateTime, C.LastModUserId
FROM CarType T
CROSS JOIN Company C) AS T1
WHERE NOT EXISTS (SELECT Name
FROM CompanyCarType T2
WHERE T2.Name = T1.Name)
There are a lot of goof examples out there using one table on the SELECT clause, but none then with 2 or more tables.
Thanks!
You can make use of rownumber while doing insert and then add Company ID in the where not exists clause.
INSERT INTO CompanyCarType
(Name, CompanyId, LastModDateTime, LastModUserId)
SELECT T.Name, C.Id , GETDATE() LastModDateTime, C.LastModUserId, ROW_NUMBER() over (partition by t.name, c.ID order by LastModDateTime) rownum
FROM CarType T CROSS JOIN Company C) as T1
WHERE t1.rownum = 1 and NOT EXISTS(SELECT Name
FROM CompanyCarType T2
WHERE T2.Name = T1.Name and t2.CompanyId = t1.ID)

How to concatenate 3 tables MSSQL Server

In a single database, I have three tables as below:
I want to concatenate them in some single table, any suggestion?
If I am understanding correctly your answer you want something like this:
CREATE TABLE COMBINED (/*Insert the set of fields here*/);
GO
INSERT INTO COMBINED
SELECT Table1.ProposalId
, /*all other Table1 fields*/
, Table2.ProposalId
/*all other Table2 fields*/
, Table3.ProposalId
, /*all other Table2 fields*/
FROM Table1
FULL JOIN Table2
ON Table1.ProposalId = Table2.ProposalId
FULL JOIN Table3
ON Table1.ProposalId = Table3.ProposalId;
This will match the rows with the same proposal id (but keep the all the its values as proposalid_1,proposalid_2... and for those that do not match will bring the full row and NULL for every other fields of the rest of the tables.
Thanks for your help the issue has been solved.
SELECT mtlreq.proposalid, mtlreq.prp_mtlreq_taskgrp,mtlreq.prp_mtlreq_taskcode,mtlreq.prp_mtlreq_itemcode,mtlreq.prp_mtlreq_rateper,mtlreq.prp_mtlreq_qty,mtlreq.prp_mtlreq_Inter_MaterCost,mtlreq.prp_mtlreq_UOM,mtlreq.item_short_desc, resreq.proposalid,resreq.prp_resreq_taskcode ,resreq.prp_resreq_resource,resreq.prp_resreq_usage,resreq.prp_resreq_uom,resreq.prp_resreq_rate,resreq.prp_resreq_overhd_pers
FROM (
SELECT proposalid,prp_mtlreq_taskgrp,prp_mtlreq_taskcode,prp_mtlreq_itemcode,prp_mtlreq_rateper,prp_mtlreq_qty,prp_mtlreq_Inter_MaterCost,prp_mtlreq_UOM,item_short_desc,
ROW_NUMBER() OVER (ORDER BY proposalid) AS rn
FROM prjdet_prp_taskwork_mtlreq ) AS mtlreq
FULL OUTER JOIN (
SELECT proposalid,prp_resreq_taskcode ,prp_resreq_resource,prp_resreq_usage,prp_resreq_uom,prp_resreq_rate,prp_resreq_overhd_pers,
ROW_NUMBER() OVER (ORDER BY proposalid) AS rn
FROM prjdet_prp_taskwork_resreq) AS resreq
ON mtlreq.rn = resreq.rn
FULL OUTER JOIN (
SELECT proposalid,mtprp_delv_lineno,mtprp_delv_itemcode,mtprp_delv_cost,mtprp_delv_linelevelmrg,mtprp_delv_proposedamt,
ROW_NUMBER() OVER (ORDER BY proposalid) AS rn
FROM prjproposal_delidtl ) AS delidtl
on mtlreq.rn = delidtl.rn and resreq.rn=delidtl.rn

Optimize CASE Test in SQL Server

I'm wondering if there's any way to optimize the following SELECT query. (Note: I typed this when writing my question for nonexistent tables and I might not have the correct syntax.)
The goal is, if Table2 contains any related rows I want to set the value of the third column to the number of related rows in Table2. Otherwise, if Table3 contains any related rows I want to set the column to the number of related rows in Table3. Otherwise, I want to set the column value to 0.
SELECT Id, Title,
CASE
WHEN EXISTS (SELECT * FROM Table2 t2 WHERE t2.RelatedId = Table1.Id) THEN
(SELECT COUNT(1) FROM Table2 t2 WHERE t2.RelatedId = Table1.Id)
WHEN EXISTS (SELECT * FROM Table3 t3 WHERE t3.RelatedId = Table1.Id) THEN
(SELECT COUNT(1) FROM Table3 t3 WHERE t3.RelatedId = Table1.Id)
ELSE 0
END AS RelatedCount
FROM Table1
I don't like the fact that I'm basically performing the same query twice (in two cases). Is there any way to do what I want while only performing the query once?
Note that this is part of a much larger query with multiple JOINs and UNIONs so it's not easy to take a completely different approach.
This query should perform much better. You are not just performing the same query twice; since they are correlated subqueries, they will run once per row.
SELECT Id, Title,
coalesce(t2.Count, t3.Count, 0) AS RelatedCount
FROM Table1 t
left outer join (
SELECT RelatedId, count(*) as Count
FROM Table2
group by RelatedId
) t2 on t1.Id = t2.RelatedId
left outer join (
SELECT RelatedId, count(*) as Count
FROM Table3
group by RelatedId
) t3 on t1.Id = t3.RelatedId

Resources