Belege_Planung_Ist_FC.sql 1.3 KB

123456789101112131415161718
  1. WITH V1 (c1, c2, c3, c4, c5, c6, c7) as (
  2. SELECT 'desk01' as c1,
  3. CASE WHEN (T1.[DEPARTMENT_TYPE_ID] IS NOT NULL) THEN ((left(T1.[DEPARTMENT_TYPE_ID],2))) ELSE ('01') END as c2,
  4. rtrim(substring(T1.[DEPARTMENT_TYPE_ID], 3, 1)) as c3,
  5. rtrim(T2.[ACCT_NR]) as c4,
  6. CAST(T3.[BOOKKEEP_PERIOD] as varchar(6)) as c5,
  7. CASE WHEN ((((T2.[ACCT_NR] LIKE '8%') and (not T2.[ACCT_NR] BETWEEN '89050000' AND '89500001')) and (not T2.[ACCT_NR] BETWEEN '8812000' AND '8812002'))
  8. or ((T2.[ACCT_NR] LIKE '9%') and (T2.[ACCT_NR] <> '99999999'))) THEN ((T3.[DEBIT_AMOUNT] + T3.[CREDIT_AMOUNT]) * -1) ELSE (T3.[DEBIT_AMOUNT] + T3.[CREDIT_AMOUNT]) END as c6,
  9. T3.[DEBIT_QUANTITY] + T3.[CREDIT_QUANTITY] as c7
  10. FROM [desk01].[dbo].[ACCT_DOC_KEY] T3
  11. INNER JOIN [desk01].[dbo].[ACCOUNT_INFO] T2 on T2.[ACCT_NR] = T3.[ACCT_NO]
  12. INNER JOIN [desk01].[dbo].[DEPARTMENT_TYPE] T1 on T3.[DEPARTMENT] = T1.[DEPARTMENT_TYPE_ID]
  13. WHERE T2.[TYPE_ACCTT] IN ('2','1') and year(T3.[BOOKKEEP_DATE]) >= year(getdate()) - 2)
  14. SELECT c1 as 'Datenbank', c2 as 'Betrieb_Nr', c3 as 'Marke', c4 as 'Konto_Nr', c5 as 'Bookkeep_Period', sum(c6) as 'Betrag', sum(c7) as 'Menge'
  15. FROM V1
  16. GROUP BY c1, c2, c3, c4, c5
  17. ORDER BY c1, c2, c3, c4, c5