我按照以下方式获得了每个客户和每个日期的订单数据:
ID Date Record ColNumber RowNumber
----------------------------------------------------------------
ColNumber / RowNumber是表格中信息的位置 (例如:产品描述在公式的第一列,
FORMULAR
________________
Description BarCode Amount RetailPrice Others
-----------------------------------------------------
因此,公式上第四个产品的描述将采用以下形式:
ID Date Record ColNumber RowNumber
----------------------------------------------------------------
ID Date Description 1 4
"记录"包含公式的所有列(描述,条形码,金额,零售价等)。
因此,目标是建立一个包含以下列的表:
ID Date Description BarCode Amount Retail
------------------------------------------------------------------------
我觉得我需要混合使用PIVOT和JOIN,但是我所取得的成就让我回到了原来的#34; raw"表
以下是让您了解的示例:
IF OBJECT_ID ('tempdb..#temptab') IS NOT NULL
DROP TABLE #temptab
CREATE TABLE #temptab (
ID INT NOT NULL,
SellDate date NOT NULL,
Record nvarchar(255),
ColNumber int,
RowNumber int)
INSERT INTO #temptab (ID, SellDate, Record, ColNumber, RowNumber) VALUES
(1, '2017-01-01', 'Cookie1', 1, 1),
(1, '2017-01-01', '21312332', 1, 2),
(1, '2017-01-01', '3', 1, 2),
(1, '2017-01-01', 'Banana1', 2, 1),
(2, '2017-01-01', 'Apple1', 1, 1),
(3, '2017-01-01', 'Peach1', 1, 1),
(3, '2017-01-01', '546462', 1, 2);
给出了:
ID SellDate Record ColNumber RowNumber
1 2017-01-01 Cookie1 1 1
1 2017-01-01 CH212332 2 1
1 2017-01-01 3 3 1
1 2017-01-01 Banana1 1 2
2 2017-01-01 Apple1 1 1
3 2017-01-01 Peach1 1 1
3 2017-01-01 546462 2 1
最后我想要一张看起来像那样的表
ID SellDate Description BarCode Amount
1 2017-01-01 Cookie1 CH212332 3
1 2017-01-01 Banana1
2 2017-01-01 Apple1
3 2017-01-01 Peach1 546462
(缺失值将由信息的重新填充,我显然没有在表格中添加整个数据,或者使用NULL'
答案 0 :(得分:0)
试试这个,它会给你预期的结果
SELECT ID
,SellDate
,ISNULL(Record, '') AS Record
,ISNULL(BarCode, '') AS BarCode
,ISNULL(Amount, '') AS Amount
FROM
(
SELECT ID, SellDate,
CASE WHEN ISNUMERIC(Record)=0 THEN Record ELSE NULL END AS Record
,CASE WHEN ISNUMERIC(Record)=1 AND
LEN(CASE WHEN ISNUMERIC(Record)=1 THEN Record ELSE NULL END)>1 THEN Record ELSE NULL END AS BarCode
,CASE WHEN LEN(CASE WHEN ISNUMERIC(Record)=1 THEN Record ELSE NULL END)=1 THEN Record ELSE NULL END AS Amount
FROM #temptab
)DT
结果
ID SellDate Record BarCode Amount
--------------------------------------------------
1 2017-01-01 Cookie1
1 2017-01-01 21312332
1 2017-01-01 3
1 2017-01-01 Banana1
2 2017-01-01 Apple1
3 2017-01-01 Peach1
3 2017-01-01 546462