SQL WHERE / HAVING条件

时间:2018-11-30 16:18:38

标签: sql sql-server tsql

目前,我正在从事一个预测项目,以估计现金流量。 SQL查询如下所示:

SELECT [Date] AS ds, SUM([Sales Amount]) AS y, [Item ID]
FROM dbo.[Table]
GROUP BY [Date], [Item ID]
ORDER BY ds;

为了预测销售额,我使用R包,该包严格要求必须至少有2个实例出现预测值(销售)。

但是,在我的查询中,有一些实例仅将一项交易了一次。

您能帮我解决一个HAVINGWHERE条件,该条件排除所有仅一次交易的项目吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

我要添加一个计数并使用它:

<div class="d-flex">
  <span>{{status}}</span>
<div class="data-block"
     [class.example1]="status === 'exampleValue1'"
     [class.example2]="status === 'exampleValue2'"
     [class.example3]="status === 'exampleValue3'"
     [class.example4]="status === 'exampleValue4'">
</div>

答案 1 :(得分:0)

您可以在WHERE子句中使用额外的过滤条件:

SELECT
  [Date] AS ds
 ,SUM([Sales Amount]) AS y
 ,[Item ID]
FROM dbo.[Table]
WHERE [Item ID] in ( -- filters out the items with less than 2 samples
  select distinct [Item ID] 
  from dbo.[Table] 
  group by [Item ID], [Date] having count(*) > 1
)
GROUP BY [Date]
        ,[Item ID]
ORDER BY ds