从两个不同的查询中获取信息

时间:2016-05-03 10:25:12

标签: sql

这是我现在正在使用的查询

他们是:

SELECT Filetype AS 'Tipo do ficheiro', 
    ((COUNT(Filetype) * 100) / (SELECT COUNT(*) FROM infofile)) AS 'Percentagem (%)',
    NULL AS 'Total(KB)'
    FROM infofile 
    GROUP BY Filetype
    UNION ALL
    SELECT NULL,
    ((COUNT(Filetype) * 100) / (SELECT COUNT(*) FROM infofile)), 
    SUM(Filesize)
    FROM infofile

我想在此处更改的是将此查询的结果包含在主要

中的可能性
    SELECT Filetype, SUM(Filesize) AS 'Total(KB)'
FROM infofile
GROUP BY Filetype

这是我目前的输出。我要添加的是每个扩展的大小。例如.exe - 225

但是使用那个SELECT enter image description here

3 个答案:

答案 0 :(得分:2)

如果它是所有内容的SUM,那么只需将其放入子查询中,而不与主查询进行任何连接。

还修改了Percentagem中的括号,小心BODMAS。

SELECT Filetype, 
((COUNT(Filetype) * 100) / (SELECT COUNT(*) FROM infofile)) AS Percentagem,
(SELECT SUM(Filesize) FROM infofile) AS 'Total (KB)'
FROM infofile 
GROUP BY Filetype

SUM作为自己的行..

SELECT NULL,
NULL, 
SUM(Filesize)
FROM infofile 
UNION ALL
SELECT Filetype, 
((COUNT(Filetype) * 100) / (SELECT COUNT(*) FROM infofile)) AS Percentagem,
NULL AS 'Total (KB)'
FROM infofile 
GROUP BY Filetype

第二个查询将输出如下内容:

Filetype Percentagem Total (KB)
NULL     NULL        7894561
1        10%         NULL
2        20%         NULL
3        30%         NULL
4        40%         NULL

答案 1 :(得分:0)

select Filetype, 
      (COUNT(Filetype) * 100 / (SELECT COUNT(*) FROM infofile)) AS 'Percentagem', 
      SUM(Filesize)  AS 'Total (KB)' 
FROM  infofile 
GROUP BY Filetype

答案 2 :(得分:0)

你能尝试一下。

SELECT A.Filetype, 
    ((COUNT(A.Filetype) * 100) / (SELECT COUNT(*) FROM infofile)) AS  Percentagem,
     B.filesize AS Totalkb
    FROM infofile A,
    (SELECT SUM(Filesize) filesize FROM infofile) B
    GROUP BY Filetype,Totalkb