如何在不同条件下计算SQL中的不同值?

时间:2017-05-15 23:05:00

标签: sql-server sql-server-express

我的数据如下所示。我想计算产品总数,并创建一个表格,总结不同日期范围的计数。请参见下图作为示例。这里是我的码头:

SELECT( 
SELECT Count(DISTINCT Product) FROM Table1 WHERE  Date BETWEEN '01/01/2017 AND 
'01/15/2017,
SELECT Count(DISTINCT Product) FROM Table1 WHERE  Date BETWEEN '01/16/2017 AND 
'01/31/2017,    
);

但是我收到了一个错误:

Incorrect syntax near ','

enter image description here

4 个答案:

答案 0 :(得分:2)

这将有效

SELECT  
(SELECT Count(DISTINCT Product) FROM Table1 WHERE  Date BETWEEN '01/01/2017' AND 
'01/15/2017'),
(SELECT Count(DISTINCT Product) FROM Table1 WHERE  Date BETWEEN '01/16/2017' AND 
'01/31/2017')    
;

SQL中有一些语法错误:

  • 使用BETWEEN语句时,您必须只输入引号 每个日期。不要在引号中包含AND。
  • 您需要在每个内部SELECTS周围放置括号。
  • 结尾处有一个额外的逗号(在结束括号之前)

但是,此查询不会返回值5和2,因为您在每个SELECT中指定了DISTINCT。这将只给你3和2,因为从第一个查询返回的Product(A / D / E)只有3个不同的值。如果您想要行数,请删除distinct。

最后,我建议您在SQL中使用日期文字时使用YYYY-MM-DD语法。这消除了关于什么是日期和月份的任何歧义。例如,1/4/2017可能是1月4日或4月1日,具体取决于SQL Server的配置方式。如果您指定2017-04-01,那么这将始终被解释为4月1日,2017-01-04将始终为1月4日。

答案 1 :(得分:0)

简单地说:

    SELECT 
     (SELECT COUNT(DISTINCT Product) FROM yourtable T1 WHERE Date BETWEEN '01/01/2017' AND '01/15/2017') C1,
     (SELECT COUNT(DISTINCT Product) FROM yourtable T2 WHERE Date BETWEEN '01/16/2017' AND '01/31/201') C2

输出:3 | 2

SELECT 
 (SELECT COUNT(*) FROM yourtable T1 WHERE Date BETWEEN '01/01/2017' AND '01/15/2017') C1,
 (SELECT COUNT(*) FROM yourtable T2 WHERE Date BETWEEN '01/16/2017' AND '01/31/201') C2

输出:5 | 2

答案 2 :(得分:0)

如果最后一个代码不起作用,您需要使用Convert and Cast:

Declare @tb table(product  varchar(50),[Date] date)
insert into @tb

select 'A' as Product, cast(substring(CONVERT(VARCHAR(10), cast('1/1/2017' as date),110),7,4) + '-' +
substring(CONVERT(VARCHAR(10), cast('1/1/2017' as date),110),1,2) + '-' +
substring(CONVERT(VARCHAR(10), cast('1/1/2017' as date),110),4,2) as date) as [Date] union all

select 'A' as Product, cast(substring(CONVERT(VARCHAR(10), cast('1/1/2017' as date),110),7,4) + '-' +
substring(CONVERT(VARCHAR(10), cast('1/1/2017' as date),110),1,2) + '-' +
substring(CONVERT(VARCHAR(10), cast('1/1/2017' as date),110),4,2)as date) as [Date] union all

select 'D' as Product, cast(substring(CONVERT(VARCHAR(10), cast('1/5/2017' as date),110),7,4) + '-' +
substring(CONVERT(VARCHAR(10), cast('1/5/2017' as date),110),1,2) + '-' +
substring(CONVERT(VARCHAR(10), cast('1/5/2017' as date),110),4,2) as date) as [Date] union all

select 'E' as Product, cast(substring(CONVERT(VARCHAR(10), cast('1/6/2017' as date),110),7,4) + '-' +
substring(CONVERT(VARCHAR(10), cast('1/6/2017' as date),110),1,2) + '-' +
substring(CONVERT(VARCHAR(10), cast('1/6/2017' as date),110),4,2)as date) as [Date] union all


select 'E' as Product, cast(substring(CONVERT(VARCHAR(10), cast('1/10/2017' as date),110),7,4) + '-' +
substring(CONVERT(VARCHAR(10), cast('1/10/2017' as date),110),1,2) + '-' +
substring(CONVERT(VARCHAR(10), cast('1/10/2017' as date),110),4,2)as date) as [Date] union all


select 'D' as Product, cast(substring(CONVERT(VARCHAR(10), cast('1/25/2017' as date),110),7,4) + '-' +
substring(CONVERT(VARCHAR(10), cast('1/25/2017' as date),110),1,2) + '-' +
substring(CONVERT(VARCHAR(10), cast('1/25/2017' as date),110),4,2)as date) as [Date] union all


select 'A' as Product, cast(substring(CONVERT(VARCHAR(10), cast('1/30/2017' as date),110),7,4) + '-' +
substring(CONVERT(VARCHAR(10), cast('1/30/2017' as date),110),1,2) + '-' +
substring(CONVERT(VARCHAR(10), cast('1/30/2017' as date),110),4,2)as date) as [Date]

--Copy from here
select acnt as 'Count(01/01/2017-01/15/2017)',bcnt as 'Count(01/16/2017-01/31/2017)' from
(select count(1)   as acnt from @tb where [Date] BETWEEN '01/01/2017' AND '01/15/2017') as a
left join
(select count(1)   as bcnt from @tb where [Date]  BETWEEN '01/16/2017' AND '01/31/2017') as b
On a.acnt != b.bcnt or a.acnt = b.bcnt

答案 3 :(得分:0)

;WITH CTE(Product ,[Date])
As
(
SELECT 'A','1/1/2017'  Union all
SELECT 'A','1/1/2017'  Union all
SELECT 'D','1/5/2017'  Union all
SELECT 'E','1/6/2017'  Union all
SELECT 'E','1/10/2017' Union all
SELECT 'D','1/25/2017' Union all
SELECT 'A','1/30/2017'
)
SELECT SUM([Count Of between '1/1/2017' AND '1/15/2017']) AS [Count Of between (1/1/2017 AND 1/15/2017)]
    ,SUM([Count Of between '1/16/2017' AND '1/31/2017']) AS [Count Of between (1/16/2017     AND 1/31/2017)]
FROM (
    SELECT COUNT(Product) OVER (
            PARTITION BY [Date] ORDER BY Product
            ) AS [Count Of between '1/1/2017' AND '1/15/2017']
        ,ISNULL(NULL, '') AS [Count Of between '1/16/2017' AND '1/31/2017']
    FROM cte
    WHERE [Date] BETWEEN '1/1/2017'
            AND '1/15/2017'

    UNION ALL

    SELECT ISNULL(NULL, '')
        ,COUNT(Product) OVER (
            PARTITION BY [Date] ORDER BY Product
            ) AS [Count Of between '1/16/2017' AND '1/31/2017']
    FROM cte
    WHERE [Date] BETWEEN '1/16/2017'
            AND '1/31/2017'
    ) Dt

输出

  Count Of between (1/1/2017 AND 1/15/2017) |   Count Of between (1/16/2017  AND 1/31/2017)
--------------------------------------------------------------------------------
    5                                             2