SQL BETWEEN Time doesn't show results? - sql

I want to find BETWEEN time but not show result.im share database please share valuable idea....
SQL code
SELECT *
FROM ci_time_slot
WHERE type like '%B%'
and sloat_name BETWEEN '8:00 AM' and '12:00 PM'
table
id sloat_name type
1 8:00 AM A,B,C,D
2 8:15 AM A
3 8:30 AM A,B
4 8:45 AM A,C
5 9:00 AM A,B,D
6 9:15 AM A
7 9:30 AM A,B,C
8 9:45 AM A
9 10:00 AM A,B,D
10 10:15 AM A,C
11 10:30 AM A,B
12 10:45 AM A
13 11:00 AM A,B,C,D
14 11:15 AM A
15 11:30 AM A,B
16 11:45 AM A,C
17 12:00 PM A,B,D
need result
show this type of result depend on time
id sloat_name type
1 8:00 AM A,B,C,D
2 8:15 AM A
3 8:30 AM A,B
4 8:45 AM A,C
5 9:00 AM A,B,D
6 9:15 AM A
7 9:30 AM A,B,C
8 9:45 AM A
9 10:00 AM A,B,D
10 10:15 AM A,C
11 10:30 AM A,B
12 10:45 AM A
13 11:00 AM A,B,C,D
14 11:15 AM A
15 11:30 AM A,B
16 11:45 AM A,C
17 12:00 PM A,B,D

You haven't mentioned yet which dbms are you using ,I'm using MySQL for demonstration. You have to cast sloat_name to time to use between.
Try :
SELECT c.*
FROM ci_time_slot c
WHERE type like '%B%'
and cast(sloat_name as time) between '08:00:00' and '12:00:00';
Result:
id sloat_name type
1 8:00 AM A,B,C,D
3 8:30 AM A,B
5 9:00 AM A,B,D
7 9:30 AM A,B,C
9 10:00 AM A,B,D
11 10:30 AM A,B
13 11:00 AM A,B,C,D
15 11:30 AM A,B
17 12:00 PM A,B,D
Demo

Related

Generate rows with time intervals between 2 dates in Oracle

I have table in which Sunday to Saturdy "Doctor Start" and "End Time" is given.
I want to create time slots of 15 minutes.
On the basis of that, the patient clicks on calendar datetime interval which shows slots that have already been booked.
The following example shows how to split time into slices of 15 minutes. It uses hierarchical query. A little bit of explanation:
line 2: trunc function, applied to a date value, returns "beginning" of that day (at midnight). Adding 15 / (24*60) adds 15 minutes (as there are 24 hours in a day and 60 minutes in an hour). Multiplying 15 by level works as a "loop", i.e. adds 15-by-15-by-15 ... minutes to previous value.
line 4: similar to line 2, but it makes sure that a day (24 hours * 60 minutes) is divided to 15-minutes parts
line 6: start time is trivial
line 7: end time just adds 15 minutes to start_time
line 9: return only time between 10 and 16 hours (you don't have patients at 02:15 AM, right?)
SQL> with fifteen as
2 (select trunc(sysdate) + (level * 15)/(24*60) c_time
3 from dual
4 connect by level <= (24*60) / 15
5 )
6 select to_char(c_time, 'hh24:mi') start_time,
7 to_char(c_time + 15 / (24 * 60), 'hh24:mi') end_time
8 from fifteen
9 where extract(hour from cast (c_time as timestamp)) between 10 and 15;
START_TIME END_TIME
---------- ----------
10:00 10:15
10:15 10:30
10:30 10:45
10:45 11:00
11:00 11:15
11:15 11:30
11:30 11:45
11:45 12:00
12:00 12:15
12:15 12:30
12:30 12:45
12:45 13:00
13:00 13:15
13:15 13:30
13:30 13:45
13:45 14:00
14:00 14:15
14:15 14:30
14:30 14:45
14:45 15:00
15:00 15:15
15:15 15:30
15:30 15:45
15:45 16:00
24 rows selected.
SQL>

I need help in sql query to get this o/p

I have a table structure as follows..
and here is sample data...
tblTeam
----------------------------------
Name TeamID
Royal Challengers Bangalore 1
Chennai Super Kings 2
Delhi Daredevils 3
Sunrisers Hyderabad 4
Kolkata Knight Riders 5
Mumbai Indians 6
Kings XI Punjab 7
Rajasthan Royals 8
Deccan Chargers 9
Kochi Tuskers Kerala 10
Pune Warriors 11
------------------------------------------------
tblSchedule
------------------------------------------------
ScheduleID DateTime Team_1 Team_2 VenuID
1 4/18/08 8:00 PM 1 5 6
2 4/19/08 5:00 PM 2 7 9
3 4/19/08 8:30 PM 3 8 4
4 4/20/08 4:30 PM 5 9 1
5 4/20/08 8:00 PM 1 6 5
6 4/21/08 8:00 PM 8 7 27
7 4/22/08 8:00 PM 3 9 10
8 4/23/08 8:00 PM 2 6 2
9 4/24/08 8:00 PM 8 9 10
10 4/25/08 8:00 PM 6 7 9
11 4/26/08 4:00 PM 5 2 2
12 4/26/08 8:00 PM 1 8 6
-----------------------------------------------
The yellow key in the pic denote primary key and blue one foreign key.
and my requirement is like this....
DateTime Team-1 Team-2
Apr 8, 2015 8:00:00 PM Kolkata Knight Riders Mumbai Indians
Please help to get that o/p...
Join tblTeam twice with different alias names (T1 & T2):
SELECT ScheduleID,DateTime,T1.Name as [Team-1],T2.Name as [Team-2]
FROM tblSchedule S JOIN
tblTeam T1 ON S.Team_1=T1.TeamID JOIN
tblTeam T2 ON S.Team_2=T2.TeamID
ORDER BY S.ScheduleID
Sample Result:
ScheduleID DateTime Team-1 Team-2
----------------------------------------------------------------------------------------
1 April, 18 2008 20:00:00 Royal Challengers Bangalore Kolkata Knight Riders
2 April, 19 2008 17:00:00 Chennai Super Kings Kings XI Punjab
3 April, 19 2008 20:30:00 Delhi Daredevils Rajasthan Royals
4 April, 20 2008 16:30:00 Kolkata Knight Riders Deccan Chargers
5 April, 20 2008 20:00:00 Royal Challengers Bangalore Mumbai Indians
Sample result in SQL Fiddle
I like to use subquery for this type of problem to avoid the extra joining
product.
SELECT
CONVERT(varchar(20), DateTime, 100) AS DateTime,
(SELECT Name FROM tblTeam WHERE s.Team_1 = TeamID) AS Team-1,
(SELECT Name FROM tblTeam WHERE s.Team_2 = TeamID) AS Team-2
FROM tblSchedule s
Extra reading

how to get no of transactions on an hourly range

Table has columns Receiptno: and [TransDate] and Transtime. eg: Data below
0080052594 2012-10-28 1899-12-30 19:01:38.000
0080052595 2012-10-28 1899-12-30 19:05:09.000
0080052596 2012-10-28 1899-12-30 19:05:15.000
I need query to get hourly interval and the no: of transactions in the below format
Hour Inetrval No: Trans
09:01-10:00 10
10:01-11:00 16
Which DBMS are you using? What specific data type are your fields?
In Access, you'd use a Totals query (GROUP BY in SQL), you could do it using an additional field (or layered queries). You could also use inline code string comparisons. This would work with string or DateTime fields for transaction date and time.
For example, TABLE1:
RECEIPTNO TRANSDATE TRANSTIME
1 1/12/2015 4:32:00 PM
2 1/12/2015 4:45:00 PM
3 1/12/2015 4:52:00 PM
4 1/12/2015 3:57:00 PM
5 1/12/2015 4:07:00 PM
6 1/12/2015 4:09:00 PM
7 1/12/2015 6:15:00 PM
8 1/12/2015 12:34:00 PM
9 1/12/2015 2:45:00 PM
10 1/12/2015 3:15:00 PM
11 1/12/2015 3:17:00 PM
12 1/12/2015 3:49:00 PM
13 1/12/2015 3:47:00 PM
14 1/12/2015 2:52:00 PM
15 1/12/2015 2:36:00 PM
16 1/12/2015 2:17:00 PM
17 1/12/2015 2:25:00 PM
18 1/12/2015 4:12:00 PM
QUERY1 to count by hour as string:
SELECT Count(TABLE1.RECEIPTNO) AS CountOfRECEIPTNO, TABLE1.TRANSDATE, Left([TRANSTIME],InStr([TRANSTIME],":")-1) AS [HOUR]
FROM TABLE1
GROUP BY TABLE1.TRANSDATE, Left([TRANSTIME],InStr([TRANSTIME],":")-1)
ORDER BY Left([TRANSTIME],InStr([TRANSTIME],":")-1);
Results:
CountOfRECEIPTNO TRANSDATE HOUR
1 1/12/2015 12
5 1/12/2015 2
5 1/12/2015 3
6 1/12/2015 4
1 1/12/2015 6

How to count the records per half hour from a period (datetimefrom and datetimeto) field?

I have a table which looks like you can see below:
Id Date ScheduledTimeFrom ScheduledTimeTo ActualTimeFrom ActualTimeTo
1 2013-01-01 1899-12-30 07:00:00 1899-12-30 18:00:00 1899-12-30 07:23:00 1899-12-30 17:15:00
I need to calculate per half hour how many records exists, the output should be like:
Time Actual Count:
7:00 4
7:30 4
8:00 4
8:30 4
9:00 4
9:30 5
10:00 5
10:30 6
11:00 7
11:30 8
12:00 8
12:30 8
13:00 8
13:30 8
14:00 8
14:30 8
15:00 7
15:30 7
16:00 7
16:30 6
17:00 5
17:30 4
18:00 4
I already tried to make a helper table which should hold the times per halfhour. I have joined this helpertable with the table that contains the data and after that I tried to use a group by function but it was not working.
My query was like:
Create table period (timefrom datetime, timeto datetime)
insert into period
select '1899-12-30 07:00:00.000', '1899-12-30 07:30:00.000'
Union all
select '1899-12-30 07:30:00.000', '1899-12-30 08:00:00.000'
select *
from period p left join table1 t on t.ActualTimeFrom < p.timeto and t.ActualTimeTo >=p.timefrom
Grouping this give me no desired result....
Anyone an idea how to come to the result?
P.s. I am using sql server 2005.
After snooping around and testing it on my side, looks like this date function could be the answer:
DATEADD(mi,DATEDIFF(mi,0,YOUR_DATE_COLUMN)/30*30,0)

SQL How Many Employees Are Working, Group By Hour

I have a table, a timetable, with check-in and check-out times of the employees:
ID Date Check-in Check out
1 1-1-2011 11:00 18:00
2 1-1-2011 11:00 19:00
3 1-1-2011 16:00 18:30
4 1-1-2011 17:00 20:00
Now I want to know how many employees are working, every (half) hour.
The result I want to see:
Hour Count
11 2
12 2
13 2
14 2
15 2
16 3
17 3
18 2,5
19 1
Every 'Hour' you must read as 'till the next full hour', ex. 11 -> 11:00 - 12:00
Any ideas?
Build an additional table, called Hours, containing the following data:
h
00:00
00:30
01:00
...
23:30
then, run
Select h as 'hour' ,count(ID) as 'count' from timetable,hours where [Check_in]<=h and h<=[Check_out] group by h