473,419 Members | 1,910 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

Sum() function wont work because of group by, what to do?

Sl1ver
196 100+
i got sum() and count() functions in my sql query, now it doesnt work because i use group by statements, but if i dont groupby then i get errors telling me that i should group by what to do?
Feb 23 '10 #1
4 1792
ck9663
2,878 Expert 2GB
Could you post here what you have so far?

~~ CK
Feb 23 '10 #2
Sl1ver
196 100+
Look at the first select , you will see the sum function
and just a little down just after 'from' you will see the other calculation that form part of that.
This query return 11 results from the db
Take receivedqty for instance got all different qty's but when i do the sum function all i get duplicate of the data in a new column named TotalLineItems.
And the count function just returns 1 for each record, it should be 11

Expand|Select|Wrap|Line Numbers
  1. declare @PONum varchar(200);
  2. set @PONum = 'RC-739942'
  3.  
  4. select 
  5. convert(varchar(10), getdate(), 112) as [date],
  6. Receives.poh__number as OrderNum, 
  7. --Receives.poh__date as OrderDate, 
  8. Receives.itm__code as Item,
  9. isnull(Receives.itm__description, Receives.itm__variant)  as [description],
  10. --Receives.pol__qty as OrderQty, 
  11. Receives.poh__user9 as DeliveryNote,
  12. Receives.scn__code as Scanner,
  13. Receives.ith__date as ReceivedDate, 
  14. Receives.iti__unload_count as ReceivedQty,
  15. sum(Receives.[sum]) as TotalLineItems,
  16. sum(Receives.[count]) as totallines,
  17. --Checks.ith__name as CheckNum, 
  18. --Checks.iti__qty as CheckQty, 
  19. --Checks.ith__date as CheckDate, 
  20. --Checks.iti__unload_count as CheckedQty,
  21. --PutAways.ith__name as PutAwayNum, 
  22. --PutAways.iti__qty as ToBePutAwayQty, 
  23. --PutAways.ith__date as PutAwayDate, 
  24. --PutAways.iti__unload_count as PutAwayQty,
  25. Receives.usr__firstname as ReceiveUser
  26. --Checks.usr__firstname as CheckUser, 
  27. --PutAways.usr__firstname as PutAwayUser
  28. from 
  29. (select 
  30. count(*) as [count], iti__unload_count as [sum], scn__code,poh__user9, convert(varchar,itm__description) as itm__description, convert(varchar,itm__variant) as itm__variant, ith__id, iti__id, poh__number, poh__user1, poh__date, itm__code, pol__qty, ith__name, ith__code, ith__date, iti__qty, iti__unload_count, usr__firstname
  31. from bx_PurchaseOrderHeader R
  32. join bx_PurchaseOrderLine RL on RL.pol__poh_id = R.poh__id
  33. join bx_PurchaseOrderTransfer RT on RT.pot__poh_id = R.poh__id
  34. join bx_ItemLocationTransferHeader RTH on RTH.ith__id = RT.pot__ith_id
  35. join bx_ItemLocationTransferInstruction RTI on RTI.iti__ith_id = RTH.ith__id and RTI.iti__itm_id = RL.pol__itm_id
  36. left join bx_itemlocationtransfertransaction on iti__id = ilt__iti_id
  37. left join bx_session on ssn__id = ilt__ssn_id
  38. left join bx_scanner on scn__id = ssn__scn_id
  39. left join bx_user on usr__id = ssn__usr_id
  40. join bx_Item I on I.itm__id = RL.pol__itm_id
  41. where R.poh__number = @PONum /*R.poh__number like 'RC-%' and poh__date > getdate() - 5*/
  42. and RTH.ith__ltt_id = 4
  43. group by 
  44. scn__code,
  45. poh__user9,
  46. ith__id, iti__id, poh__number,convert(varchar,itm__description),convert(varchar,itm__variant), poh__user1, poh__date, itm__code, pol__qty,ith__name,ith__code, ith__date, iti__qty, iti__unload_count, usr__firstname
  47.  
  48. ) Receives
  49. join 
  50. (select 
  51. ith__id, ith__name, ith__code, itm__code, iti__qty, iti__unload_count, ith__date, usr__firstname
  52. from bx_ItemLocationTransferHeader CTH
  53. join bx_ItemLocationTransferInstruction CTI on CTI.iti__ith_id = CTH.ith__id
  54. left join bx_itemlocationtransfertransaction on iti__id = ilt__iti_id
  55. left join bx_session on ssn__id = ilt__ssn_id
  56. left join bx_scanner on scn__id = ssn__scn_id
  57. left join bx_user on usr__id = ssn__usr_id
  58. join bx_Item I on I.itm__id = iti__itm_id
  59. where CTH.ith__ltt_id = 0
  60. and CTH.ith__name like 'CK-%'
  61. group by
  62. ith__id, ith__name, ith__code, itm__code, iti__qty, iti__unload_count, ith__date, usr__firstname
  63. ) Checks on Checks.ith__id = Receives.ith__id + '_CHECK'
  64. and Checks.itm__code = Receives.itm__code
  65. left join 
  66. (select 
  67. ith__id, iti__id, poh__number, poh__user1, poh__user9, poh__date, itm__code, pol__qty, ith__name, ith__code, ith__date, iti__qty, iti__unload_count, usr__firstname
  68. from bx_PurchaseOrderHeader P
  69. join bx_PurchaseOrderLine PL on PL.pol__poh_id = P.poh__id
  70. join bx_PurchaseOrderTransfer PT on PT.pot__poh_id = P.poh__id
  71. join bx_ItemLocationTransferHeader PTH on PTH.ith__id = PT.pot__ith_id
  72. join bx_ItemLocationTransferInstruction PTI on PTI.iti__ith_id = PTH.ith__id and PTI.iti__itm_id = PL.pol__itm_id
  73. left join bx_itemlocationtransfertransaction on iti__id = ilt__iti_id
  74. left join bx_session on ssn__id = ilt__ssn_id
  75. left join bx_scanner on scn__id = ssn__scn_id
  76. left join bx_user on usr__id = ssn__usr_id
  77. join bx_Item I on I.itm__id = PL.pol__itm_id
  78. where P.poh__number like 'PA-%' /*and poh__date > getdate() - 5*/
  79. and PTH.ith__ltt_id = 5
  80. group by
  81. ith__id, iti__id, poh__number, poh__user1, poh__user9, poh__date, itm__code, pol__qty, ith__name, ith__code, ith__date, iti__qty, iti__unload_count, usr__firstname
  82. ) PutAways on PutAways.poh__user1 = Receives.poh__user1 and PutAways.itm__code = Receives.itm__code
  83. and Checks.ith__name like '%'+PutAways.poh__user9+'%'
  84. group by 
  85. Receives.poh__number, 
  86. Receives.poh__date, 
  87. Receives.itm__code, 
  88. Receives.pol__qty, 
  89. Receives.ith__date, 
  90. Receives.iti__unload_count,
  91. Checks.ith__name, 
  92. Checks.iti__qty, 
  93. Checks.ith__date, 
  94. Checks.iti__unload_count,
  95. PutAways.ith__name, 
  96. PutAways.iti__qty, 
  97. PutAways.ith__date, 
  98. PutAways.iti__unload_count,
  99. Receives.usr__firstname, 
  100. Checks.usr__firstname, 
  101. PutAways.usr__firstname, 
  102. Receives.itm__description, 
  103. Receives.itm__variant,
  104. Receives.[sum],
  105. Receives.poh__user9,
  106. Receives.scn__code,
  107. Receives.[count]
  108. order by Receives.poh__number, Receives.ith__date, PutAways.ith__date
  109.  
Feb 24 '10 #3
ck9663
2,878 Expert 2GB
How about posting some sample data and how you want it returned, instead.

~~ CK
Feb 25 '10 #4
Sl1ver
196 100+
got it sorted out, got the total count and sum of products calculated in the report writer. SHot for the help tho
Feb 26 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: MX1 | last post by:
I have a query written in MS Access that has a few calculated fields. Is it possible to refer to that query in a form field. I'd like the form field to show the sum of one of the columns from the...
3
by: Kevin Carter | last post by:
Hi i have a table that contains several fields field 1(area), field2(quantity),Field3(price) What i want to do is return total quatity and total price against a distict area i have the...
1
by: peter pilsl | last post by:
I have a question to sql and dont know exactely if this is the right place to ask. If not I would be glad if anyone could point me to the correct group/list/resource. Thnx. I want to perform a...
0
by: dba123 | last post by:
THERE HAS TO BE A SOLUTION FOR THIS!!!! How can I get around the limitation in SSRS 2005 of being able to SUM a Group referenced field in my FOOTER!!! It's driving me nuts My footer field's...
5
by: David | last post by:
Hi, I have an asp page which prints data as follows: The sum in brackets is a calculated result on the page, SQ * RS2("SelectorQty"), a variable SQ which holds a qty multiplied by a record...
10
by: Rudolf Bargholz | last post by:
Perhaps some kind soul could help me out with an SQL I have been trying all day to get to work, where one colum is just not summing up the way I want it to. I have the following data GRP_SEQ ...
1
by: gillian3114 | last post by:
why this sum function won't work ? eg Sum(personal) lblPersonal.Caption it wont retrieve the sum value of personal Dim cn6 As New ADODB.Connection Dim strCNString4 As String Dim rs6 As New...
2
by: gillian3114 | last post by:
why this sum function won't work ? eg Sum(personal) lblPersonal.Caption it wont retrieve the sum value of the personal field Dim cn6 As New ADODB.Connection Dim strCNString4 As String Dim rs6...
2
by: dwasbig9 | last post by:
Hi Group (fairly limited knowledge of Access and almost none of Access VBA. Using Access 2003). I need to sum time, I've found through the groups archive an sql extract that led me to this ...
54
by: bearophileHUGS | last post by:
Empty Python lists don't know the type of the items it will contain, so this sounds strange: 0 Because that may be an empty sequence of someobject: 0 In a statically typed language in...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.