我知道这段代码错了,输入应该像这样给出3个日期
select Factory,Sum(quantity) from table where date='23-09-2017' and date='24-09-2017' and date='25-09-2017 group by factory
答案 0 :(得分:0)
从表格中选择工厂,总和(数量),其中日期IN('23 -09-2017','24-09-2017','25-09-2017')按工厂分组
答案 1 :(得分:0)
可以使用BETWEEN
,OR
和IN
完成此操作。
<强>查询强>
<强> Using BETWEEN
强>
select Factory,
Sum(quantity)
from table
where [date] between '23-09-2017' and date='25-09-2017'
group by factory;
可以使用BETWEEN
,因为您的日期是连续三个日期。
<强> Using OR
强>
select Factory,
Sum(quantity)
from table
where date='23-09-2017' or date='24-09-2017' or date='25-09-2017'
group by factory;
<强> Using IN
强>
select Factory,
Sum(quantity)
from table
where date in ('23-09-2017','24-09-2017','25-09-2017')
group by factory;
答案 2 :(得分:0)
请尝试在&#39;如果日期不在范围内,或者在日期介于某个范围之间使用。
select Factory, Sum(quantity) from table1
WHERE Convert(Date, datefield) in '2017-09-23','2017-09-24','2017-09-25')
group by Factory
select Factory, Sum(quantity) from table1
where Convert(Date, datefield)between '2017-09-23' and '2017-09-25'
group by Factory
答案 3 :(得分:0)
您可以寻找:
select Factory,
SUM( CASE WHEN date='23-09-2017' THEN quantity END) '23-09-2017'
SUM( CASE WHEN date='24-09-2017' THEN quantity END) '24-09-2017'
SUM( CASE WHEN date='25-09-2017' THEN quantity END) '25-09-2017'
from table
group by factory
答案 4 :(得分:0)
declare @date1 datetime,
@date2 datetime,
@date3 datetime
select @date1 = '2017-09-24',@date2 = '2017-09-30',@date3 = '2017-03-31'
select Fac 'Particular',
sum(case when cast(tran_date as date ) = cast(@date1 as date ) then Qty
else 0 end ) AS '@date1',
sum(case when cast(tran_date as date ) = cast(@date2 as date ) then Qty
else 0 end )AS '@date2',
sum(case when cast(tran_date as date ) = cast(@date3 as date ) then Qty
else 0 end )AS '@date3'
from [table] where (cast(tran_date as date ) = cast(@date1 as date )
OR cast(tran_date as date ) = cast(@date2 as date )
OR cast(tran_date as date ) = cast(@date3 as date ))
group by fac
答案 5 :(得分:0)
你可以这样做:
SELECT Factory,
SUM(quantity)
FROM Table
WHERE YEAR(date) = 2017
AND MONTH(date) = 09
AND DAY(date) IN (23, 24, 25)
GROUP BY Factory
我希望我有所帮助。
答案 6 :(得分:0)
当您在sql中调用特定日期时,如'20170922',它将搜索22 09 2017 00:00时间,因此您应在查询中使用BETWEEN。预期查询应该如下;
Select SUM(quantity),Datepart(day,Date),Factory
FROM table
Where (Date BETWEEN '2017-09-23' AND '2017-09-24')
OR (Date BETWEEN '2017-09-24' AND '2017-09-25')
Group By Factory ,DATEPART(day,Date)