-2

我在存储过程中有这个查询:

SELECT    DISTINCT
 a.UserProfileId,
 a.FromProfileId,
  mm.FromProfileId as ToProfileID
FROM      ( SELECT
          mup.UserProfileId,
          mmb.FromProfileId
        FROM      dbo.Mindcracker_MailBox mmb
        LEFT JOIN Mindcracker_UserProfile mup
          ON mmb.ToProfileId=mup.UserProfileId
        WHERE     mup.UserProfileId=1144 ) a
LEFT JOIN  dbo.Mindcracker_MailBox mm
ON a.FromProfileId=mm.ToProfileId

上述查询产生以下结果:

UserProfielId  FromProfileID  ToProfielID
1144    1152    1144
1144    1152    1378
1144    1152    1483
1144    1333    1143
1144    1333    1148
1144    1333    1290
1144    1333    1297
1144    1333    1333
1144    1333    1378
1144    1378    1143
1144    1378    1185
1144    1378    1333
1144    1378    1378
1144    1483    1143
1144    1483    1144
1144    1483    1297
1144    1483    1483
1144    11526   1148
1144    11526   1290
1144    11526   1333
1144    11526   1378
1144    11526   11526

我只想获得基于 FromProfileId 列的最高值,我的输出应该是这样的:

1144    1152    1144
1144    1333    1143
1144    1378    1143
1144    1483    1143
4

6 回答 6

0

try using ranking_functions 教程

 with cte as (
        SELECT    DISTINCT
         a.UserProfileId,
         a.FromProfileId,
          mm.FromProfileId as ToProfileID
        FROM      ( SELECT
                  mup.UserProfileId,dense_rank() over (order by mmb.FromProfileid) as rn

                FROM      dbo.Mindcracker_MailBox mmb
                LEFT JOIN Mindcracker_UserProfile mup
                  ON mmb.ToProfileId=mup.UserProfileId
                WHERE     mup.UserProfileId=1144 ) a
        LEFT JOIN  dbo.Mindcracker_MailBox mm
        ON a.FromProfileId=mm.ToProfileId
        order by profile_ID
        )

        select * from cte where rn between 1 AND 4
于 2013-11-12T05:10:42.477 回答
0

尝试添加这个

ORDER BY amount [ColumnName] LIMIT 4
于 2013-11-12T04:54:46.037 回答
0

尝试此查询以获得输出时的前 4 个值。

执行此查询后,您将收到存储过程的结果:

SELECT TOP 4 FromProfielId , UserProfielId, ToProfielID FROM [Your TableName];

我希望你能得到你的解决方案。

于 2013-11-12T04:48:30.210 回答
0

我认为您应该删除 distinct 并按 UserProfileId、FromProfileId 添加组

于 2013-11-12T04:52:12.107 回答
0

对从存储过程获得的结果集执行以下查询。

SELECT TOP 1 * FROM [TableName] ORDER BY FromProfielId;
于 2013-11-12T04:59:28.193 回答
0

虽然这个问题是很久以前的问题,但可能有人会发现它很有用。

要获取每个数据集的第一行,您需要使用窗口函数对结果集进行窗口化。

我只是制作你的结果集并根据它查询输出。它可以扩展到原始查询。

创建表

CREATE TABLE #tbl(UserProfielId INT, FromProfileID INT, ToProfielID INT)

样本数据

INSERT #tbl
SELECT 1144  ,  1152  ,  1144 UNION ALL
SELECT 1144  ,  1152  ,  1378 UNION ALL
SELECT 1144  ,  1152  ,  1483 UNION ALL
SELECT 1144  ,  1333  ,  1143 UNION ALL
SELECT 1144  ,  1333  ,  1148 UNION ALL
SELECT 1144  ,  1333  ,  1290 UNION ALL
SELECT 1144  ,  1333  ,  1297 UNION ALL
SELECT 1144  ,  1333  ,  1333 UNION ALL
SELECT 1144  ,  1333  ,  1378 UNION ALL
SELECT 1144  ,  1378  ,  1143 UNION ALL
SELECT 1144  ,  1378  ,  1185 UNION ALL
SELECT 1144  ,  1378  ,  1333 UNION ALL
SELECT 1144  ,  1378  ,  1378 UNION ALL
SELECT 1144  ,  1483  ,  1143 UNION ALL
SELECT 1144  ,  1483  ,  1144 UNION ALL
SELECT 1144  ,  1483  ,  1297 UNION ALL
SELECT 1144  ,  1483  ,  1483 UNION ALL
SELECT 1144  ,  11526 ,  1148 UNION ALL
SELECT 1144  ,  11526 ,  1290 UNION ALL
SELECT 1144  ,  11526 ,  1333 UNION ALL
SELECT 1144  ,  11526 ,  1378 UNION ALL
SELECT 1144  ,  11526 ,  11526

询问

使用ROW_NUMBER函数将您的结果集窗口化CTE,然后根据您的主查询进行查询。

;WITH C AS(
    SELECT  ROW_NUMBER() OVER (PARTITION BY FromProfileID ORDER BY FromProfileID) AS Rn
            ,UserProfielId
            ,FromProfileID
            ,ToProfielID
    FROM #tbl
)

SELECT  UserProfielId
        ,FromProfileID
        ,ToProfielID
FROM C
WHERE Rn = 1

输出

UserProfielId   FromProfileID   ToProfielID
1144            1152            1144
1144            1333            1143
1144            1378            1143
1144            1483            1143
1144            11526           1148

SQLFiddle 演示

于 2015-05-08T00:12:21.783 回答