SQL Server-选择每种组合

时间:2019-02-21 20:10:12

标签: sql sql-server tsql

我们在下面有两个表格,我正在尝试编写一个查询,该查询将为团队中的每个人选择“每个人都购买”。例如,它应该显示PersonA与PurchaseID 1和2关联,因为它们与TeamA在同一团队中。

这可能吗?我以为交叉连接会起作用,但是似乎带来了太多的专栏。我正在运行SQL Server。

谢谢

购买

1.Patient Name: abcd
    Test Name: GlucoseTest     Result: 24    Ref.Range: 10-30 mg/dl
    Test Name: TSH             Result: 0.5   Ref.Range: 0.1-0.9 mg/dl

2.Parient Name: efgh
    Test Name: GlucoseTest     Result: 24    Ref.Range: 10-30 mg/dl
    Test Name: TSH             Result: 0.5   Ref.Range: 0.1-0.9 mg/dl
    Test Name: GlucoseTest     Result: 24    Ref.Range: 10-30 mg/dl
    Test Name: TSH             Result: 0.5   Ref.Range: 0.1-0.9 mg/dl

3.Parient Name: efgh
    Test Name: GlucoseTest     Result: 24    Ref.Range: 10-30 mg/dl
.
.
.

团队

| PurchaseID    | PersonID  |
|------------   |---------- |
| 1             | TeamA     |
| 2             | TeamA     |
| 3             | PersonA   |
| 4             | PersonB   |
| 5             | TeamB     |

预期结果(在PurchaseID 1上进行过滤时):

| TeamID    | PersonID  |
|--------   |---------- |
| 1         | PersonA   |
| 1         | TeamA     |
| 1         | PersonC   |
| 2         | PersonB   |
| 2         | TeamB     |

2 个答案:

答案 0 :(得分:0)

您的数据结构有点奇怪,但我想我了解您想要的。

如果PersonA进行了购买,而PersonA在TeamA上,那么应该显示TeamA上的每个人都与购买相关,对吗?就像“我为团队购买了这些甜甜圈,所以团队中的每个人都得到了甜甜圈”。

您可能想要的是,想要在PersonID上加入Purchase to Team。但是,然后使用内联表值函数中的CROSS APPLY函数来返回与“当前行”中的人员在同一团队中的所有人员。

我使用了两个常见的表表达式来表示您的表,因此我可以运行它。您只需要SELECT部分​​:

with Purchases as (
      select 1 as PurchaseID, 'TeamA'   as PersonID
union select 2 as PurchaseID, 'TeamA'   as PersonID
union select 3 as PurchaseID, 'PersonA' as PersonID
union select 4 as PurchaseID, 'PersonB' as PersonID
union select 5 as PurchaseID, 'TeamB'   as PersonID
)
, Teams as (
      select 1 as TeamID, 'PersonA' as PersonID
union select 1 as TeamID, 'TeamA'   as PersonID
union select 1 as TeamID, 'PersonC' as PersonID
union select 2 as TeamID, 'PersonB' as PersonID
union select 2 as TeamID, 'TeamB'   as PersonID
)
select Purchases.PurchaseID
     , EveryTeamMember.PersonID
  from Purchases
  join Teams
    on Teams.PersonID = Purchases.PersonID
cross apply (
             select PersonID
               from Teams InnerTable
              where InnerTable.TeamID = Teams.TeamID
            ) as EveryTeamMember
where Purchases.PurchaseID = 1

答案 1 :(得分:0)

如果您要在PersonID以Team开头时获得所有团队成员,那么我认为您应该对以Team和UNION 开头的所有PersonID进行交叉申请(不是UNION全部)单人购买:

DECLARE @Purchases TABLE (
    PurchaseID INT,
    PersonID Varchar(50)
)
INSERT INTO @Purchases(PersonID,PurchaseID) VALUES ('TeamA', 1);
INSERT INTO @Purchases(PersonID,PurchaseID) VALUES ('TeamA', 2);
INSERT INTO @Purchases(PersonID,PurchaseID) VALUES ('PersonA', 3);
INSERT INTO @Purchases(PersonID,PurchaseID) VALUES ('PersonB', 4);
INSERT INTO @Purchases(PersonID,PurchaseID) VALUES ('TeamB', 5);



DECLARE @Teams TABLE (
    TeamID INT,
    PersonID Varchar(50)
)
INSERT INTO @Teams(PersonID,TeamID) VALUES ('PersonA', 1);
INSERT INTO @Teams(PersonID,TeamID) VALUES ('TeamA', 1);
INSERT INTO @Teams(PersonID,TeamID) VALUES ('PersonC', 1);
INSERT INTO @Teams(PersonID,TeamID) VALUES ('PersonB', 2);
INSERT INTO @Teams(PersonID,TeamID) VALUES ('TeamB', 2);


SELECT T1.PurchaseID,TeamPersons.PersonID
FROM @Purchases T1
INNER JOIN @Teams T2
ON T2.PersonID = T1.PersonID AND T1.PersonID LIKE'Team%'
CROSS APPLY (
             SELECT PersonID
               FROM @Teams T3
              WHERE T3.TeamID = T2.TeamID
            ) AS TeamPersons

UNION 

SELECT T1.PurchaseID
     , T1.PersonID
  FROM @Purchases T1
WHERE T1.PersonID NOT LIKE 'Team%'

结果

enter image description here