Finding Two Consecutive Dates - mysql

Hi I have a table "EMP_LEAVE" with some records as below
Employee | Leave_Start_Date | Leave_End_Date
1 22-09-2014 26-09-2014
1 29-09-2014 03-10-2014
1 15-12-2014 19-12-2014
1 22-12-2014 24-12-2014
2 07-01-2014 10-01-2014
2 13-01-2014 17-01-2014
2 20-01-2014 24-01-2014
3 10-02-2014 13-02-2014
3 17-02-2014 21-02-2014
I want to write a SQL query to find:
Employee who has taken leaves more than or equal to "Two Consecutive" weeks or I should say Two weeks in a row?
If Yes then whats there Leave_Start_Date and Leave_End_Date for those total leaves?
Any help or direction will be appreciated.

You can write some simple query as below.
select employee,
Leave_start_date,
Leave_end_date
from emp_leave
where datediff(Leave_start_date, Leave_end_date) >= 15
This query will give you the employees which have taken leave for more than 15 days.
Thanks,
Sarat

Related

How to sum specific rows and columns in SQL?

pnr mnd pris
1 1 600
1 7 900
2 1 600
2 7 600
3 1 40
3 7 40
I have trouble how to sum specific rows on the columns. Looking at the above, the table is called travel and it has 3 columns:
pnr - Personal Number
mnd - Month
Pris - Price
So what I want is to sum total of the price for the a specific month, so in this case, it should be 1240 USD and month 1. For the month 7, it should be 1540 USD.
I have trouble to do the query correct. So far from I have tried is this:
SELECT t.rnr, t.mnd, SUM(t.pris)
FROM travel AS t
WHERE t.mnd = 1
The result I get is 3720 USD which I have no idea how the SQL managed to calculate this for me.
Appreciate if someone could please help me out!
For this you need to drop the pnr column from the output (it is not relevant and will cause your data to split) and add a GROUP BY:
SELECT t.mnd, SUM(t.pris)
FROM travel AS t
WHERE t.mnd = 1
GROUP BY t.mnd
Live demo: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=b34ec2bb9c077c2d74ffc66748c5c142
(The use of an aggregate function without grouping, as you've got now, is not a standard SQL feature and can often be turned off in MySQL. If turned on, you might not always get the result you expected/intended.)
just group your result with mnd column
SELECT t.mnd, SUM(t.pris)
FROM travel AS t
group by t.mnd

Sort values in two column and insert order position into another column in mysql

I have a database about sports event that contains:
*User ID
*Amount of Points that the user got on that event
*Time (HH:MM:SS) that took the user to complete track.
How can I first sort them by no. of points, then if two users have same amount of points, by time (shorter is better); and then insert the places to rows?
I have database like that:
ID No. of Points Time Place
------------------------------------
1 15 00:56:00
2 13 00:55:15
3 17 01:00:00
4 17 00:57:00
5 19 00:52:15
I need to have it with places:
ID No. of Points Time Place
------------------------------------
1 15 00:56:00 4
2 13 00:55:15 5
3 17 01:00:00 3
4 17 00:57:00 2
5 19 00:52:15 1
I hope, you understand that. Sorry for bad English.
Best regards,
You can do this with update statement as follows.
SET #placeValue:=0;
UPDATE [Table Name] SET Place=#placeValue:=#placeValue+1 ORDER BY
[Amount of Points] DESC,Time ASC

get value from mysql database using month no and week no

Is it possible to retrieve the records from the table using month no and week no
for example I have a table
CustomerID CustomerName date(data type date)
1 sam 2016-06-1
2 dam 2016-06-2
3 kam 2016-06-8
4 ram 2016-06-9
4 ram 2016-07-8
how can i retrieve the month no 6 and week no 1 records
after the select query expected result is
CustomerID CustomerName date
1 sam 2016-06-1
2 dam 2016-06-2
it will give 2 records because date 1 and 2 fall under first week
if question is not clear please reply
thanks !
You can use month and week functions. Documentation here.
select * from table where month(`date`) = 6 and week(`date`) = 1

SQL count based on timediff and column containing a string

I'm using SQL Workbench.
cust_num date notes
1234 2016-02-01 advice
1234 2016-02-01 something else
1234 2016-02-02 order
1234 2016-02-03 order
4421 2016-02-15 advice
4421 2016-02-17 order
4421 2016-02-18 something else
4421 2016-02-24 order
I know the above is a bit unclear, but basically, there's 3 columns in the above table. One showing customer_num (customer number), one showing date and one showing a notes field.
From the above, I want to perform two queries. I am newish to this so, I hope this is clear. I'm using SQL workbench.
i) I want to count the number of DISTINCT 'customer_num's that placed an order within 4 days of receiving advice.
So the answer based on the table above would be 3. This is because cust_num '1234' made two orders within 4 days and cust_num '4421' made 1 order. So that totals 3
ii)I want to count the number of DISTINCT customer_num's that placed an order within 15 days of receiving advice. Only stipulation is that I don't want to re-count those from (i) that placed an order within 4 days. I want to exclude them.
So the answer to this would be 1. Customer_num '4421' placed 1 order that was bigger than 4 days but smaller than or including 15 days.
Any help really appreciated. Thank you.
One method is to use exists:
select count(distinct cust_num)
from customers t
where exists (select 1
from customers t2
where t2.cust_num = t.cust_num and
t2.date between t.date and date_add(t.date, interval 3 day)
);
The two queries have the same structure. You just need to change the condition in the where clause in the subquery.

MySQL query records for continuous date range

here's my example table (room reservation system):
id available room_id
----------------------------
1 2014-02-05 4
2 2014-02-06 4
3 2014-02-07 4
4 2014-02-09 4
5 2014-02-10 4
i want to query if room with id 4 is available between 2014-02-05 and 2014-02-10.
i know i can query by using the BETWEEN operator, but the problem is that i need to consider continuous date ranges, so it should return zero records as the record for 2014-02-08 is missing.
any ideas?
thanks
Here is an idea. Count the number of rows that match and then compare these to the number of days in the period:
select room_id
from example
where available between date('2014-02-05') and date('2014-02-10')
group by room_id
having count(*) = datediff(date('2014-02-05'), date('2014-02-10')) + 1;