sql procedure

  • I want to display a various statements according to the condition through stored procedure like,

    id_student=1 got below remark in subject

    like

    Id_studentId_subjectgrade

    1 1Good

    1 2Fair

    1 3Poor

    1 4Good

    1 5Fair

    1 6Poor

    now i want to display a statements below,

    for Good

    Your performance is Good in 1,4

    for Fair

    Your performance is Fair in 2,5

    for Poor

    Your performance is Fair in 3,6

    I am using mysql database please guide me how to show it ??

    I need to show in below format

    Id_student Statement

    1 Your performance is Good in 1,4

    1 Your performance is Fair in 2,5

    1 Your performance is Fair in 3,6

    please help me.

    Thanks & Regards,
    Pallavi

  • Hi

    This is a Microsoft SQL server forum, not a MySQL forum.

    While a number of people here may have MySQL experience, you may be best posting this on a dedicated MySQL forum.

    But it looks like you want to do string concatination, which in T-SQL you can do by

    'Your performance is '+grade

    I would do a google search for "MySQL string concat"

  • Yes i need concatenation here i user group_concat function but ,

    select group_concat('your perfomance is good in',trait_name order by trait_name separator ',')

    but its showing your perfomance is good in numerical,'your perfomance is good in verbal,'your perfomance is good in cognition.

    which i dont want i need such

    'your perfomance is good in numerical,verbal,cogntion

    and i used

    'your perfomance is good in +' grade as u given it shows result 0 result.

    🙁

    Thanks & Regards,
    Pallavi

  • As I say, this is a MS SQL forum, not a MySQL forum, I do not know MySQL.

    The example I gave was for MS SQL, you will need to find the MySQL alternative or post the question on a MySQL forum.

    Group_Concat is not what you want, that will group the rows together and concatenate.

  • i want to concat row with comma separator and before that need to add one statement which is common ..

    Thanks & Regards,
    Pallavi

  • pallavi.unde (1/14/2013)


    i want to concat row with comma separator and before that need to add one statement which is common ..

    post the sample data with expected output

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

  • If you download this SQLCLR component...

    http://groupconcat.codeplex.com/[/url]

    ...then you can write code like this in SQL Server very similar to what you would expect to be able to write in MySQL. Adding the ability to more easily port MySQL code to SQL Server is part of why I wrote the component.

    CREATE TABLE dbo.x

    (

    Id_student INT,

    Id_subject INT,

    grade VARCHAR(10)

    )

    INSERT INTO dbo.x

    (Id_student, Id_subject, grade)

    VALUES (1, 1, 'Good'),

    (1, 2, 'Fair'),

    (1, 3, 'Poor'),

    (1, 4, 'Good'),

    (1, 5, 'Fair'),

    (1, 6, 'Poor');

    SELECT Id_student,

    'Your performance is ' + grade + ' in ' + GroupConcatTest.dbo.group_concat(Id_subject) AS [Statement]

    FROM dbo.x

    GROUP BY Id_student,

    grade;

    There is an XML method as well. If you download group_concat check in the demo scripts for many comparable examples of using group_concat and the XML method.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • In SQL Server, you could use the following:

    SELECT 'Your performance is ' + CONVERT(Varchar, grade);

    I would consult the MySQL documentation under "string concatenation" for details on how it's done in that database.

  • Ed Wagner (1/14/2013)


    In SQL Server, you could use the following:

    SELECT 'Your performance is ' + CONVERT(Varchar, grade);

    I would consult the MySQL documentation under "string concatenation" for details on how it's done in that database.

    This problem goes a bit beyond simple string concatenation.

    Consider how you would display the list of subjects the student did poorly in, e.g.

    Your performance is Poor in 3,6

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • The fields can be concatenated together however they're needed. I certainly don't claim to know MySQL at all - I was just offering a SELECT statement that would return what was needed.

  • All you showed was the SELECT column list. Could you please provide the full query based on the test table I provided in an earlier post? I would like to see how you would get the grouping of the subject IDs for a particular student.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Now I see the trouble with what you were asking. Does this give you what you're after? It uses a correlated subquery [child] to concatenate the list for each row in [parent]. The [formatted] name is just for formatting it nicely and removing the trailing comma. Since there was no table name in the original post, I used a temporary table named [#test], but you can change it however you like.

    SELECT formatted.grade, LEFT(formatted.grade_id_list, LEN(formatted.grade_id_list) - 1)

    FROM (SELECT DISTINCT parent.grade, (SELECT CONVERT (Varchar, child.id_grade) + ','

    FROM #test child

    WHERE child.grade = parent.grade

    ORDER BY child.grade

    FOR XML PATH ('')) grade_id_list

    FROM #test parent) formatted

    ORDER BY formatted.grade;

    I have no idea if this will work in MySQL at all.

  • Ed Wagner (1/14/2013)


    Now I see the trouble with what you were asking. Does this give you what you're after? It uses a correlated subquery [child] to concatenate the list for each row in [parent]. The [formatted] name is just for formatting it nicely and removing the trailing comma. Since there was no table name in the original post, I used a temporary table named [#test], but you can change it however you like.

    SELECT formatted.grade, LEFT(formatted.grade_id_list, LEN(formatted.grade_id_list) - 1)

    FROM (SELECT DISTINCT parent.grade, (SELECT CONVERT (Varchar, child.id_grade) + ','

    FROM #test child

    WHERE child.grade = parent.grade

    ORDER BY child.grade

    FOR XML PATH ('')) grade_id_list

    FROM #test parent) formatted

    ORDER BY formatted.grade;

    I have no idea if this will work in MySQL at all.

    Welcome! We are now on the same page.

    That is the XML PATH technique I alluded to in my earlier post. It is equivalent to using the SQLCLR object I linked to which attempts to provide a replacement for MySQL's GROUP_CONCAT on SQL Server.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

Viewing 13 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic. Login to reply