How can I optimise this sql query? - mysql

Here are 4 tables....
tbl_std_working_hour
tbl_attendance
tbl_holiday
tbl_leave
I want to find out the employees absentee reports by this query....but it takes times when I have applied this for many employees...is there any way to simplify this query?
SELECT date
FROM tbl_std_working_hour
WHERE date NOT IN (SELECT date FROM tbl_attendance WHERE emp_id = '$emp_id')
AND date NOT IN (SELECT date FROM tbl_holiday)
AND date NOT IN (SELECT date FROM tbl_leave WHERE emp_id = '$emp_id')
AND total_hour <> '00:00:00'
AND date >= '$start'
AND date <= '$end'
AND emp_id = '$emp_id'

First, I would rewrite using NOT EXISTS:
SELECT wh.date
FROM tbl_std_working_hour wh
WHERE NOT EXISTS (SELECT 1
FROM tbl_attendance a
WHERE a.date = wh.date AND a.emp_id = wh.emp_id
) AND
NOT EXISTS (SELECT 1
FROM tbl_holiday h
WHERE h.date = wh.date
) AND
NOT EXISTS (SELECT 1
FROM tbl_leave l
WHERE l.emp_id = wh.emp_id and l.date = wh.date
)
WHERE wh.total_hour <> '00:00:00' AND
wh.date >= '$start' AND
wh.date <= '$end' AND
wh.emp_id = '$emp_id';
Then add the following composite (multi-column) indexes:
tbl_std_working_hour(emp_id, date, total_hour)
tbl_attendance(emp_id, date)
tbl_holiday(date) (might already exist if date is the primary key or unique)
tbl_leave(emp_id) (might already exist if emp_id is the primary key or unique)
Note that I changed the subqueries to refer to the emp_id in the outer query. This makes it easier to change the emp_id. In addition, your query should be using parameters for the values in the WHERE clause.

This is also a better way that can work better using UNION ALL
SELECT date
FROM tbl_std_working_hour AS tswh
WHERE NOT EXISTS(
SELECT date FROM tbl_attendance ta WHERE ta.date = tswh.date AND ta.emp_id = tswh.emp_id
UNION ALL
SELECT date FROM tbl_holiday th WHERE th.date = tswh.date
UNION ALL
SELECT date FROM tbl_leave tl WHERE tl.date = tswh.date AND tl.emp_id = tswh.emp_id)
AND total_hour <> '00:00:00'
AND date >= '$start'
AND date <= '$end'
AND emp_id = '$emp_id'

Related

SQL query from time_in column but not from time_out column

I'm using mysql. I have a table time_record. There are four columns namely id, time_in, time_out and students_id. I only want to query the record of the latest time_in of the student but if the latest record of the student is in time_out, it will not query. How do I query the student's latest time_in record if given that the student still does not times out?
Thanks
Here is my sample code (though it returns both records from timein and timeout)
select concat (
st.student_fname,
' ',
st.student_lname
) as 'Name',
t.students_id,
t.time_in,
t.time_out,
case
when t.time_in > t.time_out
then t.time_in
else t.time_out
end as MostRecentDate
from classes c
join student_classes s on c.id = s.classes_id
join timerecords t on t.students_id = s.students_id
join students st on s.students_id = st.student_id
where c.employees_id = 'sessionvalue2'
and
where date (t.time_in) between date (now()) and date (now())
From what I understand, you want to query all results in which time_in is the latest entry and exclude results where time_out is the latest entry.
Try this:
SELECT DISTINCT(tr.id), tr.time_in
FROM time_record tr
WHERE tr.time_in > tr.time_out
ORDER BY tr.time_in DESC
I think your time_out columns may have nulls due to which the comparison results in false in that case and it return time_out.
Also,
date (t.time_in) between date (now()) and date (now())
doesn't make any sense. If you want to check the time_in for today's date, do:
date(t.time_in) = curdate();
Use curdate() instead of date(now()).
or Sargable so that index can be used if any:
date >= curdate()
and date < date_add(curdate(), interval 1 day)
Try flipping the condition like this:
select concat (
st.student_fname,
' ',
st.student_lname
) as 'Name',
t.students_id,
t.time_in,
t.time_out,
case
when t.time_in < t.time_out
then t.time_out
else t.time_in
end as MostRecentDate
from classes c
join student_classes s on c.id = s.classes_id
join timerecords t on t.students_id = s.students_id
join students st on s.students_id = st.student_id
where c.employees_id = 'sessionvalue2'
and date >= curdate()
and date < date_add(curdate(), interval 1 day)

SQL query for each ID in table

I have a query for 1 particular customer_id. How can I execute this query for each customer_id in table?
SELECT *
FROM table
WHERE date <= '2015-12-31 23:59:59' AND customer_id = 100
ORDER BY date DESC
LIMIT 1
You can use NOT EXISTS():
SELECT * FROM YourTable t
WHERE t.date <= '2015-12-31 23:59:59'
AND NOT EXISTS(SELECT 1 FROM YourTable s
WHERE t.customer_id = s.customer_id
AND t.date < s.date)
This will select only a record after the date filter where NOT EXISTS another record for the same id with a bigger date. Its basically the same as limit 1 for all.
You can use Inner join:
SELECT * FROM YourTable t INNER JOIN
(SELECT * FROM Table) s
ON t.customer_id = s.customer_id
WHERE t.date = '2015-12-31 23:59:59'
ORDER BY date DESC

MySQL Update with same table subquery

I have a table (EMP_ID, START_DATE, END_DATE) that contains a series of date ranges. What I want to do is ensure that they are all contiguous, so the END_DATE should be one less than the next START_DATE for any given EMP_ID.
I have the following query that lets me identify the records that are not contiguous:
SELECT H.EMP_ID,
H.START_DATE,
H.END_DATE,
DATE(
( SELECT MIN(START_DATE)
FROM TSRHierarchy I
WHERE I.START_DATE > H.START_DATE
AND I.EMP_ID = H.EMP_ID
)
) AS NEXT_DATE
FROM TSRHierarchy H
HAVING END_DATE <> DATE_ADD(NEXT_DATE, INTERVAL -1 DAY)
ORDER BY H.EMP_ID, H.START_DATE;
What I can't do is figure out how to turn this into an UPDATE statement? The MySQL documentation states 'Currently, you cannot update a table and select from the same table in a subquery.' which may be part of my problem.
Any suggestions for a work-around?
Try this UPDATE query using JOIN
UPDATE TSRHierarchy t
JOIN
( SELECT H.EMP_ID,
H.START_DATE,
H.END_DATE,
DATE((SELECT MIN(START_DATE)
FROM TSRHierarchy I
WHERE I.START_DATE > H.START_DATE
AND I.EMP_ID = H.EMP_ID
)) AS NEXT_DATE
FROM TSRHierarchy H
HAVING END_DATE <> DATE_ADD(NEXT_DATE, INTERVAL -1 DAY)
) AS t2
ON t.EMP_ID = t2.EMP_ID
SET t.END_DATE = t2.NEXT_DATE

initialise all values in a query to 0 and top to NULL if there's no result

Hi all i execute this query to get a table where there's statistics of some database information.. i'd like to intialise the fields that don't exist ( because the query is executed in different dates and sometimes there's a day where there's nothing ) so i'd like it to return 0 and NULL ( in TOP column )
SELECT
SUM(IF(`TOP` = 'one',`Nb`,0)) as first_one,
SUM(IF(`TOP` = 'two',`Nb`,0)) as second_one,
SUM(IF(`TOP` = 'three',`Nb`,0)) as thrid_one,
SUM(IF(`TOP` NOT IN ('three','two','one'),`Nb`,0)) as forth_one,
GROUP_CONCAT(IF(`TOP` NOT IN ('three','two','one'),`TOP`,'') SEPARATOR '') as `OR`
FROM (
SELECT
COUNT(*) as Nb,
'one' as `TOP`
FROM
mytable
WHERE
TYPE = 'MSS'
AND YEAR(date) = YEAR(CURDATE())
AND MONTH(date) = MONTH(CURDATE())
UNION ALL
SELECT
COUNT(*) as Nb,
'two' as `TOP`
FROM
mytable
WHERE
TYPE = 'MSS'
AND S=0
AND YEAR(date) = YEAR(CURDATE())
AND MONTH(date) = MONTH(CURDATE())
UNION ALL
SELECT
COUNT(*) as Nb,
'three' as `TOP`
FROM
mytable
WHERE
TYPE = 'MSS'
AND S<>0
AND YEAR(date) = YEAR(CURDATE())
AND MONTH(date) = MONTH(CURDATE())
UNION ALL
SELECT
`Nb`,
`TOP`
FROM(
SELECT
COUNT(*) as Nb ,
`OR` as `TOP`
FROM
mytable
WHERE
TYPE = 'MSS'
AND YEAR(date) = YEAR(CURDATE())
AND MONTH(date) = MONTH(CURDATE())
GROUP BY
`OR`
ORDER BY
Nb DESC
LIMIT 1
) as tmp
)as tmp1
Assuming that in tmp1 you have data you need but with "gaps" (days when there were no data at all) you could RIGHT JOIN tmp1 to table tmp2 using day (I assume that you have such column in tmp1 table). So tmp2 would be just list of days:
SELECT '2013-05-17' as day UNION SELECT '2013-05-18' UNION SELECT ...
I could elaborate my answer if you'd like to provide your DB schema.
You can replace each subquery with:
SELECT
IFNULL(tmp.Nb,0) as Nb,
IFNULL(tmp.`TOP`, 'value') as `TOP`
FROM (
--subquery
) as tmp
Example for the first subquery:
SELECT
IFNULL(tmp.Nb,0) as Nb,
IFNULL(tmp.`TOP`, 'one') as `TOP`
FROM (
SELECT
COUNT(*) as Nb,
'one' as `TOP`
FROM
mytable
WHERE
TYPE = 'MSS'
AND YEAR(date) = YEAR(CURDATE())
AND MONTH(date) = MONTH(CURDATE())
) as tmp
SQL is good at grouping existing entities into categories, but bad at "creating" entities itself. I would advise either a generic number table (really just the numbers from 0 to a few hundredthousand) if you have also non-date categories or as Wiktor suggested a date-Table which gets filled every now and then and has the next few years as well as the time since your program is working.
With a date table
list_dates (
id int(11) not null primary key auto_increment,
dateval date not null
)
you could start your queries from that table (with a reasonable range, of course) and count every thing else:
select list_dates.dateval as date, count(*) as cnt
from list_dates
left join actions on actions.actiontime >= (cast list_dates.date_val as datetime)
and actions.actiontime < (cast list_dates.date_val `interval 1 day as datetime)
where list_dates.dateval between '$fromDate' and '$toDate'
group by list_dates.dateval
;
or starting with a number table numbers
select $fromDate + interval numbers.number day as date, count(*) as cnt
from numbers
left join actions
on actions.actiontime >= (cast $fromDate + interval numbers.number day as datetime)
and actions.actiontime < (cast $fromDate + interval (1 + numbers.number) day as datetime)
where numbers.number >= 0 and numbers.number < $countDates
group by numbers.number
;
One Day
If you really want just that one day (today) then you can of course use a anonymous subselect- Table instead, so it becomes
select list_dates.dateval as date, count(*) as cnt
from ( select curdate() as dateval ) as list_dates
left join actions on actions.actiontime >= (cast list_dates.date_val as datetime)
and actions.actiontime < (cast list_dates.date_val `interval 1 day as datetime)
where list_dates.dateval between '$fromDate' and '$toDate'
group by list_dates.dateval
;

MySQL Select, join, comparing dates fails

I have these two tables:
DateRanges
some_id start_date end_date
---------------------------------
1 2012-12-01 2012-12-15
1 2013-01-01 2013-01-15
3 2013-01-03 2013-01-10
Items
id name
----------------
1 Some name
2 Other name
3 So on...
What I try to achieve is to get, for each element in Items table, the biggest start_date (ignoring the smaller dates/date ranges for that Item) and check if the current date is in that range, like in the next table (let's say today's 02 January 2013):
id name TodayIsInTheRange
---------------------------------------------
1 Some name true
2 Other name false
3 So on... false
I have tried to obtain the 3rd table with the next query:
SELECT A.*, (B.`start_date` <= CURRENT_DATE AND CURRENT_DATE <= B.`end_date`) AS `TodayIsInTheRange`
FROM `Items` as A
LEFT JOIN `DateRanges` as B ON
A.id = B.some_id
SORT BY B.`end_date` DESC
But with this query my items repeat themselves because I have two records in DateRanges for the same item.
I use SQL Server, but I think something like this should be pretty close:
SELECT
I.Id,
I.Name,
(DR.start_date <= CURRENT_DATE AND CURRENT_DATE <= DR.end_date) AS `TodayIsInTheRange`
FROM `Items` AS I
LEFT JOIN
(SELECT Some_Id, MAX(Start_Date) as MaxStartDate
FROM `DateRanges`
GROUP BY Some_ID) AS HDR ON I.Id = HDR.Some_Id
LEFT JOIN `DateRanges` AS DR ON HDR.Some_Id = DR.Some_Id AND HDR.MaxStartDate = DR.Start_Date
select * from items join date_ranges dr0 on items.id = dr0.some_id
where start_date =
(select max(start_date) from date_ranges dr1 where dr0.some_id = dr1.some_id);
SELECT
a.*,
( b.start_date <= CURRENT_DATE
AND CURRENT_DATE <= b.end_date ) AS TodayIsInTheRange
FROM
Items AS a
LEFT JOIN
( SELECT some_id, MAX(start_date) AS start_date
FROM DateRanges
GROUP BY some_id
) AS m
JOIN
DateRanges AS b
ON b.some_id = m.some_id
ON a.id = m.some_id
ORDER BY b.end_date DESC ;
try to use GROUP BY and MAX function. The first provide you only one row for each item.id, the second tell you if there is at least one date in your range
SELECT A.*, MAX(B.`start_date` <= CURRENT_DATE AND CURRENT_DATE <= B.`end_date`) AS `TodayIsInTheRange`
FROM `Items` as A
LEFT JOIN `DateRanges` as B ON
A.id = B.some_id
GROUP BY A.id
ORDER BY B.`end_date` DESC