Question : Get Buying Cost From Current Stock

Hi All,

I have stock table structure below:

TMSTOCK:

1. ItemCode
2. TrsId
3. TrsDate
4. StockIn
5. StockOut
6. BuyingPrice

Key : ItemCode + TrsId

Sample Data:

ItemCode  TrsId  TrsDate        StockIn  StockOut   BuyingPrice
CCL           B1     2009/01/01   100                        1
CCL           S1     2009/01/01   0           50             0
CCL           S1     2009/01/01   0           30             0  
FNT            B1     2009/01/01   200                        1
FNT            S1     2009/01/01   0           50             0
FNT            S1     2009/01/01   0           30             0  
FNT            S1     2009/01/01   0           30             0  

The query result wanted:

ItemCode  TrsId  TrsDate         StockLast   BuyingPrice
CCL           B1     2009/01/01    80             1
FNT            B1     2009/01/01    90             1

How to query it ?

Thank you.

Answer : Get Buying Cost From Current Stock

execute each CTE individually and observe the result. You will come to know what I did. If still doubts, let me know.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
declare @TMSTOCK as table(ItemCode varchar(20),TrsId varchar(20),TrsDate datetime,StockIn int, StockOut int,BuyingPrice int)
insert into @TMSTOCK values('CCL',            'B1',     '2009/01/01',   100,         null,               1)
insert into @TMSTOCK values('CCL',            'S1',     '2009/01/01',   0  ,         50,             0)
insert into @TMSTOCK values('CCL',            'S2',     '2009/01/01',   0  ,         30,             0   )
insert into @TMSTOCK values('CCL',            'B2',     '2009/02/01',   100,         null,               2)
insert into @TMSTOCK values('CCL',            'S3',     '2009/02/01',   0  ,         30,             0   )
insert into @TMSTOCK values('CCL',            'B3',     '2009/02/01',   100,         null,               3)
insert into @TMSTOCK values('CCL',            'B4',     '2009/03/01',   100,         null,               3)
insert into @TMSTOCK values('FNT',            'B5',     '2009/01/01',   200,         null,               1)
insert into @TMSTOCK values('FNT',            'S4',     '2009/01/01',   0  ,         50,             0)
insert into @TMSTOCK values('FNT',            'S5',     '2009/01/01',   0  ,         30,             0   )
insert into @TMSTOCK values('FNT',            'S6',     '2009/01/01',   0  ,         30,             0   )

select * from @TMSTOCK
/*
CCL	B1	2009-01-01 00:00:00.000	100	NULL	1
CCL	S1	2009-01-01 00:00:00.000	0	50	0
CCL	S2	2009-01-01 00:00:00.000	0	30	0
CCL	B2	2009-02-01 00:00:00.000	100	NULL	2
CCL	S3	2009-02-01 00:00:00.000	0	30	0
CCL	B3	2009-02-01 00:00:00.000	100	NULL	3
CCL	B4	2009-03-01 00:00:00.000	100	NULL	3
FNT	B5	2009-01-01 00:00:00.000	200	NULL	1
FNT	S4	2009-01-01 00:00:00.000	0	50	0
FNT	S5	2009-01-01 00:00:00.000	0	30	0
FNT	S6	2009-01-01 00:00:00.000	0	30	0
*/

select *,row_number() over (order by (select 1)) rn from @TMSTOCK

/*
CCL	B1	2009-01-01 00:00:00.000	100	NULL	1	1
CCL	S1	2009-01-01 00:00:00.000	0	50	0	2
CCL	S2	2009-01-01 00:00:00.000	0	30	0	3
CCL	B2	2009-02-01 00:00:00.000	100	NULL	2	4
CCL	S3	2009-02-01 00:00:00.000	0	30	0	5
CCL	B3	2009-02-01 00:00:00.000	100	NULL	3	6
CCL	B4	2009-03-01 00:00:00.000	100	NULL	3	7
FNT	B5	2009-01-01 00:00:00.000	200	NULL	1	8
FNT	S4	2009-01-01 00:00:00.000	0	50	0	9
FNT	S5	2009-01-01 00:00:00.000	0	30	0	10
FNT	S6	2009-01-01 00:00:00.000	0	30	0	11
*/

;with cte_1 as (
select *,row_number() over (order by (select 1)) rn from @TMSTOCK),
       cte_2 as (
select *,(select max(rn) from cte_1 c1 where c1.rn < c2.rn and c1.BuyingPrice <> 0) new_rn
  from cte_1 c2)
select * from cte_2
/*
CCL	B1	2009-01-01 00:00:00.000	100	NULL	1	1	NULL
CCL	S1	2009-01-01 00:00:00.000	0	50	0	2	1
CCL	S2	2009-01-01 00:00:00.000	0	30	0	3	1
CCL	B2	2009-02-01 00:00:00.000	100	NULL	2	4	1
CCL	S3	2009-02-01 00:00:00.000	0	30	0	5	4
CCL	B3	2009-02-01 00:00:00.000	100	NULL	3	6	4
CCL	B4	2009-03-01 00:00:00.000	100	NULL	3	7	6
FNT	B5	2009-01-01 00:00:00.000	200	NULL	1	8	7
FNT	S4	2009-01-01 00:00:00.000	0	50	0	9	8
FNT	S5	2009-01-01 00:00:00.000	0	30	0	10	8
FNT	S6	2009-01-01 00:00:00.000	0	30	0	11	8
*/

 ;with cte_1 as (
select *,row_number() over (order by (select 1)) rn from @TMSTOCK),
       cte_2 as (
select *,(select max(rn) from cte_1 c1 where c1.rn < c2.rn and c1.BuyingPrice <> 0) new_rn
  from cte_1 c2),
       cte_3 as (
select *,case BuyingPrice when 0 then (select BuyingPrice from cte_2 c2 where c2.rn = c1.new_rn) else BuyingPrice end Seq
  from cte_2 c1)
select * from cte_3
/*
CCL	B1	2009-01-01 00:00:00.000	100	NULL	1	1	NULL	1
CCL	S1	2009-01-01 00:00:00.000	0	50	0	2	1	1
CCL	S2	2009-01-01 00:00:00.000	0	30	0	3	1	1
CCL	B2	2009-02-01 00:00:00.000	100	NULL	2	4	1	2
CCL	S3	2009-02-01 00:00:00.000	0	30	0	5	4	2
CCL	B3	2009-02-01 00:00:00.000	100	NULL	3	6	4	3
CCL	B4	2009-03-01 00:00:00.000	100	NULL	3	7	6	3
FNT	B5	2009-01-01 00:00:00.000	200	NULL	1	8	7	1
FNT	S4	2009-01-01 00:00:00.000	0	50	0	9	8	1
FNT	S5	2009-01-01 00:00:00.000	0	30	0	10	8	1
FNT	S6	2009-01-01 00:00:00.000	0	30	0	11	8	1
*/

;with cte_1 as (
select *,row_number() over (order by (select 1)) rn from @TMSTOCK),
       cte_2 as (
select *,(select max(rn) from cte_1 c1 where c1.rn < c2.rn and c1.BuyingPrice <> 0) new_rn
  from cte_1 c2),
       cte_3 as (
select *,case BuyingPrice when 0 then (select BuyingPrice from cte_2 c2 where c2.rn = c1.new_rn) else BuyingPrice end Seq
  from cte_2 c1),
       cte_4 as (
select *,
       sum(isnull(StockIn,0)) over (partition by ItemCode,TrsDate,Seq) StockIn_New,
       sum(isnull(StockOut,0)) over (partition by ItemCode,TrsDate,Seq) StockOut_New,
       row_number() over (partition by ItemCode,TrsDate,Seq order by TrsId) row_num
  from cte_3)
select *
  from cte_4 
/*
CCL	B1	2009-01-01 00:00:00.000	100	NULL	1	1	NULL	1	100	80	1
CCL	S1	2009-01-01 00:00:00.000	0	50	0	2	1	1	100	80	2
CCL	S2	2009-01-01 00:00:00.000	0	30	0	3	1	1	100	80	3
CCL	B2	2009-02-01 00:00:00.000	100	NULL	2	4	1	2	100	30	1
CCL	S3	2009-02-01 00:00:00.000	0	30	0	5	4	2	100	30	2
CCL	B3	2009-02-01 00:00:00.000	100	NULL	3	6	4	3	100	0	1
CCL	B4	2009-03-01 00:00:00.000	100	NULL	3	7	6	3	100	0	1
FNT	B5	2009-01-01 00:00:00.000	200	NULL	1	8	7	1	200	110	1
FNT	S4	2009-01-01 00:00:00.000	0	50	0	9	8	1	200	110	2
FNT	S5	2009-01-01 00:00:00.000	0	30	0	10	8	1	200	110	3
FNT	S6	2009-01-01 00:00:00.000	0	30	0	11	8	1	200	110	4
*/
 ;with cte_1 as (
select *,row_number() over (order by (select 1)) rn from @TMSTOCK),
       cte_2 as (
select *,(select max(rn) from cte_1 c1 where c1.rn < c2.rn and c1.BuyingPrice <> 0) new_rn
  from cte_1 c2),
       cte_3 as (
select *,case BuyingPrice when 0 then (select BuyingPrice from cte_2 c2 where c2.rn = c1.new_rn) else BuyingPrice end Seq
  from cte_2 c1),
       cte_4 as (
select *,
       sum(isnull(StockIn,0)) over (partition by ItemCode,TrsDate,Seq) StockIn_New,
       sum(isnull(StockOut,0)) over (partition by ItemCode,TrsDate,Seq) StockOut_New,
       row_number() over (partition by ItemCode,TrsDate,Seq order by TrsId) row_num
  from cte_3)
select ItemCode,TrsId,TrsDate,StockIn_New-StockOut_New Stack_Last,BuyingPrice
  from cte_4 where row_num = 1
/*
CCL     B1      2009-01-01 00:00:00.000 20      1
CCL     B2      2009-02-01 00:00:00.000 70      2
CCL     B3      2009-02-01 00:00:00.000 100     3
CCL     B4      2009-03-01 00:00:00.000 100     3
FNT     B5      2009-01-01 00:00:00.000 90      1
*/
Random Solutions  
 
programming4us programming4us