我有两个表格,一个包含垃圾桶列表,另一个包含该垃圾桶收集的工作日。
我想要做的是返回所有垃圾桶及其下一次收集日期的列表。
我已经创建了此查询,它可以返回下一次收集日期,但每次只能针对一个垃圾桶查询。我不想为数据库中的每个垃圾桶运行此查询。
以下是我的查询:
有没有办法在单个查询中返回每个垃圾箱的下一个收集日期?
编辑:已经使用联接和其他东西更新了查询。
declare @bins table (
id int IDENTITY(1,1) PRIMARY KEY,
name nvarchar(255),
collectionCode nvarchar(255)
)
declare @collectionDays table (
id int IDENTITY(1,1) PRIMARY KEY,
weekday int,
collectionCode nvarchar(255)
)
insert into @bins (name, collectionCode) values
('Bin 1','MWF'),
('Bin 2','MWF'),
('Bin 3','ED'),
('Bin 4','ED'),
('Bin 5','ED'),
('Bin 6','ED'),
('Bin 7','ED'),
('Bin 8','ED'),
('Bin 9','ED'),
('Bin 10','MWF')
insert into @collectionDays (weekday, collectionCode) values
(0,'MWF'),
(2,'MWF'),
(4,'MWF'),
(0,'ED'),
(1,'ED'),
(2,'ED'),
(3,'ED'),
(4,'ED'),
(5,'ED'),
(6,'ED')
我想要做的是返回所有垃圾桶及其下一次收集日期的列表。
我已经创建了此查询,它可以返回下一次收集日期,但每次只能针对一个垃圾桶查询。我不想为数据库中的每个垃圾桶运行此查询。
以下是我的查询:
select top 1
name,
format(dateadd(day, (datediff(day, weekday, getdate()) / 7) * 7 + 7, weekday), 'dd/MM/yyyy') AS date
from @bins b
join @collectionDays c on b.collectionCode = c.collectionCode
where b.id = 1
order by date asc
如果我去掉top 1
和b.id = 1
的条件,它将返回每个工作日的所有垃圾箱和下一个日期。如果我尝试使用group by
,会出现错误:在选择列表中,列“@collectionDays.weekday”无效,因为它不包含在聚合函数或GROUP BY子句中。
select
name,
format(dateadd(day, (datediff(day, weekday, getdate()) / 7) * 7 + 7, weekday), 'dd/MM/yyyy') AS date
from @bins b
join @collectionDays c on b.collectionCode = c.collectionCode
group by b.id
order by date asc
有没有办法在单个查询中返回每个垃圾箱的下一个收集日期?
编辑:已经使用联接和其他东西更新了查询。
GROUP BY
,你的查询中没有聚合。此外,你为什么还在使用32年前的语法?它已经在1992年被明确的JOIN语法所取代;你现在已经有了29年可以采用它;真的应该改过来了。 - Thom A103
的CONVERT
函数,它更快。 - Thom A