Having Trouble with FOR XML Path - No Concatenating - sql

I have a situation where an order can contain multiple license purchases - and if the order does contain multiple licenses, I want to display the license descriptions in a single cell with the values separated by commas. If we were on SQL 2017, I could use STRING_AGG but we are on SQL 2016 so I am trying the tried and true STUFF / FOR XML Path method.
From the screenshot below, Customer 4341073 had two license purchases on Order ID 18519173:
When I add the STUFF / FOR XML Path to the T-SQL, I am not able to achieve the desired result of showing the license description in the same record - each license still has it's own row.
SELECT x.CustomerID ,
x.ATOLicenseTypeID ,
x.ATOLicense ,
x.AuthorizationBeginDate ,
x.AuthorizationEndDate ,
x.OrderID ,
x.OrderDate ,
STUFF ( (
SELECT ',' + lt.description
FROM dbo.LicenseTypes AS lt
--INNER JOIN #XMLPATH ON lt.id = x.OrderLicenseTypeID
WHERE lt.id = x.OrderLicenseTypeID
--GROUP BY ',' + lt.description
FOR XML PATH ( '' )
) , 1 , 1 , '' ) AS Licenses
FROM #XMLPATH AS x
--GROUP BY x.CustomerID ,
-- x.ATOLicenseTypeID ,
-- x.ATOLicense ,
-- x.AuthorizationBeginDate ,
-- x.AuthorizationEndDate ,
-- x.OrderID ,
-- x.OrderDate ,
-- x.OrderLicenseTypeID;
I've tried different ways to join the sub-query to the outer query and added and removed GROUP BY to achieve the desired result but nothing is working for me.
Any suggestions on where I am going wrong with this query?
Sample dataset:
DROP TABLE IF EXISTS #XMLPATH;
CREATE TABLE #XMLPATH
(
CustomerID INT ,
ATOLicenseTypeID INT ,
ATOLicense VARCHAR (500) ,
AuthorizationBeginDate DATE ,
AuthorizationEndDate DATE ,
OrderID INT ,
OrderDate DATETIME ,
OrderLicenseTypeID INT
);
INSERT INTO #XMLPATH
VALUES ( 4341073, 52, 'Temporary Resident Fishing', N'2019-01-07T00:00:00', N'2019-01-07T00:00:00', 18519136, N'2019-01-07T12:01:55.317', 2141 ) ,
( 4341073, 52, 'Temporary Resident Fishing', N'2019-01-07T00:00:00', N'2019-01-07T00:00:00', 18519173, N'2019-01-07T12:34:13.107', 204 ) ,
( 4341073, 52, 'Temporary Resident Fishing', N'2019-01-07T00:00:00', N'2019-01-07T00:00:00', 18519173, N'2019-01-07T12:34:13.107', 2141 );
SELECT * FROM #XMLPATH;
SELECT x.CustomerID ,
x.ATOLicenseTypeID ,
x.ATOLicense ,
x.AuthorizationBeginDate ,
x.AuthorizationEndDate ,
x.OrderID ,
x.OrderDate ,
STUFF ( (
SELECT ',' + lt.description
FROM dbo.LicenseTypes AS lt
--INNER JOIN #XMLPATH ON lt.id = x.OrderLicenseTypeID
WHERE lt.id = x.OrderLicenseTypeID
--GROUP BY ',' + lt.description
FOR XML PATH ( '' )
) , 1 , 1 , '' ) AS Licenses
FROM #XMLPATH AS x
GROUP BY x.CustomerID ,
x.ATOLicenseTypeID ,
x.ATOLicense ,
x.AuthorizationBeginDate ,
x.AuthorizationEndDate ,
x.OrderID ,
x.OrderDate ,
x.OrderLicenseTypeID;

In order to get all rows of one OrderID as one result-row, you must not include the separating information (the OrderLicenseTypeID) into the GROUP BY. But then you have the issue you've encountered: You cannot use this ID within your FOR XML construct.
The trick is (as your out-commented trials show), to add the source table to the sub-select and filter there with a grouped column. But you have to use different aliases to deal with them as two different sets. Try this:
(I had to add one more temp table to test this...)
SELECT x.CustomerID ,
x.ATOLicenseTypeID ,
x.ATOLicense ,
x.AuthorizationBeginDate ,
x.AuthorizationEndDate ,
x.OrderID ,
x.OrderDate ,
STUFF ( (
SELECT ',' + lt.description
FROM #XMLPATH x2
INNER JOIN #LicenseTypes AS lt ON lt.id=x2.OrderLicenseTypeID
WHERE x2.OrderID = x.OrderID --you might need to add more columns here....
--in most cases we want to add an ORDER BY
FOR XML PATH ( '' )
) , 1 , 1 , '' ) AS Licenses
FROM #XMLPATH AS x
GROUP BY x.CustomerID ,
x.ATOLicenseTypeID ,
x.ATOLicense ,
x.AuthorizationBeginDate ,
x.AuthorizationEndDate ,
x.OrderID ,
x.OrderDate;
Btw: Starting with v2017 there is STRING_AGG(), which makes this much easier...

Related

How to write below query in PostgreSQL?

I am migrating procedure from SQL Server to PostgreSQL. I can not write this query in PostgreSQL. How to use temp within WITH clause in PostgreSQL?
WITH TEMP AS
(
SELECT a.AssignNo as LowestAssignNo
, a.AssignNo
, a.AssignName
, HierarchyLevel
, UpperAssignNo
FROM TAssignInfo a
WHERE DeleteYesNo = 'N'
AND UseYesNo = 'Y'
UNION ALL
SELECT a.LowestAssignNo
, b.AssignNo
, b.AssignName
, b.HierarchyLevel
, b.UpperAssignNo
FROM TEMP a
INNER JOIN TAssignInfo b
ON a.UpperAssignNo = b.AssignNo
WHERE DeleteYesNo = 'N'
AND UseYesNo = 'Y'
)
SELECT DISTINCT LowestAssignNo
, STUFF(( SELECT '|' + AssignName
FROM TEMP A
WHERE a.LowestAssignNo = b.LowestAssignNo
AND HierarchyLevel > 1
ORDER BY HierarchyLevel
FOR XML PATH('')
), 1, 1, '') AS AssignNamePath
INTO #NewAssignInfo
FROM TEMP B
WHERE B.HierarchyLevel <> 0
ORDER BY LowestAssignNo DESC
enter image description here

Stop Recursion Once Condition Satisfied

I am using a CTE to explode out a Bill of Materials and need to locate all those materials that have recursive components.
What I was attempting, was to limit the number of cycles (levels) deep, by setting BOM_Level in the child node to a maximum bound:
exec pr_sys_drop_object '#BOMExploded'
;with BOM
as
(
select
Prod_Plant_CD
, Demand_Plant_CD
, material_cd
, component_cd
, component_quantity
, component_quantity AS Calculated_Component_Quantity
, BOM_Level
, Demand_Quantity
, CONVERT(float,1) AS Produced_Ratio
, Material_CD AS Demand_Material_CD
from #firstLevel a
UNION ALL
SELECT
b.Plant_CD as 'Prod_Plant_CD'
, a.Demand_Plant_CD
, b.Material_CD
, b.Component_CD
, b.component_quantity
, b.component_quantity
, a.BOM_Level + 1
, a.Demand_Quantity
, a.Produced_Ratio * a.Component_Quantity -- Produced Quantity for the current level = Produced Quantity (level -1) * Component_Quantity (level -1)
, a.Demand_Material_CD
FROM BOM a
inner join #BOM_ProdVersion_Priority b
on a.component_cd = b.material_cd
inner join #base_routes c
on a.Demand_Plant_CD = c.Recipient_Plant_CD
and b.Plant_CD = c.Source_Plant_CD
and c.Material_CD = b.Material_CD -- Need to have material_cd to link
where b.Material_CD != b.Component_CD
and b.Component_Quantity > 0
and BOM_Level < 5 -- Give the max number of levels deep we are allowed to cyncle to
)
select *
into #BOMExploded
from BOM
OPTION (MAXRECURSION 20)
Using this method however, would require a post-process to locate when the cycling on the recursive component level started, then back trace.
How can a CTE recursive query be stopped given a certain condition?
ie. when top-level material_cd = component_cd for a deeper BOM_Level
If I understand you correctly, you don't need to stop at a certain depth/level, or rather you want to stop at a certain level, but you also need to stop in case you start cycling through materials repeatedly.
In the case of the following recursive path: mat_1->mat_2->mat_3->mat_1, you would want to stop before that last mat_1 starts cycling again to mat_2 and so on.
If that's correct, then your best bet is to add a Path field to your recursive query that tracks each term that it finds as it moves down the recursive path:
exec pr_sys_drop_object '#BOMExploded'
;with BOM
as
(
select
Prod_Plant_CD
, Demand_Plant_CD
, material_cd
, component_cd
, component_quantity
, component_quantity AS Calculated_Component_Quantity
, BOM_Level
, Demand_Quantity
, CONVERT(float,1) AS Produced_Ratio
, Material_CD AS Demand_Material_CD
, CAST(material_cd AS VARCHAR(100)) AS Path
from #firstLevel a
UNION ALL
SELECT
b.Plant_CD as 'Prod_Plant_CD'
, a.Demand_Plant_CD
, b.Material_CD
, b.Component_CD
, b.component_quantity
, b.component_quantity
, a.BOM_Level + 1
, a.Demand_Quantity
, a.Produced_Ratio * a.Component_Quantity -- Produced Quantity for the current level = Produced Quantity (level -1) * Component_Quantity (level -1)
, a.Demand_Material_CD
, a.Path + '|' + b.material_cd
FROM BOM a
inner join #BOM_ProdVersion_Priority b
on a.component_cd = b.material_cd
inner join #base_routes c
on a.Demand_Plant_CD = c.Recipient_Plant_CD
and b.Plant_CD = c.Source_Plant_CD
and c.Material_CD = b.Material_CD -- Need to have material_cd to link
where b.Material_CD != b.Component_CD
and b.Component_Quantity > 0
and BOM_Level < 5 -- Give the max number of levels deep we are allowed to cyncle to
and a.path NOT LIKE '%' + b.material_cd + '%'
)
select *
into #BOMExploded
from BOM
OPTION (MAXRECURSION 20)
Now you have a path that is pipe delimited and you can test your current material_cd to see if it's already in the path. If it is, then you end that leg of the recursion to prevent cycling.
Updating to include a version where we capture material_cd cycles and only report those at the end of the recursion:
exec pr_sys_drop_object '#BOMExploded'
;with BOM
as
(
select
Prod_Plant_CD
, Demand_Plant_CD
, material_cd
, component_cd
, component_quantity
, component_quantity AS Calculated_Component_Quantity
, BOM_Level
, Demand_Quantity
, CONVERT(float,1) AS Produced_Ratio
, Material_CD AS Demand_Material_CD
, CAST(material_cd AS VARCHAR(100)) AS Path
, CAST(NULL AS CHAR(5)) as Cycle_Flag
, 0 as Cycle_Depth
from #firstLevel a
UNION ALL
SELECT
b.Plant_CD as 'Prod_Plant_CD'
, a.Demand_Plant_CD
, b.Material_CD
, b.Component_CD
, b.component_quantity
, b.component_quantity
, a.BOM_Level + 1
, a.Demand_Quantity
, a.Produced_Ratio * a.Component_Quantity -- Produced Quantity for the current level = Produced Quantity (level -1) * Component_Quantity (level -1)
, a.Demand_Material_CD
, a.Path + '|' + b.material_cd
, CASE WHEN a.path NOT LIKE '%' + b.material_cd + '%' then 'Cycle' END AS Cycle_Flag,
, CASE WHEN a.path NOT LIKE '%' + b.material_cd + '%' then a.Cycle_Depth + 1 END as Cycle_Depth
FROM BOM a
inner join #BOM_ProdVersion_Priority b
on a.component_cd = b.material_cd
inner join #base_routes c
on a.Demand_Plant_CD = c.Recipient_Plant_CD
and b.Plant_CD = c.Source_Plant_CD
and c.Material_CD = b.Material_CD -- Need to have material_cd to link
where b.Material_CD != b.Component_CD
and b.Component_Quantity > 0
and a.cycle_depth < 2 --stop the query if we start cycling, but only after we capture at least one full cycle
)
select *
into #BOMExploded
from BOM
WHERE cycle_flag IS NOT NULL
OPTION (MAXRECURSION 20)
This will capture cycle_depth which is a counter that measures how deep into a cycle we get. We stop the recursion after we get to cycle_depth of 1 so the cycle can be captures in the final select.

SQL Pivot Issue

If you could please help with this. The code generates an error:
Msg 8156, Level 16, State 1, Line 236
The column 'Classification_Value_Id' was specified multiple times for 'piv1'.
I am doing this on SQL Server. The steps for the code is as follows:
1. Unpivot the data from the source table DB.[dbo].[Classification] into one column
2. Join this unpivoted data to a table called DB.dbo.[Classification_Value] to return/add the column 'cv.Classification_Name' to the data set
3. Pivot this dataset (This is the part returning the error)
CODE:
SELECT
activityCode
, actjvPartnerRef
, actMonth
, actSalesChannel
, addCBPCharge
, agentId
, appType
, areaCode
--SELECT
--polRef,[Arrangement_Id],UnpivotedData.Classification_Value_Id,UnpivotedData.Classification_Scheme_Id,ColValues, ColNames,cv.Classification_Name
FROM
(
SELECT top 10
[polRef]
, [Arrangement_Id]
, [Classification_Scheme_Id]
, [Classification_Value_Id]
-- ,[Arrangement_Classification_Type_Id]
-- ,[Effective_TimeStamp]
-- ,[End_date]
, CAST((ISNULL([character_measure],'')) AS NVARCHAR(MAX)) AS character_measure
, CAST((ISNULL([datetime_measure],'')) AS NVARCHAR(MAX)) AS datetime_measure
, CAST([decimal_measure] AS NVARCHAR(MAX)) AS decimal_measure
, CAST((ISNULL([integer_measure],'')) AS NVARCHAR(MAX)) AS integer_measure
, CAST((ISNULL([logical_measure],'')) AS NVARCHAR(MAX)) AS logical_measure
, CAST((ISNULL([charmax_measure],'')) AS NVARCHAR(MAX)) AS charmax_measure
, CAST((ISNULL([long_measure],'')) AS NVARCHAR(MAX)) AS long_measure
FROM DB.[dbo].[Classification]
) AS SrcDataConverted
UNPIVOT
(
ColValues FOR ColNames IN
(
character_measure
, datetime_measure
, decimal_measure
, integer_measure
, logical_measure
, charmax_measure
, long_measure
)
) AS UnpivotedData
LEFT JOIN DB.dbo.[Classification_Value] cv
ON cv.[Classification_Scheme_Id] = UnpivotedData.[Classification_Scheme_Id]
AND cv.Classification_Value_Id = UnpivotedData.Classification_Value_Id
PIVOT
(MAX(ColValues) for Classification_Name in (
activityCode
, actjvPartnerRef
, actMonth
, actSalesChannel
, addCBPCharge
, agentId
, appType
, areaCode
)) AS piv1;
Any help would be much appreciated
Thank you
StuarLC:
An additional derived table needs to wrap the results of the UNPIVOT before commencing the re-PIVOT, as the join introduces a duplicated Classification_Value_Id and Classification_Scheme_id, which is needed for the join.
select
activityCode
, actjvPartnerRef
, actMonth
, actSalesChannel
, addCBPCharge
, agentId
, appType
, areaCode
from (
SELECT polRef
, [Arrangement_Id]
, UnpivotedData.Classification_Value_Id
, UnpivotedData.Classification_Scheme_Id
, ColValues
, ColNames
, Classification_Name
FROM (
SELECT [polRef]
, [Arrangement_Id]
, [Classification_Scheme_Id]
, [Classification_Value_Id]
, CAST((ISNULL([character_measure],'')) AS NVARCHAR(MAX)) AS character_measure
, CAST((ISNULL([datetime_measure],'')) AS NVARCHAR(MAX)) AS datetime_measure
, CAST([decimal_measure] AS NVARCHAR(MAX)) AS decimal_measure
, CAST((ISNULL([integer_measure],'')) AS NVARCHAR(MAX)) AS integer_measure
, CAST((ISNULL([logical_measure],'')) AS NVARCHAR(MAX)) AS logical_measure
, CAST((ISNULL([charmax_measure],'')) AS NVARCHAR(MAX)) AS charmax_measure
, CAST((ISNULL([long_measure],'')) AS NVARCHAR(MAX)) AS long_measure
FROM DB.[dbo].[Classification]
) AS SrcDataConverted
UNPIVOT
(
ColValues FOR ColNames IN
(
character_measure
, datetime_measure
, decimal_measure
, integer_measure
, logical_measure
, charmax_measure
, long_measure
)
) AS UnpivotedData
LEFT JOIN
DB.dbo.[Classification_Value] cv
ON cv.[Classification_Scheme_Id] = UnpivotedData.[Classification_Scheme_Id]
AND cv.Classification_Value_Id = UnpivotedData.Classification_Value_Id
) as src
PIVOT
(
MAX(ColValues) for Classification_Name in (
activityCode
, actjvPartnerRef
, actMonth
, actSalesChannel
, addCBPCharge
, agentId
, appType
, areaCode
)
) AS piv1;

Incorrect syntax near ')' on the last parenthesis when changing from openquery to a subquery on a local database

Why does this query return "Incorrect syntax near ')' " on the last parenthesis when changing from openquery to a subquery on a local database?
You can see the commented lines where the openquery started before (and worked)
select right(rtrim(a.vouchernumber),9) as voucherkey
, a.vouchernumber
, vendorkey
, rtrim(invoicenumber) as invoicenumber
, invoicedate
, duedate
, documenttype
, PostDate
, qty
, amt =
CASE
WHEN documenttype = 'D' THEN -(amt)
WHEN b.transactiontype = 'V' THEN -(amt)
ELSE
amt
End
, extendedlineamt
, intercompanyid
, acct
, NaturalAcct
, c.segmentdescription as NaturalAcctDesc
, Dept
, f.segmentdescription as DeptDesc
, Site
, d.segmentdescription as SiteDesc
, Project
, e.segmentdescription as ProjDesc
, b.*
, rtrim(vendorname) as vendorname
, rtrim(vendoraddress1) as vendoraddress1
, vendoraddress2
, vendoraddress3
, vendorcity
, rtrim(vendorstate) as vendorstate
, rtrim(vendorzipcode) as vendorzipcode
, rtrim(vendoraddress1) + ' ' + rtrim(vendoraddress2) +' ' + rtrim(vendorcity) + ', ' + rtrim(vendorstate) as VendorAddress
, Dept + '.' + Site + '.' + Project as ProjectId
--from openquery (LinkedServer,
-- 'select a.vouchernumber
from (
select a.vouchernumber
, a.vendorkey
, a.invoicenumber
, a.invoicedate
, a.duedate
, a.documenttype
, a.recdate as PostDate
, b.qty
, b.amt
, b.extendedlineamt
, b.intercompanyid
, b.acct
, Substring(Acct, 1, 4) as NaturalAcct
, Substring(Acct,8, 3 ) as Site
, Substring(Acct,11, 4 ) as Project
, Substring(Acct,5,3) as Dept
, v.vendorname
, v.vendoraddress1
, v.vendoraddress2
, v.vendoraddress3
, v.vendorcity
, v.vendorstate
, v.vendorzipcode
from aphdr a
join aplin b
on a.vouchernumber = b.vouchernumber
left join apvend v
on a.vendorkey = v.vendorkey
where a.recdate between '2011-01-01' and '2012-10-02'
and cast(Substring(Acct,11, 4 ) as varchar(4)) like 'VIR'
and left(a.vouchernumber,1) <> 'G'
and Company = 'ASSFD'
)
You need to alias your subquery.
put a alias name at the end of your query.
You need to give the subquery an alias (or name).
here i've just added tbl1 to the very end
select right(rtrim(a.vouchernumber),9) as voucherkey
, a.vouchernumber
, vendorkey
, rtrim(invoicenumber) as invoicenumber
, invoicedate
, duedate
, documenttype
, PostDate
, qty
, amt =
CASE
WHEN documenttype = 'D' THEN -(amt)
WHEN b.transactiontype = 'V' THEN -(amt)
ELSE
amt
End
, extendedlineamt
, intercompanyid
, acct
, NaturalAcct
, c.segmentdescription as NaturalAcctDesc
, Dept
, f.segmentdescription as DeptDesc
, Site
, d.segmentdescription as SiteDesc
, Project
, e.segmentdescription as ProjDesc
, b.*
, rtrim(vendorname) as vendorname
, rtrim(vendoraddress1) as vendoraddress1
, vendoraddress2
, vendoraddress3
, vendorcity
, rtrim(vendorstate) as vendorstate
, rtrim(vendorzipcode) as vendorzipcode
, rtrim(vendoraddress1) + ' ' + rtrim(vendoraddress2) +' ' + rtrim(vendorcity) + ', ' + rtrim(vendorstate) as VendorAddress
, Dept + '.' + Site + '.' + Project as ProjectId
--from openquery (LinkedServer,
-- 'select a.vouchernumber
from (
select a.vouchernumber
, a.vendorkey
, a.invoicenumber
, a.invoicedate
, a.duedate
, a.documenttype
, a.recdate as PostDate
, b.qty
, b.amt
, b.extendedlineamt
, b.intercompanyid
, b.acct
, Substring(Acct, 1, 4) as NaturalAcct
, Substring(Acct,8, 3 ) as Site
, Substring(Acct,11, 4 ) as Project
, Substring(Acct,5,3) as Dept
, v.vendorname
, v.vendoraddress1
, v.vendoraddress2
, v.vendoraddress3
, v.vendorcity
, v.vendorstate
, v.vendorzipcode
from aphdr a
join aplin b
on a.vouchernumber = b.vouchernumber
left join apvend v
on a.vendorkey = v.vendorkey
where a.recdate between '2011-01-01' and '2012-10-02'
and cast(Substring(Acct,11, 4 ) as varchar(4)) like 'VIR'
and left(a.vouchernumber,1) <> 'G'
and Company = 'ASSFD'
) tbl1

Getting MAX Value of a Field for INSERT Statement

I am trying to insert a new record in a small settings table and I would like to get the MAX value of the DisplayOrder field and add 10 to it. I get errors with the MAX function in the value of the insert.
INSERT INTO tMrMenu
([ParentId]
,[DisplayOrder]
,[ItemName]
,[ItemDescription]
,[ItemURL]
,[ItemImage]
,[CreateDate]
,[CreateUser]
,[LastUpdateDate]
,[LastUpdateUser]
,[module]
,[isactive])
SELECT
( 1
, (SELECT MAX(DisplayOrder) + 10 FROM tMrMenu)
, 'EDM Summary Text'
, 'EDM Summary Text'
, '/Offline/Reports/EdmSummaryText'
, 'cli.gif'
, GETDATE()
, 'Garry.Bargsley'
, GETDATE()
, 'Garry.Bargsley'
, 'MR'
, 1)
You have extra parenthesis:
INSERT INTO tMrMenu
([ParentId]
,[DisplayOrder]
,[ItemName]
,[ItemDescription]
,[ItemURL]
,[ItemImage]
,[CreateDate]
,[CreateUser]
,[LastUpdateDate]
,[LastUpdateUser]
,[module]
,[isactive])
SELECT
1
, (SELECT MAX(DisplayOrder) + 10 FROM tMrMenu)
, 'EDM Summary Text'
, 'EDM Summary Text'
, '/Offline/Reports/EdmSummaryText'
, 'cli.gif'
, GETDATE()
, 'Garry.Bargsley'
, GETDATE()
, 'Garry.Bargsley'
, 'MR'
, 1
Declare #max int
SET #max = (SELECT MAX...)
INSERT INTO tMrMenu...
SELECT
...
#max,
...
INSERT INTO tMrMenu
([ParentId]
,[DisplayOrder]
,[ItemName]
,[ItemDescription]
,[ItemURL]
,[ItemImage]
,[CreateDate]
,[CreateUser]
,[LastUpdateDate]
,[LastUpdateUser]
,[module]
,[isactive])
SELECT MAX(DisplayOrder) + 10
, 'EDM Summary Text'
, 'EDM Summary Text'
, '/Offline/Reports/EdmSummaryText'
, 'cli.gif'
, GETDATE()
, 'Garry.Bargsley'
, GETDATE()
, 'Garry.Bargsley'
, 'MR'
, 1
FROM tMrMenu