I am trying to get the total sum from columns of a specific data type(money) for multiple tables in a database. Currently I am able to get the list of columns from specific tables but I am unable to get the sums from those columns.
This is what I have now
use database 1
Select Column_Name
From information_schema.columns
Where TABLE_NAME = 'claimant'
and data_type = 'money'
The result looks something like below
table_name | column_name |
---|---|
table_1 | column_a |
table_1 | column_b |
table_1 | column_c |
what I would like
table_name | column_name | total_sum |
---|---|---|
table_1 | column_a | 66.20 |
table_1 | column_b | 300.50 |
table_1 | column_c | 5389.42 |
update for @Squirrel Here is the code I have but it's still giving me issues with truncation.
{
declare @sql nvarchar(max);
select @sql = 'with cte as (' char(13)
'select' char(13)
string_agg(char(9) quotename(column_name) ' = sum(' quotename(COLUMN_NAME) ')', ',' char(13)) char(13)
'from ' max(quotename(table_name)) char(13)
')' char(13)
'select a.table_name, a.column_name, a.total_sum ' char(13)
'from cte ' char(13)
'cross apply (' char(13)
char(9) 'values' char(13)
string_agg(char(9) '(''' table_name ''',''' column_name ''',' quotename(COLUMN_NAME) ')', ',' char(13)) char(13)
') a (table_name, column_name, total_sum)'
from information_schema.columns AS A
INNER JOIN EDL01.STAGING.TABLE_DETAILS B
ON A.TABLE_NAME = B.DEST_TABLE_NAME
where A.table_name = B.DEST_TABLE_NAME
and data_type = 'money'
print @sql
exec sp_executesql @sql
}
below is the create table
CREATE TABLE [staging].[TABLE_DETAILS](
[SOURCE_TABLE_NAME] [varchar](100) NULL,
[DEST_TABLE_NAME] [varchar](100) NULL,
[TYPE] [varchar](10) NULL,
[PRIORITY] [int] NULL,
[SOURCE_TABLE_DATABASE] [varchar](50) NULL,
[SOURCE_TABLE_SCHEMA] [varchar](50) NULL,
[DEST_TABLE_DATABASE] [varchar](50) NULL,
[DEST_TABLE_SCHEMA] [varchar](50) NULL
) ON [PRIMARY]
GO
Below is part of the results
select a.table_name, a.column_name, a.total_sum
from cte
cross apply (
values
('PAYMENT','BILLEDAMOUNT',[BILLEDAMOUNT]),
('PAYMENT','AMOUNT',[AMOUNT]),
('SIMS_PAYMENT','CHECKAMOUNT',[CHECKAMOUNT]),
('BILLREVIEWHEADER','JURISDICTIONAMOUNT1',[JURISDICTIONAMOUNT1]),
('BILLREVIEWHEADER','JURISDICTIONAMOUNT2',[JURISDICTIONAMOUNT2]),
('BILLREVIE
CodePudding user response:
You need to form the query dynamically and then execute it using sp_executesql
or exec()
Note : char(9)
is tab, char(13)
is carriage return. These are added to format the query so that it is readable when you print
it out for verification.
declare @sql nvarchar(max);
select @sql = 'with cte as (' char(13)
'select' char(13)
string_agg(char(9) quotename(column_name) ' = sum(' quotename(column_name) ')', ',' char(13)) char(13)
'from ' max(quotename(table_name)) char(13)
')' char(13)
'select a.table_name, a.column_name, a.total_sum ' char(13)
'from cte ' char(13)
'cross apply (' char(13)
char(9) 'values' char(13)
string_agg(char(9) '(''' table_name ''', ''' column_name ''',' quotename(column_name) ')', ',' char(13)) char(13)
') a (table_name, column_name, total_sum)'
from information_schema.columns
where table_name = 'table_1'
and data_type = 'money'
print @sql
exec sp_executesql @sql
For your sample table, the generated dynamic query is
with cte as (
select
[column_a] = sum([column_a]),
[column_b] = sum([column_b]),
[column_c] = sum([column_c])
from [table_1]
)
select a.table_name, a.column_name, a.total_sum
from cte
cross apply (
values
('table_1', 'column_a',[column_a]),
('table_1', 'column_b',[column_b]),
('table_1', 'column_c',[column_c])
) a (table_name, column_name, total_sum)
EDIT using a loop to iterate each table. Basically it execute above query for each of the table and insert the result into a temp table
see db<>fiddle demo