Derived Table and Aggregate Function Not Working Together

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • beancounter
    New Member
    • Aug 2013
    • 2

    Derived Table and Aggregate Function Not Working Together

    I am trying to get this query using a derived table and an aggregate function to work in Excel Query:
    Code:
    select purordcom.*  from 
    (select purOrdDet.PurAcct,
    sum(PurOrdDet.ExtCost),PurOrdDet.PONbr from AEMApp.dbo.PurOrdDet PurOrdDet group by PurOrdDet.PurAcct,PurOrdDet.PONbr) as PurOrdCom
    But I get a generic error. So I broke it up into two queries - one stripped of the derived table and the other stripped of the aggregate function - to try and isolate the problem:
    1.
    Code:
    select PurOrdCom.* from  (select PurOrdDet.PurAcct,PurOrdDet.ExtCost,PurOrdDet.PONbr from AEMApp.dbo.PurOrdDet PurOrdDet) as PurOrdCom
    2.
    Code:
    select purOrdDet.PurAcct,sum(PurOrdDet.ExtCost),PurOrdDet.PONbr from AEMApp.dbo.PurOrdDet PurOrdDet group by PurOrdDet.PurAcct,PurOrdDet.PONbr
    They both worked fine. But when I combine them I get the error again.

    Thank you so much!
    Last edited by Rabbit; Aug 21 '13, 10:11 PM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Please use code tags when posting code.

    The problem is that you can't have unaliased column definitions with in a subquery. So the solution is to give it an alias.

    But my question is, why bother subquerying the aggregation if you're just going to do a select all against it? It's pointless.

    Comment

    • beancounter
      New Member
      • Aug 2013
      • 2

      #3
      Rabbit,

      Got your message on code tags.

      Your solution worked.

      The query I posted was originally part of a larger query linking this derived table to another table. When it didn't work, I stripped parts of it out to uncover the source of the error.

      Thanks again. I can't tell you how frustrated I was with this!

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Glad you got it working. Good luck on the rest of your project.

        Comment

        Working...