Issue with nested SQL query

  • Hi Team,

    I am getting the following error message when I am trying to execute this nested query

    "Incorrect syntax near 'Hours'."

    Could some one correct me where I did wrong with this query..

    select CC,CategoryName,

    TypeName,temp.[No of Visits],cast(sum(ActualDuration)/60.0 as numeric(35,2)) 'Hours' ,

    CONVERT(VARCHAR(2),cast(sum(ActualDuration)/60.0 as numeric(35,2)) /60 % 60) 'mins'

    from (

    Select Client.ReferenceCode2 CC, CategoryName,

    TypeName, count(EventID)"No of Visits"

    from Event

    inner join EventType on Event.EventTypeRef = EventType.EventTypeID

    inner join EventCategory on EventType.EventCategoryRef = EventCategory.EventCategoryID

    inner join Client on Event.ClientRef = Client.ClientID

    inner join Visit on Visit.ClientRef = Client.ClientID

    Where

    EventDate between '2014-02-01' and '2014-03-01'

    and TypeName = 'Lcare'

    Group by Client.ReferenceCode2, CategoryName, TypeName, count(EventID)

    )temp

    group by ClientCostCentre,CategoryName,

    TypeName,temp.[No of Visits],cast(sum(ActualDuration)/60.0 as numeric(35,2)) 'Hours'

    Thanks

    Krishna

  • You can't have aliases in a GROUP BY clause.

    My guess is you copied the group by from the SELECT...

    MM



    select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);

  • Forum Etiquette: How to post Reporting Services problems
  • [/url]
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • [/url]
  • How to Post Performance Problems - by Gail Shaw
  • [/url]

  • Hi,

    Thanks for the tip.

    I have removed 'Hours' from the GroupBY clause. But, no luck.

    I am 100% sure that ActualDuration is a valid column.. It could be am doing some thing wrong with the select statement..

    The following is the error messages.

    Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause.

    Invalid column name 'ActualDuration'.

  • You have more problems than I spotted at first.

    ActualDuration doesn't exist anywhere in the source sub query "temp", so you can't SELECT it.

    You can't have aggregate functions or aliases in GROUP BY.

    Have a read of the link Forum Etiquette: How to post data/code on a forum to get the best help[/url] and then come back with something we can help you with better.

    MM



    select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);

  • Forum Etiquette: How to post Reporting Services problems
  • [/url]
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • [/url]
  • How to Post Performance Problems - by Gail Shaw
  • [/url]

    Viewing 4 posts - 1 through 3 (of 3 total)

    You must be logged in to reply to this topic. Login to reply