I have two tables.
job_table.
job_id job_type job_type *****
1 1 Day *****
2 2 Night
3 3 Day & Night.
4 3 Day & Night.
and task_entry table.
task_entry_id job_type task_option_type
1 1 Day
2 1 Day
3 1 Day
4 2 Night
5 3 Day
6 3 Night
7 3 Day
8 3 Night
If job id is 3 then there will be 2 entries one for Day and one for Night.
Else only one entry.
I want to get the total count of jobs with task_entries like this,
job_id task_entry_count
1 3
2 1
3 2
That is if job_type is 3 then count should be count/2. (Day&Night).
Else count should be normal count.
You can JOIN the two tables together and then use GROUP BY to get the number of total count of jobs.
SELECT jt.job_id,
ROUND(SUM (CASE WHEN jt.job_type = 3 THEN 0.5 ELSE 1 END), 0) AS task_entry_count
FROM job_table jt INNER JOIN task_entry te
ON jt.job_type = te.job_type
GROUP BY jt.job_id
Related
I am working on small school database and I need some help with selecting students that are only booked on different days in week 1 and week 2.
My table structure is like below:
ChildId Day Room Week
======= === ==== ====
1 1 20 1
1 2 20 1
2 1 20 2
3 1 20 1
3 2 20 1
3 1 20 2
===== === === ===
I would like to get all the children that meet the following criteria:
ONLY booked in week two (This is ChildId 2 and I can get this from the following query)
SELECT DISTINCT b.childid FROM booking b
where b.childid NOT IN (SELECT childid FROM bookingtemplate WHERE weekno = 1)
I also need
all children in week two whose days are different then week one. That will be ChildID 3 as he is booked on Monday and Tuesday on Week 1 and on Monday on week 2.
I am unable to get the children whose days are different in both the weeks:
Thank you
You can do it with aggregation and the conditions in the HAVING clause:
SELECT ChildId
FROM booking
WHERE Week IN (1, 2)
GROUP BY ChildId
HAVING MIN(Week) = 2
OR GROUP_CONCAT(DISTINCT CASE WHEN Week = 1 THEN Day END ORDER BY Day) <>
GROUP_CONCAT(DISTINCT CASE WHEN Week = 2 THEN Day END ORDER BY Day);
See the demo.
You can self-join the table and filter the cases where days are different.
SELECT DISTINCT
b1.ChildId
FROM
booking b1
INNER JOIN
booking b2
ON
b1.ChildId = v2.ChildId
AND b1.Week = 1 AND b2.Week = 2
AND b1.Day <> b2.Day
Here i want to take the count like same values in table,let say example in my table Accomodation is two time are there so,Accomodation count should come 2 and remaining count is 1
getting_fecilities
fid eventId fecilityName
1 5 Accomodation
2 5 Breakfast
3 5 Lunch
4 5 Dinner
5 6 Food(VEG)
6 5 Parking
7 5 Accomodation
MYSQL
SELECT `fecilityName`,COUNT(*) AS count FROM getting_fecilities WHERE eventId=5 group by `fecilityName`
MY Result
fecilityName count
Accomdation 1
Accomdation 1
Breakfast 1
Dinner 1
Lunch 1
Parking 1
My Expected results
fecilityName count
Accomdation 2
Breakfast 1
Dinner 1
Lunch 1
Parking 1
Your query is giving the Expected Result.
Have a look at it at the below link:
http://sqlfiddle.com/#!9/ba6457/4
SELECT `fecilityName`, COUNT(fecilityName) AS count
FROM getting_fecilities WHERE eventId = 5
group by `fecilityName`;
I'm trying to write a query which will return which garment generated the most revenue during the last three months compared to the same period last year.
I want to display the results in one table. The garment_id from the queries might match but might not. If it matches then I would like to display the results in the same row. I suppose I want to order by garment_id.
I have so far come up with this which almost works but returns incorrect/strange values for TotalDaysHired, MoneyIn, LastYrTotalDaysHired and LastYrMoneyIn; I've no idea why. I've also tried joining the two queries with an inner join ON a.garmentid = b.garmentid order by a.garmentid which wouldn't even run.
SELECT garment_hire_line.date_out as 'dateout',
garment_hire_line.garment_id as 'garmentid',
catalogue.description as 'description',
SUM(garment_hire_line.days) AS 'TotalDaysHired',
SUM(garment_hire_line.days*catalogue.daily_rate) AS 'MoneyIn',
'' as 'LastYrTotalDaysHired',
'' as 'LastYrMoneyIn'
FROM garment_hire_line
INNER JOIN garment ON garment_hire_line.garment_id = garment.garment_id
INNER JOIN catalogue ON garment.catalogue_id = catalogue.catalogue_id
WHERE garment_hire_line.date_out>DATE_SUB(NOW(),INTERVAL 3 MONTH)
GROUP by garment_hire_line.garment_id
UNION
SELECT garment_hire_line.date_out as 'dateout',
garment_hire_line.garment_id as 'garmentid',
catalogue.description as 'description','' as 'TotalDaysHired',
'' as 'MoneyIn',
SUM(garment_hire_line.days) AS 'LastYrTotalDaysHired',
SUM(garment_hire_line.days*catalogue.daily_rate) AS 'LastYrMoneyIn'
FROM garment_hire_line
INNER JOIN garment ON garment_hire_line.garment_id = garment.garment_id
INNER JOIN catalogue ON garment.catalogue_id = catalogue.catalogue_id
WHERE garment_hire_line.date_out<DATE_SUB(NOW(),INTERVAL 1 YEAR)
AND garment_hire_line.date_out>DATE_SUB(NOW(),INTERVAL '1:3' YEAR_MONTH)
GROUP by garment_hire_line.garment_id
garment_hire_line
garment_line_id date_out Days return_date discount hire_id garment_id
8 12/06/2012 4 16/06/2012 0 1 4
9 12/06/2012 5 17/06/2012 0 1 2
10 12/06/2012 4 16/06/2012 0 1 4
11 11/07/2012 3 14/07/2012 10 2 2
12 10/08/2012 3 13/08/2012 0 3 4
13 09/09/2012 2 11/09/2012 5 4 3
14 09/01/2017 3 12/09/2017 0 5 3
Catalogue
catalogue_id| description| designer| Type| daily_rate| supplier_name| supplier_purchase_price| date_purchased| quantity|
1 Hat Elizabeth Kate Accessory 3 Lids 25 28/02/2017 3
2 Dress Calvin Klein Designer Outfit 20 Glam 260 12/05/2012 2
3 Handbag Gucci Accessory 4 Hold On 200 01/01/2017 4
4 Witches Dress null Fancy Dress 12 Fancy Fred 74 21/05/2012 7
Garment
garment_id Colour sizing catalogue_id location_id supplier_id
1 Black Medium 1 2 1
2 Black 10 2 2 2
3 Black 8 2 2 3
4 Black 0 3 2 4
5 Red Child 4 2 1
6 Black Medium 1 2 2
7 Black 10 2 2 3
The main issue is that in your SQL query you use '' for some fields that really are supposed to return numerical data. So you get some records with '' and 3.00 in the same column, which is a conflict in data type. This causes some undesired conversions leading to the garbage output you saw.
Fix this by replacing '' with null.
But you might also want to look into some other issues:
As you group by the garment ID, you have no control over which date is being displayed in the first column when a particular garment was hired multiple times in the same period.
In fact this query would not be valid in newer versions of MySql (unless you set an option to allow it) and violates standard SQL rules. So you should either remove that column from the query, or apply an aggregation function to it (like MAX), or group by it.
By using a union you will not really take advantage of showing the two periods side-by-side: 2 out of the 4 fields in one record will always be null. You can do this without union and filter what you sum up with an expression in the sum aggregate function.
So I would suggest this query:
SELECT MAX(garment_hire_line.date_out) as `LastDateOut`,
garment_hire_line.garment_id as `garmentid`,
catalogue.description as `description`,
SUM(CASE WHEN garment_hire_line.date_out > DATE_SUB(NOW(),INTERVAL 3 MONTH)
THEN garment_hire_line.days
ELSE 0
END) AS `TotalDaysHired`,
SUM(CASE WHEN garment_hire_line.date_out > DATE_SUB(NOW(),INTERVAL 3 MONTH)
THEN garment_hire_line.days*catalogue.daily_rate
ELSE 0
END) AS `MoneyIn`,
SUM(CASE WHEN garment_hire_line.date_out <= DATE_SUB(NOW(),INTERVAL 3 MONTH)
THEN garment_hire_line.days
ELSE 0
END) AS `LastYrTotalDaysHired`,
SUM(CASE WHEN garment_hire_line.date_out <= DATE_SUB(NOW(),INTERVAL 3 MONTH)
THEN garment_hire_line.days*catalogue.daily_rate
ELSE 0
END) AS `LastYrMoneyIn`
FROM garment_hire_line
INNER JOIN garment ON garment_hire_line.garment_id = garment.garment_id
INNER JOIN catalogue ON garment.catalogue_id = catalogue.catalogue_id
WHERE garment_hire_line.date_out > DATE_SUB(NOW(),INTERVAL 3 MONTH)
OR ( garment_hire_line.date_out > DATE_SUB(NOW(),INTERVAL 15 MONTH)
AND garment_hire_line.date_out < DATE_SUB(NOW(),INTERVAL 12 MONTH)
)
GROUP BY garment_hire_line.garment_id
ORDER BY 1
If you prefer to have a separate line for each date, then remove the MAX function, but add garment_hire_line.date_out in the GROUP BY list.
I'm trying to make a summary making a indicator if any data in the SELECT past day from now... or just show a day (if > 0 : + , if < 0 : - ).
Like this: these are my tables.
We supose, today is 2013-12-25
tb_employee:
ID_EMP EMPLOYEE
1 Employee 1
2 Employee 2
3 Employee 3
tb_requirement:
ID_REQ REQUIREMENT
1 Requirement 1
2 Requirement 2
3 Requirement 3
4 Requirement 4
tb_detail:
ID_DET ID_EMP ID_REQ EXPIRATION
1 1 1 2013-12-29
2 1 2 2013-12-28
3 1 3 2013-12-31
4 2 2 2014-01-05
5 2 3 2013-12-20
6 2 4 2013-12-15
Now, the SELECT QUERY should show like this:
ID_EMP EMPLOYEE REQUIREMENTS_GOT ANY_REQ_EXPIRE
1 Employee 1 3 YES
2 Employee 2 3 NO
I hope i explained well. Maybe it could be with DATEDIFF ?
Thank you for answers... and of course, Merry Christmas !
Since you're trying to determine if any of the requirements expired, you should compare the minimal expiry date to today's date. There's no need to use datediff - a simple > operator packed in a case statement would do:
SELECT id_emp,
employee,
COUNT(*) AS requirements_got,
CASE WHEN CURDATE() > MIN(expiration) THEN 'yes' ELSE 'no' END AS any_req_expire
FROM tb_detail
JOIN tb_employee ON tb_detail.id_emp = tb_employee.id_emp
GROUP BY id_emp, employee
Row |day |Prob
--------------------------
1 5 0.3
2 3 0.1
3 2 0.6
4 4 0.5
5 1 0.4
I want to merge day 1 to day 3 to a category and day 4 to 5 to another category
how to do that? Prob of the category is not just simply adding each day's prob.
Current code:
SELECT day, SUM( new ) / SUM( new+ old ) **AS** Prob
FROM mydata
WHERE year > 2005 and ( day >=1 and day <=5)
GROUP by day;
SEE SQL FIDDLE
select '1' as CATEGORy,SUM(NEW_Task)/SUM(NEW_TASK+OLD_TASK) AS PROB from task
where day
between 1 and 3
UNION
select '2' as CATEGORy,SUM(NEW_Task)/SUM(NEW_TASK+OLD_TASK) AS PROB from task
where day between 4 and 5
Based on what I understand from your question I just tried Query. Is this what you are trying to achieve or anything else