0

Consider the following scenario (if it helps think Northwind Orders / OrderDetails).

I have two tables LandingHeaders and LandingDetails, that record details about commercial fishing trips. Typically, over the course of a week, a fishing vessel can make several trips to sea, and so will end up with several LandingHeader/LandingDetail records.

At the end of each week the company that purchases the results of these fishing trips need to work out the value of each landing made by each vessel and then pay the owner of that vessel whatever money is due. To add to fun there are some vessels owned by the same person, so the company purchasing the fish would prefer if the value of all the landings from all of the vessels owned by a given individual were amalgamated into a single payment.

Until now the information required to perform this task was spread across more that a simple master-detail table structure and as such it has required several stored procedures (along with the judicious use of dictionaries in the main application doing the work) to achieve the desired end result. External circumstances beyond my control have forced some major database changes and I have taken the opportunity to restructure the LandingHeader table such that it contains all the necessary information that might be needed.

From the landing Header table I need to record the following fields;

  1. LandingHeaderId of sql type int
  2. VesselOwnerId of sql type int
  3. LandingDate (Just used as part of query in reality) of sql type datetime

From the LandingDetails Table I need to record the following fields;

  1. ProductId of sql type int
  2. Quantity of sql type decimal (10,2)
  3. UnitPrice of sql type money

I have been thinking about creating a query that takes as Parameters VesselOwnerID , SartDate and EndDate.

As output I need to know which LandingId's are associated with the owner and the total Quantity for each Distinct ProductId (along with the UnitPrice which will be the same for each ProductId over the selected period) spread over the various landingDetails associated with the LandingHeaders over the given period.

I have been thinking along the lines of output rows that might look a little like this; enter image description here

Can this sort of thing be done from a standard master - detail type table relationship or will I still need to resort to multiple stored procedures.

A longer term goal is to have a query that could be used to produce xml that could be adapted for use with a web api.

halfer
  • 19,824
  • 17
  • 99
  • 186
Dom Sinclair
  • 2,458
  • 1
  • 30
  • 47
  • 1
    "along with the UnitPrice which will be the same for each ProductId over the selected period" That seems to me like a potential disaster. are you **absolutely sure** the unit price will not change for the given period? – Zohar Peled May 08 '18 at 11:12
  • @ZoharPeled Absolutely certain. Prices per productid are aggregated over the course of a week according to market returns. The selected period will always be for a week, and moreover a week that coincides with that pricing period. Business rules already exist to ensure that that is the case. – Dom Sinclair May 08 '18 at 11:20
  • @ZoharPeled I should have asked, what makes you think that that the same UnitPrice could, or would be a disaster and more importantly , if it were not how would one ensure that the absolutely correct sum was calculated for each product id? – Dom Sinclair May 08 '18 at 11:26
  • 1
    It's quite easy, actually. You store the unitPrice per product per landing, right? All you have to do us add it to the group by clause of the query, so in case you have the same product in two different landings with two different unit prices, it would appear in the result set in two different rows - one for each unit price. – Zohar Peled May 08 '18 at 11:29
  • If you could [edit] your question to include sample data as [DDL](https://en.wikipedia.org/wiki/Data_definition_language) + [DML](https://en.wikipedia.org/wiki/Data_manipulation_language). I could probably answer it. – Zohar Peled May 08 '18 at 11:43

0 Answers0