我正在创建一个自定义SQL视图,我们可以在其中按位置查找项目数量。目前,它可以按列完美地提供信息,但是我可以为该信息提供子查询或别名以使该信息连续显示吗?
我尝试了选择子查询,但是它返回
信息512,级别16,状态1,第1行
子查询返回的值超过1。当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做。
SELECT SKU, DESCRIPTION, QUANTITY, LOCATION
FROM INVENTORY
输出:
RB RED BALL 0 OVERSTOCK
RB RED BALL 1 ROOM 1
RB RED BALL 3 ROOM 2
RB RED BALL 5 ROOM 3
我很想看看:
SKU DESCRIPTION QTYOVERSTOCK QTYROOM1 QTYROOM2 QTYROOM3
--------------------------------------------------------------
RB RED BALL 0 1 3 5
答案 0 :(得分:2)
您只需要一个基本的数据透视查询:
SELECT
SKU,
DESCRIPTION,
MAX(CASE WHEN LOCATION = 'OVERSTOCK' THEN QUANTITY END) AS QTYOVERSTOCK,
MAX(CASE WHEN LOCATION = 'ROOM 1' THEN QUANTITY END) AS QTYROOM1,
MAX(CASE WHEN LOCATION = 'ROOM 2' THEN QUANTITY END) AS QTYROOM2,
MAX(CASE WHEN LOCATION = 'ROOM 3' THEN QUANTITY END) AS QTYROOM3
FROM INVENTORY
GROUP BY
SKU,
DESCRIPTION;