MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #16  
Old March 8th, 2013, 12:42 PM
kloud kloud is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 15 kloud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 56 m 48 sec
Reputation Power: 0
Does this help?

each SalesProposal has 1 or more SalesProposalStatus row
each SalesProposal has one Customer row
each SalesProposal has 0 or more Dispatch rows
each SalesProposal has 1 or more Invoice rows
each Invoice has multiple InvoiceItem rows

Reply With Quote
  #17  
Old March 8th, 2013, 01:02 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,374 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 1 m 52 sec
Reputation Power: 4140
Quote:
Originally Posted by kloud
Does this help?
yes, we're almost there

it looks like you're summing invoice items across all invoices for a salesproposal

but what if there are multiple dispatches at the same time?

in particular, how to resolve this factor --
Code:
SUM(CASE WHEN dbo.SalesProposal.PreDispatchInvoicing = 1 
                THEN (dbo.SalesProposal.ForecastQuantity - dbo.InvoiceItem.Quantity) 
                ELSE ((dbo.Dispatch.Weight * dbo.Dispatch.ConversionFactor) - 
                      dbo.InvoiceItem.Quantity) END)  AS InvoiceableQuantity
when there are both multiple dispatches and multiple invoiceitems

this is where the wrong results will be computed

also, are you sure there can be more than one salesproposalstatus for a given salesproposal?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #18  
Old March 8th, 2013, 01:15 PM
kloud kloud is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 15 kloud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 56 m 48 sec
Reputation Power: 0
Quote:
Originally Posted by r937
also, are you sure there can be more than one salesproposalstatus for a given salesproposal?


My apologies - I was thinking of another table that's similarly names. You're correct - only one status per Salesproposal.

I need the following from the query:
InvoiceQuantity
This is the total of the quantity column in the invoiceitem table for all invoices against the salesproposal
SalesProposal->Invoice(s)->InvoiceItem(s)

DeliveredQuantity
This is the total of quantity delivered (the dispatch table records deliveries). The quantity delivered is a product of the weight column and conversion factor column in the dispatch table.

InvoiceableQuantity
This is tricky. Salesproposals can be invoiced in 2 ways - by collating a number of dispatches and assigning an invoice to them. In that case, the dispatches are basically invoiceitems. The other way is pre dispatch where invoicing is done upfront before product ships. So a quantity is assigned to an invoice with a generic product type. These invoices will have only one invoiceitem against them. The flag PreDispatchInvoicing differentiates these invoicing methods, hence the case statement for it. If it's true, then the invoiceable quantity will come from subtracting the InvoicedQuantity (above) from a value called ForecastQuantity which is a field in the sales proposal. If it's false the the invoiceable quantity is simply calculated by subtracting the InvoicedQuantity from the DeliveredQuantity.

That's it. There are one or two other fields to add to the main select (no aggregates) but if I get this working I'll be able to finish it.

Thanks.

Reply With Quote
  #19  
Old March 9th, 2013, 03:27 PM
kloud kloud is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 15 kloud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 56 m 48 sec
Reputation Power: 0
Just posting the solution in case anybody's interested. Help from another forum. Derived tables were the answer. Very neat solution.

Code:
SELECT        dbo.Customer.CustomerCode, dbo.SalesProposal.SPNo + ' (' + dbo.Forest.Forest + ')' AS SalesProposal, dbo.SalesProposal.SalesProposalID, 
                         dbo.SalesProposal.PreDispatchInvoicing, CASE WHEN dbo.SalesProposal.PreDispatchInvoicing = 1 THEN 'Pre Dispatch' ELSE 'Post Dispatch' END AS Invoicing, 
                         Invoice_Derived.InvoicedQuantity AS InvoicedQuantity, Dispatch_Derived.DeliveredQuantity AS DeliveredQuantity, 
                         CASE WHEN dbo.SalesProposal.PreDispatchInvoicing = 1 THEN dbo.SalesProposal.ForecastQuantity - Invoice_Derived.InvoicedQuantity ELSE Dispatch_Derived.DeliveredQuantity
                          - Invoice_Derived.InvoicedQuantity END AS InvoiceableQuantity
FROM            dbo.SalesProposal INNER JOIN
                         dbo.SalesProposalStatus ON dbo.SalesProposal.StatusID = dbo.SalesProposalStatus.SalesProposalStatusID AND 
                         dbo.SalesProposalStatus.SalesProposalStatus = 'Sold' INNER JOIN
                         dbo.Forest ON dbo.SalesProposal.ForestID = dbo.Forest.ForestID INNER JOIN
                         dbo.Customer ON dbo.SalesProposal.CustomerID = dbo.Customer.CustomerID LEFT OUTER JOIN
                             (SELECT        dbo.Invoice.SalesProposalID, SUM(dbo.InvoiceItem.Quantity) AS InvoicedQuantity
                               FROM            dbo.Invoice INNER JOIN
                                                         dbo.InvoiceItem ON dbo.Invoice.InvoiceID = dbo.InvoiceItem.InvoiceID
                               GROUP BY dbo.Invoice.SalesProposalID) AS Invoice_Derived ON dbo.SalesProposal.SalesProposalID = Invoice_Derived.SalesProposalID LEFT OUTER JOIN
                             (SELECT        SalesProposalID, SUM(Weight * ConversionFactor) AS DeliveredQuantity
                               FROM            dbo.Dispatch
                               GROUP BY SalesProposalID) AS Dispatch_Derived ON Dispatch_Derived.SalesProposalID = dbo.SalesProposal.SalesProposalID

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > COUNTing issue

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap