I have a table in SQL Server which has 50 columns and 200 Million records. Currently, I'm hitting this table 50 times to do count on distinct column values for all columns like below:
INSERT INTO dbo.RPTS_LoadValues
(
Column_Value,
Record_Value,
Record_Ct
)
SELECT 'Transaction_Dt',
CONVERT(VARCHAR(50), Transaction_Dt),
COUNT_BIG(1)
FROM dbo.VehicleImport
GROUP BY Transaction_Dt
UNION ALL
SELECT 'Purchase_Dt',
CONVERT(VARCHAR(50), Purchase_Dt),
COUNT_BIG(1)
FROM dbo.VehicleImport
GROUP BY Purchase_Dt
UNION ALL
SELECT 'PurchaseType_Cd',
CONVERT(VARCHAR(50), PurchaseType_Cd),
COUNT_BIG(1)
FROM dbo.VehicleImport
GROUP BY PurchaseType_Cd;
Above query run for 3 columns which is Transaction_Dt, Purchase_Dt and PurchaseType_Cd there are other 47 columns like Car_Type, Model_Ds, Model_Year etc.
Is there any other way using T-SQL, C# or Python that I hit the table only once and able to count on column's distinct record values for all columns? Please provide useful resources or links that can help.