求教:有入库时间和出库间,我想得到每月销售和历史库存表
------解决方案--------------------
SELECT
样式号
, 日期
, SUM( CASE WHEN LX = 1 AND 出院时间 IS NOT NULL THEN 1 ELSE 0 END ) AS 销售件数
, SUM( CASE WHEN LX = 0 AND 入库时间 IS NOT NULL THEN 1 ELSE 0 END ) AS 出库件数
FROM (
SELECT 样式号, CONVERT( VARCHAR(7), 入库时间, 23 ) AS 日期, 0 AS LX
FROM TABLENAME
UNION ALL
SELECT 样式号, CONVERT( VARCHAR(7), 出院时间, 23 ) AS 日期, 1 AS LX
FROM TABLENAME ) AS A
GROUP BY 样式号, 日期