从多个子查询返回一个单行

时间:2019-02-17 13:39:42

标签: sql sql-server

我有一个查询,可以在特定时间的特定比赛中从多个赌徒那里获得所有赔率。我想为每项匹配的所有相关信息返回一行。

当前查询

SELECT DISTINCT  f.fixtureDate,
        (SELECT team FROM bet.team WHERE teamId = homeTeam),
        (SELECT team FROM bet.team WHERE teamId = awayTeam),
        (SELECT oddValue WHERE bookieID = '5D9130BD-00E6-4D72-BFC5-01DA15CA4CE8' AND direction = '599065F7-96A9-4DF5-8577-2203BE9AA2E0') AS 'company1H',
        (SELECT oddValue WHERE bookieID = '5D9130BD-00E6-4D72-BFC5-01DA15CA4CE8' AND direction = '5C27D6EF-DC8E-4A82-B18C-3139E3884691') AS 'company1D',
        (SELECT oddValue WHERE bookieID = '5D9130BD-00E6-4D72-BFC5-01DA15CA4CE8' AND direction = 'ABD92DB9-58D8-4C60-8A34-4C00EBBAB4A5') AS 'company1A'
FROM bet.betEntry be JOIN bet.fixture f ON be.fixtureId = f.fixtureId 
WHERE fixtureDate = CONVERT(VARCHAR, '2018/12/08 16:00',120) and lastUpdated = CONVERT(VARCHAR, '2018/12/08 16:00',120)

当前输出:

|---------------------|------------------|---------------------|------------------|---------------------|------------------|
|         Date        |     Home Team    |      Away Team      |     company1H    |      company1D      |     company1A    |
|---------------------|------------------|---------------------|------------------|---------------------|------------------|
|    08/12/18 16:00   |      Arsenal     |     HuddersField    |       1.55       |         NULL        |       NULL       |
|---------------------|------------------|---------------------|------------------|---------------------|------------------|
|    08/12/18 16:00   |      Arsenal     |     HuddersField    |       NULL       |         2.10        |       NULL       |
|---------------------|------------------|---------------------|------------------|---------------------|------------------|
|    08/12/18 16:00   |      Arsenal     |     HuddersField    |       NULL       |         NULL        |       5.20       |
|---------------------|------------------|---------------------|------------------|---------------------|------------------|

如何组合输出,使输出看起来像下面的示例

所需的输出:

|---------------------|------------------|---------------------|------------------|---------------------|------------------|
|         Date        |     Home Team    |      Away Team      |     company1H    |      company1D      |     company1A    |
|---------------------|------------------|---------------------|------------------|---------------------|------------------|
|    08/12/18 16:00   |      Arsenal     |     HuddersField    |       1.55       |         2.10        |       5.20       |
|---------------------|------------------|---------------------|------------------|---------------------|------------------|

1 个答案:

答案 0 :(得分:0)

您需要条件聚合。我认为这是查询:

SELECT f.fixtureDate, th.team, ta.team,
       MAX(CASE WHEN bookieID = '5D9130BD-00E6-4D72-BFC5-01DA15CA4CE8' AND direction = '599065F7-96A9-4DF5-8577-2203BE9AA2E0'
                THEN oddValue
           END) AS company1H,
       MAX(CASE WHEN bookieID = '5D9130BD-00E6-4D72-BFC5-01DA15CA4CE8' AND direction = '5C27D6EF-DC8E-4A82-B18C-3139E3884691'
                THEN oddValue
           END) AS company1D,
       MAX(CASE WHEN bookieID = '5D9130BD-00E6-4D72-BFC5-01DA15CA4CE8' AND direction = 'ABD92DB9-58D8-4C60-8A34-4C00EBBAB4A5'
                THEN oddValue
           END) AS company1A
FROM bet.betEntry be JOIN
     bet.fixture f
     ON be.fixtureId = f.fixtureId LEFT JOIN
     bet.team th
     ON ?.teamId = th.homeTeam LEFT JOIN
     bet.team ta
     ON ?.teamId = ta.awayTeam
WHERE ?.fixtureDate = '2018-12-08 16:00' AND
      ?.lastUpdated = '2018-12-08 16:00'
GROUP BY f.fixtureDate, th.team, ta.team;

注意:

  • 我不明白您为什么要使用日期格式将字符串转换为字符串以与日期进行比较。看起来真的很复杂。
  • 您应限定所有列名称。我在查询中放入了?.,以确定需要它们的地方。
  • 有条件的聚合应该做您想要的事。