我正在尝试计算8 AM-8PM之间(例如2013年5月2日)与8 PM-8AM(次日)之间的交易次数
具有这些时间的字段称为 - CREATED_DT
所售商品的字段名为 - ITEM_ID
有人可以帮忙吗?无法在论坛上找到它。
谢谢, 或
答案 0 :(得分:0)
select count(ITEM_ID) from your_table
where CREATED_DT between '2013-05-20 08:00:00' and '2013-05-20 20:00:00'
答案 1 :(得分:0)
像这样的东西:
SELECT
COUNT(1)
FROM
dbo.YOUR_TABLE
WHERE
CAST(CREATED_DT AS DATE) = '03.20.2013'
AND
CAST(CREATED_DT AS TIME(0)) BETWEEN '08:00:00' AND '20:00:00'
MSDN:TSQL time
答案 2 :(得分:0)
因为您正在寻找相对计数
select day2-day1 from
(
select
sum(case when created_dt = '2013-05-20' then 1 else 0 end) as day1
, sum(case when created_dt = '2013-05-21' then 1 else 0 end) as day2
from your_table
where CREATED_DT between '2013-05-20 08:00:00' and '2013-05-20 20:00:00'
or CREATED_DT between '2013-05-21 08:00:00' and '2013-05-21 20:00:00'
)t
答案 3 :(得分:0)
这是在上午8点到晚上8点之间给出的
select count(ITEM_ID) from your_table
where CREATED_DT between
CONVERT(varchar(15),CAST('2013-05-20 08:00:00' AS TIME),100) and
CONVERT(varchar(15),CAST('2013-05-20 20:00:00' AS TIME),100)