Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to join multiple tables (SQL) to find the difference between an order and shipped quantities?


How to join multiple tables (SQL) to find the difference between an order and shipped quantities?

Author
Message
kaminskidevelopment
kaminskidevelopment
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 8
I have a collection of orders that were submitted to an internal ERP, which were processed and partially shipped.

The table hierarchy is as follows:
•Order table contains OrderReference(PO), OrderID
•OrderItems table contains Product/Lot/Qty requested (OrderID is parentID that joins these tables)
•Shipping contains OrderReference(PO), ShippingID
•ShippingArchive contains Product/Lot/Qty that was shipped (ShippingID is parent that joins these tables)

I want to create a report that looks as follows:
OrderReference#

Order Requested / Shipped Items / Missing Pieces

PC/Lot/Qty - PC/Lot/Qty - PC/Lot/Qty

I have used a temp table to get some of the data but I realize I'm taking the wrong approach. I should be able to do all of this with a single query, though it will seemingly be a bit complex.

I get held up when I try to think about how to join where pieces don't exist, and where to perform the mathematics in my sorting and filtering. Please help!

Sincere thanks Smile
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16626 Visits: 17024
Hi and welcome to the forums. In order to help we will need a few things:

1. Sample DDL in the form of CREATE TABLE statements
2. Sample data in the form of INSERT INTO statements
3. Expected results based on the sample data

Please take a few minutes and read the first article in my signature for best practices when posting questions.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
kaminskidevelopment
kaminskidevelopment
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 8
Thanks for getting back to me so quickly!

Is there any quick/easy way to pull these out of SQL Management Stuido, or do I have to hand write these statements for you?

Thanks!
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16626 Visits: 17024
kaminskidevelopment (7/12/2013)
Thanks for getting back to me so quickly!

Is there any quick/easy way to pull these out of SQL Management Stuido, or do I have to hand write these statements for you?

Thanks!


The create table stuff is pretty simple. Right the table(s), the select "script table as -> create to -> ...". For the data you will probably want to hand write it. This does NOT need to be a complete data dump. Just a few rows in each table that will represent your situation. Of course, make sure you obfuscate the data as we don't want to see anything that might be private type of information.

You can use SSMS to script data for you but the setup is somewhat tedious and by the time you are done with that you could have just done it manually.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
kaminskidevelopment
kaminskidevelopment
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 8
CREATE TABLE [dbo].[ORDER](
[ID] [int] IDENTITY(1,1) NOT NULL,
[OrderRef1] [varchar](10) NULL,
[OrderRef2] [varchar](10) NULL,
[OrderDate] [varchar](10) NULL,
[Shipped] [bit] NOT NULL,
[ReceiptSent] [bit] NOT NULL,
)

CREATE TABLE [dbo].[orderITEMS](
[ID] [int] IDENTITY(1,1) NOT NULL,
[OutboundDeliveryID] [int] NOT NULL,
[OrderLineNr] [int] NULL,
[ItemNumber] [varchar](18) NULL,
[BatchNr] [varchar](10) NULL,
[QtyToShip] [varchar](13) NULL,
)

CREATE TABLE [dbo].[Shipping](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Client] [int] NULL,
[PO] [nvarchar](255) NULL,
[Date] [datetime] NULL,
)


CREATE TABLE [dbo].[ShippingArchive](
[ID] [int] IDENTITY(1,1) NOT NULL,
[OrigID] [int] NULL,
[Client] [int] NULL,
[ProductCode] [nvarchar](25) NULL,
[LotID] [int] NULL,
[Lot] [nvarchar](25) NULL,
[Qty] [int] NULL,
[StatusID] [int] NULL,
[StatusDate] [datetime] NULL,
)
kaminskidevelopment
kaminskidevelopment
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 8
ORDER

ID OrderRef1 OrderDate Shipped ReceiptSent
1525 1181115159 12/01/2013 0 0
1524 1181115158 10/01/2013 0 0

orderITEMS

ID OutboundDeliveryID OrderLineNr ItemNumber BatchNr QtyToShip
1663 1525 901 71565555 2Y637N 20
1662 1524 20 122000600 NULL 1

shipping

id client po date
177620 400 0080014959 2013-07-11 00:00:00.000
177618 400 0080014957 2013-07-11 00:00:00.000

shippingArchive

ID OrigID Client ProductCode LotID Lot Qty StatusID StatusDate
4262720 0 400 71034600 2838356 13EM10643 1 177618 2013-07-11 00:00:00.000
4262722 0 400 71034600 2838356 13EM10643 1 177620 2013-07-11 00:00:00.000
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16626 Visits: 17024
How do you know which order a shipment is for? I don't see anything that connects those together.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
kaminskidevelopment
kaminskidevelopment
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 8
What do you mean by expected results?
kaminskidevelopment
kaminskidevelopment
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 8
The naming conventions are poor.

The order connects to the shipment via the PO - PO in shipping is the OrderRef1 in the ORDER table
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16626 Visits: 17024
kaminskidevelopment (7/12/2013)
What do you mean by expected results?


That means that based on your sample data what should the desired output be. Yours is reasonably easy enough to figure that out so don't waste any time putting that together.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search