I have a SQL question. I have two tables, tableA
has 4 records, tableB
has 0 records right now, but will go over 200 total records. I was wondering if there is away to hide the last two records of tableA
if tableB
is under 200 records?
What I got so far is very simple
SELECT
id, dateSlot, timeSlot
FROM
tableA a
INNER JOIN
tableB b ON a.id = b.dateTimeSlotId;
I just don't know how to hide records based on another tables total records.
Can anyone help?
It is only an idea. if the tables are not symmetric you need to improve the logic.
declare @tableA table (id int)
declare @tableB table (dateTimeSlotId int , dateSlot date, timeSlot time)
insert @tableA values (1),(2),(3),(4),(5),(6),(7)
insert @tableB values
(1,'20170801', '00:00'),
(2,'20170802', '00:01'),
(3,'20170803', '00:02'),
(4,'20170804', '00:03'),
(5,'20170805', '00:04'),
(6,'20170806', '00:05'),
(7,'20170807', '00:06')
;with cte as(
SELECT ROW_NUMBER() over (order by id) rNumber, id, dateSlot, timeSlot
FROM @tableA a INNER JOIN
@tableB b
ON a.id = b.dateTimeSlotId)
SELECT id, dateSlot, timeSlot
FROM cte where rNumber <= (SELECT case when Count(1) >= 200 then Count(1) -2 else Count(1) end from @tableB)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.