2017-05-15 166 views
0

我有一個數據,看起來如下。我想統計產品的總數,並創建一個表格來總結不同日期範圍的計數。以下圖爲例。這裏的碼頭我:如何在不同條件下計算SQL中的不同值?

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

回答

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。
  • 你需要圍繞每個內部選擇放置括號。
  • 有一個在端部的多餘的逗號(閉合托架之前)

然而,該查詢不會返回的5和2的值,因爲要指定在每個不同進行選擇。這將只給你3和2,因爲從第一個查詢返回的產品(A/D/E)只有3個不同的值。如果您需要行數,請刪除不同行。

最後,我建議您在SQL中使用日期文字時使用YYYY-MM-DD語法。這消除了關於日期和月份的歧義。例如,1/4/2017可能是1月4日或4月1日,具體取決於您的SQL Server的配置方式。如果指定了2017年4月1日,那麼這將一直被解釋成4月1日和2017年1月4日將永遠是1月4日

+0

偉大的工作,謝謝。 – Curious

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

0

如果最後的代碼不工作,你需要使用轉換和演員:

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 
+0

它是詳細的,但如果你需要真正的查詢只是複製最後的代碼。 –

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