2017-06-08 3 views
0

Datensatz:Kombination Reihen von-Code

CREATE TABLE Returned(
    Code varchar(20) not null, 
    RUnits int not null, 
    RCost int not null, 
    RPrice int not null, 
    RDate date not null); 


    Insert into Returned(Code, Runits, rcost, rprice, rdate) 
    values 
    ('ORANGES123', 10, 200, 500, '2017-04-01'), 
    ('BANANAS123', 15, 350, 900, '2017-04-01'), 
    ('APPLES123', 7, 234, 756, '2017-04-01'), 
    ('ORANGES123', 10, 200, 500, '2017-04-02'), 
    ('BANANAS123', 15, 350, 900, '2017-04-02'), 
    ('APPLES123', 7, 234, 756, '2017-04-02'); 



    CREATE TABLE Cancelled(
    Code varchar(20) not null, 
    CUnits int not null, 
    CCost int not null, 
    CPrice int not null, 
    CDate date not null 
); 

    Insert into Cancelled(Code, Cunits, Ccost, Cprice, Cdate) 
    values 
    ('ORANGES123', 3, 100, 200, '2017-04-01'), 
    ('BANANAS123', 5, 243, 500, '2017-04-01'), 
    ('APPLES123', 10, 235, 537, '2017-04-01'), 
    ('ORANGES123', 3, 100, 200, '2017-04-02'), 
    ('BANANAS123', 5, 243, 500, '2017-04-02'), 
    ('APPLES123', 10, 235, 537, '2017-04-02'); 

Sqlfiddle hier:

http://sqlfiddle.com/#!9/f10634

Hintergrund:

Ich habe 2 Tabellen. Eine Tabelle für Rücksendungen und eine Tabelle für Stornierungen. Ich möchte eine Zusammenfassung über die vergangene Woche der Gesamtanzahl von Einheiten/Kosten/Preis für einen bestimmten Artikelcode erhalten. Zum Beispiel aus der sqlfiddle, ORANGES123, möchte ich meine Frage zurückzukehren:

ItemCode , TotalReturnedUnits, TotalReturnedCost, TotalReturnedPrice,TotalCancelledUnits, TotalCancelledCost, TotalCancelledPrice 
ORANGES123,     20,    400,    1000,     6,    200,     400 

Dies scheint einfach, aber aus irgendeinem Grund, wenn ich tue eine innere durch ItemCode zwischen meinen beiden Tabellen in SQL Server verbinden, Einheiten zwischen Die Tabellen "Storniert" und "Zurückgegeben" werden kombiniert und die Zählungen werden zwischen Zurückgegeben/Storniert verschmutzt.

Ich fühle mich wie ich etwas sehr einfach vermisse.

Jede Hilfe wäre willkommen.

Die eigentliche Abfrage, mit der ich arbeite, ist hier. Ich versuchte, die sqlfiddle so nahe wie möglich zu modellieren: mit diesem

SELECT sales.Code AS Code, 
     sales.Quantity AS QtyReturned, 
     price.WghtAvgCost * sales.Quantity AS ReturnedCost, 
     price.CurrentPrice * sales.Quantity AS ReturnedPrice, 
     orders.CancelledUnits, 
     orders.CancelledCost, 
     orders.CancelledPrice, 
     price.CurrentPriceType AS PriceType 
FROM salesTable sales 
    INNER JOIN costPriceTable price ON sales.Code= price.Code 
                AND (sales.BusinessDate BETWEEN price.StartDate AND price.EndDate) 
                AND sales.LocationId = price.LocationId 
    INNER JOIN ordersTable orders 
     ON sales.Code= orders.Code 
+0

Können wir Ihre Select-Anweisung bitte sehen? – jimmy8ball

+0

Können Sie Ihre Anfrage einbeziehen? –

+0

Wenn ich es wäre, hätte ich nur einen Tisch statt zwei. – Strawberry

Antwort

1

Start - Ich glaube, Sie den Rest herausfinden können, ...

SELECT *, 'returned' source FROM returned 
UNION ALL 
SELECT *, 'cancelled' FROM cancelled; 
+------------+--------+-------+--------+------------+-----------+ 
| Code  | RUnits | RCost | RPrice | RDate  | source | 
+------------+--------+-------+--------+------------+-----------+ 
| ORANGES123 |  10 | 200 | 500 | 2017-04-01 | returned | 
| BANANAS123 |  15 | 350 | 900 | 2017-04-01 | returned | 
| APPLES123 |  7 | 234 | 756 | 2017-04-01 | returned | 
| ORANGES123 |  10 | 200 | 500 | 2017-04-02 | returned | 
| BANANAS123 |  15 | 350 | 900 | 2017-04-02 | returned | 
| APPLES123 |  7 | 234 | 756 | 2017-04-02 | returned | 
| ORANGES123 |  3 | 100 | 200 | 2017-04-01 | cancelled | 
| BANANAS123 |  5 | 243 | 500 | 2017-04-01 | cancelled | 
| APPLES123 |  10 | 235 | 537 | 2017-04-01 | cancelled | 
| ORANGES123 |  3 | 100 | 200 | 2017-04-02 | cancelled | 
| BANANAS123 |  5 | 243 | 500 | 2017-04-02 | cancelled | 
| APPLES123 |  10 | 235 | 537 | 2017-04-02 | cancelled | 
+------------+--------+-------+--------+------------+-----------+ 
+0

Danke, das hat mich weit genug gebracht. – jackfrost5234

0

Sie können eine Join-Abfrage verwenden, um dies zu erreichen, Folgen Sie der gleichen Struktur für Ihre Stornierungen.

JOIN 
(SELECT 
Code, sum(RUnits) as TotalReturnedUnits, sum(RCost) as TotalReturnedCost, 
sum(RPrice) as TotalReturnedPrice 
from Returned 
where 

--parameterised dates can be used 
--RDate between @Start and @End 

group by Code 
)returns 
ON salesTable.Code = returns.Code