查询运行max和min

时间:2016-05-16 08:40:11

标签: sql sql-server tsql

所以我正在尝试运行一个查询,但是我遇到了一些问题,因为我正在使用nvarchar列来获取一个百分比列,它给出了我在不同数据数据库中的百分比有。该列被称为“Filetype”,我所拥有的是我放在那里的所有扩展名f.e:.exe,.zip等。

然后我认为我可以在同一个查询中获得MAXMIN百分比问题,因为数据类型值并不那么容易。我在Microsoft Visual Studio中进行了查询

SELECT 
Filetype AS [Extensão], 
COUNT(*) AS [Nº de ficheiros],
CAST(((COUNT(Filetype) * 100.0) / (SELECT COUNT(*) FROM infofile)) AS DECIMAL(10,2)) AS [Percentagem (%)],
SUM(Filesize) AS [Total(KB)],
NULL AS [Convertido para MB],
MIN(COUNT(*)) OVER () * 100.0 / (SUM(COUNT(*)) OVER ()) AS [Min. Percentagem (%)],
MAX(COUNT(*)) OVER () * 100.0 / SUM(COUNT(*)) OVER () AS [Max. Percentagem (%)]
FROM infofile  
GROUP BY Filetype
UNION ALL
SELECT '---------------',
COUNT('Nº de extensões'),
((COUNT(Filetype) * 100) / (SELECT COUNT(Filetype) FROM infofile)), 
SUM(Filesize),
SUM(Filesize) / 1024,
NULL,
NULL
FROM infofile

但是,如果我使用此查询,它将填充所有行,我想要它做的是UNION ALL之后的行。我现在将向您显示输出enter image description here

我希望显示MAXMIN,因为我会用箭头向您展示。 enter image description here

该行是我显示所有最终结果的地方。我希望通过添加MAXMIN

来更改它

您的查询结果 enter image description here

4 个答案:

答案 0 :(得分:0)

您可以将交叉连接中的逻辑复制到第二个查询中的数据,如下所示;

SELECT Filetype AS [Extensão]
    ,COUNT(*) AS [Nº de ficheiros]
    ,CAST((
            (COUNT(Filetype) * 100.0) / (
                SELECT COUNT(*)
                FROM infofile
                )
            ) AS DECIMAL(10, 2)) AS [Percentagem (%)]
    ,SUM(Filesize) AS [Total(KB)]
    ,NULL AS [Convertido para MB]
    ,MIN(COUNT(*)) OVER () * 100.0 / (SUM(COUNT(*)) OVER ()) AS [Min. Percentagem (%)]
    ,MAX(COUNT(*)) OVER () * 100.0 / SUM(COUNT(*)) OVER () AS [Max. Percentagem (%)]
FROM infofile
GROUP BY Filetype

UNION ALL
(
SELECT '---------------'
    ,COUNT('Nº de extensões')
    ,(
        (COUNT(Filetype) * 100) / (
            SELECT COUNT(Filetype)
            FROM infofile
            )
        )
    ,SUM(Filesize)
    ,SUM(Filesize) / 1024
    ,a.[Min. Percentagem (%)]
    ,a.[Max. Percentagem (%)]
FROM infofile i
CROSS JOIN
(SELECT 
MAX(a.[Min. Percentagem (%)]) [Min. Percentagem (%)]
,MAX(a.[Max. Percentagem (%)]) [Max. Percentagem (%)]
FROM
(SELECT Filetype AS [Extensão]
    ,COUNT(*) AS [Nº de ficheiros]
    ,CAST((
            (COUNT(Filetype) * 100.0) / (
                SELECT COUNT(*)
                FROM infofile
                )
            ) AS DECIMAL(10, 2)) AS [Percentagem (%)]
    ,SUM(Filesize) AS [Total(KB)]
    ,NULL AS [Convertido para MB]
    ,MIN(COUNT(*)) OVER () * 100.0 / (SUM(COUNT(*)) OVER ()) AS [Min. Percentagem (%)]
    ,MAX(COUNT(*)) OVER () * 100.0 / SUM(COUNT(*)) OVER () AS [Max. Percentagem (%)]
FROM infofile
GROUP BY Filetype) ft
) a
) 

答案 1 :(得分:0)

如果我正确地提出了您的问题,您需要在select语句中添加一个聚合行,您可以在其中派生聚合行的最小值和最大值。以下是使用#temp表可以执行的操作。此外,您可以在不需要时删除#temp表。

SELECT 
Filetype AS [Extensão], 
COUNT(*) AS [Nº de ficheiros],
CAST(((COUNT(Filetype) * 100.0) / (SELECT COUNT(*) FROM infofile)) AS DECIMAL(10,2)) AS [Percentagem (%)],
SUM(Filesize) AS [Total(KB)],
SUM(Filesize) / 1024 AS [Convertido para MB],
CAST(((COUNT(Filetype) * 100.0) / (SELECT COUNT(*) FROM infofile)) AS DECIMAL(10,2)) AS [Min. Percentagem (%)],
CAST(((COUNT(Filetype) * 100.0) / (SELECT COUNT(*) FROM infofile)) AS DECIMAL(10,2)) AS [Max. Percentagem (%)]
INTO #temp
FROM infofile  
GROUP BY Filetype;

SELECT *
FROM #temp
UNION ALL
SELECT '---------------',
SUM([Nº de ficheiros]),
SUM([Percentagem (%)]), 
SUM([Total(KB)]),
SUM([Total(KB)]) / 1024,
MIN([Min. Percentagem (%)]),
MAX([Max. Percentagem (%)])
FROM #temp;

DROP TABLE #temp;

如果这不成功,请告诉我。

答案 2 :(得分:0)

你可以这样做。

with cte as
(
SELECT 
Filetype AS [Extensão], 
COUNT(*) AS [Nº de ficheiros],
CAST(((COUNT(Filetype) * 100.0) / (SELECT COUNT(*) FROM infofile)) AS DECIMAL(10,2)) AS [Percentagem (%)],
SUM(Filesize) AS [Total(KB)],
NULL AS [Convertido para MB],
MIN(COUNT(*)) OVER () * 100.0 / (SUM(COUNT(*)) OVER ()) AS [Min. Percentagem (%)],
MAX(COUNT(*)) OVER () * 100.0 / SUM(COUNT(*)) OVER () AS [Max. Percentagem (%)]
FROM infofile  
GROUP BY Filetype
)
select [Extensão],[Nº de ficheiros],[Percentagem (%)],[Total(KB)],[Convertido para MB],NULL AS [Min. Percentagem (%)],NULL AS [Max. Percentagem (%)]   from cte
UNION ALL
SELECT '---------------',
COUNT('Nº de extensões'),
((COUNT(Filetype) * 100) / (SELECT COUNT(Filetype) FROM infofile)), 
SUM(Filesize),
SUM(Filesize) / 1024,
(Select MAX([Min. Percentagem (%)]) from cte) as [Min. Percentagem (%)] ,
(Select MAX([Max. Percentagem (%)] from cte as [Max. Percentagem (%)])
FROM infofile 

我没有做任何事情,只是把你的第一个查询放在一个cte中,然后用它来返回你在UNION ALL之后的查询的最小amd max%。我希望这是你的预期输出。

答案 3 :(得分:0)

可能不需要第一部分的最小/最大百分比,仅在第二部分。至于我从评论中得到它,请看

WITH totals_by_ext AS (
  SELECT 
    Filetype AS [Extensão], 
    COUNT(*) AS [Nº de ficheiros],
    CAST(((COUNT(Filetype) * 100.0) / (SELECT COUNT(*) FROM infofile)) AS DECIMAL(10,2)) AS [Percentagem (%)],
    SUM(Filesize) AS [Total(KB)],
    CAST(NULL AS DECIMAL(10,2)) AS [Convertido para MB],
    CAST(NULL AS DECIMAL(10,2)) AS [Min. Percentagem (%)],
    CAST(NULL AS DECIMAL(10,2)) AS [Max. Percentagem (%)]
  FROM infofile  
  GROUP BY Filetype
)
SELECT * 
FROM totals_by_ext
UNION ALL
SELECT 
  '---------------',
  COUNT([Extensão]),
  SUM([Nº de extensões]) * 100 / (SELECT COUNT(Filetype) FROM infofile)),
  SUM([Total(KB)]),
  SUM([Total(KB)]) / 1024,
  MIN([Percentagem (%)]) AS [Min. Percentagem (%)],
  MAX([Percentagem (%)]) AS [Max. Percentagem (%)]
FROM totals_by_ext;