I have a table that assigns workdays to worker-IDs.
Table: workdays Table: jobs
+------+-------+ +------+-----------+
| id | days | | id | jobs |
+------+-------+ +------+-----------+
| 1 | mon | | 1 | cleaning |
| 1 | tue | | 2 | cooking |
| 2 | sat | | 3 | driving |
| 3 | mon | | 4 | cleaning |
| 3 | tue | | 5 | cooking |
| 3 | sat | +------+-----------+
| 4 | wed |
| 4 | mon |
| 5 | tue |
+------+-------+
This is the query I use to select the IDs of people who work on any of the days I specify (say Monday and Tuesday):
SELECT * FROM workdays WHERE days IN ( 'mon', 'tue' )
Now I'd like to select those people who work on ALL of the days I specify. How can I do that?
edit: Added second table.
assuming days is unique for every id.
SELECT id
FROM workdays
WHERE days IN ('mon', 'tue')
GROUP BY id
HAVING COUNT(*) = 2
otherwise, you need to have DISTINCT in HAVING clause to count only unique values.
HAVING COUNT(DISTINCT days) = 2
Related
I have a table that looks like the below and would like to a count of all bookings with a a count of all guests per week. There are 30 guest fields:
locations
| Field | Type |
| ------------ | ---------- |
| location | text |
| day_number | int(11) |
| month_number | int(11) |
| year | text |
| week_number | tinyint(4) |
| start | datetime |
| end | datetime |
| guest_1 | text |
| guest_2 | text |
| … | |
| guest_30 | text |
I am using:
select locaton, count(*) AS `Number of bookings per week`, week_number,
month_number, `month`
FROM locations
GROUP BY location, week_number;
That gets me:
| location | ...Bookings | week_number | month_number | January |
| ------------ | ----------- | ----------- | ------------ | -------- |
| Location One | 3 | 2 | 1 | January |
| Location One | 5 | 3 | 1 | January |
| Location One | 2 | 4 | 1 | January |
| Location One | 2 | 5 | 2 | February |
| Location One | 5 | 6 | 2 | February |
| Location One | 1 | 7 | 2 | February |
| Location One | 3 | 8 | 2 | February |
There are many locations.
How can I combine a count of guests over the 30 guest fields for each week and location in my query?
Thanks for any help in advance.
Simply count the guests for each combination and then sum up those counts
SELECT location,week_number,month_number,SUM(
(guest_1 <> "") +
(guest_2 <> "") +
(guest_3 <> "") +
....
(guest_30 <> "")
) AS num_guests
FROM locations
GROUP BY location,week_number,month_number
It's difficult because this table is not normalized.
In my opinion is this possible please re-create this solution and create new table GUEST with foreign key from LOCATION.
Next step is create subquery for example:
(you query) t1
having 30 = (select count(*) from GUEST where idlocation=t1.idlocation and year=t1.year, week_number=t1.week_number)
It will be more better resolve because in this situation you can add more guest without change logical table.
In your situation you always check every column separately.
I have some tables from which I need to get data.
Here is my structure:
employees
| id | name |
+----+---------+
| 1 | Michael |
| 2 | Sarah |
reports
| id | employee_id | month | year | value | group_id |
+----+-------------+-------+------+-------+----------+
| 1 | 1 | 01 | 2018 | 35 | 1 |
| 2 | 1 | 02 | 2018 | 12 | 1 |
| 3 | 2 | 02 | 2018 | 2 | 2 |
groups
| id | name | employee_id |
+----+------+-------------+
| 1 | G11 | 1 |
| 2 | Z15 | 2 |
Now I need to get groups with employee WHERE employee with group_id AND month AND year DON'T HAVE REPORT, eg.
When I look for 01.2018, it should returns me only Z15 but when I look for 04.2018 it should return Z15 and G11.
How can I do this? At this moment I have sth like this:
SELECT
groups.*,
employees.*,
-- all fields from reports
FROM
groups
INNER JOIN
employees
ON
employees.id = groups.employee_id
My column names are slightly different from yours. That's deliberate...
SELECT g.*
FROM groups g
LEFT
JOIN reports r
ON r.group_id = g.group_id
AND r.yearmonth = 201801
WHERE r.report_id IS NULL;
I have a ratings table, where each user can add one rating a day. But each user might miss several days between ratings.
I'd like to get the average rating for each user_id's first 7 entries of created_at.
My table:
mysql> desc entries;
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| rating | tinyint(4) | NO | | NULL | |
| user_id | int(10) unsigned | NO | MUL | NULL | |
| created_at | timestamp | YES | | NULL | |
+------------+------------------+------+-----+---------+----------------+
Ideally I'd just get something like:
+------------+------------------+
| day | average_rating |
+------------+------------------+
| 1 | 2.53 |
+------------+------------------+
| 2 | 4.30 |
+------------+------------------+
| 3 | 3.67 |
+------------+------------------+
| 4 | 5.50 |
+------------+------------------+
| 5 | 7.23 |
+------------+------------------+
| 6 | 6.98 |
+------------+------------------+
| 7 | 7.22 |
+------------+------------------+
The closest I've been able to get is:
SELECT rating, user_id, created_at FROM entries ORDER BY user_id asc, created at desc
Which isn't very close at all...
Is it even possible? Will the performance be terrible? It's something that would need to run every time a web page is loaded, so would it be better to just run this once a day and save the results? (to another table!?)
edit - second attempt
Working towards a solution, I think this would get the rating for each user's first day:
select rating from entries where user_id in
(select user_id from entries order by created_at limit 1);
But I get:
ERROR 1235 (42000): This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
So now I'm going to play around with JOIN to see if that helps.
edit - third attempt, getting closer
I found this stackoverflow post, which is closer to what I want.
select e1.* from entries e1 left join entries e2
on (e1.user_id = e2.user_id and e1.created_at > e2.created_at)
where e2.id is null;
It gets the rating for the first day for each user.
Next step is to work out how to get days 2 to 7. I can't use 1.created_at > e2.created_at for that, so I'm really confused now.
edit - fourth attempt
Okay, I think it's not possible. Once I worked out how to turn off 'full group by' mode, I realised I'll probably need to use a subquery with limit <user_id>, <day_num>, for which I get:
ERROR 1235 (42000): This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
My current method is to just get the entire table, and use PHP to calculate the average for each day.
If I understand correctly you want to take the last 7 ratings the user gave, ordered by the date they gave the rating. The last 7 ratings of one user may fall on different days to another user, however they will be averaged together regardless of date.
First we need to order the data by user and date and give each user their own incrementing row count. I do this by adding two variables, one for the last user id and one for the row number:
select e.created_at,
e.rating,
if(#lastUser=user_id,#row := #row+1, #row:=1) as row,
#lastUser:= e.user_id as user_id
from entries e,
( select #row := 0, #lastUser := 0 ) vars
order by e.user_id asc,
e.created_at desc;
If the previous user_id is different we reset the row counter to 1. The result from this is:
+---------------------+--------+------+---------+
| created_at | rating | row | user_id |
+---------------------+--------+------+---------+
| 2017-01-10 00:00:00 | 1 | 1 | 1 |
| 2017-01-09 00:00:00 | 1 | 2 | 1 |
| 2017-01-08 00:00:00 | 1 | 3 | 1 |
| 2017-01-07 00:00:00 | 1 | 4 | 1 |
| 2017-01-06 00:00:00 | 1 | 5 | 1 |
| 2017-01-05 00:00:00 | 1 | 6 | 1 |
| 2017-01-04 00:00:00 | 1 | 7 | 1 |
| 2017-01-03 00:00:00 | 1 | 8 | 1 |
| 2017-01-02 00:00:00 | 1 | 9 | 1 |
| 2017-01-01 00:00:00 | 1 | 10 | 1 |
| 2017-01-13 00:00:00 | 1 | 1 | 2 |
| 2017-01-11 00:00:00 | 1 | 2 | 2 |
| 2017-01-09 00:00:00 | 1 | 3 | 2 |
| 2017-01-07 00:00:00 | 1 | 4 | 2 |
| 2017-01-05 00:00:00 | 1 | 5 | 2 |
| 2017-01-03 00:00:00 | 1 | 6 | 2 |
| 2017-01-01 00:00:00 | 1 | 7 | 2 |
| 2017-01-13 00:00:00 | 1 | 1 | 3 |
| 2017-01-01 00:00:00 | 1 | 2 | 3 |
| 2017-01-03 00:00:00 | 1 | 1 | 4 |
| 2017-01-01 00:00:00 | 1 | 2 | 4 |
| 2017-01-02 00:00:00 | 1 | 1 | 5 |
+---------------------+--------+------+---------+
We now simply wrap this in another statement to select the avg where the row number is less than or equal to seven.
select e1.row day, avg(e1.rating) avg
from (
select e.created_at,
e.rating,
if(#lastUser=user_id,#row := #row+1, #row:=1) as row,
#lastUser:= e.user_id as user_id
from entries e,
( select #row := 0, #lastUser := 0 ) vars
order by e.user_id asc,
e.created_at desc) e1
where e1.row <=7
group by e1.row;
This outputs:
+------+--------+
| day | avg |
+------+--------+
| 1 | 1.0000 |
| 2 | 1.0000 |
| 3 | 1.0000 |
| 4 | 1.0000 |
| 5 | 1.0000 |
| 6 | 1.0000 |
| 7 | 1.0000 |
+------+--------+
I'm trying to create a MySQL query to select the daily price from a table that is between a date range from another. I only want to use 'starting-ending' months and days from the table "seasons" and I want to pass the year dynamically to the query.
This is my query: (I'm giving it the Year to exclude the one on the table)
SELECT a.season, b.base_price
FROM seasons a
JOIN pricebyseason b ON a.id=b.season_id
WHERE b.prop_id='6' AND '2015-11-29' BETWEEN DATE_FORMAT(a.starting,'2015-%m-%d') AND DATE_FORMAT(a.ending,'2016-%m-%d')
ORDER BY b.base_price DESC
It works but not with all dates.
These are the tables:
seasons (these are static date values)
+----+--------------+------------+------------+
| id | season | starting | ending |
+----+--------------+------------+------------+
| 1 | Peak Season | 2015-12-11 | 2016-01-09 |
| 2 | High Season | 2015-11-27 | 2016-04-15 |
| 3 | Mid Season | 2015-04-16 | 2015-09-01 |
| 4 | Low Season | 2015-09-02 | 2015-11-26 |
| 5 | Spring Break | 2015-03-05 | 2015-03-21 |
+----+--------------+------------+------------+
pricebyseason
+----+---------+-----------+------------+
| id | prop_id | season_id | base_price |
+----+---------+-----------+------------+
| 1 | 6 | 1 | 950 |
| 2 | 6 | 2 | 750 |
| 3 | 6 | 3 | 450 |
| 4 | 6 | 4 | 400 |
| 5 | 6 | 5 | 760 |
+----+---------+-----------+------------+
What I want to achive is query the dialy price between checkin, checkout selection
I create this sqlfiddle: http://sqlfiddle.com/#!9/4a6f4
This is a previuos query that is not working either:
SELECT a.base_price,b.season,b.starting,b.ending
FROM pricebyseason a JOIN seasons b ON a.season_id=b.id
WHERE a.prop_id='6' AND
(DATE_FORMAT(b.starting,'%m-%d') <= '12-27' OR DATE_FORMAT(b.starting,'2016-%m-%d') >= '2015-12-27')
AND
(DATE_FORMAT(b.ending,'%m-%d') >= '12-27' OR DATE_FORMAT(b.ending,'2016-%m-%d') <= '2015-12-27')
ORDER BY base_price DESC
And here are some sample dates for each season: '2016-01-08','2015-12-27','2016-04-14','2015-11-29','2016-04-15','2015-09-01','2016-09-02','2015-11-26','2016-10-10','2016-03-18','2016-06-22','2015-06-15'
Thank a lot
I have a table customer_order as follows
mysql> select * from customer_order;
+---------+---------+-----------+------------------+----------------+
| cust_id | orderno | region_cd | order_start_date |order_del_date |
+---------+---------+-----------+------------------+----------------+
| CU_082 | ONO_001 | reg1 | 2012-04-25 | 2012-08-25 |
| CU_082 | ONO_002 | reg1 | 2012-04-28 | 2012-11-28 |
| CU_083 | ONO_002 | reg2 | 2012-04-28 | 2012-11-28 |
| CU_082 | ONO_003 | reg1 | 2012-04-25 | 2012-08-25 |
| CU_084 | ONO_004 | reg4 | 2012-04-25 | 2012-10-25 |
I need a table like this...which i get....as follows
mysql> select order_start_date,order_del_date,orderno,cust_id from customer_order wh
ere order_start_date >= '2012-04-25' AND order_del_date <='2012-12-28' and cust_i
d IN ('36082','36088') order by cust_id ;
+------------------+----------------+---------+---------+
| order_start_date | order_del_date | pid | emp_id |
+------------------+----------------+---------+---------+
| 2012-04-25 | 2012-05-25 | ONO_001 | CU_082 |
| 2012-08-22 | 2012-12-28 | ONO_004 | CU_082 |
| 2012-06-22 | 2012-08-28 | ONO_003 | CU_082 |
| 2012-05-27 | 2012-06-25 | ONO_002 | CU_082 |
| 2012-04-25 | 2012-05-25 | ONO_001 | CU_082 |
| 2012-05-27 | 2012-06-25 | ONO_001 | CU_082 |
| 2012-04-30 | 2012-06-25 | ONO_001 | CU_088 |
| 2012-06-28 | 2012-07-15 | ONO_002 | CU_088 |
| 2012-07-28 | 2012-08-25 | ONO_003 | CU_088 |
| 2012-07-16 | 2012-09-25 | ONO_004 | CU_088 |
+------------------+----------------+---------+---------+
now i need to query on this table ...
to get
for each customer here we get data for the period from wat date to wat date his order processing details in the above table..
now for each customer i shld find the period for which thr is no order processing...
eg cust_id =CU_088
he has his order processed from 30 apr to 25 june
den from 28th june to 15 july
(here thr is a diff that is thr is no order taken or any processing done from 26th to 27th june..this is wat is the required result)
**one more imp consideration is...
in the next entry we find thr is a order process from 28th july to 25th aug
w.r.t previous entry i.e, 28th june to 15july we find that for this customer thr is no order taken or processed from 16th to 27th july..
but with the last entry tat is 16th july to 25th sept he has an order with different order_no thrfore the gap 16th july to 27th july is filled here so this kind of a condition also needs to be checked...
I need to get the output as something like this..
+------------------+----------------+---------+---------+
| order_start_date | order_del_date | pid | emp_id |
+------------------+----------------+---------+---------+
| 2012-06-26 | 2012-06-27 | ONO_001 | CU_088 |
+------------------+----------------+---------+---------+
that is either the query or procedure which is more efficient should give me the period wer in thr was no action done for customer...
help me write the query which does the all the above things.
I m new to db queries..so please help me out..
To fetch every 'gap' between orders, you can use a self-join:
SELECT o1.cust_id,
o1.order_del_date + INTERVAL 1 DAY AS gap_begin,
MIN(o2.order_start_date) - INTERVAL 1 DAY AS gap_end
FROM customer_order o1
JOIN customer_order o2 ON o1.cust_id = o2.cust_id
AND o1.order_del_date <= o2.order_start_date
GROUP BY o1.cust_id, o1.order_del_date
HAVING gap_begin < gap_end