如何根据字段值将查询结果转入其他列
How to pivot query result into additional columns based on a field value
因此,我了解了装配所需的组件的库存位置和数量。
有两个位置我想获取 BINS 和数量,我想为每个组件并排列出它们。
这是我最近的尝试:
DECLARE @INVMTL TABLE(
[COMPONENT] VARCHAR(50),
[LOCATION] VARCHAR(50),
[BIN] VARCHAR(50),
[QTY] VARCHAR(50))
DECLARE @FGMTL TABLE(
[COMPONENT] VARCHAR(50),
[LOCATION] VARCHAR(50),
[BIN] VARCHAR(50),
[QTY] VARCHAR(50))
INSERT INTO @INVMTL
SELECT [COMPONENT]
, [LOCATION]
, [BIN]
, [QTY]
FROM vw_WorkOrdersKitLoc AS INVMTL
WHERE [INVMTL].[LOCATION] = 'INVMTL'
AND [INVMTL].[ITEMNO] = 'ASSEMBLYNAME'
INSERT INTO @FGMTL
SELECT [COMPONENT]
, [LOCATION]
, [BIN]
, [QTY]
FROM vw_WorkOrdersKitLoc AS FGMTL
WHERE [FGMTL].[LOCATION] = 'FGMTL'
AND [FGMTL].[ITEMNO] = 'ASSEMBLYNAME'
SELECT COALESCE([INVMTL].[COMPONENT], [FGMTL].[COMPONENT])
, [INVMTL].[LOCATION]
, [INVMTL].[BIN]
, [INVMTL].[QTY]
, [FGMTL].[LOCATION]
, [FGMTL].[BIN]
, [FGMTL].[QTY]
FROM @INVMTL AS INVMTL
LEFT OUTER JOIN @FGMTL AS FGMTL ON FGMTL.COMPONENT = INVMTL.COMPONENT
This is what it returns. As you can see the extra right columns have duplicated data where it should be null.
我也知道有一种更短、更简洁的方法,我只是忘记了。
Here is the view returning all the data I want to reorganize
I made a spreadsheet of how I would like the retrieve this data
感谢您的帮助
您的连接不够具体 - 您只是要求数据库将这里的这个组件与那里的那个组件相匹配,这意味着一个 ABC,1
组件,一侧的位置将匹配 1:1一个 ABC,2
在另一个给出 ABC,1,ABC,2
但是一侧有两个 ABC,另一侧有 3 个 ABC 最终会相乘(笛卡尔爆炸)变成 6,如果你只匹配组件(ABC)来自每个:
(ABC,1 and ABC,2) join (ABC,3 and ABC,4 and ABC,5)
gives:
ABC,1,ABC,3
ABC,1,ABC,4
ABC,1,ABC,5
ABC,2,ABC,3
ABC,2,ABC,4
ABC,2,ABC,5
要查看电子表格应用的匹配逻辑并不容易。看起来 LOC 是按 alpha 升序排序的,然后第 1 行与第 1 行匹配,第 2 行与第 2 行等匹配。您需要提供一个假密钥才能加入:
DECLARE @INVMTL TABLE(
[COMPONENT] VARCHAR(50),
[LOCATION] VARCHAR(50),
[BIN] VARCHAR(50),
[QTY] VARCHAR(50),
fakekey INT)
DECLARE @FGMTL TABLE(
[COMPONENT] VARCHAR(50),
[LOCATION] VARCHAR(50),
[BIN] VARCHAR(50),
[QTY] VARCHAR(50),
fakekey INT)
INSERT INTO @INVMTL
SELECT [COMPONENT]
, [LOCATION]
, [BIN]
, [QTY]
, row_number() over(partition by COMPONENT order by location) as fakekey
FROM vw_WorkOrdersKitLoc AS INVMTL
WHERE [INVMTL].[LOCATION] = 'INVMTL'
AND [INVMTL].[ITEMNO] = 'ASSEMBLYNAME'
INSERT INTO @FGMTL
SELECT [COMPONENT]
, [LOCATION]
, [BIN]
, [QTY]
, row_number() over(partition by COMPONENT order by location) as fakekey
FROM vw_WorkOrdersKitLoc AS FGMTL
WHERE [FGMTL].[LOCATION] = 'FGMTL'
AND [FGMTL].[ITEMNO] = 'ASSEMBLYNAME'
SELECT COALESCE([INVMTL].[COMPONENT], [FGMTL].[COMPONENT])
, [INVMTL].[LOCATION]
, [INVMTL].[BIN]
, [INVMTL].[QTY]
, [FGMTL].[LOCATION]
, [FGMTL].[BIN]
, [FGMTL].[QTY]
FROM @INVMTL AS INVMTL
FULL OUTER JOIN @FGMTL AS FGMTL ON FGMTL.COMPONENT = INVMTL.COMPONENT and fgmtl.fakekey = invmtl.fakekey
对于更短的方式,您可以使用 WITH:
WITH INVMTL as (
SELECT [COMPONENT]
, [LOCATION]
, [BIN]
, [QTY]
, row_number() over(partition by COMPONENT order by location) as fakekey
FROM vw_WorkOrdersKitLoc AS INVMTL
WHERE [INVMTL].[LOCATION] = 'INVMTL'
AND [INVMTL].[ITEMNO] = 'ASSEMBLYNAME'
), FGMTL as (
SELECT [COMPONENT]
, [LOCATION]
, [BIN]
, [QTY]
, row_number() over(partition by COMPONENT order by location) as fakekey
FROM vw_WorkOrdersKitLoc AS FGMTL
WHERE [FGMTL].[LOCATION] = 'FGMTL'
AND [FGMTL].[ITEMNO] = 'ASSEMBLYNAME'
)
SELECT COALESCE([INVMTL].[COMPONENT], [FGMTL].[COMPONENT])
, [INVMTL].[LOCATION]
, [INVMTL].[BIN]
, [INVMTL].[QTY]
, [FGMTL].[LOCATION]
, [FGMTL].[BIN]
, [FGMTL].[QTY]
FROM
INVMTL
FULL OUTER JOIN FGMTL
ON
FGMTL.COMPONENT = INVMTL.COMPONENT and
fgmtl.fakekey = invmtl.fakekey
或为 FGMTL 等定义视图。
因此,我了解了装配所需的组件的库存位置和数量。 有两个位置我想获取 BINS 和数量,我想为每个组件并排列出它们。 这是我最近的尝试:
DECLARE @INVMTL TABLE(
[COMPONENT] VARCHAR(50),
[LOCATION] VARCHAR(50),
[BIN] VARCHAR(50),
[QTY] VARCHAR(50))
DECLARE @FGMTL TABLE(
[COMPONENT] VARCHAR(50),
[LOCATION] VARCHAR(50),
[BIN] VARCHAR(50),
[QTY] VARCHAR(50))
INSERT INTO @INVMTL
SELECT [COMPONENT]
, [LOCATION]
, [BIN]
, [QTY]
FROM vw_WorkOrdersKitLoc AS INVMTL
WHERE [INVMTL].[LOCATION] = 'INVMTL'
AND [INVMTL].[ITEMNO] = 'ASSEMBLYNAME'
INSERT INTO @FGMTL
SELECT [COMPONENT]
, [LOCATION]
, [BIN]
, [QTY]
FROM vw_WorkOrdersKitLoc AS FGMTL
WHERE [FGMTL].[LOCATION] = 'FGMTL'
AND [FGMTL].[ITEMNO] = 'ASSEMBLYNAME'
SELECT COALESCE([INVMTL].[COMPONENT], [FGMTL].[COMPONENT])
, [INVMTL].[LOCATION]
, [INVMTL].[BIN]
, [INVMTL].[QTY]
, [FGMTL].[LOCATION]
, [FGMTL].[BIN]
, [FGMTL].[QTY]
FROM @INVMTL AS INVMTL
LEFT OUTER JOIN @FGMTL AS FGMTL ON FGMTL.COMPONENT = INVMTL.COMPONENT
This is what it returns. As you can see the extra right columns have duplicated data where it should be null. 我也知道有一种更短、更简洁的方法,我只是忘记了。
Here is the view returning all the data I want to reorganize
I made a spreadsheet of how I would like the retrieve this data
感谢您的帮助
您的连接不够具体 - 您只是要求数据库将这里的这个组件与那里的那个组件相匹配,这意味着一个 ABC,1
组件,一侧的位置将匹配 1:1一个 ABC,2
在另一个给出 ABC,1,ABC,2
但是一侧有两个 ABC,另一侧有 3 个 ABC 最终会相乘(笛卡尔爆炸)变成 6,如果你只匹配组件(ABC)来自每个:
(ABC,1 and ABC,2) join (ABC,3 and ABC,4 and ABC,5)
gives:
ABC,1,ABC,3
ABC,1,ABC,4
ABC,1,ABC,5
ABC,2,ABC,3
ABC,2,ABC,4
ABC,2,ABC,5
要查看电子表格应用的匹配逻辑并不容易。看起来 LOC 是按 alpha 升序排序的,然后第 1 行与第 1 行匹配,第 2 行与第 2 行等匹配。您需要提供一个假密钥才能加入:
DECLARE @INVMTL TABLE(
[COMPONENT] VARCHAR(50),
[LOCATION] VARCHAR(50),
[BIN] VARCHAR(50),
[QTY] VARCHAR(50),
fakekey INT)
DECLARE @FGMTL TABLE(
[COMPONENT] VARCHAR(50),
[LOCATION] VARCHAR(50),
[BIN] VARCHAR(50),
[QTY] VARCHAR(50),
fakekey INT)
INSERT INTO @INVMTL
SELECT [COMPONENT]
, [LOCATION]
, [BIN]
, [QTY]
, row_number() over(partition by COMPONENT order by location) as fakekey
FROM vw_WorkOrdersKitLoc AS INVMTL
WHERE [INVMTL].[LOCATION] = 'INVMTL'
AND [INVMTL].[ITEMNO] = 'ASSEMBLYNAME'
INSERT INTO @FGMTL
SELECT [COMPONENT]
, [LOCATION]
, [BIN]
, [QTY]
, row_number() over(partition by COMPONENT order by location) as fakekey
FROM vw_WorkOrdersKitLoc AS FGMTL
WHERE [FGMTL].[LOCATION] = 'FGMTL'
AND [FGMTL].[ITEMNO] = 'ASSEMBLYNAME'
SELECT COALESCE([INVMTL].[COMPONENT], [FGMTL].[COMPONENT])
, [INVMTL].[LOCATION]
, [INVMTL].[BIN]
, [INVMTL].[QTY]
, [FGMTL].[LOCATION]
, [FGMTL].[BIN]
, [FGMTL].[QTY]
FROM @INVMTL AS INVMTL
FULL OUTER JOIN @FGMTL AS FGMTL ON FGMTL.COMPONENT = INVMTL.COMPONENT and fgmtl.fakekey = invmtl.fakekey
对于更短的方式,您可以使用 WITH:
WITH INVMTL as (
SELECT [COMPONENT]
, [LOCATION]
, [BIN]
, [QTY]
, row_number() over(partition by COMPONENT order by location) as fakekey
FROM vw_WorkOrdersKitLoc AS INVMTL
WHERE [INVMTL].[LOCATION] = 'INVMTL'
AND [INVMTL].[ITEMNO] = 'ASSEMBLYNAME'
), FGMTL as (
SELECT [COMPONENT]
, [LOCATION]
, [BIN]
, [QTY]
, row_number() over(partition by COMPONENT order by location) as fakekey
FROM vw_WorkOrdersKitLoc AS FGMTL
WHERE [FGMTL].[LOCATION] = 'FGMTL'
AND [FGMTL].[ITEMNO] = 'ASSEMBLYNAME'
)
SELECT COALESCE([INVMTL].[COMPONENT], [FGMTL].[COMPONENT])
, [INVMTL].[LOCATION]
, [INVMTL].[BIN]
, [INVMTL].[QTY]
, [FGMTL].[LOCATION]
, [FGMTL].[BIN]
, [FGMTL].[QTY]
FROM
INVMTL
FULL OUTER JOIN FGMTL
ON
FGMTL.COMPONENT = INVMTL.COMPONENT and
fgmtl.fakekey = invmtl.fakekey
或为 FGMTL 等定义视图。