想要在加入时只获得一行和其他行空白行

时间:2013-12-07 08:28:49

标签: sql sql-server tsql join

我有一个名为tableA的表:

Col1            Col2           Col3
------------------------------------
A25             45              12
A25             45              12
A25             45              12
B26             10              20

另一个名为tableB的表:

Col1                     Col2
------------------------------
A25                       D1
A25                       D2
A25                       D3
B26                       D4

当我在SQL(T-SQL)中使用tableA.Col1 = tableB.Col1加入这两个表时,我只希望获得等于左表行的第一行而其他表是空白

我用自己的话加入查询是:

Select
  tableA.*, -- IF THERE ARE NO DUPLICATES,
            -- BUT PUT BLANKS FOR SUCCESSIVE ROWS IF THERE ARE DUPLICATES
  tableB.Col2
from tableA JOIN tableB
where tableA.Col1 = tableB.Col1

换句话说,我想要像这样的结果

Colm1       Colm2       Colm3          Colm4
-------------------------------------------- 
A25          45           12             D1   
                                         D2
                                         D3
B26          10          20              D4 

感谢您的帮助

4 个答案:

答案 0 :(得分:3)

看起来您正在处理表示层。 SQL Server并不擅长这一点,但可以做到。请务必使用Order By以确保您拥有正确的行顺序。请参阅此SQL Fiddle

Select 
 Colm1 = Case When RowId = 1 Then TableA.Col1 Else '' End
  ,Colm2 = Case When RowId = 1 Then Convert(varchar, TableA.Col2) Else '' End
  ,Colm3 = Case When RowId = 1 Then Convert(varchar, TableA.Col3) Else '' End
  ,Colm4 = TableB.Col2
From TableA
  Join
    (
      Select Col1
        ,Col2
        ,RowId = Row_Number() Over (Partition By Col1 Order By Col2)
      From TableB
    ) TableB
    On TableA.Col1 = TableB.Col1
Group By   TableA.Col1
  ,Case When RowId = 1 Then TableA.Col1 Else '' End
  ,Case When RowId = 1 Then Convert(varchar, TableA.Col2) Else '' End
  ,Case When RowId = 1 Then Convert(varchar, TableA.Col3) Else '' End
  ,TableB.Col2
Order By TableA.Col1, TableB.Col2

更新:现在显示空白而不是空值

答案 1 :(得分:0)

尝试此查询:

SELECT  x.*, y.Col2, y.Col3
FROM
(
    SELECT  b.Col1, b.Col2, 
        ROW_NUMBER() OVER(PARTITION BY b.Col1 ORDER BY b.Col2) RowNum
    FROM    dbo.TableB b
) x
OUTER APPLY
(
    SELECT  TOP(1) a.Col2, a.Col3
    FROM    dbo.TableA a
    WHERE   x.RowNum = 1
    AND x.Col1 = a.Col1
) y

答案 2 :(得分:0)

试试这个SQLFiddle

SELECT
CASE WHEN B.Row = 1 THEN A.Col1 END A,
CASE WHEN B.Row = 1 THEN A.Col2 END B,
CASE WHEN B.Row = 1 THEN A.Col3 END C,
B.Col2 D

FROM
(SELECT  Col1,Col2,Col3
 FROM Table1
 Group By Col1,Col2,Col3
) AS A
INNER JOIN
(
SELECT Col1, Col2 ,
ROW_NUMBER() OVER(PARTITION BY Col1 ORDER BY Col1) AS ROW FROM Table2
) AS B
ON A.Col1 = B.Col1

答案 3 :(得分:0)

这应该有效:

SELECT 
    case when row_number() over(partition by a.col1 order by a.col1)=1 then a.col1 else ' ' end, 
    case when row_number() over(partition by a.col1 order by a.col1)=1 then convert(varchar,a.col2) else ' ' end, 
    case when row_number() over(partition by a.col1 order by a.col1)=1 then convert(varchar,a.col3) else ' ' end, 
    b.col2
FROM(select distinct col1, col2, col3 from tableA) a
INNER JOIN tableB b 
ON(a.col1=b.col1)