Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 135735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:50:15+00:00 2026-05-11T06:50:15+00:00

I’m trying to write a stored procedure that will return two calculated values for

  • 0

I’m trying to write a stored procedure that will return two calculated values for each record according to the rules below, but I haven’t figured out how to structure the SQL to make it happen. I’m using SQL Server 2008.

First, the relevant tables, and the fields that matter to the problem.

ProductionRuns

 RunID (key, and RunID is given to the stored proc as its parameter)  ContainerName  ProductName  TemplateID 

TemplateMeasurements

 MeasurementTypeID  TemplateID 

SimpleBounds

 MeasurementTypeID  TemplateID  UpperBound  LowerBound 

ContainerBounds

 MeasurementTypeID  TemplateID  UpperBound  LowerBound  ContainerName 

ProductBounds

 MeasurementTypeID  TemplateID  UpperBound  LowerBound  ProductName 

And this is what I’m trying to return. I want to return a calculated upper bound and lower bound value for each TemplateMeasurements record that has a matching TemplateID with the ProductionRuns record that has the supplied runID.

The calculated upper and lower bounds basically get the tightest bound that can be obtained as a result of the simple, container and product bounds, if they qualify.

If a SimpleBounds record exists with the correct MeasurementTypeID and TemplateID, then that becomes one of the qualifying bounds for a particular MeasurementTypeID and record of TemplateMeasurements.

For a ContainerBound record to qualify, the TemplateID and MeasurementTypeID must match, but also the ContainerName must match the value for ContainerName in the ProductionRuns record. Also for ProductBounds, the same is true, but for ProductName.

For a particular MeasurementTypeID, take all the qualifying bounds, and find the least Upper Bound, and that will be the calculated Upper Bound that is to be returned. Find the greatest Lower Bound of the qualifiers and that will be the returned Lower Bound.

I have no idea how to put together SQL to do this however.

Also, if none of the three bound tables qualify for a particular MeasurementTypeID, then null could be returned.

My thought would be some kind of left outer join, but I’m not sure how to extend that to three tables that could all have null in the results.

Thanks for the help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:50:16+00:00Added an answer on May 11, 2026 at 6:50 am

    I don’t have time to test this right now, but hopefully this will get you pretty close:

    SELECT      PR.RunID,      PR.TemplateID,      CASE           WHEN MAX(SB.LowerBound) > MAX(CB.LowerBound) AND                       MAX(SB.LowerBound) > MAX(PB.LowerBound) THEN MAX(SB.LowerBound)           WHEN MAX(CB.LowerBound) > MAX(PB.LowerBound) THEN MAX(CB.LowerBound)           ELSE MAX(PB.LowerBound)      END AS LowerBound,      CASE           WHEN MIN(SB.UpperBound) < MIN(CB. UpperBound) AND                       MIN(SB. UpperBound) < MIN(PB. UpperBound) THEN MIN(SB. UpperBound)           WHEN MIN(CB. UpperBound) < MIN(PB. UpperBound) THEN MIN(CB. UpperBound)           ELSE MIN(PB. UpperBound)      END FROM      ProductionRuns PR INNER JOIN TemplateMeasurements TM ON       TM.TemplateID = PR.TemplateID LEFT OUTER JOIN SimpleBounds SB ON      SB.TemplateID = PR.TemplateID AND      SB.MeasurementTypeID = TM.MeasurementTypeID LEFT OUTER JOIN ContainerBounds CB ON      CB.TemplateID = PR.TemplateID AND      CB.MeasurementTypeID = TM.MeasurementTypeID AND      CB.ContainerName = PR.ContainerName LEFT OUTER JOIN ProductBounds PB ON      PB.TemplateID = PR.TemplateID AND      PB.MeasurementTypeID = TM.MeasurementTypeID AND      PB.ProductName = PR.ProductName GROUP BY      PR.RunID,      PR.TemplateID 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 167k
  • Answers 167k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There are a couple of named templates you can override… May 12, 2026 at 1:36 pm
  • Editorial Team
    Editorial Team added an answer I don't have a dataset handy to test this, but… May 12, 2026 at 1:36 pm
  • Editorial Team
    Editorial Team added an answer Why you're using html entities? Just switch to utf8. Otherwise… May 12, 2026 at 1:36 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.