SQL Server Include GROUP BY in query 1 of a UNION, instead of two queries UNIONed to get fill rates/row counts by category id

4si2a6ki  于 2023-03-28  发布在  Go
关注(0)|答案(1)|浏览(234)

I have a table of example data:

CREATE TABLE #TempTable 
(
    CategoryID varchar(10), --Non-Unique
    ArbitraryColumnName varchar(10), --Non-Unique
)

INSERT INTO #TempTable (CategoryID, ArbitraryColumnName)
VALUES
    ('1', '14578123'),
    ('2', NULL),
    ('1', '13402348'),
    ('4', '12302354'),
    ('4', NULL),
    ('5', '12678912')
CategoryIDArbitraryColumnName
114578123
2NULL
113402348
412302354
4NULL
512678912

I am trying to get the counts for all of the categories together, and the separates categories, in the same table. Here is my query to do so:

SELECT
    CAST('TempTable' AS varchar(55)) AS TableName
    ,CAST('ArbitraryColumnName' AS varchar(55)) AS FieldName
    ,CAST('All CategoryIDs' AS varchar(55)) CategoryID
    ,COUNT(*) AS Table_RowCount
    ,SUM(CASE WHEN a.ArbitraryColumnName IS NOT NULL THEN 1 ELSE 0 END) AS Field_NonNullCount
    ,COUNT(DISTINCT a.ArbitraryColumnName) AS Field_DistinctCount
    ,CAST(100 * AVG(CASE WHEN a.ArbitraryColumnName IS NOT NULL THEN 1.0 ELSE 0.0 END) AS 
numeric(10,2)) AS Field_FillRate
FROM #TempTable a

UNION

SELECT
    CAST('TempTable' AS varchar(55)) AS TableName
    ,CAST('ArbitraryColumnName' AS varchar(55)) AS FieldName
    ,CAST(a.CategoryID AS varchar(55)) GroupID
    ,COUNT(*) AS Table_RowCount
    ,SUM(CASE WHEN a.ArbitraryColumnName IS NOT NULL THEN 1 ELSE 0 END) AS Field_NonNullCount
    ,COUNT(DISTINCT a.ArbitraryColumnName) AS Field_DistinctCount
    ,CAST(100 * AVG(CASE WHEN a.ArbitraryColumnName IS NOT NULL THEN 1.0 ELSE 0.0 END) AS 
numeric(10,2)) AS Field_FillRate
FROM #TempTable a
GROUP BY a.CategoryID

Results:

TableNameFieldNameCategoryIDTable_RowCountField_NonNullCountField_DistinctCountField_FillRate
TempTableArbitraryColumnName1222100.00
TempTableArbitraryColumnName21000.00
TempTableArbitraryColumnName421150.00
TempTableArbitraryColumnName5111100.00
TempTableArbitraryColumnNameAll CategoryIDs64466.67

What is the best way to combine the queries of the UNION to provide the same results?

uoifb46i

uoifb46i1#

I can write the above Union query as in this DBFIDDLE_DEMO

Which gives the same output as above :

SELECT
    CAST('TempTable' AS varchar(55)) AS TableName,
    CAST('ArbitraryColumnName' AS varchar(55)) AS FieldName,
    COALESCE(CAST(a.CategoryID AS varchar(55)), 'All CategoryIDs') AS CategoryID,
    COUNT(*) AS Table_RowCount,
    SUM(CASE WHEN a.ArbitraryColumnName IS NOT NULL THEN 1 ELSE 0 END) AS Field_NonNullCount,
    COUNT(DISTINCT a.ArbitraryColumnName) AS Field_DistinctCount,
    CAST(100 * AVG(CASE WHEN a.ArbitraryColumnName IS NOT NULL THEN 1.0 ELSE 0.0 END) AS numeric(10,2)) AS Field_FillRate
FROM #TempTable a
GROUP BY GROUPING SETS((a.CategoryID), ());

Output :

TableNameFieldNameCategoryIDTable_RowCountField_NonNullCountField_DistinctCountField_FillRate
TempTableArbitraryColumnName1222100.00
TempTableArbitraryColumnName21000.00
TempTableArbitraryColumnName421150.00
TempTableArbitraryColumnName5111100.00
TempTableArbitraryColumnNameAll CategoryIDs64466.67

相关问题