quote: Originally posted by julius.delorino
thank you for your help sir,but your solution did not generate the desired output ,the total count of each terminal should from source table with corresponding terminal and errorcode please see the image for reference thank you.
The query generates the desired output based on your sample data:
USE tempdb;
IF OBJECT_ID(N'tempdb..SourceTable', N'U') IS NULL
SELECT T.TERMINALID, T.TERMINALDESC, T.ERRORCODE, T.ERRORDESC, T.TOTALCOUNT
INTO SourceTable
FROM (VALUES('Terminal_1', 'Terminal_Main', '0000', 'Approved', 72),
('Terminal_1', 'Terminal_Main', '0051', 'Insufficient Funds', 1),
('Terminal_1', 'Terminal_Main', '0061', 'ExceededDailyAmtLimit', 1),
('Terminal_2', 'Terminal_Site', '0000', 'Approved', 17),
('Terminal_2', 'Terminal_Site', '0005', 'UnabletoProcess', 3),
('Terminal_2', 'Terminal_Site', '0025', 'InvalidCurrentAcct', 1)
) AS T(TERMINALID, TERMINALDESC, ERRORCODE, ERRORDESC, TOTALCOUNT);
SELECT ERRORCODE, ERRORDESC,
SUM(CASE WHEN TERMINALID = 'Terminal_1' THEN TOTALCOUNT ELSE 0 END) AS Terminal_1,
SUM(CASE WHEN TERMINALID = 'Terminal_2' THEN TOTALCOUNT ELSE 0 END) AS Terminal_2
FROM SourceTable
GROUP BY ERRORCODE, ERRORDESC;
For us, there is only the trying. The rest is not our business. ~T.S. Eliot
Muhammad Al Pasha |