Need help with SQL SELECT string

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2008
Posts: 2
Reputation: jdoneth is an unknown quantity at this point 
Solved Threads: 0
jdoneth jdoneth is offline Offline
Newbie Poster

Need help with SQL SELECT string

 
0
  #1
Oct 17th, 2008
I have been using the below SELECT string for some time and it has worked well, I now need to add a SUM and GROUP BY to this string and have not been able to get it to work.

Dim selstrHISTORY As String = "SELECT history.route_no,history.Acct_no,history.Post_date,history.tran_code," & _
"history.Amount,SUM(history.Amount),SERVICE.parcel FROM history INNER JOIN SERVICE ON service.serv_id = history.serv_id " & _
" WHERE history.route_no = 'PEN' AND history.post_date = " & "{" & DataGridViewDates.CurrentCell.Value.ToString & "}" & _
"AND history.tran_code = 'AC' GROUP BY history.Acct_no"

Need to add:
SUM(history.Amount)
GROUP BY history.Acc_no

Thanks for any help
Joe
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 305
Reputation: timothybard is an unknown quantity at this point 
Solved Threads: 25
timothybard's Avatar
timothybard timothybard is offline Offline
Posting Whiz

Re: Need help with SQL SELECT string

 
0
  #2
Oct 17th, 2008
When you make the query a Group By query, I believe you need to add that you are grouping by all the other fields as well, not just history.Acct_no.

  1. Dim selstrHISTORY As String = "SELECT history.route_no,history.Acct_no,history.Post_date,history.tran_code," & _
  2. "history.Amount,SUM(history.Amount),SERVICE.parcel FROM history INNER JOIN SERVICE ON service.serv_id = history.serv_id " & _
  3. " WHERE history.route_no = 'PEN' AND history.post_date = " & "{" & DataGridViewDates.CurrentCell.Value.ToString & "}" & _
  4. "AND history.tran_code = 'AC' GROUP BY history.route_no, history.Acct_no, history.Post_date, history.tran_code, history.Amount, SERVICE.parcel"

If you don't want to group with the other fields, then either don't include them in the query or SUM them or perform some other calculation on them.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2
Reputation: jdoneth is an unknown quantity at this point 
Solved Threads: 0
jdoneth jdoneth is offline Offline
Newbie Poster

Re: Need help with SQL SELECT string

 
0
  #3
Oct 17th, 2008
Problem solved thanks to help from timothybard

Tim you pointed me in the right direction, actual string ends up as follows.
Thanks
Dim selstrHISTORY As String = "SELECT history.route_no,history.Acct_no,history.Post_date,history.tran_code," & _
"SUM(history.Amount),SERVICE.parcel FROM history INNER JOIN SERVICE ON service.serv_id = history.serv_id " & _
" WHERE history.route_no = 'PEN' AND history.post_date = " & "{" & DataGridViewDates.CurrentCell.Value.ToString & "}" & _
"AND history.tran_code = 'AC' GROUP BY history.route_no, history.Acct_no, history.Post_date, history.tran_code, SERVICE.parcel"
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 305
Reputation: timothybard is an unknown quantity at this point 
Solved Threads: 25
timothybard's Avatar
timothybard timothybard is offline Offline
Posting Whiz

Re: Need help with SQL SELECT string

 
0
  #4
Oct 17th, 2008
Ahh.. yes.. I noticed that you had history.amount and SUM(history.amount) in your original post, somewhere along the line, I had forgotten about it.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC