按标识组合记录

时间:2013-04-24 05:49:54

标签: sql-server sql-server-2008

我正在使用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 

但仍显示错误的结果......请帮助我......我错了?感谢。

5 个答案:

答案 0 :(得分:1)

您可以在查询中使用“GROUP BY” 像,

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

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

答案 1 :(得分: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 FIDDLE DEMO

答案 2 :(得分: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

答案 3 :(得分:0)

试试这个,

   SELECT BrockerCode,
          MemberId, FY,SUM(SY),SUM(TY) FROM @tablePromotee Group By BrockerCode,
          MemberId, FY

答案 4 :(得分:0)

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

 SELECT MemberId,BrockerCode,FY,SY,TY  FROM  CTE  WHERE  RN = 1 and SY<>0
 ROUP BY BrockerCode, MemberId