0

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_PMX_ICRI]
      ,[U_PMX_LOCA]
      ,[U_PMX_LOUN]
      ,[U_PMX_ILRP]
      ,[U_PMX_ILRC]
      ,[U_PMX_NRSN]
      ,[U_PMX_NRSR]
      ,[U_PMX_QSPR]
      ,[U_PMX_QSCR]
      ,[U_PMX_QSRC]
      ,[U_PMX_RQSR]
      ,[U_PMX_QSSR]
      ,[U_PMX_SBCP]
      ,[U_PMX_BCTY]
      ,[U_PMX_EXDP]
      ,[U_PMX_EXDR]
      ,[U_PMX_PBCT]
      ,[U_PMX_PBCO]
      ,[U_PMX_SBCT]
      ,[U_PMX_SBCO]
      ,[U_PMX_PILR]
      ,[U_PMX_UOMD]
      ,[U_PMX_UOM2]
      ,[U_PMX_UM2D]
      ,[U_PMX_DQLU]
      ,[U_PMX_DQU2]
      ,[U_PMX_HBN2]
      ,[U_PMX_SEVE]
      ,[U_PMX_SQTY]
      ,[U_PMX_ALUS]
      ,[U_PMX_PREM]
      ,[U_PMX_PIMG]
      ,[U_PMX_PURM]
      ,[U_PMX_PURP]
      ,[U_PMX_SARM]
      ,[U_PMX_SARP]
      ,[U_PMX_VIDE]
      ,[U_PMX_CUDE]
      ,[U_PMX_HNVA]
      ,[U_PMX_LSPT]
      ,[U_PMX_AMBS]
      ,[U_PMX_PRMR]
      ,[U_PMX_DLCS]
      ,[U_PMX_DLCP]
      ,[U_PMX_SLDR]
      ,[U_PMX_HSER]
      ,[U_PMX_TLSN]
      ,[U_PMX_ASLA]
      ,[U_PMX_APPP]
      ,[U_PMX_APDP]
      ,[U_PMX_DPQT]
      ,[U_PMX_PILP]
      ,[U_PMX_RQPL]
      ,[U_PMX_ESQM]
      ,[U_PMX_PITY]
      ,[U_PMX_SNFO]
      ,[U_PMX_PTY2]
      ,[U_PMX_UFTR]
      ,[U_PMX_AQIL]
      ,[U_PMX_UWAF]
      ,[U_PMX_BUPQ]
      ,[U_PMX_CSOR]
      ,[U_PMX_3PIP]
      ,[U_PMX_3POP]
      ,[U_PMX_PIBP]
      ,[U_PMX_NIOP]
      ,[U_PMX_FSNC]
      ,[U_PMX_ISLT]
      ,[U_PMX_WOST]
      ,[U_PMX_WCRE]
      ,[U_PMX_ICAW]
      ,[U_PMX_UMMT]
      ,[U_PMX_U2MT]
      ,[U_PMX_UMIT]
      ,[U_PMX_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:

Current Data Output

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:

Desired Data Output

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.

  • 1
    Please provide sample data and expected results as per [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example), with data and expected results as text in your question, [not as images](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) – Stu Aug 26 '21 at 11:02
  • Which version of SQL Server do you have? – Charlieface Aug 26 '21 at 12:18
  • I'm using SQL Server Management Studio v15.0.18330.0 – tbizzlebozzle Aug 26 '21 at 15:41
  • @Stu – apologies... I can't work out how to get the table data in there as text rather than images! – tbizzlebozzle Aug 26 '21 at 15:48
  • @Charlieface - Sorry, self taught! Microsoft SQL Server 2012 (SP4-GDR) (KB4583465) - 11.0.7507.2 (X64) Nov 1 2020 00:48:37 Copyright (c) Microsoft Corporation Standard Edition (64-bit) on Windows NT 6.3 (Build 9600: ) (Hypervisor) – tbizzlebozzle Aug 26 '21 at 15:49
  • @Charlieface I've updated the question with the tables – I had previously substituted more readable names for the tables. I've now put in the actual table names. – tbizzlebozzle Aug 26 '21 at 16:09
  • That script is useless, we need actual data. Sorry I gave you wron instructions, check here instead https://stackoverflow.com/questions/982568/what-is-the-best-way-to-auto-generate-insert-statements-for-a-sql-server-table – Charlieface Aug 26 '21 at 16:16
  • @Charlieface unfortunately the no data is yielded from the FLN_ITG_DISC tables and the OITM is absolutely all product data – hundreds of thousands of lines and is too big to export! – tbizzlebozzle Aug 26 '21 at 16:37
  • OK copy out say 10 rows and create a script manually. We can't work with images – Charlieface Aug 26 '21 at 16:45

0 Answers0