Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a query as follow,

SQL
SELECT payment_txn_status, COUNT(*) AS "count", YEAR(tb.date_created) AS date_created 
FROM tpayment_txn tt 
JOIN tbooking tb 
  ON tb.booking_ref_id = tt.booking_ref_id 
GROUP BY tt.payment_txn_status, YEAR(tb.date_created)

which gives me result like ,

SQL
+-------------------+------+--------------+
|payment_txn_status |count | date_created |
+-------------------+------+--------------+
|  210              |   68 |     2011     |
|  201              |   34 |     2012     |
|  205              |   57 |     2011     |


(8 rows affected)

I have one more table named
SQL
payment_txn_status_ref 
which contains description for each payment staus code as follow,

SQL
+---------------+-----------+
|payment_status |description|
+---------------+-----------+
|201            | Created   |
|202            | Progress  |
|202            | Collected |
|205            | Paid      |


So I want to modify above query so that it will replace payment_txn_status code with their respective description from payment_txn_status_ref table

The result should be look like ,

SQL
+-------------------+------+--------------+
|payment_txn_status |count | date_created |
+-------------------+------+--------------+
|  Created          |   68 |     2011     |
|  Progress         |   34 |     2012     |
|  Paid             |   57 |     2011     |

and so on....
Posted
Comments
CGN007 24-Feb-12 6:12am    
Please update the question with the three table structure...!!!

Try something like this:
SQL
SELECT ps.description AS payment_txn_status, COUNT(*) AS "count", YEAR(tb.date_created) AS date_created
FROM tpayment_txn tt
JOIN tbooking tb
  ON tb.booking_ref_id = tt.booking_ref_id
JOIN payment_txn_status_ref ps 
  ON payment_txn_status = payment_status
GROUP BY tt.payment_txn_status, YEAR(tb.date_created)


Good luck!
 
Share this answer
 
Comments
vikram_shinde 24-Feb-12 5:19am    
it's giving error ---
Column 'payment_txn_status_ref.description ' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
E.F. Nijboer 24-Feb-12 9:17am    
What if you simply try "select description ..." without the ps. prefix? also remove the " AS payment_txn_status" here. It should simply take the description from the joined table.
It get solved... many thanks



SELECT ps.payment_status_desc AS payment_txn_status, COUNT(*) AS "count", YEAR(tb.date_created) AS date_created
FROM tpayment_txn tt
JOIN tbooking tb
ON tb.booking_ref_id = tt.booking_ref_id
JOIN tpayment_txn_status_ref ps
ON tt.payment_txn_status = ps.payment_status
GROUP BY ps.payment_status_desc, YEAR(tb.date_created)
 
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