我需要帮助重写以下查询,因为我需要按别名分组,派生表似乎是对我开放的唯一路径。由于出现无效列错误,我无法按类型和 SalesDatabase 分组。
SELECT 'Transactions' AS Type,
'SalesDb' AS SalesDatabase,
COUNT (B.Id) AS Total,
CASE
WHEN SB.name LIKE '%Online%' THEN 'Online'
WHEN SB.name LIKE '%RetailSale%' THEN 'Retail'
ELSE 'Bricks'
END AS SalesType,
ISNULL( SUM( CASE WHEN datediff( d, b.Inserted, getDate() ) < 1 THEN 1 ELSE 0 END ), 0 ) AS SalesPeriod0,
ISNULL( SUM( CASE WHEN datediff( d, b.Inserted, getDate() ) >= 1 and datediff( d, b.Inserted, getDate() ) < 8 THEN 1 ELSE 0 END ), 0 ) AS SalesPeriod1to7 ,
ISNULL( SUM( CASE WHEN datediff( d, b.Inserted, getDate() ) >= 8 THEN 1 ELSE 0 END ), 0 ) AS SalesPeriod8p
FROM SalesItem B WITH (NOLOCK)
INNER JOIN SalesBatch SB WITH (NOLOCK) ON SB.Id = B.BatchId
GROUP BY SB.NAME
任何帮助表示赞赏。