SQL ROW TO COLUMN [duplicate] - sql

This question already has answers here:
Dynamic Pivot in Oracle's SQL
(10 answers)
Closed 8 years ago.
I have a table as follow
OPERATIONS
OPERATOR-OPERATION TYPE
Ahmet DELETE
Ahmet UPDATE
Ahmet READ
Salih DELETE
Salih UPDATE
Salih READ
Salih READ
Salih CREATE
What SQL will be to get following result? When simple COUNT, GROUP BY clauses used Operation types written as ROW based.
Name- Total- DELETE- READ- UPDATE CREATE
Ahmet 3 1 1 1 0
Salih 5 1 2 1 1

select operator,
count(*) as total,
sum(case when operation_type = 'DELETE' then 1 else 0 end) as deletes,
sum(case when operation_type = 'UPDATE' then 1 else 0 end) as updates,
sum(case when operation_type = 'READ' then 1 else 0 end) as reads,
sum(case when operation_type = 'CREATE' then 1 else 0 end) as creates
from operations
group by operator

Related

Select, count, and Group the value in a table that has been joined

I am using SQL Server, and I have two tables: product_assembly and product_testing in the production database
product_assembly table:
serial_number
batch
xx11
1
xx12
1
xx13
2
xx14
2
xx15
1
xx16
3
xx17
3
xx18
3
xx19
2
Table product_testing:
serial_number
batch
xx11
Passed
xx12
Passed
xx13
Passed
xx14
Failed
xx15
Passed
xx16
Passed
xx17
Failed
xx18
Passed
xx19
Passed
How can I get a table like this.
I tried using count, join, and nested SELECT but it doesn't work.
Thank you
Batch
Passed
Failed
1
3
0
2
2
1
3
2
1
We can use conditional aggregation here:
SELECT
pa.batch,
SUM(CASE WHEN pt.batch = 'Passed' THEN 1 ELSE 0 END) AS Passed,
SUM(CASE WHEN pt.batch = 'Failed' THEN 1 ELSE 0 END) AS Failed
FROM product_assembly pa
LEFT JOIN product_testing pt
ON pt.serial_number = pa.serial_number
GROUP BY
pa.batch;

SQL - Impala - How to unfold one categorical column into many?

I have the following table :
user category number
1 A 8
1 B 6
2 A 1
2 C 9
3 B 5
I want to "unfold" or "dummify" the category column and fill them with the "number" column to obtain:
user cat_A cat_B cat_C
1 8 6 0
2 1 0 9
3 0 5 0
Is it possible to achieve this in SQL (Impala) ?
I found this question How to create dummy variable columns for thousands of categories in Google BigQuery?
However it seems a little bit complex and I'd rather do it in Pandas.
Is there a simpler solution, knowing that I have 10 categories (A, B, C, D etc)?
You can try to use condition aggregate function.
SELECT user,
SUM(CASE WHEN category = 'A' THEN number ELSE 0 END) cat_A,
SUM(CASE WHEN category = 'B' THEN number ELSE 0 END) cat_B,
SUM(CASE WHEN category = 'C' THEN number ELSE 0 END) cat_C
FROM T
GROUP BY user

Find all Columns and its count of occurrence which have value 1 in SQL Server table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
ID Name Dept Project Job Platform
A101 1 0 1 1 0
A103 0 1 1 0 1
B201 1 1 0 0 0
C301 1 0 1 1 1
Any help will be greatly appreciated
EDIT: I have tried these queries:
SELECT count(sittingreading),count(WatchingTV) FROM table_name WHERE sittingreading IS NOT NULL
Select COUNT(*) from table_name where column_value=0
Use a case statement in count() to make SQL Server only count the rows where you have the value you are looking for.
select count(case when T.Name = 0 then 1 end) as NameCount,
count(case when T.Dept = 0 then 1 end) as DeptCount,
count(case when T.Project = 0 then 1 end) as ProjectCount
from YourTable as T

SQL rows to column [duplicate]

This question already has answers here:
SQL Rows to Columns
(3 answers)
Closed 7 years ago.
This is my query.
select name, walk_to, count(*) count
from walk
where name='Rizwan'
group by name, walk_t0;
This results in data like:
NAME WALK_TO COUNT
Rizwan Cafe 2
Rizwan Arena 10
Rizwan Outside 20
There are other users' data as well. What I want is:
NAME CAFE ARENA OUTSIDE
Rizwan 2 10 20
John 3 21 90
James 5 5 9
Any idea how to write a query to get this result?
SELECT name,
SUM(CASE WHEN walk_to = 'Cafe' THEN 1 ELSE 0 END) Cafe,
SUM(CASE WHEN walk_to = 'Arena' THEN 1 ELSE 0 END) Arena,
SUM(CASE WHEN walk_to = 'Outside' THEN 1 ELSE 0 END) Outside
FROM walk
GROUP BY name
This is called pivioting for more examples look here SQL Server: Examples of PIVOTing String data
select name,
sum(case when walk_to='Cafe' then 1 else 0 end) as CAFE,
sum(case when walk_to='Arena' then 1 else 0 end) as ARENA,
sum(case when walk_to='Outside' then 1 else 0 end) as OUTSIDE
FROM walk
GROUP BY name

sql query required grouping column in single row [duplicate]

This question already has answers here:
need to return two sets of data with two different where clauses
(2 answers)
Closed 8 years ago.
I have following table structure
TicketID Status Duration
-----------------------------
1234 8 2
1233 8 10
1232 4 5
1231 8 12
1230 4 50
status 8 means Closed
status 4 means Open
It is required to have output in following way. Please do the need ful. If possible I wanted it in a single sql query.
Please help me to produce output in following way.
Row Closed (sum) Open(Sum)
---------------------------------
1 24 55
select 1 as row,
sum(case when status = 8 then 1 else 0 end) as closed,
sum(case when status = 4 then 1 else 0 end) as open
from your_table
select
1 as Row,
sum(case when Status=8 then 1 else 0 end ) as Closed_Sum,
sum(case when Status=4 then 1 else 0 end ) as Open_Sum
from
Mytable