SQLite - 组合跨表的列

时间:2018-02-01 21:16:18

标签: sqlite

我正在尝试编写自定义SQL报告,我非常接近。我无法理解最后一点。

基本上,该报告最终会显示未经批准的软件。以下是我几乎正在使用的代码。我试图将所有计算机和用户放在1个字段中。我可以单独使用它,但只是不确定如何正确地group_concat。这就是我所拥有的:

select
    Applications.Name 'Application',
    Applications.Version 'Version',
    count(Computers.Name) 'Count',
    Applications.Uninstall 'Uninstall String',
    case
        WHEN Computers.CurrentUser = '' THEN group_concat(Computers.Name, ', ')
        ELSE Computers.Name || ' (' || Computers.CurrentUser || '), '
    END AS 'Computer/Users'
FROM Computers inner join Applications on Computers.ComputerID=Applications.ComputerID
where Applications.Name LIKE '%McAfee%'
group by Applications.Name

我认为我的问题是ELSE Computers.Name || ' (' || Computers.CurrentUser || '), ',但我不确定如何使用group_concat或其他内容。

输出:

+------------------------------------------+-------------+-------+------------------------------------------------------------------------+------------------------------------------------+
|              "McAfee Agent"              | "5.0.5.658" | "334" | "MsiExec.exe /qn /norestart /X{265FA622-A254-49fb-B380-D9EF9ABFD32D}"  | "Computer1, Computer2, Computer3, Computer4, " |
+------------------------------------------+-------------+-------+------------------------------------------------------------------------+------------------------------------------------+
| "McAfee Host Intrusion Prevention"       | "8.00.0000" | "129" | "MsiExec.exe /qn /norestart /X{D2B9C003-A3CD-44A0-9DE5-52FE986C03E5}"  | "Computer1 (Computer1\User1), "                |
| "McAfee Management of Native Encryption" | "4.0.0.84"  | "9"   | ""                                                                     | "Computer27 (DOMAIN\Userasdasd), "             |
| "McAfee VirusScan Enterprise"            | "8.8.09000" | "334" | "MsiExec.exe /qn /norestart /X{CE15D1B6-19B6-4D4D-8F43-CF5D2C3356FF}"  | "Computer1, Computer2, Computer3, Computer4, " |
+------------------------------------------+-------------+-------+------------------------------------------------------------------------+------------------------------------------------+

我希望得到Computer1(用户),Computer2(User2)等......我切断了计算机列表,因为它很庞大。

1 个答案:

答案 0 :(得分:1)

group_concat()适用于您作为参数提供的任何内容。

如果你想拥有"计算机(用户)"连接的值,您必须在group_concat()

中写入它们
group_concat(CASE WHEN Computers.CurrentUser = ''
                  THEN Computers.Name
                  ELSE Computers.Name || ' (' || Computers.CurrentUser || ')'
             END,
             ', ')