Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append text to each row of the sql select query

I have a query like this

SELECT COUNT(ID) 'Records Affected', TYPE FROM MASTER
GROUP BY TYPE

The output for this is

Records Affected            TYPE
----------------            ---- 
4                            F1
3                            F2
5                            F3

Now I would like to change the query so that the output will be as follows

Records Affected
----------------
The number of records affected for F1 is : 4 
The number of records affected for F2 is : 3
The number of records affected for F3 is : 5

"The number of records affected for " + TYPE + " is : " + COUNT.

How can I add the default text to each row of the result set instead of appending in the front end. I would like to simplify my task of just showing the records in the DataGrid as Summary.

like image 346
CPK_2011 Avatar asked Jun 06 '13 14:06

CPK_2011


1 Answers

You can easily concatenate the string using the following. You will use the + to concatenate the string to the type column and the count. Note, the count needs to be converted to a varchar for this to work:

SELECT 
  'The number of records affected for '+ type +
    ' is : '+ cast(COUNT(ID) as varchar(50)) as'Records Affected'
FROM yt
GROUP BY TYPE;

See SQL Fiddle with Demo

like image 82
Taryn Avatar answered Nov 19 '22 15:11

Taryn