来自#Temporary Table

时间:2017-04-24 13:03:00

标签: sql sql-server tsql merge

我目前正在SQL Server 2012上的T-SQL中进行SQL查询,目的是将多个表连接或合并到一个表中并连接多个行。到目前为止,它的工作正常。我使用STUFF和FOR XML PATH将几行连接成1。

不幸的是,当我想将我的2个临时表合并到1个最终结果表中时,连续(合并)的行再次消失。

我的表格看起来如下:

表#BasicOffers

OfferId   |  Tile  | Manager  | DeliveryVersionId | 
----------+--------+----------+-------------------|
4         | Offer1 | John Doe |  1                |
5         | Offer2 | Jane Doe |  2                |

表#TmpLabels

DeliveryVersionId | Label                   |
------------------+-------------------------+
1                 | Service, Time, Material |
2                 | Consulting, Time        |

最终结果,遗憾的是不是所需的输出。临时表似乎再次拆分:

OfferId   | Title  | Manager  | Delivery 
----------+--------+----------+------------------------
4         | Offer1 | John Doe | Service
4         | Offer1 | John Doe | Time
4         | Offer1 | John Doe | Material
5         | Offer2 | Jane Doe | Consulting
5         | Offer2 | Jane Doe | Time

期望输出:

OfferId   | Title  | Manager  | Delivery 
----------+--------+----------+------------------------
4         | Offer1 | John Doe | Service, Time, Material
5         | Offer2 | Jane Doe | Consulting, Time

我合并表的查询如下所示:

-- Delivery Methods
    SELECT [D].DeliveryVersionId, [DM].Label
        INTO #TmpLabels
    FROM [MySchema].[Delivery] [D]
    INNER JOIN dbo.DeliveryMethods [DM] ON [DM].DeliveryMethodId = [D].DeliveryMethodId

    SELECT DeliveryVersionId,
        Label = STUFF(
            (SELECT ',' + Label FROM #TmpLabels FOR XML PATH('')), 1, 1, ''
        )
    FROM #TmpLabels 
    GROUP BY DeliveryVersionId

-- FinalResults
    SELECT [O].OfferId, [O].Title, [O].OfferManager, [DL].Label AS Delivery
    FROM #BasicOffers [O]
    INNER JOIN #TmpLabels [DL] ON [DL].DeliveryVersionId = [O].DeliveryVersionId

我真的不知道,如果它是将所有内容存储到临时表中然后合并或一次性选择它的最佳解决方案。

在任何情况下,遗憾的是我的查询似乎无法正常工作。

您对如何解决此问题有所了解吗?

非常感谢!

2 个答案:

答案 0 :(得分:2)

;WITH BasicOffers(OfferId,Tile,Manager,DeliveryVersionId )
AS
(
SELECT 4 , 'Offer1' , 'John Doe' ,  1   Union all             
SELECT 5 , 'Offer2' , 'Jane Doe' ,  2                
)
,TmpLabels(DeliveryVersionId,Label)
AS
(
SELECT 1 , 'Service, Time, Material '  Union all  
SELECT 2 , 'Consulting, Time'        
)
Select B.OfferId,B.Tile,B.Manager, T.Label AS Delivery  
From BasicOffers B
INNER JOIN TmpLabels T
ON T.DeliveryVersionId=B.DeliveryVersionId

--To COnvert into Comma separated
;WITH Cte_Convert(OfferId,Title,Manager,Delivery )
AS
(

SELECT 4 , 'Offer1' , 'John Doe' , 'Service'    Union All
SELECT 4 , 'Offer1' , 'John Doe' , 'Time'       Union All
SELECT 4 , 'Offer1' , 'John Doe' , 'Material'   Union All
SELECT 5 , 'Offer2' , 'Jane Doe' , 'Consulting' Union All
SELECT 5 , 'Offer2' , 'Jane Doe' , 'Time'
)
Select DISTINCT OfferId,Title,Manager,STUFF((SELECT DISTINCT ',' + CAST(i.Delivery AS VARCHAR(20)) FROM Cte_Convert i
WHERE i.OfferId=o.OfferId
FOR XML PATH ('')),1,1,'')AS Delivery
FROM Cte_Convert o
 I Assume either above or below code might be useful 
  --Split Delivery  column as comma separted in below Cte
 ;WITH Cte_Convert(OfferId,Title,Manager,Delivery )
AS
(
SELECT 4 , 'Offer1' , 'John Doe' , 'Service'    Union All
SELECT 4 , 'Offer1' , 'John Doe' , 'Time'       Union All
SELECT 4 , 'Offer1' , 'John Doe' , 'Material'   Union All
SELECT 5 , 'Offer2' , 'Jane Doe' , 'Consulting' Union All
SELECT 5 , 'Offer2' , 'Jane Doe' , 'Time'
)
,Final
AS
(
SELECT ROW_NUMBER()OVER(Order by (SELECT ''))AS DeliveryVersionId ,* FROM
(
SELECT  DISTINCT OfferId,Title,Manager,STUFF((SELECT DISTINCT ',' + CAST(i.Delivery AS VARCHAR(20)) FROM Cte_Convert i
WHERE i.OfferId=o.OfferId
FOR XML PATH ('')),1,1,'')AS Label
FROM Cte_Convert o
)dt
)
,BasicOffers(OfferId,Tile,Manager,DeliveryVersionId )
AS
(
SELECT 4 , 'Offer1' , 'John Doe' ,  1   Union all             
SELECT 5 , 'Offer2' , 'Jane Doe' ,  2                
)
,TmpLabels(DeliveryVersionId,Label)
AS
(
SELECT 1 , 'Service, Time, Material '  Union all  
SELECT 2 , 'Consulting, Time'        
)
Select B.OfferId,B.Tile,B.Manager, T.Label AS Delivery  
From BasicOffers B
INNER JOIN Final T
ON T.DeliveryVersionId=B.DeliveryVersionId

答案 1 :(得分:0)

我在它们之间使用临时表和左外连接来解决它。请注意,我认为 DeliveryVersionId 是#TmpLabels中的主键。

如果有帮助,请告诉我。

DECLARE @BasicOffers Table (
OfferId int,
Tile varchar(100),
Manager varchar(100),
DeliveryVersionId int)


insert into @BasicOffers values (4,'Offer1','John Doe',1)
insert into @BasicOffers values (5,'Offer2','Jane Doe',2)

DECLARE @TmpLabels Table (
DeliveryVersionId int,
Label varchar(100)
)


insert into @TmpLabels values (1,'Service, Time, Material')
insert into @TmpLabels values (2,'Consulting, Time')

Select OfferId, Tile, Manager, Label as Delivery
From @BasicOffers A Left Outer join
@TmpLabels B on A.DeliveryVersionId=B.DeliveryVersionId