I can't find an answer to this anywhere – hoping there is one! I'd like to streamline outputted concatenated values where they're the same while retaining a distinct identifier column.
I have a number of different item groups in a table that have associated margin titles and margin percentages set in another table. I can get a full list of all the items using the below code:
SELECT DISTINCT
dbo.OITM.itmsgrpcod AS 'ItemGroupCode',
CONVERT(VARCHAR(8000),
SUBSTRING(
(
SELECT 'itemgroup_' + [dbo].[@FLN_ITG_DISC_HD].[U_FLN_ITGP] + '_' +
[dbo].[@FLN_ITG_DISC_LN].[U_FLN_DSLV]
+ '_' + [dbo].[@FLN_ITG_DISC_LN].[U_FLN_PERC] + ', '
AS 'data()'
FROM [dbo].[@FLN_ITG_DISC_HD] INNER JOIN [dbo].[@FLN_ITG_DISC_LN] ON [dbo].[@FLN_ITG_DISC_HD].[DocEntry] = [dbo].[@FLN_ITG_DISC_LN].[DocEntry]
WHERE [dbo].[@FLN_ITG_DISC_HD].[U_FLN_ITGP] = dbo.OITM.itmsgrpcod
ORDER BY [dbo].[@FLN_ITG_DISC_HD].[U_FLN_ITGP]
FOR XML PATH ('')
), 1, 9999999)
) AS 'PRODUCTTAGS'
FROM
dbo.OITM
INNER JOIN [dbo].[@FLN_ITG_DISC_HD] ON dbo.OITM.itmsgrpcod = [dbo].[@FLN_ITG_DISC_HD].[U_FLN_ITGP]
INNER JOIN [dbo].[@FLN_ITG_DISC_LN] ON [dbo].[@FLN_ITG_DISC_HD].[DocEntry] = [dbo].[@FLN_ITG_DISC_LN].[DocEntry]
Here is the script to clipboard info for the tables:
USE [XXXX]
GO
SELECT [Code]
,[Name]
,[DocEntry]
,[Canceled]
,[Object]
,[LogInst]
,[UserSign]
,[Transfered]
,[CreateDate]
,[CreateTime]
,[UpdateDate]
,[UpdateTime]
,[DataSource]
,[U_FLN_ITGP]
,[U_FLN_IGDC]
FROM [dbo].[@FLN_BP_DISC_ITG_HD]
GO
USE [XXXX]
GO
SELECT [Code]
,[LineId]
,[Object]
,[LogInst]
,[U_FLN_DSLV]
,[U_FLN_DSPR]
FROM [dbo].[@FLN_BP_DISC_ITG_LN]
GO
USE [XXXXXX]
GO
SELECT [ItemCode]
,[ItemName]
,[FrgnName]
,[ItmsGrpCod]
,[CstGrpCode]
,[VatGourpSa]
,[CodeBars]
,[VATLiable]
,[PrchseItem]
,[SellItem]
,[InvntItem]
,[OnHand]
,[IsCommited]
,[OnOrder]
,[IncomeAcct]
,[ExmptIncom]
,[MaxLevel]
,[DfltWH]
,[CardCode]
,[SuppCatNum]
,[BuyUnitMsr]
,[NumInBuy]
,[ReorderQty]
,[MinLevel]
,[LstEvlPric]
,[LstEvlDate]
,[CustomPer]
,[Canceled]
,[MnufctTime]
,[WholSlsTax]
,[RetilrTax]
,[SpcialDisc]
,[DscountCod]
,[TrackSales]
,[SalUnitMsr]
,[NumInSale]
,[Consig]
,[QueryGroup]
,[Counted]
,[OpenBlnc]
,[EvalSystem]
,[UserSign]
,[FREE]
,[PicturName]
,[Transfered]
,[BlncTrnsfr]
,[UserText]
,[SerialNum]
,[CommisPcnt]
,[CommisSum]
,[CommisGrp]
,[TreeType]
,[TreeQty]
,[LastPurPrc]
,[LastPurCur]
,[LastPurDat]
,[ExitCur]
,[ExitPrice]
,[ExitWH]
,[AssetItem]
,[WasCounted]
,[ManSerNum]
,[SHeight1]
,[SHght1Unit]
,[SHeight2]
,[SHght2Unit]
,[SWidth1]
,[SWdth1Unit]
,[SWidth2]
,[SWdth2Unit]
,[SLength1]
,[SLen1Unit]
,[Slength2]
,[SLen2Unit]
,[SVolume]
,[SVolUnit]
,[SWeight1]
,[SWght1Unit]
,[SWeight2]
,[SWght2Unit]
,[BHeight1]
,[BHght1Unit]
,[BHeight2]
,[BHght2Unit]
,[BWidth1]
,[BWdth1Unit]
,[BWidth2]
,[BWdth2Unit]
,[BLength1]
,[BLen1Unit]
,[Blength2]
,[BLen2Unit]
,[BVolume]
,[BVolUnit]
,[BWeight1]
,[BWght1Unit]
,[BWeight2]
,[BWght2Unit]
,[FixCurrCms]
,[FirmCode]
,[LstSalDate]
,[QryGroup1]
,[QryGroup2]
,[QryGroup3]
,[QryGroup4]
,[QryGroup5]
,[QryGroup6]
,[QryGroup7]
,[QryGroup8]
,[QryGroup9]
,[QryGroup10]
,[QryGroup11]
,[QryGroup12]
,[QryGroup13]
,[QryGroup14]
,[QryGroup15]
,[QryGroup16]
,[QryGroup17]
,[QryGroup18]
,[QryGroup19]
,[QryGroup20]
,[QryGroup21]
,[QryGroup22]
,[QryGroup23]
,[QryGroup24]
,[QryGroup25]
,[QryGroup26]
,[QryGroup27]
,[QryGroup28]
,[QryGroup29]
,[QryGroup30]
,[QryGroup31]
,[QryGroup32]
,[QryGroup33]
,[QryGroup34]
,[QryGroup35]
,[QryGroup36]
,[QryGroup37]
,[QryGroup38]
,[QryGroup39]
,[QryGroup40]
,[QryGroup41]
,[QryGroup42]
,[QryGroup43]
,[QryGroup44]
,[QryGroup45]
,[QryGroup46]
,[QryGroup47]
,[QryGroup48]
,[QryGroup49]
,[QryGroup50]
,[QryGroup51]
,[QryGroup52]
,[QryGroup53]
,[QryGroup54]
,[QryGroup55]
,[QryGroup56]
,[QryGroup57]
,[QryGroup58]
,[QryGroup59]
,[QryGroup60]
,[QryGroup61]
,[QryGroup62]
,[QryGroup63]
,[QryGroup64]
,[CreateDate]
,[UpdateDate]
,[ExportCode]
,[SalFactor1]
,[SalFactor2]
,[SalFactor3]
,[SalFactor4]
,[PurFactor1]
,[PurFactor2]
,[PurFactor3]
,[PurFactor4]
,[SalFormula]
,[PurFormula]
,[VatGroupPu]
,[AvgPrice]
,[PurPackMsr]
,[PurPackUn]
,[SalPackMsr]
,[SalPackUn]
,[SCNCounter]
,[ManBtchNum]
,[ManOutOnly]
,[DataSource]
,[validFor]
,[validFrom]
,[validTo]
,[frozenFor]
,[frozenFrom]
,[frozenTo]
,[BlockOut]
,[ValidComm]
,[FrozenComm]
,[LogInstanc]
,[ObjType]
,[SWW]
,[Deleted]
,[DocEntry]
,[ExpensAcct]
,[FrgnInAcct]
,[ShipType]
,[GLMethod]
,[ECInAcct]
,[FrgnExpAcc]
,[ECExpAcc]
,[TaxType]
,[ByWh]
,[WTLiable]
,[ItemType]
,[WarrntTmpl]
,[BaseUnit]
,[CountryOrg]
,[StockValue]
,[Phantom]
,[IssueMthd]
,[FREE1]
,[PricingPrc]
,[MngMethod]
,[ReorderPnt]
,[InvntryUom]
,[PlaningSys]
,[PrcrmntMtd]
,[OrdrIntrvl]
,[OrdrMulti]
,[MinOrdrQty]
,[LeadTime]
,[IndirctTax]
,[TaxCodeAR]
,[TaxCodeAP]
,[OSvcCode]
,[ISvcCode]
,[ServiceGrp]
,[NCMCode]
,[MatType]
,[MatGrp]
,[ProductSrc]
,[ServiceCtg]
,[ItemClass]
,[Excisable]
,[ChapterID]
,[NotifyASN]
,[ProAssNum]
,[AssblValue]
,[DNFEntry]
,[UserSign2]
,[Spec]
,[TaxCtg]
,[Series]
,[Number]
,[FuelCode]
,[BeverTblC]
,[BeverGrpC]
,[BeverTM]
,[Attachment]
,[AtcEntry]
,[ToleranDay]
,[UgpEntry]
,[PUoMEntry]
,[SUoMEntry]
,[IUoMEntry]
,[IssuePriBy]
,[AssetClass]
,[AssetGroup]
,[InventryNo]
,[Technician]
,[Employee]
,[Location]
,[StatAsset]
,[Cession]
,[DeacAftUL]
,[AsstStatus]
,[CapDate]
,[AcqDate]
,[RetDate]
,[GLPickMeth]
,[NoDiscount]
,[MgrByQty]
,[AssetRmk1]
,[AssetRmk2]
,[AssetAmnt1]
,[AssetAmnt2]
,[DeprGroup]
,[AssetSerNo]
,[CntUnitMsr]
,[NumInCnt]
,[INUoMEntry]
,[OneBOneRec]
,[RuleCode]
,[ScsCode]
,[SpProdType]
,[IWeight1]
,[IWght1Unit]
,[IWeight2]
,[IWght2Unit]
,[CompoWH]
,[CreateTS]
,[UpdateTS]
,[VirtAstItm]
,[SouVirAsst]
,[InCostRoll]
,[PrdStdCst]
,[EnAstSeri]
,[LinkRsc]
,[OnHldPert]
,[onHldLimt]
,[PriceUnit]
,[GSTRelevnt]
,[SACEntry]
,[GstTaxCtg]
,[AssVal4WTR]
,[ExcImpQUoM]
,[ExcFixAmnt]
,[ExcRate]
,[SOIExc]
,[TNVED]
,[Imported]
,[AutoBatch]
,[U_AZU_COMM]
,[U_AZU_SUPP]
,[U_AZU_SUPPCF]
,[U_SDB_WHLO]
,[U_SDB_OFLO]
,[U_BA_IsFA]
,[U_BA_TypID]
,[U_BA_NumID]
,[U_BA_LVAFrom]
,[U_BA_LVA]
,[U_BXPArea]
,[U_BXPInvTu]
,[U_BXPLMSIn]
,[U_BXPIsMlt]
,[U_BXPIsSgB]
,[U_BXPCrdCd]
,[U_BXPPmInv]
,[U_BXPPkEmQ]
,[U_BXPPkLcN]
,[U_BXPSLReq]
,[U_BOY_TB_0]
,[U_DisCont]
,[U_DiscRoad]
,[U_DiscMTB]
,[U_DiscTrack]
,[U_DiscAll]
,[U_ParentDescription]
,[U_WebEnabled]
,[U_DiscCX]
,[U_DiscTRI]
,[U_Composition]
,[U_ParentSKUCode]
,[U_Col1Code]
,[U_Col2Code]
,[U_Col3Code]
,[U_Col4Code]
,[U_Col5Code]
,[U_Size]
,[U_Season]
,[U_AttributeGroup]
,[U_Col1Desc]
,[U_Col2Desc]
,[U_Col3Desc]
,[U_Col4Desc]
,[U_Col5Desc]
,[U_Gender]
,[U_DiscHybrid]
,[U_DiscBMX]
,[U_Discontinued]
,[U_Colour]
,[U_ExcludeLoyaltyProm]
,[U_IsB2CSaleable]
,[U_Merchandising]
,[U_U_ShopifyEnabled]
,[U_U_ShopifyHandle]
,[U_U_ShopifyTitle]
,[U_U_ShopifyVendor]
,[U_Exclusive]
,[U_Outlet]
,[U_SyncFlag]
,[U_SyncB2C]
,[U_ParentSKU]
,[U_VariantWebID]
,[U_VariantAdded]
,[U_InventoryItemId]
,[U_Range]
,[U_FLN_TC_TaskId]
,[U_FLN_TC_Date]
,[U_FLN_TC_Time]
,[U_PMX_HBBD]
,[U_PMX_SLID]
,[U_PMX_RETR]
,[U_PM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X_UMPU]
,[U_PMX_UMSA]
,[U_PMX_CSUM]
,[U_PMX_CSU2]
,[U_PMX_DQUM]
,[U_PMX_CWTO]
,[U_PMX_PCSA]
,[U_PMX_PCPU]
,[U_PMX_CWSC]
,[U_PMX_CWQC]
,[U_PMX_PRWD]
,[U_SyncB2B]
,[U_Clearance]
,[U_SpecialOrder]
,[U_ExpCommCode]
,[U_NotForResale]
,[U_Merch_Hierachy]
,[U_Current]
,[U_SyncB2BUK]
,[U_Composition_1]
,[U_Composition_2]
,[U_Composition_3]
,[U_Composition_4]
,[U_Composition_6]
,[U_Composition_7]
,[U_Composition_8]
,[U_Composition_9]
,[U_Composition_10]
,[U_Composition_11]
,[U_Composition_12]
,[U_Composition_13]
,[U_Composition_14]
,[U_Composition_15]
,[U_Composition_17]
,[U_Composition_18]
,[U_Composition_19]
,[U_Composition_20]
,[U_Composition_5]
,[U_Composition_16]
,[U_ImpCommCode]
,[U_SyncB2BIE]
,[U_WarrantyItem]
,[U_EmbargoDate]
,[U_LaunchDate]
,[U_SMUItem]
,[U_ManSize]
,[U_ShopifyProdType]
,[U_AgeRange]
,[U_SeasonType]
,[U_DiscGravel]
FROM [dbo].[OITM]
GO
This outputs data in two columns with all the distinct item groups and corresponding concatenation of margin levels like this:
However, while all the ItemGroupCodes are distinct, some rows in the ProductTags column are identical to others with the exception of the ItemGroup number. Where this is the case, I'd like to replace this number with a common identifier. So for example, where ItemGroups 100, 101 and 108 have all the same margin values, the number in the concatenated values would be replaced with GROUPA, so the output will become:
The reasoning is that I can then put this data into a temp table and call it into another query listing all SKUs based on the itemgroup number.