Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 query doubt

Author  Topic 

anuraag205
Yak Posting Veteran

58 Posts

Posted - 2012-08-28 : 09:06:33
sum(NegotiatedSellPrice+CancellationProtectionAmt) AS [Total Amount];

or this line line in a query i want to add, if "iscancellationprotection" is "ÿes" else give only the value of negotiable price.

ex:
i have coloums
negotiableprice>>>>cancellationproection>>>>cancellationprotectionamount
1111>>>>>>>>>>>>>>>yes>>>>>>>>>>>>>>>>>>>>>>2022
2222>>>>>>>>>>>>>>>no>>>>>>>>>>>>>>>>>>>>>>>2212
3333>>>>>>>>>>>>>>>yes>>>>>>>>>>>>>>>>>>>>>>2202
4444>>>>>>>>>>>>>>>no>>>>>>>>>>>>>>>>>>>>>>>2222

i want a new coloumn which adds "negotiable price" with "cancellationprotctionamount" only if iscancellationprotecion "yes".

Please Help

Thanks

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-08-28 : 09:13:48
[code]sum( NegotiatedSellPrice + case when iscancellationprotecion = 'yes' then CancellationProtectionAmt else 0 end) AS [Total Amount];[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-08-28 : 09:14:27
case when cancellationproection = 'yes' then cancellationprotectionamount + negotiableprice else 0 end
or maybe
case when cancellationproection = 'yes' then cancellationprotectionamount + negotiableprice else negotiableprice end


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

anuraag205
Yak Posting Veteran

58 Posts

Posted - 2012-08-28 : 09:39:16
@ khtan

Msg 170, Level 15, State 1, Line 7
Line 7: Incorrect syntax near '='.



Thanks
Go to Top of Page

anuraag205
Yak Posting Veteran

58 Posts

Posted - 2012-08-28 : 09:55:59
this is my actual query::

SELECT
DATENAME(Year,BookingDate) as Year,
DATENAME(Month,BookingDate) as Month,
COUNT(InvoiceNumber) AS [Total Bookings],
Sum(NegotiatedSellPrice) AS [Total Sale Price],

sum( NegotiatedSellPrice + case when IsCancellationProtection = 'yes'
then (CancellationProtectionAmt + NegotiatedSellPrice) else NegotiatedSellPrice end
FROM tblInvoice
WHERE BookingDate >= '2006-01-01'
AND BookingDate < '2020-02-01'
AND fk_StatusID in (3, 5)

GROUP BY DATENAME(Month,BookingDate) , DATENAME(Year,BookingDate),
DATEPART(MM,BookingDate),DATEPART(YYYY,BookingDate)
ORDER BY DATEPART(YYYY,BookingDate),DATEPART(MM,BookingDate)


am getting an error

Msg 156, Level 15, State 1, Line 9
Incorrect syntax near the keyword 'FROM'.

Please Help



Thanks
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-08-28 : 10:10:18
[code]
sum( NegotiatedSellPrice + (case when IsCancellationProtection = 'yes'
then (CancellationProtectionAmt + NegotiatedSellPrice) else NegotiatedSellPrice end ) )[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

anuraag205
Yak Posting Veteran

58 Posts

Posted - 2012-08-29 : 01:18:07
i HAVE A SYNTAX ERROR...CAN I USE LIKE INSTEAD OF =

Msg 245, Level 16, State 1, Line 1
Syntax error converting the varchar value 'YES' to a column of data type bit.


CAN I USE :


sum( (case when IsCancellationProtection LIKE 'YES'
then ((CancellationProtectionAmt* SiteToSalesConvRate)+ NegotiatedSellPrice) else NegotiatedSellPrice end ) )AS [TOTAL SALES]


Thanks
Go to Top of Page

anuraag205
Yak Posting Veteran

58 Posts

Posted - 2012-08-29 : 02:15:55
I got what i wanted like this:::


SELECT
DATENAME(Year,BookingDate) as Year,
DATENAME(Month,BookingDate) as Month,
COUNT(InvoiceNumber) AS [Total Bookings],
SUM (NegotiatedSellPrice) AS [Total NEGOTIABLE Sales],


sum( ( case when IsCancellationProtection = '1'
then ((CancellationProtectionAmt* (SiteToBaseConvRate/SiteToSalesConvRate)))
else 0 end ) )
AS [Total Cancellation Protection]


FROM tblInvoice
WHERE BookingDate >= '2006-01-01'
AND BookingDate < '2020-02-01'
AND fk_StatusID in (3, 5)

GROUP BY DATENAME(Month,BookingDate) , DATENAME(Year,BookingDate),
DATEPART(MM,BookingDate),DATEPART(YYYY,BookingDate)
ORDER BY DATEPART(YYYY,BookingDate),DATEPART(MM,BookingDate)


Thanks you for your support ...:):):)


Thanks
Go to Top of Page
   

- Advertisement -