Results 1 to 7 of 7

Thread: why's my SQL statement invalid?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    257

    why's my SQL statement invalid?

    I'm using this statement on the wonderful DB2 (on an AS400 ) and it says that the calculated expression is invalid so what's the correct way to write this:

    select order_num, SUM(num_ordered) * quoted_price as order_total
    from order_line order by order_num

    I tried it with parenthesis around the whole thing and without the as statement and neither way worked either
    I tried to end process on Visual Studio 2005
    but PETA stopped me saying it's smart enough
    to be a living creature

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: why's my SQL statement invalid?

    I think you should create a Function which does the calculation and use that function in the Query.


    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    257

    Re: why's my SQL statement invalid?

    me too but the assignment says not to
    I tried to end process on Visual Studio 2005
    but PETA stopped me saying it's smart enough
    to be a living creature

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: why's my SQL statement invalid?

    To get the Sum you need to use a Group By, or a sub-query.

    In this case a Group By is not valid (as you are using the result in a calculation), so here's how you would do the sub-query:
    Code:
    select order_num, (SELECT SUM(num_ordered) FROM order_line) * quoted_price as order_total
    from order_line order by order_num

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    257

    Re: why's my SQL statement invalid?

    well I ran that one and apparently it can't do that cuz it said
    Token * was not valid. Valid tokens: , FROM INTO.

    you can't put subqueries as an item in the select statement anyway, just in the where or having or exists or in type clauses at the end keep in mind the AS400 is ancient!

    I seriously doubt there is a way to do this at all. Here's the table data:
    ORDER_NUM PART_NUM NUM_ORDERED QUOTED_PRICE
    21608______AT94________11__________21.95
    21610______DR93________1__________495.00
    21610______DW11________1__________399.99
    21613______KL62________4__________329.95
    21614______KT03________2__________595.00
    21617______BV06________2__________794.95
    21617______CD52________4__________150.00
    21619______DR93________1 __________495.00
    21623______KV29________2__________1,290.00
    the same order_num's need to be crunched together so that's a group by and you just can't run a sum on the quoted price field once the order nums are grouped together so there's absolutely no way to "display the order number and order total with order total being the sum of the number of units times the quoted price on each order line for each order" like the problem asks, is there?
    I tried to end process on Visual Studio 2005
    but PETA stopped me saying it's smart enough
    to be a living creature

  6. #6
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: why's my SQL statement invalid?

    Code:
    select order_num, SUM(num_ordered * quoted_price) as order_total
    from order_line 
    group by order_num
    order by order_num

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    257

    Re: why's my SQL statement invalid?

    *groan* omg that worked I'm gonna add an extra S to the AS400 label when I get back to school
    I tried to end process on Visual Studio 2005
    but PETA stopped me saying it's smart enough
    to be a living creature

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width