Mateen
asked on
Mr xxq94813 one more wish.
Refer
https://www.experts-exchange.com/questions/20401845/could-this-be-coded-in-sql-server-2000-or-could-it-be-designed-in-powerbuilder-7.html
Mr xxq94813 I have successfully applied your
logic and has got the desired result.
The only concern is it is taking a lot of time.
Tables:
sales_invoice_mst(inv_no varchar(8),
inv_date date,cust_code varchar(4))
sales_invoice_det(inv_no varhcar(8),quantity numeric(12,2),grade varchar(1)
sales_return_mst(trans_no varchar(8),sales_return_da te date,cust_code varchar(4)
sales_return_det(trans_no varchar(8),quantity numeric(12,2),grade varchar(1)
sales_invoice_mst has got 25000 rows.
cancel_status<>'C' means this is not cancelled invoice.
Grade='K' means cut piece
grade='S' means short piece
My function
alter procedure dba.udf_sales(@cust_code varchar(4),@grade varchar(1),@date1 date,@date2 date)
as
begin
declare @sales numeric(12,2),@sales_ret numeric(12,2)
select @sales=sum(sales_invoice_d et.quantit y) from
sales_invoice_det,sales_in voice_mst where
sales_invoice_mst.inv_no=s ales_invoi ce_det.inv _no
and sales_invoice_mst.cust_cod e=@cust_co de
and sales_invoice_det.grade=@g rade
and sales_invoice_mst.cancel_s tatus<>'C'
and sales_invoice_mst.inv_date between @date1 and @date2
select @sales_ret=sum(sales_retur n_det.quan tity) from
sales_return_det,sales_ret urn_mst where
sales_return_mst.trans_no= sales_retu rn_det.tra ns_no
and sales_return_mst.cust_code =@cust_cod e
and sales_return_det.grade=@gr ade
and sales_return_mst.sales_ret urn_date between @date1 and @date2
return(isnull(@sales,0)-is null(@sale s_ret,0))
end
My sql select
select distinct(cust_code),
udf_sales(cust_code,'A','2 002-10-01' ,'2002-10- 31') +
udf_sales(cust_code,'B','2 002-10-01' ,'2002-10- 31') +
udf_sales(cust_code,'C','2 002-10-01' ,'2002-10- 31') +
udf_sales(cust_code,'K','2 002-10-01' ,'2002-10- 31') +
udf_sales(cust_code,'S','2 002-10-01' ,'2002-10- 31') total_qty,
udf_sales(cust_code,'A','2 002-10-01' ,'2002-10- 31') A,
udf_sales(cust_code,'B','2 002-10-01' ,'2002-10- 31') B,
udf_sales(cust_code,'C','2 002-10-01' ,'2002-10- 31') C,
udf_sales(cust_code,'K','2 002-10-01' ,'2002-10- 31') K,
udf_sales(cust_code,'S','2 002-10-01' ,'2002-10- 31') S
from sales_invoice_mst
order by total_qty desc;
The report is taking more than 10 minutes.
My wish is shorten the time to retrieve the report.
https://www.experts-exchange.com/questions/20401845/could-this-be-coded-in-sql-server-2000-or-could-it-be-designed-in-powerbuilder-7.html
Mr xxq94813 I have successfully applied your
logic and has got the desired result.
The only concern is it is taking a lot of time.
Tables:
sales_invoice_mst(inv_no varchar(8),
inv_date date,cust_code varchar(4))
sales_invoice_det(inv_no varhcar(8),quantity numeric(12,2),grade varchar(1)
sales_return_mst(trans_no varchar(8),sales_return_da
sales_return_det(trans_no varchar(8),quantity numeric(12,2),grade varchar(1)
sales_invoice_mst has got 25000 rows.
cancel_status<>'C' means this is not cancelled invoice.
Grade='K' means cut piece
grade='S' means short piece
My function
alter procedure dba.udf_sales(@cust_code varchar(4),@grade varchar(1),@date1 date,@date2 date)
as
begin
declare @sales numeric(12,2),@sales_ret numeric(12,2)
select @sales=sum(sales_invoice_d
sales_invoice_det,sales_in
sales_invoice_mst.inv_no=s
and sales_invoice_mst.cust_cod
and sales_invoice_det.grade=@g
and sales_invoice_mst.cancel_s
and sales_invoice_mst.inv_date
select @sales_ret=sum(sales_retur
sales_return_det,sales_ret
sales_return_mst.trans_no=
and sales_return_mst.cust_code
and sales_return_det.grade=@gr
and sales_return_mst.sales_ret
return(isnull(@sales,0)-is
end
My sql select
select distinct(cust_code),
udf_sales(cust_code,'A','2
udf_sales(cust_code,'B','2
udf_sales(cust_code,'C','2
udf_sales(cust_code,'K','2
udf_sales(cust_code,'S','2
udf_sales(cust_code,'A','2
udf_sales(cust_code,'B','2
udf_sales(cust_code,'C','2
udf_sales(cust_code,'K','2
udf_sales(cust_code,'S','2
from sales_invoice_mst
order by total_qty desc;
The report is taking more than 10 minutes.
My wish is shorten the time to retrieve the report.
ASKER
Hi, xxq4813
My tables are normal master detail tables.One to may
relation.
I have given you the table in brief from, otherwise
table has got more columns.
You know one customer may purchase product of
different grade on the same date.Therefore customer
and date should be in the master table and product,grade etc should come in the detail table.
So far , with your help, I am able to produce my report
although taking time. I have ginve the report to the
management. So please take time and give me some idea other
than view.
I like sql may be nested may have union all.
I have thought an idea and will try some time.
My idea is
1(step). select cust_code,sum(quantity) alias tqty sales tables
uion all cust_code,sum(qyantity*-1) sales return tables
Alias table1
2(step). select cust_code,grade,
case grade='A' then sum(quantity) alias qty_a
case grade='B' and 'C' and 'K' and 'S' (four cases)
from sales table
and similarly for sales return table with -1 as
multiplying factor.
Alias table2
the above tow tables come in the from clause
3.(Actually first)
select table1.cust_code,sum(table 1.tqty),su m(tabl2.qt y_A),sum(t abl2.qty_b ....
from step1,step2
where step1.cust_code=step2.cust _code
Presently I have a lot of assignments so I will try
the aobve later.
Thanks for your comment.
My tables are normal master detail tables.One to may
relation.
I have given you the table in brief from, otherwise
table has got more columns.
You know one customer may purchase product of
different grade on the same date.Therefore customer
and date should be in the master table and product,grade etc should come in the detail table.
So far , with your help, I am able to produce my report
although taking time. I have ginve the report to the
management. So please take time and give me some idea other
than view.
I like sql may be nested may have union all.
I have thought an idea and will try some time.
My idea is
1(step). select cust_code,sum(quantity) alias tqty sales tables
uion all cust_code,sum(qyantity*-1)
Alias table1
2(step). select cust_code,grade,
case grade='A' then sum(quantity) alias qty_a
case grade='B' and 'C' and 'K' and 'S' (four cases)
from sales table
and similarly for sales return table with -1 as
multiplying factor.
Alias table2
the above tow tables come in the from clause
3.(Actually first)
select table1.cust_code,sum(table
from step1,step2
where step1.cust_code=step2.cust
Presently I have a lot of assignments so I will try
the aobve later.
Thanks for your comment.
ASKER
Thanks to Mr David/dtodd from which I learnt
from level select. The following sql is working
and retrieving data within seconds.
select #table1.cust_code,
sum(#table1.quantity) t_qty,
sum(#table2.a_qty) a_qty,
sum(#table2.b_qty) b_qty,
sum(#table2.c_qty) c_qty,
sum(#table2.k_qty) d_qty,
sum(#table2.s_qty) s_qty
from
(select sales_invoice_mst.cust_cod e as cust_code,
sum(sales_invoice_det.quan tity) as quantity
from sales_invoice_mst,sales_in voice_det
where sales_invoice_mst.inv_no = sales_invoice_det.inv_no
group by sales_invoice_mst.cust_cod e
union all
select sales_return_mst.cust_code ,sum(sales _return_de t.quantity *-1)
from sales_return_mst,sales_ret urn_det
where sales_return_mst.trans_no = sales_return_det.trans_no
group by sales_return_mst.cust_code ) as #table1,
(select sales_invoice_mst.cust_cod e as cust_code,
case when sales_invoice_det.grade = 'A' then
sum(sales_invoice_det.quan tity) else 0 end a_qty,
case when sales_invoice_det.grade = 'B' then
sum(sales_invoice_det.quan tity) else 0 end b_qty,
case when sales_invoice_det.grade = 'C' then
sum(sales_invoice_det.quan tity) else 0 end c_qty,
case when sales_invoice_det.grade = 'K' then
sum(sales_invoice_det.quan tity) else 0 end k_qty,
case when sales_invoice_det.grade = 'S' then
sum(sales_invoice_det.quan tity) else 0 end s_qty
from sales_invoice_mst,sales_in voice_det
where sales_invoice_mst.inv_no = sales_invoice_det.inv_no
group by sales_invoice_mst.cust_cod e,
sales_invoice_det.grade
union all
select sales_return_mst.cust_code ,
case when sales_return_det.grade = 'A' then
sum(sales_return_det.quant ity*-1) else 0 end ,
case when sales_return_det.grade = 'B' then
sum(sales_return_det.quant ity*-1) else 0 end ,
case when sales_return_det.grade = 'C' then
sum(sales_return_det.quant ity*-1) else 0 end ,
case when sales_return_det.grade = 'K' then
sum(sales_return_det.quant ity*-1) else 0 end ,
case when sales_return_det.grade = 'S' then
sum(sales_return_det.quant ity*-1) else 0 end
from sales_return_mst,sales_ret urn_det
where sales_return_mst.trans_no = sales_return_det.trans_no
group by sales_return_mst.cust_code , sales_return_det.grade) as #table2
where #table1.cust_code = #table2.cust_code
group by #table1.cust_code
order by t_qty desc
Any other idea will be appreciated.
from level select. The following sql is working
and retrieving data within seconds.
select #table1.cust_code,
sum(#table1.quantity) t_qty,
sum(#table2.a_qty) a_qty,
sum(#table2.b_qty) b_qty,
sum(#table2.c_qty) c_qty,
sum(#table2.k_qty) d_qty,
sum(#table2.s_qty) s_qty
from
(select sales_invoice_mst.cust_cod
sum(sales_invoice_det.quan
from sales_invoice_mst,sales_in
where sales_invoice_mst.inv_no = sales_invoice_det.inv_no
group by sales_invoice_mst.cust_cod
union all
select sales_return_mst.cust_code
from sales_return_mst,sales_ret
where sales_return_mst.trans_no = sales_return_det.trans_no
group by sales_return_mst.cust_code
(select sales_invoice_mst.cust_cod
case when sales_invoice_det.grade = 'A' then
sum(sales_invoice_det.quan
case when sales_invoice_det.grade = 'B' then
sum(sales_invoice_det.quan
case when sales_invoice_det.grade = 'C' then
sum(sales_invoice_det.quan
case when sales_invoice_det.grade = 'K' then
sum(sales_invoice_det.quan
case when sales_invoice_det.grade = 'S' then
sum(sales_invoice_det.quan
from sales_invoice_mst,sales_in
where sales_invoice_mst.inv_no = sales_invoice_det.inv_no
group by sales_invoice_mst.cust_cod
sales_invoice_det.grade
union all
select sales_return_mst.cust_code
case when sales_return_det.grade = 'A' then
sum(sales_return_det.quant
case when sales_return_det.grade = 'B' then
sum(sales_return_det.quant
case when sales_return_det.grade = 'C' then
sum(sales_return_det.quant
case when sales_return_det.grade = 'K' then
sum(sales_return_det.quant
case when sales_return_det.grade = 'S' then
sum(sales_return_det.quant
from sales_return_mst,sales_ret
where sales_return_mst.trans_no = sales_return_det.trans_no
group by sales_return_mst.cust_code
where #table1.cust_code = #table2.cust_code
group by #table1.cust_code
order by t_qty desc
Any other idea will be appreciated.
ASKER
Thanks to Mr David/dtodd from which I learnt
from level select. The following sql is working
and retrieving data within seconds.
select #table1.cust_code,
sum(#table1.quantity) t_qty,
sum(#table2.a_qty) a_qty,
sum(#table2.b_qty) b_qty,
sum(#table2.c_qty) c_qty,
sum(#table2.k_qty) d_qty,
sum(#table2.s_qty) s_qty
from
(select sales_invoice_mst.cust_cod e as cust_code,
sum(sales_invoice_det.quan tity) as quantity
from sales_invoice_mst,sales_in voice_det
where sales_invoice_mst.inv_no = sales_invoice_det.inv_no
group by sales_invoice_mst.cust_cod e
union all
select sales_return_mst.cust_code ,sum(sales _return_de t.quantity *-1)
from sales_return_mst,sales_ret urn_det
where sales_return_mst.trans_no = sales_return_det.trans_no
group by sales_return_mst.cust_code ) as #table1,
(select sales_invoice_mst.cust_cod e as cust_code,
case when sales_invoice_det.grade = 'A' then
sum(sales_invoice_det.quan tity) else 0 end a_qty,
case when sales_invoice_det.grade = 'B' then
sum(sales_invoice_det.quan tity) else 0 end b_qty,
case when sales_invoice_det.grade = 'C' then
sum(sales_invoice_det.quan tity) else 0 end c_qty,
case when sales_invoice_det.grade = 'K' then
sum(sales_invoice_det.quan tity) else 0 end k_qty,
case when sales_invoice_det.grade = 'S' then
sum(sales_invoice_det.quan tity) else 0 end s_qty
from sales_invoice_mst,sales_in voice_det
where sales_invoice_mst.inv_no = sales_invoice_det.inv_no
group by sales_invoice_mst.cust_cod e,
sales_invoice_det.grade
union all
select sales_return_mst.cust_code ,
case when sales_return_det.grade = 'A' then
sum(sales_return_det.quant ity*-1) else 0 end ,
case when sales_return_det.grade = 'B' then
sum(sales_return_det.quant ity*-1) else 0 end ,
case when sales_return_det.grade = 'C' then
sum(sales_return_det.quant ity*-1) else 0 end ,
case when sales_return_det.grade = 'K' then
sum(sales_return_det.quant ity*-1) else 0 end ,
case when sales_return_det.grade = 'S' then
sum(sales_return_det.quant ity*-1) else 0 end
from sales_return_mst,sales_ret urn_det
where sales_return_mst.trans_no = sales_return_det.trans_no
group by sales_return_mst.cust_code , sales_return_det.grade) as #table2
where #table1.cust_code = #table2.cust_code
group by #table1.cust_code
order by t_qty desc
Any other idea will be appreciated.
from level select. The following sql is working
and retrieving data within seconds.
select #table1.cust_code,
sum(#table1.quantity) t_qty,
sum(#table2.a_qty) a_qty,
sum(#table2.b_qty) b_qty,
sum(#table2.c_qty) c_qty,
sum(#table2.k_qty) d_qty,
sum(#table2.s_qty) s_qty
from
(select sales_invoice_mst.cust_cod
sum(sales_invoice_det.quan
from sales_invoice_mst,sales_in
where sales_invoice_mst.inv_no = sales_invoice_det.inv_no
group by sales_invoice_mst.cust_cod
union all
select sales_return_mst.cust_code
from sales_return_mst,sales_ret
where sales_return_mst.trans_no = sales_return_det.trans_no
group by sales_return_mst.cust_code
(select sales_invoice_mst.cust_cod
case when sales_invoice_det.grade = 'A' then
sum(sales_invoice_det.quan
case when sales_invoice_det.grade = 'B' then
sum(sales_invoice_det.quan
case when sales_invoice_det.grade = 'C' then
sum(sales_invoice_det.quan
case when sales_invoice_det.grade = 'K' then
sum(sales_invoice_det.quan
case when sales_invoice_det.grade = 'S' then
sum(sales_invoice_det.quan
from sales_invoice_mst,sales_in
where sales_invoice_mst.inv_no = sales_invoice_det.inv_no
group by sales_invoice_mst.cust_cod
sales_invoice_det.grade
union all
select sales_return_mst.cust_code
case when sales_return_det.grade = 'A' then
sum(sales_return_det.quant
case when sales_return_det.grade = 'B' then
sum(sales_return_det.quant
case when sales_return_det.grade = 'C' then
sum(sales_return_det.quant
case when sales_return_det.grade = 'K' then
sum(sales_return_det.quant
case when sales_return_det.grade = 'S' then
sum(sales_return_det.quant
from sales_return_mst,sales_ret
where sales_return_mst.trans_no = sales_return_det.trans_no
group by sales_return_mst.cust_code
where #table1.cust_code = #table2.cust_code
group by #table1.cust_code
order by t_qty desc
Any other idea will be appreciated.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
U did a good job.
It turns out that most of time is spent on the "join". I am afraid to say that table design got some problem.
sales_invoice_mst(inv_no varchar(8),
inv_date date,cust_code varchar(4))
sales_invoice_det(inv_no varhcar(8),quantity numeric(12,2),grade varchar(1)
I believe that these 2 tables both have inv_no as primary key, it doesn't make any sense to separate them into 2 tables, then do a join of inv_no on 2 tables (creating a view in this situation doesn't help much).
Strictly speaking, your report is a common data warehouse report, which is already beyond what database can handle.
In Data Warehouse, we set up 3 dimension, cust_code, grade and time dimension, which will easily make your report.
Let me know how u think of this, or anything i can do for u.
Thanks.