Что у меня есть: < /strong>
У меня есть динамическая таблица Pivot в SQL Server с именем "tis_temp_dsr", которая в настоящее время содержит следующую информацию: < /p>
customer_no customer_name jan2025 feb2025 mar2025 total
CNWL000132 Juan 20 30 0 50
< /code>
Что мне нужно: < /strong>
Что мне нужно, так это следующее: < /p>
customer_no
customer_name
jan2025
feb2025
mar2025
total
Juan
20
30
0
50
< /code>
Что я сделал до сих пор: < /strong>
У меня есть следующий запрос SQL Server, который работает: < /p>
DECLARE
@cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX);
SET @cols = STUFF(
(
SELECT CONCAT(N',[', FORMAT(month_year, 'MMM yyyy'), N']')
FROM (
SELECT DISTINCT EOMONTH(tis_posting_date) AS month_year
FROM tis_temp_dsr
WHERE tis_posting_date BETWEEN '01-01-2025' AND '03-31-2025'
) t
ORDER BY month_year
FOR XML PATH(''), TYPE
).value('.', 'nvarchar(max)'), 1, 1, ''
)
SET @cols = @cols +',[Total]'
SET @query = '
; with cte as
(
select tis_customer_no,tis_account_name,
month_year = FORMAT(tis_posting_date, ''MMM yyyy''), tis_amount_with_vat
from tis_temp_dsr
union all
select tis_customer_no,tis_account_name,
month_year = ''Total'', tis_amount_with_vat = sum(tis_amount_with_vat)
from tis_temp_dsr
group by tis_customer_no,tis_account_name
)
SELECT tis_customer_no,tis_account_name, '
+ @cols + '
from
(
select tis_customer_no,tis_account_name, month_year, tis_amount_with_vat
from cte
-- the following is for row wise total
union all
select tis_customer_no = '''',tis_account_name = ''Total'', month_year, sum(tis_amount_with_vat)
from cte
group by month_year
) x
pivot
(
Sum(tis_amount_with_vat)
for month_year in (' + @cols + ')
) p '
print @query
execute(@query)
Подробнее здесь: https://stackoverflow.com/questions/795 ... th-pdo-php