前6个月我一直在拉总库存。如何在同一个查询中提取过去 6 个月内的总库存?
我也想要如下所示的查询结果:
Stock Code | the first 6 months | the second 6 months etc.
我的 SQL 查询:
SELECT TOP (100) PERCENT STOK_KODU,
COALESCE (SUM(CASE WHEN kod1 = 'G' THEN kod2 ELSE 0 END), 0)
- COALESCE (SUM(CASE WHEN kod1 = 'C' THEN kod2 ELSE 0 END), 0) AS StokToplam1
FROM s_hareket WHERE (STOK_KODU BETWEEN '01001' AND '75000')
and (tarih BETWEEN '2011-01-01' and '2011-06-30')
GROUP BY STOK_KODU
ORDER BY STOK_KODU
您应该能够使用 dateadd 和 getdate 函数来做到这一点:
如果我正确理解你的问题,你想合并结果,然后旋转它。这导致以下查询: