1

我正在使用 SQLServer2008。

我的存储过程的输出是:

--------------------------------------------------------
 BrokerCode | MemberId  |  FY    |    SY    |    TY    |
--------------------------------------------------------
    104     |    24     |  1140  |    0.00  |    0.00  |
--------------------------------------------------------
    104     |    24     |  1140  |    570   |    0.00  |
--------------------------------------------------------
    104     |    30     |  500   |    0.00  |    0.00  |
--------------------------------------------------------
    104     |    30     |  500   |    360   |    0.00  |
--------------------------------------------------------

我想得到这样的结果:

--------------------------------------------------------
 BrokerCode | MemberId  |   FY    |    SY    |    TY    |
--------------------------------------------------------
    104     |    24     |   1140  |    570   |    0.00  |
--------------------------------------------------------
    104     |    30     |   500   |    360   |    0.00  |
--------------------------------------------------------

所以我尝试使用:

WITH CTE AS ( 
SELECT BrockerCode,
     MemberId,

     RN=ROW_NUMBER()OVER(PARTITION BY MemberId ORDER BY MemberID),
     FY=MIN(FY)OVER(PARTITION BY MemberId),
     SY=MIN(SY)OVER(PARTITION BY MemberId),
     TY=MIN(TY)OVER(PARTITION BY MemberId)

     FROM   @tablePromotee )
SELECT MemberId,BrockerCode,FY,SY,TY  FROM  CTE  WHERE  RN = 1 

但它仍然显示错误的结果...请帮助我..我错在哪里?谢谢。

4

5 回答 5

1

Common Table Expression这里真的不需要。一个更简单的查询:

SELECT 
      BrokerCode,
      MemberId,
      MAX(FY) AS FY,
      MAX(SY) AS SY,
      MAX(TY) AS TY
FROM YourTable
GROUP BY BrokerCode, MemberId

SQL 小提琴演示

于 2013-04-24T06:06:13.753 回答
1

您可以在查询中简单地使用“GROUP BY”,例如,

SELECT BrockerCode,
     MemberId,
     MIN(FY)
     MIN(SY)
     MIN(TY)
     FROM   @tablePromotee GROUP BY BrockerCode, MemberId

sql server 上有多个聚合函数,您可以根据需要从中使用它们。

于 2013-04-24T06:00:44.573 回答
0

试试这个——

SELECT 
      MemberId
    , BrockerCode
    , FY = MAX(FY)
    , SY = MAX(SY)
    , TY = MAX(TY)  
FROM (
    SELECT 
          BrockerCode
        , MemberId
        , RN = ROW_NUMBER()OVER(PARTITION BY MemberId ORDER BY MemberID)
        , FY = MIN(FY)OVER(PARTITION BY MemberId)
        , SY = MIN(SY)OVER(PARTITION BY MemberId)
        , TY = MIN(TY)OVER(PARTITION BY MemberId)
    FROM @tablePromotee 
)  
WHERE RN = 1
GROUP BY 
      BrokerCode
    , MemberId  

或者试试这个 -

SELECT TOP 1
      BrockerCode
    , MemberId
    , FY = MAX(MIN(FY)OVER(PARTITION BY MemberId))
    , SY = MAX(MIN(SY)OVER(PARTITION BY MemberId))
    , TY = MAX(MIN(TY)OVER(PARTITION BY MemberId))
FROM @tablePromotee 
GROUP BY 
      BrokerCode
    , MemberId
ORDER BY MemberId
于 2013-04-24T05:55:48.710 回答
0

尝试这个,

   SELECT BrockerCode,
          MemberId, FY,SUM(SY),SUM(TY) FROM @tablePromotee Group By BrockerCode,
          MemberId, FY
于 2013-04-24T05:55:59.420 回答
0

简单地使用 group by 是行不通的,因为您最终可能会得到 o.oo 值更改现有代码的最后一行,如下所示,希望您想要这个。

 SELECT MemberId,BrockerCode,FY,SY,TY  FROM  CTE  WHERE  RN = 1 and SY<>0
 ROUP BY BrockerCode, MemberId 
于 2013-04-24T06:09:46.747 回答