I'm using PostgreSQl and this is my table in database:
how can I write a query that gives me this result :
or you can write query like
select id_salarie,
max(case when date_pointage = '2015-01-01' then round(nb_heures::numeric,2) else null end) as "2015-01-01",
max(case when date_pointage = '2015-01-02' then round(nb_heures::numeric,2) else null end)as "2015-01-02",
max(case when date_pointage = '2015-01-03' then round(nb_heures::numeric,2) else null end) as "2015-01-03"
from my_table where id_salarie = 1
group by id_salarie;
Query looks huge and terrible but works for cubes
Related
I have a table (tbl2) i have 4 columns with feedback attributes listed. I need to generate a pivot table like syntax in mysql. Need output somewhat like this pivot table. I am currently trying this
INSERT INTO 1 (`BAD/GOOD`, `PRICE YES`, `PRICE NO`, `TOTAL PRICE`) SELECT "BAD",COUNT(*) WHERE tbl2.PRICE="BAD" AND tbl2.Churn="YES",COUNT(*) WHERE tbl2.PRICE="BAD" AND tbl2.Churn="NO",COUNT(*) WHERE tbl2.PRICE="BAD" FROM tbl2_customers_churn
and also tried insert into as values
INSERT INTO 1 VALUES ("BAD",COUNT(*) FROM tbl2 WHERE tbl2.PRICE="BAD" AND tbl2.Churn="YES",COUNT(*) FROM tbl2 WHERE tbl2.PRICE="BAD" AND tbl2.Churn="NO",COUNT(*) FROM tbl2 WHERE tbl2.PRICE="BAD")
for bad and good count separately
Any advise on how to tackle this in SQL?
I think you want logic more like this:
INSERT INTO 1 (`BAD/GOOD`, `PRICE YES`, `PRICE NO`, `TOTAL PRICE`)
SELECT 'BAD',
SUM(CASE WHEN tbl2.PRICE = 'BAD' AND tbl2.Churn = 'YES' THEN 1 ELSE 0 END),
SUM(CASE WHEN tbl2.PRICE = 'BAD' AND tbl2.Churn = 'NO' THEN 1 ELSE 0 END),
SUM(CASE WHEN tbl2.PRICE = 'BAD' THEN 1 ELSE 0 END)
FROM tbl2_customers_churn tbl2;
Or a little more simply:
SELECT 'BAD',
SUM(CASE WHEN tbl2.Churn = 'YES' THEN 1 ELSE 0 END),
SUM(CASE WHEN tbl2.Churn = 'NO' THEN 1 ELSE 0 END),
COUNT(*)
FROM tbl2_customers_churn tbl2
WHERE tbl2.PRICE = 'BAD'
I have a table which consists of data where in I'm having trouble counting the corresponding rows.
Here is the sample table:
I am expecting an output like this:
You can do conditional aggregation:
select
sum(case when result = 'X' then 1 else 0 end) count_x,
sum(case when result is null then 1 else 0 end) count_blank
from mytable
I assume that by blank you mean null. If not, then you can change the condition in the second sum() from result is null to result = ''.
If you are running MySQL, this can be shortened a little:
select
sum(result = 'X') count_x,
sum(result is null) count_blank
from mytable
I have a table in oracle similar to the the example table image that I need to have output the segregated totals as shown in the second example results image.
I need the sums of each item_type where A & B are just examples for a large number of possible items that can be added.
example table:
example results:
Any help would be appreciated. thanks
Use conditional aggregation:
SELECT
store,
SUM(CASE WHEN item_type = 'A' AND item_status = 'IN' THEN 1 ELSE 0 END) a_items_in,
SUM(CASE WHEN item_type = 'A' AND item_status = 'OUT' THEN 1 ELSE 0 END) a_items_out,
SUM(CASE WHEN item_type = 'B' AND item_status = 'IN' THEN 1 ELSE 0 END) b_items_in,
SUM(CASE WHEN item_type = 'B' AND item_status = 'OUT' THEN 1 ELSE 0 END) b_items_out
FROM mytable
GROUP BY store
This question already has answers here:
How can I get multiple counts with one SQL query?
(12 answers)
Closed 4 years ago.
I am counting specific things in a SQL Server table. I am using multiple count queries, but I am wondering if it's possible to combine them into a single query with the column name and count numbers in a single table for display.
My queries are:
select count(*) as Ask_Count
from Pld_OpenSalesOrdersLines
where left(C_Item_ID, 1) = '*'
select count(*) as M_Count
from Pld_OpenSalesOrdersLines
where (left(C_Item_ID, 1) = 'M' and len(C_Item_ID) = 1)
select count(*) as MN_Count
from Pld_OpenSalesOrdersLines
where (left(C_Item_ID, 2) = 'MN' and len(C_Item_ID) = 2)
I tried a couple stupid things to combine them, but they were a failure. I honestly can't even begin to think how to combine them, maybe it's not possible?
Thanks!
You could use CASE expression to perform conditional aggregation:
select
COUNT(CASE WHEN LEFT(C_Item_ID,1)='*' THEN 1 END) AS Ask_Count,
COUNT(CASE WHEN LEFT(C_Item_ID,1)='M' AND LEN(C_Item_ID)=1 THEN 1 END) M_Count,
COUNT(CASE WHEN LEFT(C_Item_ID,2)='MN' AND LEN(C_Item_ID)=2 THEN 1 END) MN_Count
from Pld_OpenSalesOrdersLines
Use conditional aggregation:
select sum(case when LEFT(C_Item_ID,1) = '*' then 1 else 0 end) as count_1,
sum(case when LEFT(C_Item_ID,1) = 'M' AND LEN(C_Item_ID)=1 then 1 else 0 end) as count_2,
sum(case when LEFT(C_Item_ID,2) = 'MN' AND LEN(C_Item_ID)=2 then 1 else 0 end) as count_3
from Pld_OpenSalesOrdersLines;
I would write the logic like this, though:
select sum(case when C_Item_ID like '*%' then 1 else 0 end) as count_1,
sum(case when C_Item_ID = 'M' then 1 else 0 end) as count_2,
sum(case when C_Item_ID = 'MN' then 1 else 0 end) as count_3
from Pld_OpenSalesOrdersLines;
Doing a left() on a column and then checking the length is redundant. Just use =.
I'm trying to write a query that returns the following columns:
owner_id,
number_of_concluded_bookings,
number_of_declined_bookings,
number_of_inquiries
However, the problem is that my WHERE clause messes up the query because I am querying the same table. Here is the code:
SELECT owner_id,
Count(*) AS number_of_cancelled_bookings
FROM bookings
WHERE state IN ('cancelled')
GROUP BY owner_id
ORDER BY 1;
It's easy to retrieve the columns individually, but I want all of them. Say I wanted number_of_concluded_bookings as well, that would mean I'd have to alter the WHERE clause ...
Help is greatly appreciated!
Consider conditional aggregations:
SELECT owner_id,
SUM(CASE WHEN state='concluded' THEN 1 ELSE 0 END) AS number_of_concluded_bookings,
SUM(CASE WHEN state='cancelled' THEN 1 ELSE 0 END) AS number_of_cancelled_bookings,
SUM(CASE WHEN state='declined' THEN 1 ELSE 0 END) AS number_of_declined_bookings,
SUM(CASE WHEN state='inquiries' THEN 1 ELSE 0 END) AS number_of_inquiries
FROM bookings
GROUP BY owner_id