Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 一次查询多个结果_Sql_Sql Server - Fatal编程技术网

Sql 一次查询多个结果

Sql 一次查询多个结果,sql,sql-server,Sql,Sql Server,有没有更优雅的方式来做这类事情?我有一个仪表板,我正试图填充信息,我只需要一些计数。如果可能的话,我想在一个查询中完成这一切,但是下面的这个查询看起来很愚蠢。如有任何建议,将不胜感激 select UserCount = ( select count(*) from net_ou inner join art_asset on net_ou.net_ouid = art_asset.net_ouid inner join idb_interfa

有没有更优雅的方式来做这类事情?我有一个仪表板,我正试图填充信息,我只需要一些计数。如果可能的话,我想在一个查询中完成这一切,但是下面的这个查询看起来很愚蠢。如有任何建议,将不胜感激

    select 
UserCount = (
    select count(*) from net_ou 
        inner join art_asset on net_ou.net_ouid = art_asset.net_ouid
        inner join idb_interface on idb_interface.art_asset_id = art_asset.art_asset_id
        inner join nsd_interfacecode on nsd_interfacecode.nsd_interfacecodeid = idb_interface.nsd_interfacecodeid
        inner join nsd_interfacetype on nsd_interfacetype.nsd_interfacetypeid = idb_interface.nsd_interfacetypeid
        where net_ou.displayname = 'abcd' and nsd_interfacecode.code = 1 and nsd_interfacetype.physical = 1),
PrinterCount = (
    select count(*) as PrinterCount from net_ou 
        inner join art_asset on net_ou.net_ouid = art_asset.net_ouid
        inner join idb_interface on idb_interface.art_asset_id = art_asset.art_asset_id
        inner join nsd_interfacecode on nsd_interfacecode.nsd_interfacecodeid = idb_interface.nsd_interfacecodeid
        inner join nsd_interfacetype on nsd_interfacetype.nsd_interfacetypeid = idb_interface.nsd_interfacetypeid
        where net_ou.displayname = 'abcd' and nsd_interfacecode.code = 2 and nsd_interfacetype.physical = 1),
TrunkCount = (
    select count(*) as TrunkCount from net_ou 
        inner join art_asset on net_ou.net_ouid = art_asset.net_ouid
        inner join idb_interface on idb_interface.art_asset_id = art_asset.art_asset_id
        inner join nsd_interfacecode on nsd_interfacecode.nsd_interfacecodeid = idb_interface.nsd_interfacecodeid
        inner join nsd_interfacetype on nsd_interfacetype.nsd_interfacetypeid = idb_interface.nsd_interfacetypeid
        where net_ou.displayname = 'abcd' and nsd_interfacecode.code = 4 and nsd_interfacetype.physical = 1)
假设我在查询中没有遗漏任何不同的内容,那么应该会给出您的值

假设我在查询中没有遗漏任何不同的内容,那么这应该会为您提供您的值。

您可以使用此用例:

select 
        SUM(CASE WHEN nsd.interfacecode.code = 1 THEN 1 ELSE 0 END) as UserCount,
        SUM(CASE WHEN nsd.interfacecode.code = 2 THEN 1 ELSE 0 END) as PrinterCount, 
        SUM(CASE WHEN nsd.interfacecode.code = 4 THEN 1 ELSE 0 END) as TrunkCount 
    from net_ou 
        inner join art_asset on net_ou.net_ouid = art_asset.net_ouid
        inner join idb_interface on idb_interface.art_asset_id = art_asset.art_asset_id
        inner join nsd_interfacecode on nsd_interfacecode.nsd_interfacecodeid = idb_interface.nsd_interfacecodeid
        inner join nsd_interfacetype on nsd_interfacetype.nsd_interfacetypeid = idb_interface.nsd_interfacetypeid
    where net_ou.displayname = 'abcd' 
    and nsd_interfacetype.physical = 1
您可以使用以下用例:

select 
        SUM(CASE WHEN nsd.interfacecode.code = 1 THEN 1 ELSE 0 END) as UserCount,
        SUM(CASE WHEN nsd.interfacecode.code = 2 THEN 1 ELSE 0 END) as PrinterCount, 
        SUM(CASE WHEN nsd.interfacecode.code = 4 THEN 1 ELSE 0 END) as TrunkCount 
    from net_ou 
        inner join art_asset on net_ou.net_ouid = art_asset.net_ouid
        inner join idb_interface on idb_interface.art_asset_id = art_asset.art_asset_id
        inner join nsd_interfacecode on nsd_interfacecode.nsd_interfacecodeid = idb_interface.nsd_interfacecodeid
        inner join nsd_interfacetype on nsd_interfacetype.nsd_interfacetypeid = idb_interface.nsd_interfacetypeid
    where net_ou.displayname = 'abcd' 
    and nsd_interfacetype.physical = 1

什么版本的sql server?如果是2008+,签出分组将设置sql server的哪个版本?如果是2008+,请查看分组集这对性能是否有任何负面影响?乍一看,似乎有可能,但我不是大师。@Harper-不,它实际上应该比最初的运行速度快很多。它只返回一次连接的结果集,然后对每一行的.code字段求值。@Harper Shelby-这肯定会比原始查询快,我知道它会比原始查询快。我很好奇它与我的相比如何,我的没有给出漂亮的列名,但将计数放在结果的行中。@Harper-我怀疑它会比你的更快,因为你使用的是需要排序的组。这对性能有负面影响吗?乍一看,似乎有可能,但我不是大师。@Harper-不,它实际上应该比最初的运行速度快很多。它只返回一次连接的结果集,然后对每一行的.code字段求值。@Harper Shelby-这肯定会比原始查询快,我知道它会比原始查询快。我很好奇它与我的相比如何,我的没有给出漂亮的列名,但将计数放在结果的行中。@Harper-我怀疑它会比你的更快,因为你使用的是一个需要排序的组。