SQL Server行转列
时间:2021-07-01 10:21:17
帮助过:15人阅读
@SqlText NVARCHAR(
4000)
=‘SELECT DeviceName 货机名称, DeviceAddress 货机地址,CONVERT(VARCHAR(10), ReplenishmentCreatedDate, 20) 时间, ‘ -- SQL头部分 SELECT
--行转列如下:可以重命名字段名,列头用其中一个列生成,列信息用另一个字段的信息,可以让多列数据信息转换
SELECT @SqlText = @SqlText + ‘ SUM(CASE WHEN GoodsName=‘‘‘
+ GoodsName
+ ‘‘‘ THEN ISNULL(ReplenishmentTotalQuantity, 0) END) ‘‘‘
+ GoodsName
+ ‘(上货[件])‘‘,‘
+ ‘ SUM(CASE WHEN GoodsName=‘‘‘
+ GoodsName
+ ‘‘‘ THEN ISNULL(PickUpTotalQuantity,0) END) ‘‘‘
+ GoodsName
+ ‘(下货[件])‘‘,‘
FROM (
SELECT DISTINCT GoodsName
FROM #t) T
-- 拼接CASE WHEN
SELECT @SqlText = LEFT(
@SqlText,
Len(
@SqlText)
-1)
+ ‘ FROM #t GROUP BY DeviceName, DeviceAddress,CONVERT(VARCHAR(10), ReplenishmentCreatedDate, 20)‘ -- 拼接 FROM后面部分
PRINT @SqlText
EXEC (
@SqlText)
SQL Server行转列
标签:ant ESS ase sqlt nvarchar add convert tin 一个