SQL merge result set from two select queries and transpose counting - sql

I'm trying to extract data from database with a particular template.
In my database I have the following data:
System | Type | Number
A TypeA 1
A TypeB 1
A TypeA 1
B TypeA 1
I'd like to get something like this:
System | #TypeA | #TypeB
A 2 1
B 1 0
Is it possible with only one query?
I tried to do full outer join between two queries without success. I'm curious to know the best solution in terms of performance.
Thank you in advance!

You can do this with aggregation using a conditional case expression
select system,
Sum(case when type='TypeA' then 1 else 0 end) "#TypeA",
Sum(case when type='Typeb' then 1 else 0 end) "#TypeB"
from t
group by system;

Related

SQL transform table with sum based on values

i have table like this:
operation_id
order_id
qty
qty_type
detail_type
1
1
240
ready
glued
1
1
199
ready
unglued
1
1
100
done
glued
1
2
50
ready
glued
and would like to transform into this. it means to add 4 columns and to sum them from above table based on a conditions, like detail_type = 'glued', qty_type = 'ready' etc.
operation_id
order_id
qty_glued_ready
qty_unglued_ready
qty_glued_done
qty_unglued_done
1
1
240
199
10
10
can somebody help me how query should look like?
I assume it is just an example that you have mentioned in your OP and it is not accurate according to your table data you have mentioned.
I don't understand how your qty_glued_done is 10
But here is something you can start working out with:
SELECT o.`operation_id`, o.`order_id`,
SUM(CASE WHEN `detail_type`='glued' AND o.`qty_type`='ready' THEN o.`qty` ELSE 0 END) AS qty_glued_ready,
SUM(CASE WHEN `detail_type`='unglued' AND o.`qty_type`='ready' THEN o.`qty` ELSE 0 END) AS qty_unglued_ready
(and so on)
FROM `operation_table` o GROUP BY o.`operation_id`

1 or 0 Per Group

Re-edited to make it clearer:
I would like my results to appear as they do in 'Column b' based on 'Column a' groupings? so a 1 or 0 per group based on column a. Column b does not exist currently, I am trying to code this in. I was trying to use row_number or rank but this not appear to work for me. So how do I write my SQL so I can get my SQL results to mirror Column b? Any help is appreciated
Thank - you
column a
column b
aaa
1
aaa
0
ddd
1
ddd
0
ddd
0
yyy
1
yyy
0
yyy
0
You just need to wrap your row_number() in a case, something like this:
select
column_a
case row_number() over (partition by column_a)
when 1 then 1
else 0
end as column_b
from
table
/

Is there a way to count the number of values in column?

I am a newbie studying MSSQL and Database.
I am trying to write a SQL query to count values in the column.
Following table is original one.
name value
----------
A 1
A 1
A 2
B 1
B 2
I want to get a table like this.
name one two
--------------
A 2 1
B 1 1
A has two 1 and one 2 and B has one 1 and 2. It seems I can accomplish it using COUNT built-in function. I tried but failed. Is there any idea to do it?
use conditional aggregation
select name, sum(case when value=1 then 1 else 0 end) as one,
sum(case when value=2 then 1 else 0 end) as two
from table_name group by name

Joining two rows into one rows (different column) in oracle

I have table with values like this
ID |Name |Balance |Type |ProductName
1 Abdol 10000 1 RAID100
2 HIVE 5000000 2 RAID100
3 Ade 20000 1 RAID200
4 Koi 30000 1 RAID100
I want to re-arrange the table using select script to:
ProductName | Balance_Type_1 | Balance_Type_2
RAID100 40000 5000000
RAID200 20000 0
I've tried nested case when, but it always create one column with Product-name followed by 0 value besides and repeated.
Thank You
You want aggregation :
select productname,
sum(case when type = 1 then balance else 0 end) as balance_type1,
sum(case when type = 2 then balance else 0 end) as balance_type2
from table t
group by productname;
Run the aggregation query whenever is needed, do not create summary table structure in the database. It will be difficult to manage.
One option would be using pivot provided you database has version 11g+ :
select ProductName ,
sum(nvl(Balance_Type_1,0)) as Balance_Type_1,
sum(nvl(Balance_Type_2,0)) as Balance_Type_2
from t
pivot
(
sum( Balance ) for Type in ( 1 as Balance_Type_1, 2 as Balance_Type_2 )
)
group by ProductName;
Demo

SQL Aggregation prior to advanced SQL

Given a simple case like this:
Gender DeptID
--------------
M 1
F 1
M 2
F 2
F 2
What SQL statement I need to write If I want to generate the following result using SQL without using advanced CUBE, Rollup, etc., just using plain SQL-92:
GenderSum Dept1Sum Dept2Sum
----------------------------
M 1 1
F 1 2
I was wondering how such information would be generated by ETL in the past using SQL?
Remark: It is possible to use Group by on gender and union that with a group by on DetptId to get a vertical result set but this is clearly not what I want.
You can try the following pivot query:
SELECT Gender AS GenderSum,
SUM(CASE WHEN DeptID = 1 THEN 1 ELSE 0 END) AS Dept1Sum,
SUM(CASE WHEN DeptID = 2 THEN 1 ELSE 0 END) AS Dept2Sum
FROM yourTable
GROUP BY Gender