Changing a set of Hardcoded values so they are picked up from a table

  • Hi All,

    I have a stored procedure which generates a table.

    The values in part of this table are COUNTs of the values in the column CodOutcome.

    I have a table already which is called

    Positivity:

    CodOutcome NamEmotion

    0 Happiness

    1 Joy

    2 Bliss

    3 Love

    4 Contentment

    [Code]

    SELECT Emotionality,

    SUM(CASE WHEN CodEmotion = '0' THEN 1 ELSE 0 END) as 'Happiness',

    SUM(CASE WHEN CodEmotion = '1' THEN 1 ELSE 0 END) as 'Joy',

    SUM(CASE WHEN CodEmotion = '2' THEN 1 ELSE 0 END) as 'Bliss.',

    SUM(CASE WHEN CodEmotion = '3' THEN 1 ELSE 0 END) as 'Love',

    SUM(CASE WHEN CodEmotion = '4' THEN 1 ELSE 0 END) as 'Contentment',

    FROM#tmpCount

    GROUP BY SalesPerson

    ORDER BY SalesPerson

    [/Code]

    I would like to get rid of the hard coding and use the values in Positivity as varibales to populate the Query.

  • Hello,

    Your wording of what you are after is a puzzel.

    But here's a stab at what I think you want:

    select NamEmotion, count(*)

    from Positivity

    group by NamEmotion

    order by Nam Emotion

    That should give you a list of the emotion and the number of times it is in the Positivity table.

    If you want a combination of the two fields then :

    select NamEmotion + '-' + convert(varchar(5),CodOutcome), count(*)

    from Positivity

    group by NamEmotion + '-' + convert(varchar(5),CodOutcome)

    order by NamEmotion + '-' + convert(varchar(5),CodOutcome)

    Ok?

    Regards,

    Terry

  • That is not exactly what I am looking for.

    I would like to get the number of emotions for each salesperson and rather than harcoding in the Names I would like to pick those names up from another table.

  • I think I'm starting to grasp what you're after here, but to generate a solution, I'd like a bit more information than you've given.

    Please supply the following:

    A sampling of data in the table/tables emotionality and salesperson come from. (Preferably as a create/insert script so I can recreate the sample data here)

    An example of how you want your query output to look.

    Confirmation of my thinking where you're going with this:

    I'm guessing you have a table with salesperson and an emotion number(multiple lines per sales person with different numbers) in it, something like the following:

    SalesPerson CoDOutcome

    1 1

    1 3

    2 2

    2 4

    You want to take this data and generate a list of all applicable emotions per salesperson.

    I may be completely missing this again, and if I am, please correct me, and give those other requested pieces of information either way.

    Seth Phelabaum


    Consistency is only a virtue if you're not a screwup. 😉

    Links: How to Post Sample Data[/url] :: Running Totals[/url] :: Tally Table[/url] :: Cross Tabs/Pivots[/url] :: String Concatenation[/url]

Viewing 4 posts - 1 through 3 (of 3 total)

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