Re: set based guru's help wanted
From: Steve Kass (skass_at_drew.edu)
Date: 12/11/04
- Next message: -Permood: "Re: SQL Query problem take ages to run"
- Previous message: Mike Epprecht \(SQL MVP\): "Re: SQL over WAN with ADO, how to optimize?"
- In reply to: John Gilson: "Re: set based guru's help wanted"
- Next in thread: John Gilson: "Re: set based guru's help wanted"
- Reply: John Gilson: "Re: set based guru's help wanted"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 11 Dec 2004 13:33:15 -0500
Good job! Using sum(T2.x) and sum(T2.x) - T1.x is a great trick for
getting a current and previous total in the same row.
SK
John Gilson wrote:
>"Alex" <nospam@devdex.com> wrote in message news:%23SUdNC43EHA.1292@TK2MSFTNGP10.phx.gbl...
>
>
>>let's suppose s1 holds the available items (q = quantity) for a sales
>>store, and s2 holds the orders from customers. so i want to consume the
>>available quantity with the quantity ordered by a customer.
>>there is no relationship between s1 and s2
>>
>>thanx,
>>Alex
>>
>>*** Sent via Developersdex http://www.developersdex.com ***
>>Don't just participate in USENET...get rewarded for it!
>>
>>
>
>>From your expected results I'll conclude that you want to fill
>customer orders by increasing customer_id and draw from
>store inventory by increasing store_id.
>
>CREATE TABLE Inventory
>(
>store_id INT NOT NULL PRIMARY KEY,
>item_quantity INT NOT NULL CHECK (item_quantity >= 0)
>);
>
>CREATE TABLE Orders
>(
>customer_id INT NOT NULL PRIMARY KEY,
>item_quantity INT NOT NULL CHECK (item_quantity > 0)
>);
>
>// Your sample data
>INSERT INTO Inventory (store_id, item_quantity)
>VALUES (10, 2);
>INSERT INTO Inventory (store_id, item_quantity)
>VALUES (20, 3);
>INSERT INTO Inventory (store_id, item_quantity)
>VALUES (30, 2);
>
>INSERT INTO Orders (customer_id, item_quantity)
>VALUES (1, 4);
>INSERT INTO Orders (customer_id, item_quantity)
>VALUES (2, 2);
>
>SELECT I.store_id, O.customer_id,
> CASE WHEN O.end_running_quantity <= I.end_running_quantity
> THEN O.end_running_quantity
> ELSE I.end_running_quantity
> END -
> CASE WHEN O.start_running_quantity >= I.start_running_quantity
> THEN O.start_running_quantity
> ELSE I.start_running_quantity
> END AS items_consumed_tally
>FROM (SELECT I1.store_id,
> SUM(I2.item_quantity) - I1.item_quantity AS
> start_running_quantity,
> SUM(I2.item_quantity) AS end_running_quantity
> FROM Inventory AS I1
> INNER JOIN
> Inventory AS I2
> ON I2.store_id <= I1.store_id
> GROUP BY I1.store_id, I1.item_quantity) AS I
> INNER JOIN
> (SELECT O1.customer_id,
> SUM(O2.item_quantity) - O1.item_quantity AS
> start_running_quantity,
> SUM(O2.item_quantity) AS end_running_quantity
> FROM Orders AS O1
> INNER JOIN
> Orders AS O2
> ON O2.customer_id <= O1.customer_id
> GROUP BY O1.customer_id, O1.item_quantity) AS O
> ON O.start_running_quantity < I.end_running_quantity AND
> O.end_running_quantity > I.start_running_quantity
>ORDER BY store_id, customer_id;
>
>store_id customer_id items_consumed_tally
>10 1 2
>20 1 2
>20 2 1
>30 2 1
>
>--
>JAG
>
>
>
>
- Next message: -Permood: "Re: SQL Query problem take ages to run"
- Previous message: Mike Epprecht \(SQL MVP\): "Re: SQL over WAN with ADO, how to optimize?"
- In reply to: John Gilson: "Re: set based guru's help wanted"
- Next in thread: John Gilson: "Re: set based guru's help wanted"
- Reply: John Gilson: "Re: set based guru's help wanted"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|