How to fix column invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause issue

huangapple go评论95阅读模式
英文:

How to fix column invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause issue

问题

我一直在尝试处理这个问题,但似乎无法清除错误。我在查询中使用了"group by"子句,所以我不确定我哪里出错了。错误出现在我第一个左连接的地方,我尝试对重量进行求和。

  1. select
  2. l.me_id,
  3. l.CREATED_DATE,
  4. st.location_name,
  5. st.location_address_1,
  6. st.location_city,
  7. st.location_state_code,
  8. st.location_postal_code,
  9. l.mode_type,
  10. li.weight,
  11. li.hazmat,
  12. li.PRODUCT_DESCRIPTION
  13. from tp.table l
  14. LEFT OUTER JOIN
  15. (Select m.* from
  16. (SELECT truck_ID,hazmat,sum(weight) as 'Weight', product_description,
  17. Row_number() OVER(PARTITION BY ME_ID ORDER BY freight_ID DESC) AS R_NO
  18. FROM [TP].[table2] where status='active'
  19. GROUP BY ME_ID) m where R_NO=1) li
  20. ON L.truck_ID=li.truck_id
  21. LEFT OUTER JOIN
  22. (Select n.* from
  23. (SELECT ME_ID, location_name, location_address_1, location_city, location_state_code,location_postal_code,
  24. Row_number() OVER(PARTITION BY ME_ID ORDER BY stop_ID DESC) AS R_NO
  25. FROM [TP].[table3] where status='active' and STOP_SEQUENCE_NUMBER = '1') n where R_NO=1) st
  26. ON L.truck_ID=st.truck_id
  27. where year(l.CREATED_date) ='2022'
  28. and LOCATION_address_1 LIKE '%6210 GLENWAY%'
  29. or LOCATION_address_1 like '%480 WILEY%'
  30. or LOCATION_address_1 like '%575 5TH%' -- not present in data
  31. or LOCATION_address_1 like '%100 E ROOSEVELT%'
  32. or LOCATION_address_1 like '%565 5TH%' -- not present in data
  33. or LOCATION_address_1 like '%1001 SE TV%'
  34. or LOCATION_address_1 like '%435 HUDSON%'
  35. or LOCATION_address_1 like '%575 FIFTH%'-- not present in data
  36. or LOCATION_address_1 like '%3940 PLANK%'
  37. or LOCATION_address_1 like '%609 SW 8TH%'
  38. or LOCATION_address_1 like '%28145 HARRISON%'
  39. or LOCATION_address_1 like '%2620 SW 17Th%'
  40. or LOCATION_address_1 like '%833 W 16TH%'
  41. or LOCATION_address_1 like '%992 POQUONNOCK%'
  42. or LOCATION_address_1 like '%4555 DANVERS%'
  43. or LOCATION_address_1 like '%100 COMMERCE DR%'
  44. or LOCATION_address_1 like '%6600 JEFFERSON%';

希望这能帮助你解决问题。

英文:

I have been playing around with this for quite some bit and cant seem to clear the error. I have the group by clause in my query so Im not sure where im going wrong. The error is coming from my first left outer join where I try to sum the weight.

  1. select
  2. l.me_id,
  3. l.CREATED_DATE,
  4. st.location_name,
  5. st.location_address_1,
  6. st.location_city,
  7. st.location_state_code,
  8. st.location_postal_code,
  9. l.mode_type,
  10. li.weight,
  11. li.hazmat,
  12. li.PRODUCT_DESCRIPTION
  13. from tp.table l
  14. LEFT OUTER JOIN
  15. (Select m.* from
  16. (SELECT truck_ID,hazmat,sum(weight) as 'Weight', product_description,
  17. Row_number() OVER(PARTITION BY ME_ID ORDER BY freight_ID DESC) AS R_NO
  18. FROM [TP].[table2] where status='active'
  19. GROUP BY ME_ID) m where R_NO=1) li
  20. ON L.truck_ID=li.truck_id
  21. LEFT OUTER JOIN
  22. (Select n.* from
  23. (SELECT ME_ID, location_name, location_address_1, location_city, location_state_code,location_postal_code,
  24. Row_number() OVER(PARTITION BY ME_ID ORDER BY stop_ID DESC) AS R_NO
  25. FROM [TP].[table3] where status='active' and STOP_SEQUENCE_NUMBER = '1') n where R_NO=1) st
  26. ON L.truck_ID=st.truck_id
  27. where year(l.CREATED_date) ='2022'
  28. and LOCATION_address_1 LIKE '%6210 GLENWAY%'
  29. or LOCATION_address_1 like '%480 WILEY%'
  30. or LOCATION_address_1 like '%575 5TH%' -- not present in data
  31. or LOCATION_address_1 like '%100 E ROOSEVELT%'
  32. or LOCATION_address_1 like '%565 5TH%' -- not present in data
  33. or LOCATION_address_1 like '%1001 SE TV%'
  34. or LOCATION_address_1 like '%435 HUDSON%'
  35. or LOCATION_address_1 like '%575 FIFTH%'-- not present in data
  36. or LOCATION_address_1 like '%3940 PLANK%'
  37. or LOCATION_address_1 like '%609 SW 8TH%'
  38. or LOCATION_address_1 like '%28145 HARRISON%'
  39. or LOCATION_address_1 like '%2620 SW 17Th%'
  40. or LOCATION_address_1 like '%833 W 16TH%'
  41. or LOCATION_address_1 like '%992 POQUONNOCK%'
  42. or LOCATION_address_1 like '%4555 DANVERS%'
  43. or LOCATION_address_1 like '%100 COMMERCE DR%'
  44. or LOCATION_address_1 like '%6600 JEFFERSON%'

答案1

得分: 0

尝试将您的子查询更改为以下内容:

  1. SELECT ME_ID
  2. ,hazmat
  3. ,sum(weight) as 'Weight'
  4. ,product_description
  5. ,Row_number() OVER(PARTITION BY ME_ID ORDER BY freight_ID DESC) AS R_NO
  6. FROM [TP].[line_item]
  7. WHERE status='active'
  8. GROUP BY ME_ID
  9. ,hazmat
  10. ,product_description
  11. ,freight_ID

不确定这是否会给您想要的相同结果,但至少应该可以工作。

英文:

Try changing your sub-select to the following:

  1. SELECT ME_ID
  2. ,hazmat
  3. ,sum(weight) as 'Weight'
  4. ,product_description
  5. ,Row_number() OVER(PARTITION BY ME_ID ORDER BY freight_ID DESC) AS R_NO
  6. FROM [TP].[line_item]
  7. WHERE status='active'
  8. GROUP BY ME_ID
  9. ,hazmat
  10. ,product_description
  11. ,freight_ID

Not sure if that will give you the same results you want, but it should at least work.

huangapple
  • 本文由 发表于 2023年5月25日 21:56:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76333087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定