I have 2 tables, let says the first table named “Schedule” and the second named “Setting”.
TABLE “SCHEDULE”
ID Name StartTime EndTime Room
---------------------------------------------
111 AAAA 08:00 09:00 -
112 BBBB 08:00 09:00 -
113 CCCC 08:00 09:00 -
114 DDDD 08:30 09:30 -
115 EEEE 08:30 09:30 -
116 FFFF 08:30 09:30 -
TABLE “SETTING”
ID Type Room
------------------------
111 1 BPD01
112 2 BPR33
113 2 BPR33
114 2 BPR35
115 2 BPR33
I want to get the result like:
Room StartTime EndTime Total Used
---------------------------------------
BPR33 08:00 09:00 2
BPR33 08:30 09:30 1
BPR35 08:30 09:30 1
I have the code like:
SELECT ST.Room, S.StartTime, S.EndTime, COUNT(*) AS [Total Used]
FROM Schedule AS S CROSS JOIN Setting AS ST
WHERE (ID IN
(SELECT ID FROM Setting
WHERE (Type = '2')))
GROUP BY ST.Room, S.StartTime, S.EndTime
But the code resulted in showing all of the record in Setting with the counted value, the filter does not run properly.
How to do that?
I see a few potential issues here:
WHERE ID IN (SELECT ID FROM Setting WHERE Type='2')looks like it could just beWHERE Type='2'Typecolumn? If it is INT, doWHERE Type = 2as opposed toWHERE Type = '2'(which you’d use if Type was VARCHAR).WHERE S.ID = ST.ID)All together now: