0

需要来自 SQL 之神的帮助

表 1 - DistinctQuotes

此表包含有关报价的信息说。

所以我们有

+-------+-----------+--------------+------+-----------------+
|REGION | SubRegion | Market Route | Type | Quotations Count|
+-------+-----------+--------------+------+-----------------+
|EMEA   | Germany   | Direct       | DS1  | 12              |
|EMEA   | Germany   | NonDirect    | DS1  | 22              |
|EMEA   | Germany   | Direct       | DS1  | 2               |
|EMEA   | Germany   | NOnDirect    | DS1  | 4               |
|EMEA   | France    | Direct       | DS1  | 4               |
|EMEA   | UK        | Direct       | DS1  | 4               |
+-------+-----------+--------------+------+-----------------+

因此,我想显示来自 DS1 类型且 Market Route 为 Direct 的每个子区域的报价计数。

现在下面的这个查询是错误的,因为报价计数来自匹配行的报价计数的总和,而不是来自匹配 [Market Route] = 'Direct' AND [Type] = 'DS1' 的行计数。

谁能帮我更改下面的查询以获取我需要的内容?

SELECT        
 [Region]  ,[SubRegion]      
   ,SUM (CASE WHEN [Market Route] = 'Direct' AND [Type] = 'DS1' 
   THEN 1 ELSE 0 END)               as [Count of Direct quotes from  DS1] 
FROM [dbo].[V_DistinctQuotes]
  GROUP by  [SubRegion],[Region] 
4

3 回答 3

3

只需更改THEN 1 ELSE 0THEN [Quotations Count] ELSE 0?

于 2012-06-19T10:57:01.680 回答
1

不确定我是否正确理解您的问题,但如果您想要市场路线为“直接”且类型为“DS1”的报价计数(实际上是 [报价计数] 字段的总和),请执行以下操作:

SELECT        
 [SubRegion],sum([Quotations Count])
 from
[dbo].[V_DistinctQuotes]
where
MarketRoute='Direct' and [Type] ='DS1'

group by [SubRegion]

产生:

SubRegion   (No column name)
France  4
Germany 14
UK      4
于 2012-06-19T11:54:04.433 回答
1

这行得通吗?

SELECT
[Region] ,[SubRegion] ,Count(*) as [Count of Direct quotes from DS1] 
FROM  [dbo].[V_DistinctQuotes] 
WHERE [Market Route] = 'Direct' AND [Type] = 'DS1'
GROUP by [SubRegion],[Region] 
于 2012-06-19T10:55:21.053 回答