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
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Parameters SQL

Author  Topic 

Asnatyx
Starting Member

1 Post

Posted - 2013-05-24 : 08:56:02

hello

how can i work with parameters, working with case, sum etc... instructions?
For example i have these lines that select some values, these values i want to store in a parameter because i want to SUM those fields after.... how is the better way to do this?


CASE WHEN SUM(CASE WHEN A.TYPE_MOV = 1 THEN A.VALUE_MOV * -1 ELSE A.VALUE_MOV END) > 0 THEN SUM(CASE WHEN A.TYPE_MOV = 1 THEN A.VALOR_MOV * -1 ELSE A.VALUE_MOV END) ELSE 0 END AS VALUE_RECEIVE,
CASE WHEN SUM(CASE WHEN A.TYPE_MOV = 1 THEN A.VALUE_MOV * -1 ELSE A.VALUE_MOV END) < 0 THEN SUM(CASE WHEN A.TYPE_MOV = 1 THEN A.VALUE_MOV * -1 ELSE A.VALUE_MOV END) ELSE 0 END AS VALOR_PAY,


----z-----

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-05-24 : 09:13:58
--Try like this...

SELECT CASE WHEN VALOR_PAY1 WHEN >0 THEN VALUE_MOV1 ELSE 0 END VALUE_RECEIVE
,CASE WHEN VALOR_PAY1 <0 THEN VALOR_PAY1 ELSE 0 END VALOR_PAY
FROM (SELECT SUM(CASE WHEN A.TYPE_MOV = 1 THEN A.VALUE_MOV * -1 ELSE A.VALUE_MOV END) VALOR_PAY1
,SUM(CASE WHEN A.TYPE_MOV = 1 THEN A.VALOR_MOV * -1 ELSE A.VALUE_MOV END) VALUE_MOV1
FROM TableNAme
) Temp


--
Chandu
Go to Top of Page
   

- Advertisement -