I am working on a report with SQL Server database.
My tables are:
Table Name: appointments
id appointment_number applicant_name appointment_date appointment_time
----- ------------------ ------------------------ ---------------- ----------------
1 APP001 Anuj Tyagi 2022-12-28 11:00 AM
2 APP002 Puneet Pathak 2022-12-28 11:30 AM
3 APP003 Rajeev Kumar 2022-12-28 10:00 AM
Table Name: payments
id appointment_id fee_Type_name amount
----- ------------------ --------------- ----------
1 1 Consulatncy 500
2 1 Service Fee 100
3 1 Pharmacy 435
4 2 Consulatncy 800
5 2 Service Fee 160
6 2 Pharmacy 833
7 3 Consulatncy 500
8 3 Service Fee 100
Table Name: tax_details
id payment_id tax_name tax_percentage amount
----- -------------- --------------- -------------- ---------
1 1 CGST 5.00 25.00
2 1 SGST 2.50 12.50
3 2 CGST 8.00 8.00
4 2 SGST 4.00 4.00
5 3 CGST 10.00 43.50
6 3 SGST 8.00 34.80
7 4 CGST 5.00 40.00
8 4 SGST 2.50 20.00
9 5 CGST 8.00 12.80
10 5 SGST 4.00 6.40
11 6 CGST 10.00 83.30
12 6 SGST 8.00 66.64
13 7 CGST 5.00 25.00
14 7 SGST 2.50 12.50
15 8 CGST 8.00 8.00
16 8 SGST 4.00 4.00
I need to generate following report:
appointment_number applicant_name appointment_date appointment_time Consulatncy CGST SGST Service Fee CGST SGST Pharmacy CGST SGST
------------------ ------------------------ ---------------- ---------------- -------------- ------ ------ ------------- ------ ------ ---------- ------ ------
APP001 Anuj Tyagi 2022-12-28 11:00 AM 500 25.00 12.50 100.00 8.00 4.00 435.00 43.50 34.50
APP002 Puneet Pathak 2022-12-28 11:30 AM 800 40.00 20.00 160.00 12.80 6.40 833.00 83.30 66.64
APP003 Rajeev Kumar 2022-12-28 10:00 AM 500 25.00 12.50 100.00 8.00 4.00 0.00 0.00 0.00
I have tried PIVOT to convert the rows to column after creation of a view with all the data together. The problem I am facing there are:
- My fee will be dynamic as is taxes
- Putting Tax just after the fee.
Any help in this will be appreciated.
My Pivot Effort:
SELECT @@AllSumColumns= COALESCE(@AllSumColumns + ',','') + 'SUM(' + QUOTENAME([fee_name])+'))' + ' AS ' + QUOTENAME([fee_name])
FROM (
SELECT DISTINCT [fee_name] FROM [dbo].vw_fee_list_details fld
) AS PivotExample;
SELECT @AllColumns= COALESCE(@AllColumns + ',','') + QUOTENAME([fee_name])
FROM (
SELECT DISTINCT [fee_type_name] FROM [dbo].vw_fee_list_details fld WHERE fee_type = 1 and
fld.appref_id like concat(@appRefId,'%') AND
fld.e_number like concat(@eNumber,'%') AND
fld.service_center like concat(@vscName,'%') AND
CAST(fld.transaction_date AS DATE) >= @startDate AND
CAST(fld.transaction_date AS DATE) <= @endDate
) AS PivotExample
SET @SQLQuery =
N'SELECT
ROW_NUMBER() over (Order by feeTable.appointment_number, feeTable.applicant_name, feeTable.transaction_date) [S.No],
FORMAT(feeTable.transaction_date,''dd-MMM-yy'') as [Transaction Date],
feeTable.transaction_time as [Transaction Time],
feeTable.appointment_number as [Appointment Reference],
feeTable.applicant_name as [Applicant Name],
+@AllSumColumns+'
FROM(
SELECT * FROM(
SELECT * FROM vw_fee_list_details fld
WHERE
fld.appointment_number like '''+@appRefId+'%'+''' AND '+
'fld.e_number like '''+@eNumber+'%'+'''
) a
PIVOT (
sum(amount)
FOR [fee_name]
IN('+@AllColumns+')) AS PivotTable) AS feeTable
GROUP BY feeTable.appointment_number, feeTable.applicant_name, feeTable.transaction_date,feeTable.transaction_time'
EXEC sp_executesql @SQLQuery;