Elahe
Elahe

Reputation: 1399

How to change the value of every cell in sql server?

I have a table with this column:

tblTransaction
( 
Transaction_ID,
Transaction_RequestTransactionCode,
Transaction_MobileErrorCode
)

by this query:

SELECT        Transaction_RequestTransactionCode, 
              SUM(case when Transaction_MobileErrorCode = '0' then 1 else 0 end) AS _Count
FROM          tblTransaction
GROUP BY Transaction_RequestTransactionCode

I got this result :

enter image description here

but the problem is here that I want to get report from this result. so I need to have name of every transaction, instead of transactionCode.

06 --> TransactionName1
51 --> TransactionName2
...

I want to change my query some thing like this:

if (06) then Transaction_RequestTransactionCode = TransactionName1
else if (51) then Transaction_RequestTransactionCode = TransactionName2

...

how can I do that without adding another table?

Thanks for any helping.

Upvotes: 0

Views: 61

Answers (2)

Pரதீப்
Pரதீப்

Reputation: 93734

Use Case Statement

SELECT CASE
         WHEN Transaction_RequestTransactionCode = 06 THEN 'TransactionName1'
         WHEN Transaction_RequestTransactionCode = 51 THEN 'TransactionName2'
       END      AS transaction_name,
       Sum(CASE
             WHEN Transaction_MobileErrorCode = '0' THEN 1
             ELSE 0
           END) AS _Count
FROM   tblTransaction
GROUP  BY CASE
            WHEN Transaction_RequestTransactionCode = 06 THEN 'TransactionName1'
            WHEN Transaction_RequestTransactionCode = 51 THEN 'TransactionName2'
          END 

Note : If the transaction_name is stored in some other table then you can join that table and pull the respective transaction_names

Upvotes: 1

Dgan
Dgan

Reputation: 10285

User Row_Number()

SELECT        Transaction_RequestTransactionCode, 
              SUM(case when Transaction_MobileErrorCode = '0' then 1 else 0 end) AS _Count,
Row_Number () Over (Order by Transaction_RequestTransactionCode )
as 'TransactionName'+RN 
FROM          tblTransaction
GROUP BY Transaction_RequestTransactionCode

Upvotes: 0

Related Questions