У меня есть данные со значением суммы и отображением в тот день, совпадающем с SQLповорота дня месяца от строки к колонку со значением суммы
выборкой данных и кодом
http://sqlfiddle.com/#!3/ca364/6
но моя проблема я не могу поворачиваться дата строки в столбец мне нужно поворачивать (запрос от sqlfiddle выше) показано, как в этом примере
Date 1 2 3 4 5 6 7 8 9 to end of the month
TAS0B065B999P999 0 0 0 0 0 4900 9900 0 -96600
TAS1B065B999P999 0 0 0 0 0 0 0 0 0
TAS2B065B999P999 0 0 0 0 0 0 0 0 0
TAS3B065B999P999 0 0 0 0 0 0 0 0 0
TAS4B065B999P999 0 0 0 0 0 0 0 0 0
TAS5B065B999P999 0 0 0 0 0 0 0 0 0
TAS6B065B999P999 0 0 0 0 0 0 0 0 0
TAS7B065B999P999 0 0 0 0 0 0 0 0 0
TAS8B065B999P999 0 0 0 0 0 0 0 0 0
TAS9B065B999P999 0 0 0 0 0 0 0 0 0
TASAB065B999P999 0 0 0 0 0 0 0 0 0
TDS1B065B999P999 0 0 0 0 0 0 0 0 0
TDS2B065B999P999 0 0 0 0 0 0 0 0 0
TDS3B065B999P999 0 0 0 0 0 0 0 0 0
TRS1B065B999P999 0 0 0 0 0 0 0 0 0
TRS2B065B999P999 0 0 0 -72900 0 74900 -74900 90900 0
TRS3B065B999P999 0 0 0 21100 0 0 0 0 0
Пример кода
WITH t1
AS (SELECT dt,TERM,
SumCashOUT,
DATEADD(MONTH, DATEDIFF(MONTH, '1900-01-01', dt), '1900-01-01') AS firstInMonth,
DATEADD(DAY, -1, DATEADD(MONTH, DATEDIFF(MONTH, '1900-01-01', dt)+1, '1900-01-01')) AS lastInMonth
FROM
(
SELECT CONVERT( DATE, TA_DATE) AS dt,TERM,
DATENAME(dw, CONVERT(DATE, TA_DATE)) AS dn,
DATEPART(DW, CONVERT(DATE, TA_DATE)) AS dtdw,
OUT1 + OUT2 + OUT3 + OUT4 AS SumCashOUT
FROM CASH1609
GROUP BY TA_DATE,
TERM,
OUT1,
OUT2,
OUT3,
OUT4
) AS st
GROUP BY dt,TERM,
dn,
dtdw,
SumCashOUT),
Calendar
AS (SELECT DISTINCT
DATEADD(Day, c.number, t1.firstInMonth) AS d
FROM t1
JOIN master..spt_values AS c ON type = 'P'
AND DATEADD(DAY, c.number, t1.firstInMonth) BETWEEN t1.firstInMonth AND t1.lastInMonth)
SELECT DATEPART(dd, d) AS Date,TERM,
CASE
WHEN CONVERT(NUMERIC(16, 0), SumCashOUT) IS NULL
THEN 0
ELSE CONVERT(NUMERIC(16, 0), SumCashOUT)
END AS SumCashOUT
FROM calendar AS c
LEFT JOIN t1 ON t1.dt = c.d
LEFT JOIN holiday ON c.d = holiday.HDATE
ORDER BY DAte ASC;
спасибо за вашу помощь, ваш код работает как шарм :) –