|
johnnyrocket5
Starting Member
4 Posts |
Posted - 2008-03-19 : 15:12:40
|
| Hello everyone, I have the following situation, I know I could do this with cursors, but I would like to know if perhaps there's a way to do this without the use of them...(SQL code at the bottom of post)I have created a look up table with two columns: Type, Member - so, the data will look like this when queried:TYPE..............MEMBERPreparation......._SKU_MFG_GrindPreparation......._SKU_MFG_PressScrap Sales......._SKU_MFG_ScrapSalesSupport..........._SKU_MFG_APSupport..........._SKU_MFG_PPDITSupport _SKU_MFG_Purchasing_RawMaterialsThe look up table gives me a category and it's members which I need to get from another table - The members represent columns from the other table. These columns need to be summed into a new column according to category - so you'd end up with something like this:<Column1>Support = [_SKU_MFG_AP] + [SKU_MFG] + [PPDIT]+ [_SKU_MFG_Purchasing_RawMaterials]<Column2>Preparation = [_SKU_MFG_Grind] + [_SKU_MFG_Press]..and so on...So my question is...I need to build the dynamic sql that select the appropriate columns according to the categories and it's members and sum them up into columns. I know I could do this with a cursor that gets the distinct members, then another to loop through the members and generate the sql, but is there another way?Here's the code to create the tables:--CREATE LOOKUP TABLEif OBJECT_ID('tempdb..PSManufLookup', 'U') IS NOT NULL DROP TABLE PSManufLookupCREATE TABLE [dbo].[PSManufLookup]( [Type] [varchar](100) NULL, [Member] [varchar](100) NULL) ON [PRIMARY]--INSERT TYPES AND MEMBERSINSERT INTO PSMANUFLOOKUP (TYPE, MEMBER)SELECT 'Material', '_SKU_MFG_Cogs'unionSELECT 'Scrap Sales', '_SKU_MFG_ScrapSales'UNIONSELECT 'Inventory Var', '_SKU_MFG_InverntoryAdjustments'UNIONSELECT 'Preparation','_SKU_MFG_Grind'UNIONSELECT 'Preparation','_SKU_MFG_Press'UNIONSELECT 'Support','_SKU_MFG_AP'UNIONSELECT 'Support','_SKU_MFG_Purchasing_RawMaterials'UNIONSELECT 'Support','_SKU_MFG_SupplyChain_DomesticRawMaterials'UNIONSELECT 'Support','_SKU_MFG_SupplyChain_ImportRawMaterials'UNIONSELECT 'Support','_SKU_MFG_PPDIT'UNIONSELECT 'Volume Related Costs','_SKU_MFG_MatHandling'UNIONSELECT 'Volume Related Costs','_SKU_MFG_MROCost'UNIONSELECT 'Volume Related Costs','_SKU_MFG_Tooling'UNIONSELECT 'Volume Related Costs','_SKU_MFG_Wash'UNIONSELECT 'Mfg Carrying Cost','_SKU_MFG_CarryingCost'UNIONSELECT 'Mfg Freight','_SKU_MFG_InboundFreight'UNIONSELECT 'Mfg Freight','_SKU_MFG_InterplantFreight'UNIONSELECT 'Mfg Finishing','_SKU_MFG_Finishing'--table where data residesCREATE TABLE [dbo].[sourcedata]( [_SKU_NewProductFlag] [varchar](1) NULL, [_SKU_NoDaysActiveProduct] [int] NULL, [_SKU_NoUnitsSold] [int] NULL, [_SKU_MFG_CarryingCost] [float] NULL, [_SKU_MFG_Tooling] [float] NULL, [_SKU_MFG_Wash] [float] NULL, [_SKU_MFG_Grind] [float] NULL, [_SKU_MFG_Press] [float] NULL, [_SKU_MFG_MROCost] [float] NULL, [_SKU_MFG_MatlHandling] [float] NULL, [_SKU_MFG_Storage] [float] NULL, [_SKU_MFG_ScrapSales] [float] NULL, [_SKU_MFG_InventoryAdjustments] [float] NULL, [_SKU_MFG_COGS] [float] NULL, [_SKU_MFG_SupplyChain_ImportRawMaterials] [float] NULL, [_SKU_MFG_SupplyChain_DomesticRawMaterials] [float] NULL, [_SKU_MFG_Purchasing_RawMaterials] [float] NULL, [_SKU_MFG_PPDIT] [float] NULL, [_SKU_MFG_AP] [float] NULL, [_SKU_MFG_InterplantFreight] [float] NULL, [_SKU_MFG_InboundFreight] [float] NULL, [_SKU_MFG_Finishing] [float] NULL ) |
|