list-distinct中的最高值

时间:2013-09-06 09:05:23

标签: sql-server tsql

任何人都可以帮我查询,我有桌子

vendorid, agreementid, sales

12001      1004         700
5291       1004         20576
7596       1004         1908
45         103           345
41         103           9087

目标是什么? 当agreemtneid> 1然后在销售额最高时显示数据

vendorid  agreementid   sales
5291      1004          20576
41        103           9087

有什么想法吗? THX

4 个答案:

答案 0 :(得分:4)

您可以尝试使用CTEROW_NUMBER之类的内容

;WITH Vals AS (
    SELECT *, ROW_NUMBER() OVER(PARTITION BY AgreementID ORDER BY Sales DESC) RowID
    FROM MyTable
    WHERE AgreementID > 1
)
SELECT *
FROM Vals
WHERE RowID = 1

这将避免您使用相同的销售返回多个记录。

如果可以,你可以试试像

这样的东西
SELECT *
FROM MyTable mt INNER JOIN
(
    SELECT AgreementID, MAX(Sales) MaxSales 
    FROM MyTable
    WHERE AgreementID > 1
) MaxVals ON mt.AgreementID = MaxVals.AgreementID AND mt.Sales = MaxVals.MaxSales

答案 1 :(得分:1)

SELECT TOP 1 WITH TIES *
FROM MyTable
ORDER BY DENSE_RANK() OVER(PARTITION BY agreementid ORDER BY SIGN (SIGN (agreementid - 2) + 1) * sales DESC)

说明

  1. 我们通过agreementid将表MyTable分成多个分区。
  2. 对于每个分区,我们构建一个排名或行。
  3. 如果agreementid大于1,则排名将等于ORDER BY sales DESC。 否则,分区中每一行的排名都是相同的:ORDER BY 0 DESC
  4. 看看它的样子:

    SELECT *
        , SIGN (SIGN (agreementid - 2) + 1) * sales AS x
        , DENSE_RANK() OVER(PARTITION BY agreementid ORDER BY SIGN (SIGN (agreementid - 2) + 1) * sales DESC) AS rnk
    FROM MyTable
    
    +----------+-------------+-------+-------+-----+
    | vendorid | agreementid | sales | x     | rnk |
    +----------|-------------|-------+-------+-----+
    |  0       | 0           | 3     | 0     | 1   |
    | -1       | 0           | 7     | 0     | 1   |
    |  0       | 1           | 3     | 0     | 1   |
    | -1       | 1           | 7     | 0     | 1   |
    |  41      | 103         | 9087  | 9087  | 1   |
    |  45      | 103         | 345   | 345   | 2   |
    |  5291    | 1004        | 20576 | 20576 | 1   |
    |  7596    | 1004        | 1908  | 1908  | 2   |
    |  12001   | 1004        | 700   | 700   | 3   |
    +----------+-------------+-------+-------+-----+
    

    然后使用TOP 1 WITH TIES构造,我们只留下rnk等于1的行。

答案 2 :(得分:0)

你可以这样试试。

SELECT TOP 1 sales FROM MyTable WHERE agreemtneid > 1 ORDER BY sales DESC

答案 3 :(得分:0)

我真的不知道agreement_id>背后的业务逻辑。 1.在我看来,无论vendor_id如何,您都希望通过协议ID获得最大销售额(带有关系)。

首先,让我们创建一个简单的示例数据库。

-- Sample table
create table #sales
(
  vendor_id int,
  agreement_id int,
  sales_amt money
);

-- Sample data
insert into #sales values
(12001, 1004, 700),
(5291, 1004, 20576),
(7596, 1004, 1908),
(45, 103, 345),
(41, 103, 9087);

其次,让我们使用公用表表达式来解决这个问题,以获得一个结果集,该结果集使每一行与协议ID的最大销售额配对。

select语句只是应用业务逻辑来过滤数据以获得答案。

-- CTE = max sales for each agreement id
;
with cte_sales as
(
  select
    vendor_id,
    agreement_id,
    sales_amt,
    max(sales_amt) OVER(PARTITION BY agreement_id) AS max_sales
  from
    #sales
)

-- Filter by your business logic
select * from cte_sales where sales_amt = max_sales and agreement_id > 1;

下面的屏幕截图显示了您想要的确切结果。

enter image description here