Oracle count if column exists in other table with conditions - sql

I have this table
COL_A FROM TO
------------------
D1 1 3
D2 3 7
And also this other table
COL_A VALUE
-------------
D1 0
D1 2
D1 5
D2 2
D2 5
D2 6
I want to obtain this. For each row in the first table, count the rows in the second table whose value is less than, between and greater than the FROM and TO columns.
COL_A FROM TO LESS_THAN_FROM BETWEEN_FROM_TO GREATER_THAN_TO
-------------------------------------------------------------------
D1 1 3 1 1 1
D2 3 7 1 2 0

Use join and conditional aggregation:
select t.col_a, t.from, t.to,
sum(case when o.value < t.from then 1 else 0 end) as less_than,
sum(case when o.value between t.from and t.to then 1 else 0 end) as in_between,
sum(case when o.value > t.to then 1 else 0 end) as greater_than
from this_table t join
other_table o
on t.col_a = o.col_a
group by t.col_a, t.from, t.to;

Related

Slicing table data based on days

Having the following data table:
col1
days
A
2
B
3
C
1
C
5
D
3
A
3
B
7
A
4
I want to transform it into:
col1
<=2days
<=5days
<=7days
A
1
2
0
B
0
1
1
C
1
1
0
D
0
1
0
I used the below query to achieve this:
select col1,
CASE WHEN days <=2 then count(days) as "<=2days",
CASE WHEN days > 2 and days <=5 then count(days) as "<=5days",
CASE WHEN days > 5 and days <=7 then count(days) as "<=7days"
from tableA group by col1,days
But it returns a result like the following:
col1
<=2days
<=5days
<=7days
A
1
0
0
A
0
2
0
B
0
1
0
B
0
0
1
C
0
1
0
C
1
0
0
D
0
1
0
Can someone please help here?
You could use conditional aggregation as the following:
SELECT col1,
COUNT(CASE WHEN days<=2 THEN 1 END) AS '<=2days',
COUNT(CASE WHEN days<=5 and days>2 THEN 1 END) AS '<=5days',
COUNT(CASE WHEN days<=7 and days>5 THEN 1 END) AS '<=7days'
FROM tableA
GROUP BY col1
ORDER BY col1
See a demo on MySQL.

How to select data with group by and subquery calculations?

I have two tables:
list_table:
id
name
1
a
2
b
3
c
vt_table:
id
list_id
amount
direction_id
1
1
20
1
2
1
12
2
3
1
15
1
4
2
23
1
5
1
20
1
6
1
20
2
7
1
18
1
I need this result:
amount (dir_id = 1 - dir_id = 2), list_id
amount
list_id
41
1
23
2
0
3
Amount is sum of all amount fields in table vt_table where direction_id = 1 minus sum of all amount fileds in table vt_table where direction_id = 2
And I need group this calculations by list_id, and if table have no rows with list_id 3, as example, amount must be 0.
I'm trying to do it with this query:
SELECT vt.list_id
, ((SELECT COALESCE(SUM(vt.amount), 0)
FROM table_name vt
WHERE vt.direction_id = 1)
-
(SELECT COALESCE(SUM(vt.amount), 0)
FROM table_name vt
WHERE direction_id = 2)) AS result
FROM table_name vt
GROUP BY vt.list_id
But I don't know how to group it correctly and make it so that if there were no entries for some list_id, then the amount was 0 for this list_id.
I use PostgreSQL 12.
Here the examples
You can try to use OUTER JOIN with condition aggregate function with COALESCE fucntion.
Query 1:
SELECT l.id,
SUM(COALESCE(CASE WHEN vt.direction_id = 1 THEN vt.amount END,0)) -
SUM(COALESCE(CASE WHEN vt.direction_id = 2 THEN vt.amount END,0)) AS result
FROM table_name vt
RIGHT JOIN list l ON vt.list_id = l.id
GROUP BY l.id
ORDER BY l.id
Results:
| id | result |
|----|--------|
| 1 | 41 |
| 2 | 23 |
| 3 | 0 |
Try something like this, as a start:
SELECT vt.list_id
, COALESCE(SUM(CASE WHEN direction_id = 1 THEN amount END), 0)
- COALESCE(SUM(CASE WHEN direction_id = 2 THEN amount END), 0) AS result
FROM table_name vt
GROUP BY vt.list_id
;
Result using your fiddle:
list_id
result
1
41
2
23
This just misses the cases where there are no vt rows for some list.
Use an outer join to address those cases.
SELECT SUM(CASE WHEN vt.direction_id = 1 THEN vt.amount ELSE 0 END) - SUM(CASE WHEN vt.direction_id = 2 THEN vt.amount ELSE 0 END) as amount,
lt.id as list_id
FROM list_table lt
LEFT OUTER JOIN vt_table vt
ON lt.id = vt.list_id
GROUP BY lt.id
ORDER BY lt.id

oracle - summarize table with value group by with distinct value

i have table like this :
school grade type
------ ----- -----
sc1 g1 t1
sc2 g2 t1
sc3 g4 t3
sc4 g3 t2
sc1 g2 t3
... etc
column grade has 4 distinct value g1,g2,g3,g4
column type has 3 distinct value t1,t2,t3
column school has 120 distinct value
and i shoud get Summary like this table :(for each distinct column 1-school-)
school g1 g2 g3 g4 sum(g1+g2+g3+g4) t1 t2 t3 sum(t1+t2+t3)
sc1 3 5 1 1 10 21 4 2 27
sc2 2 4 5 5 16 5 23 1 29
etc....
How can I write this query to give me the desired result I achieved part of this job by using report builder V6i
i get all number of distinct school in 1st level and order them ascending
i make query for each value (g1,g2,g3,g4,t1,t2,t3)
i want to get that table in sql query without using report
Conditional aggregation:
with
like_this ( school, grade, typ ) as (
select 'sc1', 'g1', 't1' from dual union all
select 'sc2', 'g2', 't1' from dual union all
select 'sc3', 'g4', 't3' from dual union all
select 'sc4', 'g3', 't2' from dual union all
select 'sc1', 'g2', 't3' from dual
)
-- end of test data; SQL query begins below this line
select school,
count(case when grade = 'g1' then 1 end) as g1,
count(case when grade = 'g2' then 1 end) as g2,
count(case when grade = 'g3' then 1 end) as g3,
count(case when grade = 'g4' then 1 end) as g4,
count(grade) as total_g,
count(case when grade = 't1' then 1 end) as t1,
count(case when grade = 't2' then 1 end) as t2,
count(case when grade = 't3' then 1 end) as t3,
count(typ) as total_t
from like_this
group by school
order by school -- optional
;
SCH G1 G2 G3 G4 TOTAL_G T1 T2 T3 TOTAL_T
--- ------- ------- ------- ------- ------- ------- ------- ------- -------
sc1 1 1 0 0 2 0 0 0 2
sc2 0 1 0 0 1 0 0 0 1
sc3 0 0 0 1 1 0 0 0 1
sc4 0 0 1 0 1 0 0 0 1
4 rows selected.

SQL -- Multiple rows, similar value in a row, need to not show specific values

Here is the issue:
Table name = a
1 2 3
123 1 A
123 1 A
123 2 A
332 1 A
332 1 A
321 2 B
321 2 A
321 1 A
So far what I have is this:
select distinct 1,2,3 from a where a.2='1' and a.3='B';
What it returns is each result (except for 321).
I only want to select values column 1 as long as that value is not in a row where there is a 2 in column 2 or a B in column 3. Is this possible?
"not in a row where there is a 2 in column 2 or a B in column 3" can be expressed as
select distinct 1,2,3 from a where a.2!='2' or a.3!='B';
or
select distinct 1,2,3 from a where a.2 <> '2' or a.3 <> 'B';
I would use group by and having:
select col1
from t
group by col1
having sum(case when col2 = 2 then 1 else 0 end) = 0 and
sum(case when col3 = 'B' then 1 else 0 end) = 0;

Conditional UNPIVOT in TSQL

Let's say I have a table with an ID column, and several property columns
MyTable (ID, PI, P2, P3, P4)
ID P1 P2 P3 P4
1 A1 B C1 D1
2 C1 C2 B NULL
3 C2 Z NULL NULL
4 X A1 C1 NULL
So, I need to write a query to find out how many distinct property values out there, no matter in which column they are.
Value Count
A1 2
B 2
C1 3
C2 2
X1 1
...
I think I can get this by using UNPIVOT (correct me, if I am wrong)
Now, how can I get similar count but grouped by a number of non-null values in the row (the count of non-null values per row may, or may not include key columns, doesn't matter), i.e. output like this:
Value NonNullCount Count
A1 3 1
A1 4 1
B 3 1
B 4 1
C1 2 3
C1 4 1
C2 3 1
C2 2 1
...
Here is one method, using cross apply for the unpivot:
select vals.p, t.NonNullCount, count(*)
from (select t.*,
((case when p1 is not null then 1 else 0 end) +
(case when p2 is not null then 1 else 0 end) +
(case when p3 is not null then 1 else 0 end) +
(case when p4 is not null then 1 else 0 end)
) as NonNullCount
from table t
) t cross apply
(values (p1), (p2), (p3), (p4)) vals(p)
where vals.p is not null
group by vals.p, t.NonNullCount;