如何将结果限制为一定数量或更多?

时间:2018-06-23 01:57:57

标签: sql sql-server

因此,我试图将结果限制为仅下了2个或更多订单的结果,但我不确定要获得该结果我做了什么或需要做什么。

这是到目前为止我得到的:

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

export class MyComponent {
  constructor(
    private route: ActivatedRoute
  ) {}

  ngOnInit() {
    const firstParam: string = this.route.snapshot.queryParamMap.get('firstParamKey');
    const secondParam: string = this.route.snapshot.queryParamMap.get('secondParamKey');
  }
}

1 个答案:

答案 0 :(得分:2)

只需添加:

having count(distinct o.orderid) >= 2

我认为您需要的查询是:

SELECT C.CustomerID, C.CompanyName,
       OrdersPlaced = COUNT( O.OrderID),
       TotalPrice = SUM( D.Quantity * D.UnitPrice)
FROM Customers C INNER JOIN
     Orders O 
     ON C.CustomerID = O.CustomerID INNER JOIN
     [Order Details] D
     ON O.OrderID = D.OrderID
WHERE O.OrderID >= 3
GROUP BY C.CustomerID, C.CompanyName;

您还需要从OrderId中删除GROUP BY