I have 2 different queries but uses same date column group. Is there any way to use them in one sql query?
status = 1 means certain
status = 0 means optional
1. Query = 'SELECT book_day, sum(pax) as pax, count(*) FROM rezervations WHERE book_day BETWEEN :first_day AND :end_day AND status = 1 GROUP BY book_day';
2. Query = 'SELECT book_day, sum(pax) as pax, count(*) FROM rezervations WHERE book_day BETWEEN :first_day AND :end_day AND status = 0 GROUP BY book_day';
i would like to have result like
'2020-08-15' | 50 pax status 1(certain) | 20 pax status 0 (optional)
'2020-08-18' | 10 pax status 1(certain) | 5 pax status 0 (optional)
.....
...
..
.
Thanks in advance.
Case when is answered my question. Here is the query:
select
book_day,
sum(case when status = 1 then pax else 0 end) as certain,
sum(case when status = 0 then pax else 0 end) as optional
from
rezervations
group by
book_day
And result is:
(`book_day`, `certain`, `optional`)
('2020-07-26', '15', '0'),
('2020-07-29', '20', '7'),
('2020-08-15', '15', '0'),
('2020-08-27', '20', '28'),
('2020-08-28', '0', '22');
Related
I have a table as follows
table_user
id name status flag country
====================================================================================================
1 AB 1 0 US
2 BC 1 0 UK
3 CD 0 0 IN
4 DE 3 0 BR
5 EF 3 0 UK
6 FG 2 0 IN
7 GH 4 0 IN
I want to count the no. of records where
status = 1 as totalactiverecords,
status = 0 as totalinactiverecords,
status = 3 as totalrecentactiverecords,
status = 2 as totalemailnotverifiedrecords,
status = 4 as totalemailverifiedrecords,
, and country is UK, all in a single SELECT statement.
Is there any specific way to do it?
I have thought of something like
SELECT COUNT(*) as totalactiverecords WHERE status=1
COUNT(*) as totalinactiverecordsWHERE status=0,
COUNT(*) as totalemailnotverifiedrecords WHERE status=2,
COUNT(*) as totalrecentactiverecords WHERE status=3,
COUNT(*) as totalemailverifiedrecords WHERE status=4
FROM table_user where country=IN
,but that does not seem to work.
You can try this query:
SELECT count(case status when '1' then 1 else null end) as totalactiverecords,
count(case status when '0' then 1 else null end) as totalinactiverecords,
count(case status when '2' then 1 else null end) as totalemailnotverifiedrecords,
count(case status when '3' then 1 else null end) as totalrecentactiverecords,
count(case status when '4' then 1 else null end) as totalemailverifiedrecords
FROM table_user where country='IN'
I usually use CASE WHEN in this kind of problem:
SELECT sum(case when status = 1 then 1 end) as totalactiverecords,
COUNT sum(case when status = 0 then 1 end) as totalinactiverecords,
COUNT sum(case when status = 2 then 1 end) as totalemailnotverifiedrecord,
COUNT sum(case when status = 3 then 1 end) as totalrecentactiverecords,
COUNT sum(case when status = 4 then 1 end) as totalemailverifiedrecords
FROM table_user where country=IN
I have below query:
SELECT users_name, first_name, last_name,
(CASE
WHEN flag1==1 AND flag2==1 AND flag3==0 THEN 'Active'
WHEN flag1==0 AND flag2==1 AND flag3==0 THEN 'Deactive'
WHEN flag1==0 AND flag2==0 AND flag3==0 THEN 'Disabled'
ELSE ''
END) as Status FROM `users`
I have multiple flag conditions before defining it's status but on running the above query it gives me.
Syntax error near==1
What's the correct way to achieve it?
Intended Result:
username|firstname|lastname|status
Alex | Alex | mark | Active
April | April | mark | Deactive
In SQL the comparision operator is = so use = (and not == ):
SELECT users_name, first_name, last_name,
(CASE
WHEN flag1 = 1 AND flag2 = 1 AND flag3 = 0 THEN 'Active'
WHEN flag1 = 0 AND flag2 = 1 AND flag3 = 0 THEN 'Deactive'
WHEN flag1 = 0 AND flag2 = 0 AND flag3 = 0 THEN 'Disabled'
ELSE ''
END) as Status FROM `users`
i have a situation where i supposed to display columnar fashion
im using this query
SELECT status, count(*) as total_
FROM companydata
WHERE status = 'Not Emailed' or status = 'Not Emailed' or status = 'Email Sent' or status = 'Response Received' or status = 'Interested' or status =
'Not Interested'
GROUP BY status
it return something like this
status total_
-|--------------------|--------------------------
Not Emailed 9562
| Interested | 2
Response Received 45
| Email Sent | 3000
is there any way to make them look columnar fashion
Not Emailed Interested Response Received Email Sent
9562 2 45 3000
Alter your original query to count conditionally
SELECT Count(CASE WHEN status = 'Not Emailed' THEN 1 END) AS `Not Emailed`,
Count(CASE WHEN status = 'Email Sent' THEN 1 END) AS `Email Sent`,
Count(CASE WHEN status = 'Response Received' THEN 1 END) AS `Response Received`,
Count(CASE WHEN status = 'Not Interested' THEN 1 END) AS `Not Interested`
FROM yourtable
WHERE status IN ( 'Not Emailed', 'Not Emailed', 'Response Received', 'Interested', 'Not Interested' )
Alternatively instead of COUNT we can use SUM aggregate as well
Sum(CASE WHEN status = 'Not Emailed' THEN 1 ELSE 0 END) AS `Not Emailed`
This is an elaboration of prdp's answer. In MySQL, I think the easiest method is:
SELECT SUM(status = 'Not Emailed') AS `Not Emailed`,
SUM(status = 'Email Sent') AS `Email Sent`,
SUM(status = 'Response Received') AS `Response Received`,
SUM(status = 'Not Interested') AS `Not Interested`
FROM yourtable
WHERE status IN ( 'Not Emailed', 'Not Emailed', 'Response Received', 'Interested', 'Not Interested' ) ;
CASE is also correct -- and portable to most databases. This is just a convenient short-hand in MySQL.
I have a table which looks something like this...
supplier_name status count
supplier_a Unavailable 1
supplier_a Refunded 5
supplier_a Shipped 10
supplier_z Refunded 2
supplier_z Shipped 4
I know exactly what columns I want and I would like to use the values from the first table to return values structured like the following...
supplier_name unavailable refunded shipped
supplier_a 1 5 10
supplier_z 0 2 4
(note: in cases where there is no value for a specific column it should be set to 0)
Would this be possible to achieve in a query?
I think something like this should work:
SELECT Supplier_Name,
MAX(CASE WHEN STATUS = 'Unavailable' THEN Count ELSE 0 END) as Unavailable,
MAX(CASE WHEN STATUS = 'Refunded' THEN Count ELSE 0 END) as refunded,
MAX(CASE WHEN STATUS = 'Shipped' THEN Count ELSE 0 END) as shipped
FROM YourTable
GROUP BY Supplier_Name
And here is the SQL Fiddle.
Good luck.
Try this:
SELECT
supplier_name,
SUM(COALESCE(CASE WHEN status = 'unavailable' THEN `count` END, 0)) unavailable,
SUM(COALESCE(CASE WHEN status = 'refunded' THEN `count` END, 0)) refunded,
SUM(COALESCE(CASE WHEN status = 'shipped' THEN `count` END, 0)) shipped
FROM tbl
GROUP BY supplier_name
SQL FIDDLE DEMO
I have a table
id, location, status
1, london, 1
2, london, 0
3, boston, 1
4, boston, 1
I'd like my query to generate something like this: -
location, status_1, status_0
london, 1, 1
boston, 2, 0
so far I have: -
select count(id) as freq, location from my_table
group by location order by freq desc;
I'm completely lost as to where to go from here.
That sort of transformation is better done in whatever client is issuing the query, but if you have to do it in the database, then
select location,
sum(status = 1) AS status_1,
sum(status = 0) AS status_0
from my_table
group by location
it's a bit hackish, but the 'status = 1' when status really is 1 will evaluate to a boolean true, which MySQL will politely cast to an integer '1', which then gets summed up. Same goes for when status is 0 and status = 0 evaluates to true.
So you want to count the records for each status per city?
In the query below, I group by location (like your did), and then add a sum for each state. Inside the sum is a case, that either returns 1 if the record matches the desired state, or 0 otherwise. That way, you effectively count the number of records for each state per location.
select
a.location,
sum(case when a.status = 1 then 1 else 0 end) as status_1,
sum(case when a.status = 0 then 1 else 0 end) as status_0
from
YourTable a
group by
a.location
select location,
sum(case when status = 1 then 1 else 0 end) as status_1,
sum(case when status = 0 then 1 else 0 end) as status_0,
from my_table
group by location;