Click here to Skip to main content
16,017,247 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
VB
VoucherID              amount
   1                     5
   1                     5
   2                     2
   3                     7
   3                     7
   3                     7


Output needed like below:

XML
amount
  14



*** take 5+2+7=14
Posted

SQL
select VoucherID, Sum(Amount) from ((select Distinct VoucherID, amount from table)) xx
 
Share this answer
 
Comments
Shahnawazcode 3-Apr-14 3:42am    
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[USP_GetDealerReceiptTotalByExecutiveID]


@FromDate datetime,
@ToDate datetime,
@ExecutiveID int

AS
SET NoCount On
BEGIN




select s.VoucherID, Sum(s.Amount) from ((select Distinct s.VoucherID, s.Amount from dbo.VoucherDetail))


FROM dbo.VoucherEntry d,
dbo.VoucherDetail s



WHERE d.TranDate BETWEEN @FromDate AND @ToDate
AND s.ReceivedToUsersID=@ExecutiveID
AND d.ID=s.VoucherID


END

This is my store procedure
But it shows error "Incorrect syntax near the keyword 'FROM'."
AndrewCharlz 3-Apr-14 3:51am    
select s.VoucherID, Sum(s.Amount) from (
select Distinct s.VoucherID, s.Amount from dbo.VoucherDetail s
left outer join dbo.VoucherEntry d on d.ID=s.VoucherID
where s.ReceivedToUsersID=@ExecutiveID
and d.TranDate BETWEEN @FromDate AND @ToDate ) xx
Shahnawazcode 3-Apr-14 4:01am    
Thanks for your reply....

what is the meaning of xx?
I have put "select s.VoucherID, Sum(s.Amount) from ( select Distinct s.VoucherID, s.Amount from dbo.VoucherDetail s left outer join dbo.VoucherEntry d on d.ID=s.VoucherID where s.ReceivedToUsersID=@ExecutiveID and d.TranDate BETWEEN @FromDate AND @ToDate )"

it shows the following error
Incorrect syntax near the keyword 'END'.
AndrewCharlz 3-Apr-14 5:03am    
u can use any alias for that sub query without that ull get error
Shahnawazcode 3-Apr-14 5:38am    
Can you give me the complete query from this.. This will helpfull for me..

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[USP_GetDealerReceiptTotalByExecutiveID]


@FromDate datetime,
@ToDate datetime,
@ExecutiveID int

AS
SET NoCount On
BEGIN


select s.VoucherID, Sum(s.Amount) from ( select Distinct s.VoucherID, s.Amount from dbo.VoucherDetail s left outer join dbo.VoucherEntry d on d.ID=s.VoucherID where s.ReceivedToUsersID=@ExecutiveID and d.TranDate BETWEEN @FromDate AND @ToDate )

--FROM dbo.VoucherEntry d,
-- dbo.VoucherDetail s



--WHERE d.TranDate BETWEEN @FromDate AND @ToDate
--AND s.ReceivedToUsersID=@ExecutiveID
--AND d.ID=s.VoucherID
--distinct s.VoucherID


END
SQL
with cte (voucherid, amount)
as
(
  select Distinct voucherid, amount from table1
)select sum(amount) as amount from cte
 
Share this answer
 
Comments
King Fisher 3-Apr-14 3:12am    
;)
go with solution1:
else
SQL
select sum(t1.amount) from(select distinct * From sample1 )t1 left join  (select distinct *From sample1) t2 on t1.id=t2.id and t1.amount=t2.amount
 
Share this answer
 
SQL
Select voucherID, SUM(AMOUNT) FROM
(
    SELECT Row_Number() over(partition by voucherId, Amount group by voucherID, Amount) as rownr,
	voucheriD, amount
) as collection
where rownr = 1
 
Share this answer
 

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