What query would I do if this code is working on PHPMyAdmin SQL:
SELECT
DATE_FORMAT(d.date, '%b %e %Y') date,
u.employee_id, ai.time_in,
ao.time_out
FROM (SELECT date FROM hris_timein UNION SELECT date FROM hris_timeout order by date asc) d
CROSS JOIN hris_users u
LEFT JOIN hris_timein ai ON u.employee_id = ai.employee_id AND ai.date = d.date
LEFT JOIN hris_timeout ao ON u.employee_id = ao.employee_id AND ao.date = d.date
Output: see output
But when I use this code in my project, it displays an error:
Error description Unknown column 'd.date' in 'on clause'
The query looks correct, try it with quoted identifiers.
SELECT
DATE_FORMAT(d.`date`, '%b %e %Y') `date`,
u.`employee_id`,
ai.`time_in`,
ao.`time_out`
FROM
(
SELECT
`date`
FROM
`hris_timein`
UNION
SELECT
`date`
FROM
`hris_timeout`
) d
CROSS JOIN
`hris_users` u
LEFT JOIN
`hris_timein` ai
ON u.`employee_id` = ai.`employee_id` AND ai.`date` = d.`date`
LEFT JOIN
`hris_timeout` ao
ON u.`employee_id` = ao.`employee_id` AND ao.`date` = d.`date`;
Related
I have following query
SELECT YEAR(T.date), MONTH(T.date), T.production, T.lineID, SUM(rework + scrap)
FROM
(SELECT MAX(positionID), date, production, lineID
FROM productionPerPosition
WHERE lineID = 2
AND date BETWEEN '2017-01-01' AND '2017-01-31'
GROUP BY date) AS T
INNER JOIN linePosition lp ON lp.lineID = T.lineID
INNER JOIN fttErrorType fet ON fet.positionID = lp.positionID
INNER JOIN fttData fd ON fd.errorID = fet.errorID
AND fd.date = T.date
GROUP BY YEAR(T.date), MONTH(T.date)
which gives this result
Now, I would like to group these results by year and month to get sum of production and sum of last column. I've tried this query
SELECT YEAR(T.date), MONTH(T.date), SUM(T.production), T.lineID, SUM(rework + scrap)
FROM
(SELECT MAX(positionID), date, production, lineID
FROM productionPerPosition
WHERE lineID = 2
AND date BETWEEN '2017-01-01' AND '2017-01-31'
GROUP BY date) AS T
INNER JOIN linePosition lp ON lp.lineID = T.lineID
INNER JOIN fttErrorType fet ON fet.positionID = lp.positionID
INNER JOIN fttData fd ON fd.errorID = fet.errorID
AND fd.date = T.date
GROUP BY YEAR(T.date), MONTH(T.date)
Which gives me
Here production sum is wrong! It seems that GROUP BY from 7th line in first query is ignored.
Any idea how could I get needed result?
Edit: In inner SELECT I have separate production for several different positions (positionID) but I'm using only production from position that has highest positionID
Group has missing grouping columns that why it is resulting in some unexpected result
SELECT YEAR(T.date), MONTH(T.date), SUM(T.production), T.lineID, SUM(rework + scrap)
FROM
(SELECT MAX(positionID), date, production, lineID
FROM productionPerPosition
WHERE lineID = 2
AND date BETWEEN '2017-01-01' AND '2017-01-31'
GROUP BY date, production, lineID) AS T
INNER JOIN linePosition lp ON lp.lineID = T.lineID
INNER JOIN fttErrorType fet ON fet.positionID = lp.positionID
INNER JOIN fttData fd ON fd.errorID = fet.errorID
AND fd.date = T.date
GROUP BY YEAR(T.date), MONTH(T.date), T.lineID
Has explained in e4c5 comment, you have to add all the unaggregated fields to your GROUP BY. I made it in the inner SELECT and in the main SELECT:
SELECT YEAR(T.date), MONTH(T.date), SUM(T.production), T.lineID, SUM(rework + scrap)
FROM
(SELECT MAX(positionID), date, production, lineID
FROM productionPerPosition
WHERE lineID = 2
AND date BETWEEN '2017-01-01' AND '2017-01-31'
GROUP BY date, production, lineID) AS T
INNER JOIN linePosition lp ON lp.lineID = T.lineID
INNER JOIN fttErrorType fet ON fet.positionID = lp.positionID
INNER JOIN fttData fd ON fd.errorID = fet.errorID
AND fd.date = T.date
GROUP BY YEAR(T.date), MONTH(T.date), T.lineID
Got problem, I am trying to calculate of each employee Total_Hours but when am grouping it comes up with an error. However, I know what the problem is but i am not sure how to fix it.
Select
tt.uid as 'Employee',
if (tc.organisation like '%Village%' or tc.organisation like '%Personnel%', 'Village', 'Client' ) as 'Type of work',
round((sum(timestampdiff(minute, start_time, end_time))/60),2) AS 'Hours',
(select
round((sum(timestampdiff(minute, start_time, end_time))/60),2)
from timesheet.timesheet_times ttt
left outer join timesheet.timesheet_project ttp on ttt.proj_id = ttp.proj_id
left outer join timesheet.timesheet_client ttc on ttp.client_id = ttc.client_id
where
month(ttt.start_time) = month(now())
and year(ttt.start_time) = year(now())
group by
ttt.uid
)as TOTAL_HOURS_PER_EMPLOYEE,
round((
(round((sum(timestampdiff(minute, start_time, end_time))/60),2)
/
(select
round((sum(timestampdiff(minute, start_time, end_time))/60),2)
from timesheet.timesheet_times ttt
left outer join timesheet.timesheet_project ttp on ttt.proj_id = ttp.proj_id
left outer join timesheet.timesheet_client ttc on ttp.client_id = ttc.client_id
where
month(ttt.start_time) = month(now())
and year(ttt.start_time) = year(now()))
)*100),0) as percentage
from
timesheet.timesheet_times tt
left outer join timesheet.timesheet_project tp on tt.proj_id = tp.proj_id
left outer join timesheet.timesheet_client tc on tp.client_id = tc.client_id
where
month(tt.start_time) = month(now())
and tc.organisation not like '%Private%'
and year(tt.start_time) = year(now())
group by
tc.client_id
order by
round((sum(timestampdiff(minute, start_time, end_time))/60),2) desc
I am sorry if I wrote that post wrong, it's my first time on here or asked for help.
I want to get the difference of a date and a datetime and display it as elapse time, example: 4days 7hr 8min 3sec.. Have tried this but only gives me the difference of the date part:
SELECT a.id, b.creation_date ,c.user_name,d.operation,d.system_,e.name,
e.email_address,e.office,(coalesce(s.id, 0)) as status,t.status as status_name,b.attachment,
date_format(a.date ,'%Y-%m-%d') as dateTakenStage,
date_format(a.date,'%h:%i:%s %p') as timeTakenStage,
date_format(s.date ,'%Y-%m-%d') as dateTakenStatus,datediff(s.date,b.creation_date) as dated,
date_format(s.date,'%h:%i:%s %p') as timeTakenStatus,a.stage,s.details
FROM job_order_stage a
INNER JOIN job_order b
ON a.job_order=b.id
INNER JOIN job_order_type d
ON b.job_order_type=d.id
INNER JOIN client e
ON b.client=e.id
INNER JOIN account f
ON b.assigned_to=f.id
INNER JOIN user c
ON f.user=c.user_id
LEFT outer JOIN job_order_status s
ON s.job_order_stage = a.id
LEFT JOIN stage_status ss
ON s.stage_status = ss.id
Left JOIN status t
ON ss.status=t.id
WHERE b.id='201506106'
order by a.date desc, s.date desc;
You can use the TIMESTAMPDIFF() function along with CONCAT() to get the formatting you want:
SELECT CONCAT(TIMESTAMPDIFF(DAYS, s.date, b.creation_date), 'days ',
MOD(TIMESTAMPDIFF(HOUR, s.date, b.creation_date), 24), 'hr ',
MOD(TIMESTAMPDIFF(MINUTE, s.date, b.creation_date), 60), 'min ',
MOD(TIMESTAMPDIFF(SECOND, s.date, b.creation_date), 60), 'sec') AS dated
Output:
4days 7hr 8min 3sec
This query is showing error :
#1054 - Unknown column 'sp.spot_id' in 'on clause'
Query :
SELECT product.*,sp.sp_name FROM `product`
left join spot_selling on product.product_id=spot_selling.product_id
AND spot_selling.end_time >= now()
AND spot_selling.start_time <= now()
AND spot_selling.status='1'
left join(select GROUP_CONCAT(s.name SEPARATOR ',') as sp_name
from spot s group by s.spot_id) sp on sp.spot_id=spot_selling.spot_id
WHERE product.user_id='26' AND product.status!='6'
I think you just need spot_id in the subquery, so there is something to join on:
SELECT product.*, sp.sp_name
FROM `product` left join
spot_selling
on product.product_id=spot_selling.product_id AND
spot_selling.end_time >= now() AND
spot_selling.start_time <= now() AND
spot_selling.status = '1' left join
(select spot_id, GROUP_CONCAT(s.name SEPARATOR ',') as sp_name
--------------^
from spot s
group by s.spot_id
) sp
on sp.spot_id = spot_selling.spot_id
WHERE product.user_id = '26' AND product.status <> '6'
really do not know if you can, but i I need the DATE VENC to be equal to '2013-02-02'.
the values of the column date_pay:
1-2013-01-01
2-2013-02-02
3-0000-00-00
4-0000-00-00
this is my query:
SELECT s.id,
s.name,
s.nro_s,
ts.cat,
SUM( ts.pryce ) AS deuda,
SUM( ts.pryce ) DIV ts.pryce AS c_p,
date_venc = (select max(date_pay) from c ) // the date in question
FROM s
INNER JOIN c
INNER JOIN ts
WHERE s.id = '123'
AND c.id = '123'
AND c.date_pay = '0000-00-00'
AND s.ts = ts.id_ts
Sorry for my english, is very basic.
Greetings.
Assuming date_venc is DATE a possible solution
select *
from s
where s.date_venc=
(select max(cast(SUBSTRING_INDEX(date_pay,'-',-3)as DATE))from c);
also check out sqlfiddle
http://sqlfiddle.com/#!2/64197/1
and your query should probably be modified to,
SELECT s.id,
s.name,
s.nro_s,
ts.cat,
SUM( ts.pryce ) AS deuda,
SUM( ts.pryce ) DIV ts.pryce AS c_p,
date_venc
FROM s
INNER JOIN c
INNER JOIN ts
WHERE s.id = '123'
AND c.id = '123'
AND c.date_pay = '0000-00-00'
AND s.ts = ts.id_ts
AND date_venc = (select max(cast(SUBSTRING_INDEX(date_pay,'-',-3)as DATE)) from c ) // the date in question