SQL Server 无空值的透视表

ruyhziif  于 2023-01-01  发布在  其他
关注(0)|答案(3)|浏览(199)

我有下表

+-----+-------+------+---------+
| ID  | NAME  | SORT |  AMOUNT |
+-----+-------+------+---------+
|   1 | sak   | A    |     200 |
|   2 | mods  | b    |     200 |
|   3 | wef   | c    |     200 |
|   4 | sak   | b    |     300 |
|   5 | mods  | a    |     210 |
+-----+-------+------+---------+

我写了下面的代码

SELECT ID,A, B, C 
FROM 
(
Select ID,SORT,
 'SORT'+
      cast(row_number() over(partition by ID order by ID)
             as varchar(10)) Col
from TABLE
) Temp 
pivot 
(
max (SORT)
for Col in ( A, B, C  )
)piv

我得到了这个结果:

+----+------+------+------+
| ID |  A   |  B   |  C   |
+----+------+------+------+
|  1 | NULL | NULL | NULL |
|  2 | NULL | NULL | NULL |
|  3 | NULL | NULL | NULL |
|  4 | NULL | NULL | NULL |
|  5 | NULL | NULL | NULL |
+----+------+------+------+

但是需要以这种方式显示表,但我得到的是NULL而不是金额。

pqwbnv8z

pqwbnv8z1#

我知道您希望每个sak有一条记录,其中abcamount在列中。
下面是一个跨数据库解决方案,它使用条件聚合将数据集透视到一个 * 固定 * 的列列表上:

select 
    sak,
    max(case when upper(sort) = 'A' then amount end) a,
    max(case when upper(sort) = 'B' then amount end) b,
    max(case when upper(sort) = 'C' then amount end) c
from mytable
group by sak
order by sak
6qqygrtg

6qqygrtg2#

您也可以尝试以下操作:
1.根据你的结构

SELECT ID, A, B, C
FROM
(
Select ID,UPPER(SORT) SORT,AMOUNT
from MYTABLE
) Temp
pivot
(
max (AMOUNT)
for SORT in (A, B, C)
)piv
ORDER BY 1

1.预期

SELECT NAME, A, B, C
FROM
(
SELECT NAME, SORT, AMOUNT
FROM MYTABLE
)T
PIVOT
(
SUM(AMOUNT) FOR SORT IN (A, B, C)
)PVT
qkf9rpyu

qkf9rpyu3#

SET NOCOUNT ON;
    declare @column nvarchar(max)
    declare @Query nvarchar(max)
    declare @price nvarchar(max)
    declare @last nvarchar(max)
    declare @with nvarchar(10)
    DECLARE @colsWithNoNulls    NVARCHAR(MAX)

    IF @subcategory > 0
        begin
            set @with = 'and'
        end
    ELSE
        begin
            set @with = 'or'
        end

    set @column= STUFF((Select '],['+isnull(whsname,0) from OWHS WS CROSS APPLY string_split(@whs, ',') as UI WHERE WS.WhsCode = UI.value FOR XML PATH('')),1,2,'') +']'
    set @price= STUFF((Select  '],['+isnull(ListName,0) from OPLN PN CROSS APPLY string_split(ISNULL(@pln,0), ',') as UX WHERE ISNULL(PN.ListNum,0) = ISNULL(UX.value,0) FOR XML PATH('')),1,2,'') +']'
    
    set @Query='select  * from (select w.ItemCode,m.ItemName ,b.ItmsGrpNam ItemGroupName,isnull(CAST(m.usertext AS NVARCHAR(Max)),'''') SpaceOrType
    ,m.OnHand InStock,m.IsCommited Committed,m.OnOrder Ordered
    ,(m.OnHand-m.IsCommited+m.OnOrder) Available,n.ListName ListName,c.Price priceL,s.WhsName,sum(w.OnHand) Stock

    from oitw w
    right join OITM m on m.itemcode=w.itemcode
    inner join OWHS s on s.WhsCode=w.WhsCode
    inner join OITB b on b.ItmsGrpCod=m.ItmsGrpCod
    left join ITM1 c on m.ItemCode = c.ItemCode
    left join OPLN n on c.priceList = n.listNum
    left join [@CATEGORY] h on m.U_Category = h.Code
    left join [@SUBCATEGORY] i on h.Code = i.U_mainCategory
    where b.ItmsGrpCod = '''+@brand+''' or w.ItemCode like ''%'+@name+'%'' or m.ItemName like ''%'+@name+'%'' or h.Code = '''+@category+''' '+@with+' i.Code = '''+@subcategory+'''
    group by w.ItemCode,m.itemName,b.ItmsGrpNam,m.U_Category,m.U_SubCategory,CAST(m.usertext AS NVARCHAR(max)),s.WhsName,m.OnHand ,m.IsCommited,c.price,m.OnOrder,n.ListName) fg
    PIVOT (sum(priceL) for
    ListName in ('+ISNULL(@price,0)+')) as piv
    PIVOT (sum(Stock) for
    Whsname in ('+@column+')) as piv'
    EXECUTE (@Query)
END

相关问题