Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
collie
Constraint Violating Yak Guru
400 Posts |
Posted - 2010-03-15 : 16:32:29
|
GO/****** Object: Table [dbo].[BillTo] Script Date: 03/15/2010 22:23:40 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[BillTo]( [billto_id] [nvarchar](20) NOT NULL, [billtotype_id] [int] NOT NULL, [billto_name] [nvarchar](50) NOT NULL, [billto_VAT_number] [nvarchar](50) NOT NULL, [billto_contactperson] [nvarchar](50) NULL, [billto_phone] [nvarchar](50) NULL, [billto_fax] [nvarchar](50) NULL, [billto_mobile] [nvarchar](50) NULL, [billto_address] [nvarchar](100) NULL, [billto_city] [nvarchar](50) NULL, [billto_zipcode] [nvarchar](50) NULL, [billto_payments] [int] NOT NULL CONSTRAINT [DF_BillTo_billto_payments] DEFAULT ((1)), [billto_creditlimit] [int] NULL, [creditcompany_id] [int] NULL, [billtostatus_id] [int] NULL, [billto_email] [varchar](30) NULL, [billto_discount] [int] NULL, [customergroup_id] [int] NULL, [customertype_id] [int] NULL, [NOS] [bit] NULL, CONSTRAINT [PK_BillTo] PRIMARY KEY CLUSTERED ( [billto_id] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GOSET ANSI_PADDING OFFGO/****** Object: Table [dbo].[BusinessUnit] Script Date: 03/15/2010 22:23:40 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[BusinessUnit]( [businessunit_id] [int] IDENTITY(1,1) NOT NULL, [businessunit_nikeID] [int] NOT NULL, [businessunit_name] [nvarchar](255) NOT NULL, [businessunit_shortname] [nvarchar](50) NULL, CONSTRAINT [PK_BusinessUnit] PRIMARY KEY CLUSTERED ( [businessunit_id] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GO/****** Object: Table [dbo].[Carton] Script Date: 03/15/2010 22:23:40 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[Carton]( [carton_id] [nvarchar](255) NOT NULL, [packlist_id] [nvarchar](30) NULL, [businessunit_id] [int] NOT NULL, [seasonyear_id] [int] NULL, [electronicinvoice_id] [int] NULL, [cartonstatus_id] [int] NULL, [cartonstatus_date] [smalldatetime] NULL, [StatusUser_id] [int] NULL, CONSTRAINT [PK_Carton] PRIMARY KEY CLUSTERED ( [carton_id] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GO/****** Object: Table [dbo].[CartonItem] Script Date: 03/15/2010 22:23:40 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[CartonItem]( [cartonitem_color_code] [nvarchar](50) NOT NULL, [cartonitem_style_code] [nvarchar](50) NOT NULL, [cartonitem_size_id] [int] NOT NULL, [carton_id] [nvarchar](255) NOT NULL, [cartonitem_quantity] [int] NOT NULL, [electronicInvoice_id] [int] NULL, [cartonitem_netweight] [decimal](10, 2) NULL, [cartonitem_totalnet] [decimal](10, 2) NULL, [cartonitem_totalgross] [decimal](10, 2) NULL, [cartonitem_unitprice] [decimal](10, 2) NULL, [cartonitem_wholesaleprice] [decimal](10, 2) NULL, CONSTRAINT [PK_CartonItem] PRIMARY KEY CLUSTERED ( [cartonitem_color_code] ASC, [cartonitem_style_code] ASC, [cartonitem_size_id] ASC, [carton_id] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GO/****** Object: Table [dbo].[ElectronicInvoice] Script Date: 03/15/2010 22:23:40 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[ElectronicInvoice]( [electronicinvoice_id] [int] IDENTITY(1,1) NOT NULL, [electronicinvoice_upload_date] [smalldatetime] NOT NULL CONSTRAINT [DF_ElectronicInvoice_electronicinvoice_upload_date] DEFAULT (getdate()), [user_id] [int] NOT NULL, [electronicinvoice_FileId] [nvarchar](255) NOT NULL, CONSTRAINT [PK_ElectronicInvoice] PRIMARY KEY CLUSTERED ( [electronicinvoice_id] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GO/****** Object: Table [dbo].[Invoice] Script Date: 03/15/2010 22:23:40 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[Invoice]( [invoice_id] [nvarchar](50) NOT NULL, [invoice_date] [smalldatetime] NOT NULL, [invoice_netvalue] [decimal](10, 2) NOT NULL, [invoice_parentid] [nvarchar](50) NULL, [invoice_taxvalue] [decimal](10, 2) NOT NULL, [invoice_discount] [decimal](3, 2) NULL CONSTRAINT [DF_Invoice_invoice_discount] DEFAULT ((0)), [billto_id] [nvarchar](20) NOT NULL, [packlist_id] [nvarchar](30) NULL, [invoicereason_id] [int] NULL, [invoicetype_id] [int] NULL, [confirm_date] [smalldatetime] NULL, [confirm_id] [nvarchar](50) NULL, [confirmX] [nvarchar](50) NULL, [invoicestatus_id] [int] NULL, [invoicestatus_Date] [smalldatetime] NULL, [statusUser_id] [int] NULL, CONSTRAINT [PK_Invoice] PRIMARY KEY CLUSTERED ( [invoice_id] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GO/****** Object: Table [dbo].[ItemCatalog] Script Date: 03/15/2010 22:23:40 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[ItemCatalog]( [itemcatalog_color_code] [nvarchar](50) NOT NULL, [itemcatalog_style_code] [nvarchar](50) NOT NULL, [itemcatalog_size_id] [int] NULL, [isbond] [bit] NULL, [businessunit_id] [int] NOT NULL, [itemcatalog_material_description] [nvarchar](100) NULL, [itemcatalog_category_description] [nvarchar](100) NULL, [item_wholesaleunit_price] [decimal](10, 2) NOT NULL, [item_sihouette_description] [nvarchar](100) NULL, [itemgender_id] [int] NOT NULL, [itemcatalog_color_description] [nvarchar](100) NULL, [itemcatalog_label_description] [nvarchar](100) NULL, [seasonyear_id] [int] NOT NULL, CONSTRAINT [PK_ItemCatalog] PRIMARY KEY CLUSTERED ( [itemcatalog_color_code] ASC, [itemcatalog_style_code] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GO/****** Object: Table [dbo].[Order] Script Date: 03/15/2010 22:23:40 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[Order]( [order_id] [nvarchar](50) NOT NULL, [order_quantity] [int] NOT NULL, [order_customerPO] [nvarchar](200) NULL, [order_price] [decimal](10, 2) NOT NULL, [order_date] [smalldatetime] NOT NULL, [order_crd] [smalldatetime] NULL, [ordertype_id] [int] NOT NULL, [salesrep_id] [int] NOT NULL, [creditorderstatus_id] [int] NULL, [cashorderstatus_id] [int] NULL, [order_remark] [nvarchar](200) NULL, [order_discount] [int] NULL CONSTRAINT [DF_Order_order_discount] DEFAULT ((0)), [order_statusid] [int] NULL, [order_statusdate] [smalldatetime] NULL, [statusUser_id] [int] NULL, [shipto_id] [nvarchar](30) NULL, [billto_id] [nvarchar](20) NULL, [order_seasonyearid] [int] NULL, CONSTRAINT [PK_Order] PRIMARY KEY CLUSTERED ( [order_id] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GO/****** Object: Table [dbo].[PackList] Script Date: 03/15/2010 22:23:40 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[PackList]( [packlist_id] [nvarchar](30) NOT NULL, [shipto_id] [nvarchar](30) NOT NULL, [businessunit_id] [int] NOT NULL, [packlist_customerPO] [nvarchar](200) NULL, [packlist_deliverydate] [smalldatetime] NULL, [packlist_planeddelivery] [smalldatetime] NULL, [order_id] [nvarchar](50) NULL, [packlist_status] [int] NULL, [packlist_statusDate] [smalldatetime] NULL, [statusUser_id] [int] NULL, [SeasonYear] [int] NULL, [Carton_Quantity] [int] NULL, [MixedStatus] [bit] NULL, CONSTRAINT [PK_PackList] PRIMARY KEY CLUSTERED ( [packlist_id] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GO/****** Object: Table [dbo].[ShipTo] Script Date: 03/15/2010 22:23:40 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[ShipTo]( [shipto_id] [nvarchar](30) NOT NULL CONSTRAINT [DF_ShipTo_shipto_id] DEFAULT ((1)), [billto_id] [nvarchar](20) NOT NULL, [shipto_name] [nvarchar](500) NOT NULL, [shipto_address] [nvarchar](100) NULL, [shipto_contactperson] [nvarchar](30) NULL, [shipto_phone] [nvarchar](30) NULL, [shipto_fax] [nvarchar](30) NULL, [shipto_mobile] [nvarchar](50) NULL, [shipto_zipcode] [nvarchar](10) NULL, [billto_email] [varchar](30) NULL, CONSTRAINT [PK_ShipTo] PRIMARY KEY CLUSTERED ( [shipto_id] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY] select DISTINCT -- Material--,itemcatalog_material_descriptionbusinessunit_shortname,season_id+' ' +convert(nvarchar(10),seasonyear_year) as Season, WHOLESALEvalue [Wholesale value],Units from packlist as packlist inner JOIN CARTON as carton on carton.packlist_id=packlist.packlist_idinner join cartonitem as cartonitem on cartonitem.carton_id=carton.carton_idinner join (select businessunit_id ,sum (cartonitem_quantity) as Unitsfromcartonitem as cartoniteminner join carton as carton on carton.carton_id=cartonitem.carton_id group by businessunit_id,carton.seasonyear_id)cartonitemTable on carton.businessunit_id=cartonitemTable.businessunit_idinner join (SELECT SUM (item_wholesaleunit_price) AS WHOLESALEvalue,carton.businessunit_idFROM dbo.ItemCataloginner join cartonitem as cartonitem on itemcatalog_style_code+'-'+itemcatalog_color_code=cartonitem.cartonitem_style_code+'-'+cartonitem_color_codeinner join carton as carton on carton.carton_id=cartonitem.carton_idGROUP BY carton.businessunit_id,carton.seasonyear_id)AS wholesaletableon wholesaletable.businessunit_id=carton.businessunit_idinner join cartonstatus on cartonstatus.cartonstatus_id=carton.cartonstatus_idinner join businessunit on businessunit.businessunit_id=carton.businessunit_idINNER join seasonyear on seasonyear.seasonyear_id=carton.seasonyear_idinner join shipto on shipto.shipto_id=packlist.shipto_idinner join billto on billto.billto_id=SHIPTO.billto_idinner join electronicinvoice ei on ei.electronicinvoice_id=carton.electronicinvoice_idleft join invoice as invoice on invoice.packlist_id=packlist.packlist_idleft join [order] as orders on orders.order_id=packlist.order_id where (carton.packlist_id=@PacklistId or @PacklistId is null)and (billto.customergroup_id in ( select value from dbo.fn_RepSplit(@CustomerGroup,',')) or @CustomerGroup is null) and (billto.customertype_id in ( select value from dbo.fn_RepSplit(@CustomerType ,',')) or @CustomerType is null) and (billto.nos=@Nos or @Nos is null) and (billto.BillTo_Id=@BillToId or @BillToId is null)and (EI.electronicinvoice_fileid=@ElectronicInvoiceId or @ElectronicInvoiceId is null)and (packlist.packlist_status in ( select value from dbo.fn_RepSplit(@PacklistStatus,','))or @PacklistStatus is null)and (invoice.invoicestatus_id in ( select value from dbo.fn_RepSplit(@InvoiceStatus,','))or @InvoiceStatus is null)and (orders.order_statusid in ( select value from dbo.fn_RepSplit(@OrderStatus,','))or @OrderStatus is null)and (packlist.order_id=@OrderId or @OrderId is null)and (carton.carton_id=@CartonId or @CartonId is null)and (businessunit.businessunit_id in ( select value from dbo.fn_RepSplit(@BusinessUnitName ,','))or @BusinessUnitName is null) and (cartonitem.cartonitem_style_code =@Style or @Style is null)and (cartonitem.cartonitem_color_code =@Colour or @Colour is null)and (cartonitem_size_id=@Size or @Size is null)and (carton.seasonyear_id=@Season or @Season is null)and (packlist.packlist_planeddelivery=@DeliveryPlanDate or @DeliveryPlanDate is null)end The problem are with the 2 derived tables wholesaletable and cartonitemTable. It ignores the where statement and sums the entire cartonitem table grouped by the conditions. This is wrong. If for example @PacklistId parameter isn’t null than I need the derived tables to get the values according to the packlist.How can I modify the query?Thanks for the help.Whisky-my beloved dog who died suddenly on the 29/06/06-I miss u so much. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-16 : 00:28:19
|
| you need to put where clause inside derived table if you want filter to happen for calculated values inside------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
collie
Constraint Violating Yak Guru
400 Posts |
Posted - 2010-03-16 : 02:12:32
|
| Hi,Thanks visakh16.So i must add inner joins and the where clause inside the derived tables also? Is this the correct way or is there a better method?Whisky-my beloved dog who died suddenly on the 29/06/06-I miss u so much. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-16 : 03:58:57
|
| there's a better method but for that i need to know your correct requirement. post some sample data along with reqd output and we can take it from there------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
collie
Constraint Violating Yak Guru
400 Posts |
Posted - 2010-03-16 : 04:38:19
|
Hi For example I want to return results where packlist_id=P006053 The output will bePACKLIST_ID BU SEASON ITEM DESC QUANTITY WHOLESALE P006053 2 1 COTTON 53 2697.70 (53*50.90 Whisky-my beloved dog who died suddenly on the 29/06/06-I miss u so much. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
collie
Constraint Violating Yak Guru
400 Posts |
Posted - 2010-03-16 : 06:43:08
|
| Hi,Is this what you need?INSERT INTO [Carton] ([carton_id] ,[packlist_id] ,[businessunit_id] ,seasonyear_id )select '2040141278' ,'8129113', 2 ,1 union allselect'24139079' ,'8129113 ', 2, 1 union allselect'914139080' ,'8129113 ', 2, 2 union allselect'0014139079' ,'129114 ', 2, 2 union allselect '014139081' ,'129114 ', 2, 3 union allselect '84014139080' ,'129162 ', 2, 4 insert into packlist (packlist_id)select '8129113 ' union all select '129114 ' union all select '129162 ' INSERT INTO [CartonItem] ([cartonitem_color_code] ,[cartonitem_style_code] ,[cartonitem_size_id] ,[carton_id] ,[cartonitem_quantity] )select '012', '362842',172 ,'2040141278 ', 4 union allselect '012', '362842', 178 ,'2040141278 ', 4 union allselect '012', '362842', 186 ,'2040141278 ', 2 union allselect '012', '362842', 190 ,'2040141278 ', 2 union allselect '012', '362842', 201 ,'2040141278 ', 1 union allselect '012', '362843' ,172 ,'0014139079', 5 union allselect '012', '362843' ,178 ,'0014139079', 5 union allselect '012', '362843' ,186 ,'0014139079', 2 union allselect '012', '362843' ,190 ,'0014139079', 2 union allselect '012', '362843' ,201 ,'0014139079', 2 union allselect '021' ,'362843' ,172 ,'84014139080', 5 union allselect '021' ,'362843' ,178 ,'84014139080', 5 union allselect '021' ,'362843' ,186 ,'84014139080', 2 union allselect '021' ,'362843' ,190 ,'84014139080', 2 union allselect '021' ,'362843' ,201 ,'84014139080', 2 union allselect'120', '362843' ,172 ,'014139081 ', 5 union allselect'120', '362843' ,178 ,'014139081 ', 5 union allselect'120', '362843' ,186 ,'014139081 ', 2 union allselect'120', '362843' ,190 ,'014139081 ', 2 union allselect'120', '362843' ,201 ,'014139081 ', 2 union allselect'120' ,'362942' ,172 ,'014139081 ', 2 union allselect'120' ,'362942' ,178 ,'014139081 ', 2 union allselect'120', '362942' ,190 ,'014139081 ', 1 union allselect '120', '362942' ,201 ,'014139081 ', 1 INSERT INTO [ItemCatalog] ( [itemcatalog_style_code] ,[itemcatalog_color_code] ,[item_wholesaleunit_price],item_material_description ) select '332795', '012', 47.61,'Cotton' union allselect'339333' ,'012', 73.42 ,'Fleece'union allselect'339337' ,'012', 63.90 ,'xx Cotton'union allselect'339507' ,'012', 129.50,'ui' union allselect'339523' ,'012', 124.76 ,'silk'union allselect'339533' ,'012', 63.89,'oo9d' union allselect'340302' ,'012', 133.33,'premier' union allselect'340807' ,'012', 85.71,'premier UI' union allselect'362843' ,'021', 85.71,'premier UI' union allselect'340867' ,'012', 190.44 ,'Cotton mm'USE [test]GO/****** Object: Table [dbo].[itemcatalog] Script Date: 03/16/2010 12:22:53 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[itemcatalog]( [itemcatalog_style_code] [nvarchar](50) NULL, [itemcatalog_color_code] [nvarchar](50) NULL, [item_wholesaleunit_price] [decimal](10, 2) NULL, [item_material_description] [nvarchar](50) NULL) ON [PRIMARY]GO/****** Object: Table [dbo].[carton] Script Date: 03/16/2010 12:22:53 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[carton]( [carton_id] [nvarchar](50) NOT NULL, [packlist_id] [nvarchar](50) NULL, [businessunit_id] [int] NULL, [seasonyear_id] [int] NULL, CONSTRAINT [PK_carton] PRIMARY KEY CLUSTERED ( [carton_id] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GO/****** Object: Table [dbo].[Packlist] Script Date: 03/16/2010 12:22:53 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[Packlist]( [packlist_id] [nvarchar](50) NOT NULL, CONSTRAINT [PK_Packlist] PRIMARY KEY CLUSTERED ( [packlist_id] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GO/****** Object: Table [dbo].[cartonitem] Script Date: 03/16/2010 12:22:53 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[cartonitem]( [cartonitem_color_code] [nvarchar](50) NULL, [cartonitem_style_code] [nvarchar](50) NULL, [cartonitem_size_id] [int] NULL, [carton_id] [nvarchar](50) NULL, [cartonitem_quantity] [int] NULL) ON [PRIMARY]GOOutput wanted:units=cartonitem quantityPacklist_id=129162The grouping should be by bu and season.bu season Units Total wholesale value2 4 16 (85.71*16) 1371.36Whisky-my beloved dog who died suddenly on the 29/06/06-I miss u so much. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-16 : 07:19:45
|
| [code]SELECT c.[businessunit_id],c.seasonyear_id,SUM(ci.totalqty) as units,SUM(ci.totalqty * ic.item_wholesaleunit_price) AS [Total wholesale value]FROM packlist pJOIN [Carton] cON c.packlist_id=p.packlist_idJOIN (SELECT [cartonitem_color_code] ,[cartonitem_style_code] ,carton_id, SUM(cartonitem_quantity) AS totalqty FROM [CartonItem] GROUP BY [cartonitem_color_code] ,[cartonitem_style_code] ,carton_id)ciON ci.carton_id=c.carton_idJOIN [ItemCatalog] icON ic.itemcatalog_style_code = ci.cartonitem_style_codeAND ic.itemcatalog_color_code=ci.cartonitem_color_codeWHERE p.packlist_id='129162'GROUP BY c.[businessunit_id],c.seasonyear_id[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
collie
Constraint Violating Yak Guru
400 Posts |
Posted - 2010-03-16 : 11:40:19
|
Hi,Thanks. it works fine until i join the query with another table invoice. What happens is that the value of units and wholesale is multiplied. Why does this happen?invoice table:invoice_id packlist_idPacklist_id can have more than one invoice. Packlist_id can be null.SELECT c.[businessunit_id],c.seasonyear_id,SUM(ci.totalqty) as units,SUM(ci.totalqty * ic.item_wholesaleunit_price) AS [Total wholesale value]FROM packlist pJOIN [Carton] cON c.packlist_id=p.packlist_id----JOIN (SELECT [cartonitem_color_code] ,[cartonitem_style_code] ,carton_id, SUM(cartonitem_quantity) AS totalqty FROM [CartonItem] GROUP BY [cartonitem_color_code] ,[cartonitem_style_code] ,carton_id)ciON ci.carton_id=c.carton_id---JOIN [ItemCatalog] icON ic.itemcatalog_style_code = ci.cartonitem_style_codeAND ic.itemcatalog_color_code=ci.cartonitem_color_codeleft join invoice on invoice.packlist_id=p.packlist_id where c.packlist_id='70381271'group by c.businessunit_id,c.seasonyear_id Thanks for the help :)Whisky-my beloved dog who died suddenly on the 29/06/06-I miss u so much. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-16 : 11:48:25
|
| simple..you do have a 1 to many relationship existing in invoice table (multiple records per joining field value which is packlist_id)------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|