Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
ALTER PROCEDURE [dbo].[K_FS_GetFeedDivisionReport] 
@orderno varchar(50)
AS
BEGIN

SET NOCOUNT ON;
select OT.OrderNo,FT.Feedtype,ModeofPack='Plastic',BG.Bagtype,OD.Numofbags,   
(OD.numofbags*BG.Bagtype) as TotalQty,sum(TotalQty) as Total from  
K_FS_OrderconfirmDetails OD
inner join K_FS_bagtype BG on OD.bagtype=BG.sno
inner join k_FPS_FeedType FT  on FT.sno=OD.feedtype
inner join K_FS_OrderDetails OT on OT.sno = OD.orderno where OD.orderno = @orderno
group by  OT.OrderNo,FT.Feedtype,BG.Bagtype,OD.Numofbags
END



Here I calculate TotalQty,I want to calculate sum of TotalQty but TotalQty is a alias name Is it possible?other how can i solve it..please help me...
Posted

You cannot use the alias, this has to do with how the SELECT-statement is evaluated. See this wiki[^] for a description.

Instead of using the alias use the full expression again.
Replace
SQL
SUM(TotalQty)
with
SQL
SUM(OD.numofbags*BG.Bagtype)
 
Share this answer
 
v2
Comments
Siva Hyderabad 3-Jan-14 10:27am    
yes,but i want to calculate the sum of "TotalQty" column ..i mean all rows of Totalqty
André Kraak 3-Jan-14 10:32am    
It should do just that.

For each unique combination of the fields specified in the GROUP BY-clause the total value should be calculated.
thatraja 3-Jan-14 10:45am    
He gave you right answer. Final query is

select OT.OrderNo,FT.Feedtype,ModeofPack='Plastic',BG.Bagtype,OD.Numofbags, SUM(OD.numofbags*BG.Bagtype) as Total from K_FS_OrderconfirmDetails OD inner join K_FS_bagtype BG on OD.bagtype=BG.sno inner join k_FPS_FeedType FT on FT.sno=OD.feedtype inner join K_FS_OrderDetails OT on OT.sno = OD.orderno where OD.orderno = @orderno group by OT.OrderNo,FT.Feedtype,BG.Bagtype,OD.Numofbags
SQL
ALTER PROCEDURE [dbo].[K_FS_GetFeedDivisionReport]
    @orderno varchar(50)
    AS
    BEGIN

    SET NOCOUNT ON;
    select OT.OrderNo,FT.Feedtype,ModeofPack='Plastic',BG.Bagtype,OD.Numofbags,
    (OD.numofbags*BG.Bagtype) as TotalQty,
    Total = (Select Sum (OD.numofbags*BG.Bagtype) from  K_FS_OrderconfirmDetails OD
    inner join K_FS_bagtype BG on OD.bagtype=BG.sno
    inner join k_FPS_FeedType FT  on FT.sno=OD.feedtype
    inner join K_FS_OrderDetails OT on OT.sno = OD.orderno where OD.orderno = @orderno)

 from
    K_FS_OrderconfirmDetails OD
    inner join K_FS_bagtype BG on OD.bagtype=BG.sno
    inner join k_FPS_FeedType FT  on FT.sno=OD.feedtype
    inner join K_FS_OrderDetails OT on OT.sno = OD.orderno where OD.orderno = @orderno
    group by  OT.OrderNo,FT.Feedtype,BG.Bagtype,OD.Numofbags,OD.numofbags*BG.Bagtype
    END
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900