Please help me out this question?

  • ALZDBA (9/18/2011)


    probably about the only thing missing to get a meaningful result is the product indication:

    The original specification calls for quantity, not total price (that's why I used COUNT in my set-based solution).

  • Perry Whittle (9/18/2011)


    Jeff Moden (9/18/2011)


    This is just so wrong... What is the name of the school where they are teaching you to do such a thing in SQL Server?

    RBARs R Us 😀

    Too funny. 🙂 I really do wish I could find the schools that are teaching this stuff so I could suggest a change to their curriculum to them especially if they're using MS SQL Server.

    BTW, congratulations on your recent accolade Mr Moden 😉

    Thanks, Perry. I feel extremely lucky, deeply humbled, and honored all at the same time.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • SQL Kiwi (9/18/2011)


    ALZDBA (9/18/2011)


    probably about the only thing missing to get a meaningful result is the product indication:

    The original specification calls for quantity, not total price (that's why I used COUNT in my set-based solution).

    Of course we tend to encourage some thinking and interpreting :w00t:

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • Declare @tab1 table

    (id int identity(1,1),name varchar(100),total int)

    Insert into @tab1

    Select ProductName,count(*)as Total from Products p join Sales s

    on p.ProductID = s.ProductID

    group by ProductName

    Declare @count int

    ,@incvalue int

    ,@out_msg varchar(10)

    Set @incvalue = 1

    Select @count = COUNT(*) from @tab1

    While (@count >=@incvalue)

    Begin

    Declare @total int

    ,@prodname varchar(15)

    Select @total = total

    ,@prodname = name

    from @tab1

    Where id = @incvalue

    Print 'a total of'+' '+convert(varchar,@total)+' '+@prodname+' '+'were sold '

    Set @incvalue = @incvalue+1

    END

    O/p is :

    a total of 6 DVD were sold

    a total of 6 Microwave were sold

    a total of 7 Monitor were sold

    a total of 4 Refrigerator were sold

    a total of 4 Speakers were sold

    a total of 5 VCR were sold

    Is this the o/p u need or anything else

  • Jeff Moden (9/18/2011)


    Thanks, Perry. I feel extremely lucky, deeply humbled, and honored all at the same time.

    Trust me buddy luck has nothing to do with it. IMHO you were the winner by a country mile 😉

    -----------------------------------------------------------------------------------------------------------

    "Ya can't make an omelette without breaking just a few eggs" 😉

  • Thanks, Perry. :blush:

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 6 posts - 16 through 20 (of 20 total)

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