﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / T-SQL (SS2K5)  / Stored Proc - Dealing with Orders and OrderDetails... / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 25 May 2013 03:23:10 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Stored Proc - Dealing with Orders and OrderDetails...</title><link>http://www.sqlservercentral.com/Forums/Topic853723-338-1.aspx</link><description>steve whenever i have an overall status that depends on the details, i do not store it in the parent/Orders table. I always try to calculate it based on the details, for example.I create a view instead, and the view calculates the status, so it is automatically correct based on the details.an example might be something like this: notice the case statement and the group by sub select to figure it all out:[code]CREATE TABLE Orders (OrderID int identity(1,1) not null primary key, DateOrdered, CustomerNumber)CREATE TABLE OrderDetails(DetailsID int identity(1,1) not null primary key, OrderID int references Orders(OrderID)ProductOrdered varchar(30) ProductPrice  decimal(19,4)Shipped char(1) default('N') )CREATE VIEW VW_ORDERS ASSELECT Orders.OrderID,Orders.DateOrdered,Orders.CustomerNumberCASE   WHEN DETAILS.SHIPPEDCOUNT = DETAILS.TOTALCOUNT  THEN 'Shipped'  WHEN DETAILS.SHIPPEDCOUNT =0  THEN 'Not Shipped'  WHEN DETAILS.SHIPPEDCOUNT &amp;lt; DETAILS.TOTALCOUNT  THEN 'Partial Shipment'END As StatusFROM VW_ORDERSLEFT OUTER JOIN (SELECT OrderID,SUM(CASE WHEN Shipped = 'Y' THEN 1 ELSE 0 END) AS SHIPPEDCOUNT,COUNT(DetailsID)  AS TOTALCOUNTFROM OrderDetailsGROUP BY OrderID) DETAILSON OrderDetails.OrderId = DETAILS.OrderID[/code]</description><pubDate>Tue, 26 Jan 2010 10:39:18 GMT</pubDate><dc:creator>Lowell</dc:creator></item><item><title>Stored Proc - Dealing with Orders and OrderDetails...</title><link>http://www.sqlservercentral.com/Forums/Topic853723-338-1.aspx</link><description>In my database, I have 2 tables, Orders and OrderDetails.  This may be obvious, but Orders holds information relevant (DateOrdered, OrderStatus, CustomerNumber) and the OrderDetails table contains information relevant to the details (ProductOrdered, ProductPrice, Shipped)I want to know the best way to set it up so that when all of the Details of an Order are market Shipped, the order then gets an OrderStatus as Shipped.  In addition, for example, if at some point in the process a all of the details get marked as shipped, the  OrderStatus changes to Shipped - and someone goes and unmarks a detail as shipped for some reason - the order is then marked back to Pending.I would think that this is a pretty common scenario - I am just looking for some direction.Thanks in advance.sb</description><pubDate>Tue, 26 Jan 2010 08:56:15 GMT</pubDate><dc:creator>stephenmbell</dc:creator></item></channel></rss>