Results 1 to 4 of 4

Thread: sql statement

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2004
    Posts
    100

    sql statement

    I generate report with Data Report without using Data Environment.

    I have table like below
    weight(NoRujukan,Sampel)

    I want the data of the table group by NoRujukan.

    My sql statement is,

    VB Code:
    1. rs.Open "SELECT weight.NoRujukan, weight.Sampel FROM weight group by NoRujukan", conn, adOpenStatic, adLockOptimistic

    But, when execute, the error of the sql is something about aggregate function.
    Wht does it mean?

    Can anyone give me a sample of how to group data report?

  2. #2
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    Aggregate functions are functions like SUM, COUNT, AVG, etc. When doing a GROUP BY query, any fields in the SELECT clause that are not included in the GROUP BY clause must use an aggregate function. For example, your query could be corrected as follows:
    VB Code:
    1. rs.Open "SELECT weight.NoRujukan, [b]SUM(weight.Sampel)[/b] FROM weight group by NoRujukan", conn, adOpenStatic, adLockOptimistic
    "It's cold gin time again ..."

    Check out my website here.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2004
    Posts
    100
    Originally posted by BruceG
    VB Code:
    1. rs.Open "SELECT weight.NoRujukan, [b]SUM(weight.Sampel)<b>Aggregate functions are functions like SUM, COUNT, AVG, etc. When doing a GROUP BY query, any fields in the SELECT clause that are not included in the GROUP BY clause must use an aggregate function. For example, your query could be corrected as follows:
    2. </b> FROM weight group by NoRujukan", conn, adOpenStatic, adLockOptimistic
    [/B]
    I use the code above and there is error which is --->DataField 'Sampel' not found.

    If i didnt use the "SUM" and "GROUP BY", the report can be displayed.

    Why is that so?
    Thank You in advance.

  4. #4
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    Originally posted by azrina
    I use the code above and there is error which is --->DataField 'Sampel' not found.

    If i didnt use the "SUM" and "GROUP BY", the report can be displayed.

    Why is that so?
    Thank You in advance.
    Sum is an expression hence the result will be automatically named Expr1 unless you indicate the fieldname/alias it will use. Since your using the fieldname in the datareport control's datafield property, indicate the alias.

    VB Code:
    1. 'Query
    2. rs.Open "SELECT weight.NoRujukan, SUM(weight.Sampel) [b]AS AGGR_Sampel[/b] FROM weight group by NoRujukan", conn, adOpenStatic, adLockOptimistic
    3.  
    4. 'In datareport
    5. DataReport1.Sections("Details").Controls("txtSampel").DataField = "AGGR_Sampel"

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