Count of matching IDs with in C as query - sql

I'm looking to add a column to display a count of all records where the drgpackid matches.
Essentially I want one line in the example provided and a count of how many records have that ID and meet the conditions of the query.
with C as (
select t1.*
from DrgPack t1 join
DrgPack t2
on t1.DrgID = t2.DrgID and t1.CentralMaintFieldMask <> t2.CentralMaintFieldMask
)
select *
from rxworkflowpack
where drgpackid in (select ID from c where CentralMaintFieldMask = 0)

There are a thousand ways to do this, like adding another CTE with the counts and joining to it
with C as (
select t1.*
from DrgPack t1 join
DrgPack t2
on t1.DrgID = t2.DrgID and t1.CentralMaintFieldMask <> t2.CentralMaintFieldMask
),
D as (
select drgpackid, count(*) from rxworkflowpack group by drgpackid)
select *
from rxworkflowpack left join D on rxworkflowpack.drgpackid = d.drgpackid
where drgpackid in (select ID from c where CentralMaintFieldMask = 0)

You can use window function like this:
with C as (
select t1.*
from DrgPack t1 join
DrgPack t2
on t1.DrgID = t2.DrgID and t1.CentralMaintFieldMask <> t2.CentralMaintFieldMask
)
select DISTINCT *, COUNT(*) OVER (PARTITION BY drgpackid) AS CountRecords from rxworkflowpack
where drgpackid in (select ID from c where CentralMaintFieldMask = 0)

You should use < to not double count
select t1.drgpackid, count(*) as cnt
from DrgPack t1
join DrgPack t2
on t1.DrgID = t2.DrgID
and t1.CentralMaintFieldMask < t2.CentralMaintFieldMask
join rxworkflowpack
on rx.ID = t1.drgpackid
and rx.CentralMaintFieldMask = 0
group by t1.drgpackid

Related

Select Multiple value from a table but group by one value

I am trying to select multiple values from two tables but i want to group by single value. I have tried using max(value) in select but max is returning the greatest one and not the exact one.
Here are my tables
The result i need is something like this
Result : HeadQuarterId - A, PropertyName - Name1, Amount - 102
HeadQuarterId - B, PropertyName - Name5, Amount - 30
Here is my query
SELECT Headquarterid,Max(PropertyName),sum(Amount)
FROM Table1 A LEFT OUTER JOIN Table2 B
ON A.Propetyid = B.PropertyId
GROUP BY Headquarterid
Here i have used Left Outer Join so that i will get all the data from left table even it is not available in right table.
Also i cannot use A.HeadquarterID = A.PropertyId in where condition since i have other dependency in that table. Please suggest someother way to achieve this result.
I think I understand. You want the headquarters with the maximum value, which happens to be A. If so:
select t1.*, sum(t2.amount) over () as total
from t1 left join
t2
on t2.PropertyId = t1.PropertyId
order by t2.amount desc
fetch first 1 row only;
Note: Not all databases support fetch first. It might be spelled limit or use select top (1) for instance.
I would recommend to get the headquartename per ID in a cte / subquery, then join it again to T1 and left join T1 to T2 in a second cte / subquery. This way you can calculate your sums basing on a single group:
WITH cte AS(
SELECT ROW_NUMBER() OVER (PARTITION BY t1.ID ORDER BY CASE WHEN t1.ID = t1.PROPERTYID THEN 0 ELSE 1 END) rn, t1.ID, t1.Name
FROM t1
),
cte2 AS(
SELECT c.name cName, t1.*, t2.Value
FROM t1
INNER JOIN cte c ON c.ID = t1.ID AND c.rn = 1
LEFT JOIN t2 ON t1.Propertyid = t2.propertyid
)
SELECT c2.id, c2.cname, sum(c2.value) value
FROM cte2 c2
GROUP BY c2.id, c2.cname
See SQLFiddle for details: http://sqlfiddle.com/#!18/8bf66/13/2
Of course you can build the first cte without the row_number only by using the WHERE ID = PROPERTYID - matter of taste I'd say...
As per your sample data you want window function :
select distinct t1.HeadQuarterId,
max(t1.PropertyName) over (partition by t1.HeadQuarterId) as PropertyName,
sum(t2.amount) over (partition by t1.HeadQuarterId) as amount
from t1 left join
t2
on t2.PropertyId = t1.PropertyId;
This provided the result i expected.
SELECT HQTRS1 AS headId,Max(LLORD1) AS headName, sum(Amount) AS amount
FROM
(SELECT DISTINCT HeadQuarterId AS HQTRS1, PropertyName AS LLORD1 FROM Table_1 WHERE HeadQuarterId = PropertyId) AS temp
INNER JOIN Table_1 AS A ON A.HeadQuarterId = temp.HQTRS1
LEFT OUTER JOIN Table_2 B
ON B.PropertyId = A.PropertyId
GROUP BY HQTRS1

Select another column of matching record - Oracle SQL

I have this query:
SELECT
t1.*,
(
SELECT
MIN(t2.e_nm)
FROM
table2 t2
WHERE
t2.c_type = t1.c_type
AND t2.h_level = t1.h_level
AND t2.loop = t1.loop
AND t2.e_id = t1.e_id
HAVING
COUNT(*) = 1
) AS e_nm
FROM
table1 t1
ORDER BY
t1.f_name,
t1.line_num;
When e_nm gets selected from table2 as second parameter, I also want to grab another column of matching record - seq_nm from table1.
How can I do it in the above query?
If the count(*) = 1, then you can use a join with aggregation
SELECT t1.*, t2.e_nm, t2.x
FROM table1 t1 LEFT JOIN
(SELECT t2.c_type, t2.h_level, t2.loop, t2.e_id,
MIN(t2.e_nm) as e_nm, MIN(x) as x
FROM table2 t2
GROUP BY t2.c_type, t2.h_level, t2.loop, t2.e_id
HAVING COUNT(*) = 1
) t2
ON t2.c_type = t1.c_type AND
t2.h_level = t1.h_level AND
t2.loop = t1.loop AND
t2.e_id = t1.e_id
ORDER BY t1.f_name, t1.line_num;
This works because you have the COUNT(*) = 1, so only one row matches. If you didn't, you could still use KEEP:
MIN(x) KEEP (DENSE_RANK FIRST ORDER B t2.e_num ASC) as x

Join to only work if a record present in SQL

I have 2 tables - table1 and table2.
In table2, there are multiple records matching criteria for table1 based on:
c_type, h_level, loop, e_id
I want records from RIGHT table ONLY if there is EXACT 1 match. If not, element_nm should be NULL, so I have the exact same records in the output as left table.
SELECT a.*,
b.element_nm
FROM table1 a
LEFT JOIN table2 b ON
a.c_type = b.c_type
AND a.h_level = b.h_level
AND a.loop = b.loop
AND a.e_id = b.e_id
ORDER BY a.file_name,
a.line_num asc;
As this is about one value only, you can use a Sub query in the select clause. Otherwise you'd use a subquery in a LEFT OUTER JOIN or use OUTER APPLY.
SELECT
t1.*,
(
SELECT MIN(t2.element_nm)
FROM table2 t2
WHERE t2.c_type = t1.c_type
AND t2.h_level = t1.h_level
AND t2.loop = t1.loop
AND t2.e_id = t1.e_id
HAVING COUNT(*) = 1
) AS element_nm
FROM table1 t1
ORDER BY t1.file_name, t1.line_num;
Thorsten's answer works when you want only one column from the second table. But if you want multiple columns, it is a bit cumbersome.
Alternatively:
SELECT a.*, b.*
FROM table1 a LEFT JOIN
(SELECT b.*,
COUNT(*) OVER (PARTITION BY b.c_type, b.h_level, b.loop, b.e_id) as cnt
FROM b
) b
ON a.c_type = b.c_type AND
a.h_level = b.h_level AND
a.loop = b.loop AND
a.e_id = b.e_id AND
b.cnt = 1
ORDER BY a.file_name, a.line_num asc;
you should use Row_Number , like this :
WITH cte AS (
SELECT ROW_NUMBER() OVER(PARTITION BY a.c_type ,a.h_level,a.loop ,a.e_id ) rnum
,a.c_type ,a.h_level,a.loop ,a.e_id FROM table1 a
LEFT JOIN table2 b ON a.c_type = b.c_type AND a.h_level = b.h_level AND a.loop = b.loop AND a.e_id = b.e_id
)
,cte2 AS (SELECT * FROM cte WHERE rnum = 2)
SELECT a.*,
CASE WHEN cte2.element_nm IS NULL then b.element_nm ELSE NULL END element_nm
FROM table1 a
LEFT JOIN table2 b ON a.c_type = b.c_type AND a.h_level = b.h_level AND a.loop = b.loop AND a.e_id = b.e_id
LEFT JOIN cte2 ON a.c_type = cte2.c_type AND a.h_level = cte2.h_level AND a.loop = cte2.loop AND a.e_id = cte2.e_id

SQL - Search for cases where sum(amount) column across 2 tables are not equal

Table1 has one row per id, table 2 has one row per id where entrytype = 0 and multiple rows per id where entrytype = 1.
I run the following queries:
SELECT sum(amount) FROM table1 where id = 'AUS|License|Maintenance|Aug_2016'
select sum(amount) from table2 where entrytype = 0 and id = 'AUS|License|Maintenance|Aug_2016'
select sum(amount) from table2 where entrytype = 1 and id = 'AUS|License|Maintenance|Aug_2016'
And get the following results:
7689.12
7689.12
7689.119999
Now, I want to search both tables' sum(amount) for cases where table1 sum(amount) <> table2 sum(amount) for a particular id
If I've understood your requirement correctly, you could join each of your queries on their common id field, and then simply select those id values for which the summation is not equal across the three subqueries, e.g.:
SELECT a.id, a.s1, b.s2, c.s3
FROM
(
(
SELECT id, SUM(amount) AS s1
FROM table1
GROUP BY id
) a
INNER JOIN
(
SELECT id, SUM(amount) AS s2
FROM table2
WHERE entrytype = 0
GROUP BY id
) b ON a.id = b.id
)
INNER JOIN
(
SELECT id, SUM(amount) AS s3
FROM table2
WHERE entrytype = 1
GROUP BY id
) c ON a.id = c.id
WHERE NOT (a.s1 = b.s2 AND a.s1 = c.s3)
You didn't specify a DBMS, so I've included the extra parentheses required by MS Access, just in case.
By using INNER JOINs, I've also assumed that you're only interested in records which exist in all three subqueries; if one of the subqueries is the master, join the other two subqueries using LEFT JOINs so that all records in the master are returned.
Including the two additional columns mentioned in the subsequent comments -
SELECT a.id, a.s1, b.s2, c.s3, a.s1 - b.s2 AS s4, a.s1 - c.s3 AS s5
FROM
(
(
SELECT id, SUM(amount) AS s1
FROM table1
GROUP BY id
) a
INNER JOIN
(
SELECT id, SUM(amount) AS s2
FROM table2
WHERE entrytype = 0
GROUP BY id
) b ON a.id = b.id
)
INNER JOIN
(
SELECT id, SUM(amount) AS s3
FROM table2
WHERE entrytype = 1
GROUP BY id
) c ON a.id = c.id
WHERE NOT (a.s1 = b.s2 AND a.s1 = c.s3)
If you want it for a particular id, you can use cross join to bring them together:
select t1.amount, t2_0.amount, t2_1.amount
from (SELECT sum(amount) as amount FROM table1 where id = 'AUS|License|Maintenance|Aug_2016'
) t1 cross join
(select sum(amount) as amount from table2 where entrytype = 0 and id = 'AUS|License|Maintenance|Aug_2016'
) t2_0 cross join
(select sum(amount) as amount from table2 where entrytype = 1 and id = 'AUS|License|Maintenance|Aug_2016'
) t2_1;

Postgresql SELECT LEFT JOIN with columns on case

SELECT a1,a2,a3,a4,count(a5),b1,b2,b3
FROM table1
LEFT JOIN table2 ON a1=b1 AND a2=b2 (*here i need to join
next columns a3=b3 only if from table2 will be returned more than 1 records
other wise first 2 columns will be enough*)
group by a1,a2,a3,a4,a5,b1,b2,b3
Anybody knows how to perform this trick ?
Well, if I understand correctly:
FROM table1 t1 LEFT JOIN
(SELECT t2.*, COUNT(*) OVER (PARTITION BY b1, b2) as cnt
FROM table2 t2
)
ON t1.a1 = t2.b1 AND t1.a2 = t2.b2 AND
(cnt = 1 OR t1.a3 = t2.a3)