必须只显示两行作为以下数据的输出

时间:2015-09-03 14:47:02

标签: sql sql-server

我有这样的数据,这里有两行是重复的,我不会考虑哪个列。我的问题是我想只显示两行,其中包括whichclaim列。

输入:

 whichclaim     ClaimSummaryUID         matchingclaimUID        ClaimLineUID    ClaimNumber     LoadMonth
    Ref         20151041213802201505    20151070050702201505    2306110201505   201510412138    201505
    Ref         20151070050702201505    20151041213802201505    2306099201505   201510700507    201505
    Target      20151041213802201505    20151070050702201505    2306110201505   201510412138    201505
    Target      20151070050702201505    20151041213802201505    2306099201505   201510700507    201505

输出:

  whichclaim    ClaimSummaryUID         matchingclaimUID        ClaimLineUID    ClaimNumber     LoadMonth
    Target      20151041213802201505    20151070050702201505    2306110201505   201510412138    201505
    Ref         20151070050702201505    20151041213802201505    2306099201505   201510700507    201505

我需要任何一行应该是目标而其他应该是参考。 这个你能帮我吗。先谢谢你。

2 个答案:

答案 0 :(得分:0)

试试这个

WITH CTE AS
(
SELECT whichclaim ,ClaimSummaryUID,matchingclaimUID ,ClaimLineUID,ClaimNumber,LoadMonth,ROW_NUMBER() OVER(PARTITION BY whichclaim order by ClaimSummaryUID) As Row_Count
FROM Table_Name
)
SELECT * From CTE 
WHERE whichclaim ='Ref' AND Row_Count = 1
UNION 
SELECT * From CTE 
WHERE whichclaim ='Target' AND Row_Count = 2

答案 1 :(得分:0)

这将有效

SELECT 
    c.WhichClaim
    ,c.ClaimSummaryUID
    ,c.MatchingClaimUID
    ,c.ClaimLineUID
    ,c.ClaimNumber
    ,c.LoadMonth
FROM ClaimTable AS c
JOIN 
 (
  SELECT
      WhichClaim,
      CASE WhichClaim
          WHEN 'Ref' THEN MAX(ClaimSummaryUID)
          ELSE MIN(ClaimSummaryUID) 
      END as ClaimSummaryUID
  FROM ClaimTable
  GROUP BY WhichClaim
 )AS t ON c.WhichClaim = t.WhichClaim AND c.ClaimSummaryUID = t.ClaimSummaryUID

http://sqlfiddle.com/#!9/16432/6