tql select语句中的子查询

时间:2015-05-21 10:19:44

标签: sql tsql stored-procedures sql-server-2008-r2

我有一个列出用户的查询,但它需要一个子查询来获取它们今天是否可用。我需要第四栏'可用性'迭代每个用户并显示它们是否具有可用性或显示为空。我已经尝试了所有我能想到的子查询,游标等等,但没有快乐。欢迎任何指示!

SELECT inter.authno,
       inter.FirstName,
       inter.Surname,
       COALESCE(( 
                 Select at.typeName as [Availability]
                 FROM [database].[dbo].[Interviewer] inter 
                 full join [database].[dbo].[availability] av 
                 on inter.authno = av.authno
                 full join [database].[dbo].[availability_days] ad 
                 on av.availID = ad.availID
                 full join [database].[dbo].[availibiltyType] at
                 on av.typeID = at.typeid
                 where exists(
                      select authno 
                      from [database].[dbo].[Interviewer]
                 )
                 and ad.actualDay = '2015-05-21'
                 ), null ) AS [Availability]

 FROM [database].[dbo].[Interviewer] inter

查询给出了以下结果,但它应该只显示为Harry Kane可用,其余的应为null。

authno FirstName Surname Availability
-------------------------------------
10     Minch     Yoda    Available 
11     Darth     Vadar   Available 
12     Darth     Maul    Available 
14     Obi Wan   Kenobi  Available
15     Qui-Gon   Jinn    Available
16     Darth     Sidious Available
17     Boba      Fett    Available
24     Harry     Kane    Available
39     mark      o'neill Available

我还尝试了下面提供的代码建议,它提供了我需要的一些结果,但它显示了所有结果,而不是今天的可用性类型。

        SELECT 
    inter.authno,
    inter.FirstName,
    inter.Surname,
    at.typeName as [Availability]

    FROM [database].[dbo].[Interviewer] inter 

    left JOIN [database].[dbo].[availability] av
    on inter.authno = av.authno

    left JOIN [database].[dbo].[availability_days] ad 
    on av.availID = ad.availID 
    and ad.actualDay = '2015-07-21' 

    left JOIN [database].[dbo].[availibiltyType] at 
    on av.typeID = at.typeid

输出:

  +----+---------+-----------+--------------+
        | 10 |  Minch  |   Yoda    |     NULL     |
        +----+---------+-----------+--------------+
        | 11 | Darth   | Vadar     | NULL         |
        | 12 | Darth   | Maul      | NULL         |
        | 13 | Luke    | Skywalker | NULL         |
        | 14 | Obi Wan | Kenobi    | NULL         |
        | 15 | Qui-Gon | Jinn      | Annual Leave |
        | 16 | Darth   | Sidious   | NULL         |
        | 17 | Boba    | Fett      | UO           |
        | 17 | Boba    | Fett      | Available    |
        | 18 | test22  | test33    | NULL         |
        | 19 | test7   | test7     | NULL         |
        | 22 | Bob     | Marley    | NULL         |
        | 23 | JO      | JO        | NULL         |
        | 24 | Harry   | Kane      | Annual Leave |
        | 24 | Harry   | Kane      | Available    |
        | 24 | Harry   | Kane      | Available    |
        | 24 | Harry   | Kane      | Annual Leave |
        | 24 | Harry   | Kane      | Annual Leave |
        | 24 | Harry   | Kane      | NW           |
        | 24 | Harry   | Kane      | NW           |
        | 24 | Harry   | Kane      | Available    |
        | 39 | mark    | o'neill   | US           |
        +----+---------+-----------+--------------+

我也尝试了下面的内容,它为我提供了我只需要的确切结果,我需要显示所有用户是否在表中有日期。即如果我改变日期,哈利凯恩就会消失。

        SELECT 
    inter.authno,
    inter.FirstName,
    inter.Surname,
    at.typeName as [Availability]

    FROM [database].[dbo].[Interviewer] inter 

    left JOIN [database].[dbo].[availability] av
    on inter.authno = av.authno

    left JOIN [database].[dbo].[availability_days] ad 
    on av.availID = ad.availID 

    left JOIN [database].[dbo].[availibiltyType] at 
    on av.typeID = at.typeid

            where ad.actualDay = '2015-05-21'or ad.actualDay is null

今天的输出:

    +--------+-----------+-----------+--------------+
    | authno | FirstName |  Surname  | Availability |
    +--------+-----------+-----------+--------------+
    |     10 | Minch     | Yoda      | NULL         |
    |     11 | Darth     | Vadar     | NULL         |
    |     12 | Darth     | Maul      | NULL         |
    |     13 | Luke      | Skywalker | NULL         |
    |     14 | Obi Wan   | Kenobi    | NULL         |
    |     16 | Darth     | Sidious   | NULL         |
    |     18 | test22    | test33    | NULL         |
    |     19 | test7     | test7     | NULL         |
    |     22 | Bob       | Marley    | NULL         |
    |     23 | JO        | JO        | NULL         |
    |     24 | Harry     | Kane      | Available    |
    +--------+-----------+-----------+--------------+

Output for 2015-05-10

+--------+-----------+-----------+--------------+
| authno | FirstName |  Surname  | Availability |
+--------+-----------+-----------+--------------+
|     10 | Minch     | Yoda      | NULL         |
|     11 | Darth     | Vadar     | NULL         |
|     12 | Darth     | Maul      | NULL         |
|     13 | Luke      | Skywalker | NULL         |
|     14 | Obi Wan   | Kenobi    | NULL         |
|     16 | Darth     | Sidious   | NULL         |
|     18 | test22    | test33    | NULL         |
|     19 | test7     | test7     | NULL         |
|     22 | Bob       | Marley    | NULL         |
|     23 | JO        | JO        | NULL         |
+--------+-----------+-----------+--------------+

2 个答案:

答案 0 :(得分:4)

如果我理解正确,您需要为您的查询版本提供相关子查询:

SELECT inter.authno,
       inter.FirstName,
       inter.Surname,
       (Select at.typeName as [Availability]
        FROM [database].[dbo].[availability] av join
             [database].[dbo].[availability_days] ad 
             on av.availID = ad.availID join
             [database].[dbo].[availibiltyType] at
             on av.typeID = at.typeid
        where inter.authno = av.authno and ad.actualDay = '2015-05-21'
       ) AS [Availability]
FROM [database].[dbo].[Interviewer] inter;

一些注意事项:

  • COALESCE(<x>, NULL)没有意义。只需使用<X>
  • 即可
  • 使用子查询,您应该使用IFNULL()而不是COALESCE(),因为SQL Server(我认为是)COALESCE()的有缺陷的实现。
  • 您的子查询需要与外部查询相关联。
  • 我不知道EXISTS条款应该做什么。如果表有任何行,那么它将始终返回TRUE。
  • 子查询中没有full join s的原因。
  • 我希望您的版本返回错误&#34;子查询返回多行&#34;。

答案 1 :(得分:2)

没有看到你的桌子结构这是一个猜测,但是一个受过教育的人。 试试这个:

SELECT inter.authno,
       inter.FirstName,
       inter.Surname,
       at.typeName as [Availability]
 FROM [database].[dbo].[Interviewer] inter 
 LEFT JOIN [database].[dbo].[availability] av on inter.authno = av.authno
 LEFT JOIN [database].[dbo].[availability_days] ad on av.availID = ad.availID and ad.actualDay = '2015-05-21'
 LEFT JOIN [database].[dbo].[availibiltyType] at on av.typeID = at.typeid

对于您可能遇到的未来sql问题,请包括相关表格DDL,一些示例数据(最好是DML语句)和所需的输出。