繁体   English   中英

SQL隐藏/显示基于其他表中行数的行

[英]SQL Hide/Show rows based on row count from another table

我有一个SQL问题。 我有两个表, tableA具有4条记录, tableB有0条记录的权利,但会超过200个总记录。 我想知道如果tableB在200条记录以下,是否可以隐藏tableA的最后两条记录?

到目前为止,我得到的非常简单

SELECT 
    id, dateSlot, timeSlot
FROM 
    tableA a 
INNER JOIN
    tableB b ON a.id = b.dateTimeSlotId;

我只是不知道如何基于另一个表的总记录来隐藏记录。

有人可以帮忙吗?

这只是一个主意。 如果表不对称,则需要改进逻辑。

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) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM