From 子句中的 SQL 子查询计数不起作用
Count with SQL SubQuery in From Clause Not Working
我试图获取某个日期范围内的工作日数并将其分配给一个变量,但我似乎无法获取。我只尝试了我的子查询,它可以很好地为我提供不同的日期,但我需要对它们进行计数。这是我希望可以使用的代码:
declare @end date='2016/05/06'
declare @begin date = DATEADD(month, DATEDIFF(month, 0, @end), 0)
declare @begin31 date = DATEADD(MONTH, -1, @end)
declare @tmprocdays int
@tmprocdays = select count(*) from (select distinct WORKDATE from Daily where WORKDATE between @begin and @end) <<<----- NOT WORKING
错误说问题出在 ) 我试过将 while 放在括号中,但也没有用。
试试这个
declare @end date='2016/05/06'
declare @begin date = DATEADD(month, DATEDIFF(month, 0, @end), 0)
declare @begin31 date = DATEADD(MONTH, -1, @end)
declare @tmprocdays int
set @tmprocdays = select count(*) from (select distinct WORKDATE from Daily where WORKDATE between @begin and @end) a <<<----- NOT WORKING
或 将上面的最后两行替换为:
select @tmprocdays = count(*)
from
(
select distinct WORKDATE from Daily where WORKDATE between @begin and @end
) a
或更好
select @tmprocdays = count(distinct WORKDATE) from Daily where WORKDATE between @begin and @end
主要问题是子查询周围缺少括号。
以下是对代码的一些修正:
declare @end date = '2016-05-06';
declare @begin date = DATEADD(month, DATEDIFF(month, 0, @end), 0);
declare @begin31 date = DATEADD(MONTH, -1, @end);
declare @tmprocdays int;
select @tmprocdays = count(distinct WORKDATE)
from Daily
where WORKDATE between @begin and @end ;
变化:
- 将
@tmprocdays =
放在 select
语句本身中。您的子查询缺少括号。
- 在每行的末尾添加了分号。
- 删除了子查询,改用
count(distinct)
。
- 将日期更改为 YYYY-MM-DD 格式。这至少是一个ISO标准。
最后三个是装饰品。
from 子句必须始终包含 table 名称,或者在子查询的情况下,它必须是别名:
如果您在子查询后添加任何标识符(字母单词),它将正常工作,例如:
@tmprocdays = select count(*) from (select distinct WORKDATE from Daily where WORKDATE between @begin and @end) xxx
我试图获取某个日期范围内的工作日数并将其分配给一个变量,但我似乎无法获取。我只尝试了我的子查询,它可以很好地为我提供不同的日期,但我需要对它们进行计数。这是我希望可以使用的代码:
declare @end date='2016/05/06'
declare @begin date = DATEADD(month, DATEDIFF(month, 0, @end), 0)
declare @begin31 date = DATEADD(MONTH, -1, @end)
declare @tmprocdays int
@tmprocdays = select count(*) from (select distinct WORKDATE from Daily where WORKDATE between @begin and @end) <<<----- NOT WORKING
错误说问题出在 ) 我试过将 while 放在括号中,但也没有用。
试试这个
declare @end date='2016/05/06'
declare @begin date = DATEADD(month, DATEDIFF(month, 0, @end), 0)
declare @begin31 date = DATEADD(MONTH, -1, @end)
declare @tmprocdays int
set @tmprocdays = select count(*) from (select distinct WORKDATE from Daily where WORKDATE between @begin and @end) a <<<----- NOT WORKING
或 将上面的最后两行替换为:
select @tmprocdays = count(*)
from
(
select distinct WORKDATE from Daily where WORKDATE between @begin and @end
) a
或更好
select @tmprocdays = count(distinct WORKDATE) from Daily where WORKDATE between @begin and @end
主要问题是子查询周围缺少括号。
以下是对代码的一些修正:
declare @end date = '2016-05-06';
declare @begin date = DATEADD(month, DATEDIFF(month, 0, @end), 0);
declare @begin31 date = DATEADD(MONTH, -1, @end);
declare @tmprocdays int;
select @tmprocdays = count(distinct WORKDATE)
from Daily
where WORKDATE between @begin and @end ;
变化:
- 将
@tmprocdays =
放在select
语句本身中。您的子查询缺少括号。 - 在每行的末尾添加了分号。
- 删除了子查询,改用
count(distinct)
。 - 将日期更改为 YYYY-MM-DD 格式。这至少是一个ISO标准。
最后三个是装饰品。
from 子句必须始终包含 table 名称,或者在子查询的情况下,它必须是别名:
如果您在子查询后添加任何标识符(字母单词),它将正常工作,例如:
@tmprocdays = select count(*) from (select distinct WORKDATE from Daily where WORKDATE between @begin and @end) xxx