选择某些字段仅出现在多行中的行

时间:2017-01-05 16:56:38

标签: sql-server sql-server-2008 tsql

我想从多个表中提取信息,但只想在一个人有多个条目时显示行,即多行信息

这是代码。我一直在使用HAVING count(person)> 1使用各种方法,但似乎无法正确使用。

非常感谢。

SELECT DISTINCT
          table1.person
        , table1.id
        , table1.type
        , table1.active
        , table2.stage
        , table3.start
        , table3.end

FROM
          people table1
        , storyType table2
        , storyContract table3

WHERE
        table1.type = table2.type_id
    AND table2.stage = 1
    AND table1.id = table3.contract_id
    AND table3.start > '2015-01-01 00:00:00'

目前的结果是

person          id      start               end                 type    active  stage

Keith Richards  202971  24/11/2015 00:00    07/03/2016 00:00    8       1       Amber
Keith Richards  218325  07/03/2016 00:00    07/04/2016 00:00    10      1       Red
Steve Perryman  217788  02/03/2016 00:00    04/07/2016 00:00    8       1       Amber
Cyril Knowles   202438  20/11/2015 00:00    24/06/2016 00:00    10      1       Red
Pat Jennings    215324  11/02/2016 00:00    29/08/2016 00:00    8       1       Amber
Alan Gilzean    200575  06/11/2015 00:00    08/11/2015 00:00    8       1       Amber
Bill Wyman      203575  27/11/2015 00:00    14/01/2016 00:00    8       1       Amber
Bill Wyman      209740  14/01/2016 00:00    10/03/2016 00:00    9       1       Green
Bill Wyman      219330  11/03/2016 00:00    01/09/2016 00:00    10      1       Red
Mike England    209288  12/01/2016 12:54    01/02/2016 12:54    8       1       Amber
Charlie Watts   198363  14/10/2015 12:40    05/11/2015 00:00    8       1       Amber
Charlie Watts   200281  05/11/2015 00:00    13/06/2016 00:00    10      1       Red
Brian Jones     208265  06/01/2016 14:38    04/02/2016 00:00    8       1       Amber
Brian Jones     214052  04/02/2016 00:00    17/03/2016 00:00    9       1       Green
Brian Jones     220425  17/03/2016 00:00    04/07/2016 00:00    10      1       Red
Martin Chivers  209195  12/01/2016 00:00    04/07/2016 00:00    8       1       Amber
Alan Mullery    212919  29/01/2016 00:00    04/07/2016 00:00    8       1       Amber
Mick Jagger     199134  20/10/2015 00:00    17/12/2015 00:00    8       1       Amber
Mick Jagger     212690  28/01/2016 00:00    24/06/2016 00:00    8       1       Amber
Martin Peters   195833  30/09/2015 00:00    04/07/2016 00:00    8       1       Amber

想要的结果

person          id      start               end                 type    active  stage

Keith Richards  202971  24/11/2015 00:00    07/03/2016 00:00    8       1       Amber
Keith Richards  218325  07/03/2016 00:00    07/04/2016 00:00    10      1       Red
Bill Wyman      203575  27/11/2015 00:00    14/01/2016 00:00    8       1       Amber
Bill Wyman      209740  14/01/2016 00:00    10/03/2016 00:00    9       1       Green
Bill Wyman      219330  11/03/2016 00:00    01/09/2016 00:00    10      1       Red
Charlie Watts   198363  14/10/2015 12:40    05/11/2015 00:00    8       1       Amber
Charlie Watts   200281  05/11/2015 00:00    13/06/2016 00:00    10      1       Red
Brian Jones     208265  06/01/2016 14:38    04/02/2016 00:00    8       1       Amber
Brian Jones     214052  04/02/2016 00:00    17/03/2016 00:00    9       1       Green
Brian Jones     220425  17/03/2016 00:00    04/07/2016 00:00    10      1       Red
Mick Jagger     199134  20/10/2015 00:00    17/12/2015 00:00    8       1       Amber
Mick Jagger     212690  28/01/2016 00:00    24/06/2016 00:00    8       1       Amber

4 个答案:

答案 0 :(得分:1)

您可以调整TheGameiswar的解决方案: 使用分析函数count而不是row_number

;With cte
as
(
--this can be your join logic.Replace everything except count(*) from join
select 
*,count(*) over (partition by person) as PersonCount
from
table
)
select * from cte where PersonCount>1

答案 1 :(得分:0)

;With cte
as
(
--this can be your join logic.Replace everything except rownum from join
select 
*,row_number() over (partition by person order by (select null)) as rownum
from
table
)
select * from cte where rownum>1

答案 2 :(得分:0)

使用having语句获取具有计数条件的结果,然后加入到该结果中。

示例:

DECLARE @Orders TABLE (Person VARCHAR(32), Ord VARCHAR(128));

INSERT INTO @Orders (Person, Ord) VALUES ('Brett', 'Shirt'), ('Brett', 'Pants'), ('Mary', 'Dress')

--Everything as is
Select *
From @Orders


-- getting ordersOver2
; With ordersOver2 as 
    (
    Select 
        Person
    ,   COUNT(Ord) AS cnt
    FROM @Orders
    GROUP BY Person
    HAVING COUNT(Ord) > 1
    )
-- relating this to main set
Select a.*
From @Orders a
    JOIN ordersOver2 b ON b.Person = a.Person

答案 3 :(得分:0)

找到重复person行并将其过滤掉的表格,可能您的storyContract表格已重复person

SELECT /* DISTINCT <- no need for distinct probably */
          table1.person
        , table1.id
        , table1.type
        , table1.active
        , table2.stage
        , table3.start
        , table3.end

FROM
          people table1
        , storyType table2
        , storyContract table3

WHERE
        table1.type = table2.type_id
    AND table2.stage = 1
    AND table1.id = table3.contract_id
    AND table3.start > '2015-01-01 00:00:00'
    -- ----- additional filter ----------------------
    AND Exists(
          SELECT  1
          FROM    storyContract as c
          WHERE   c.contract_id = table1.id
          HAVING  COUNT(1) > 1
    )
    -- ----------------------------------------------