SQL Server How to select data with calculation in different rows in SQL

ktecyv1j  于 2023-08-02  发布在  其他
关注(0)|答案(2)|浏览(98)

My Table is

itemcode  in out  sum value   datetime     PP   
-------|---|---|-----------|-----------|---------   
A1234  | 1 | 0 | 100       |04/03/2009 | E01  
A1234  | 0 | 1 | -100      |05/03/2009 | E01    
A1234  | 1 | 0 | 100       |06/03/2009 | E01   
A1234  | 0 | 1 | -100      |07/03/2009 | E01   
A1234  | 1 | 0 | 100       |08/03/2009 | E01   
A1234  | 9 | 0 | 900       |09/03/2009 | S01  
A1234  | 0 | 3 | -300      |10/03/2009 | S01  
A1234  | 8 | 0 | 800       |11/03/2009 | S01 
A1235  | 9 | 0 | 900       |12/03/2009 | E01   
A1235  | 0 | 2 | -200      |13/03/2009 | E01  
A1235  | 0 | 3 | -300      |14/03/2009 | E01  
A1235  | 8 | 0 | 800       |15/03/2009 | S01

Result would be:

itemcode  Remain  sum value   datetime     PP   
   -------|-------|-----------|-----------|----
   A1234  | 1     | 100       |07/03/2009 | E01  
   A1234  | 6     | 600       |09/03/2009 | S01  
   A1234  | 8     | 800       |11/03/2009 | S01 
   A1235  | 4     | 400       |12/03/2009 | E01 
   A1235  | 8     | 800       |15/03/2009 | S01

How to calculate in different rows and find the last purchase late. Group by itemcode and PP

Thanks

of1yzvn4

of1yzvn41#

Hugo,

See the code below. You did not give a table name, so I named it as @a. I avoided column itemcode as it is same for all the records.

DECLARE @a TABLE 
(id INT IDENTITY(1,1), inp int, out INT,SumV int,dt DATE,PP VARCHAR(3) )
INSERT INTO @a VALUES
(1,0,100,'2014-01-01'   ,'E01'),
(0,1,-100,'2014-01-02'  ,'E01'),
(7,0,700,'2014-01-03'   ,'E01'),
(0,5,-500,'2014-01-04'  ,'E01'),
(6,0,600,'2014-01-05'   ,'E01'),
(0,7,-700,'2014-01-06'  ,'S01'),
(9,0,900,'2014-01-07'   ,'S01'),
(0,3,-300,'2014-01-08'  ,'S01')

SELECT * FROM @a

SELECT  ROW_NUMBER() OVER (ORDER BY A.dt) [#],
        A.id,
        A.dt [datetime],
        SUM(B.inp) - SUM(B.out) [Remain],
        SUM(B.SumV) [Sum value],
        A.PP
FROM    @a A 
JOIN    @a B ON B.id <= A.id
WHERE   A.inp = 0
GROUP   BY A.id,A.dt,A.pp
ORDER   BY A.id

Result:

enxuqcxy

enxuqcxy2#

This question was asked a long time ago. The desired output is wrong according to the input data and the grouping that was requested. It is a group by answer after joining in_count and out_count into a single field/column/result.

Schema (MySQL v5.7)

CREATE TABLE stock_transaction (
    item_code VARCHAR(10),
    in_count INT,
    out_count INT,
    sum_value INT,
    date_time DATE,
    pp VARCHAR(3)
);

INSERT INTO stock_transaction (item_code, in_count, out_count, sum_value, date_time, pp)
VALUES 
    ('A1234', 1, 0, 100, '2009-03-04', 'E01'),
    ('A1234', 0, 1, -100, '2009-03-05', 'E01'),
    ('A1234', 1, 0, 100, '2009-03-06', 'E01'),
    ('A1234', 0, 1, -100, '2009-03-07', 'E01'),
    ('A1234', 1, 0, 100, '2009-03-08', 'E01'),
    ('A1234', 9, 0, 900, '2009-03-09', 'S01'),
    ('A1234', 0, 3, -300, '2009-03-10', 'S01'),
    ('A1234', 8, 0, 800, '2009-03-11', 'S01'),
    ('A1235', 9, 0, 900, '2009-03-12', 'E01'),
    ('A1235', 0, 2, -200, '2009-03-13', 'E01'),
    ('A1235', 0, 3, -300, '2009-03-14', 'E01'),
    ('A1235', 8, 0, 800, '2009-03-15', 'S01');

Query

select item_code, 
sum( in_count + -1 * out_count ) as remain,
sum( sum_value ) as sum_value,
max( date_time ) as date_time,
pp
from stock_transaction
group by item_code, pp;
item_codeppremainsum_valuedate_time
A1234E0111002009-03-08
A1234S011414002009-03-11
A1235E0144002009-03-14
A1235S0188002009-03-15

View on DB Fiddle

相关问题