• madsovenielsen (8/1/2013)


    Thanks for the quick reply.

    Im getting the following error when using the query:

    Msg 8120, Level 16, State 1, Line 1

    Column 'VisWebHoldSkema.FagNavn' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

    Query:

    SELECT FagNavn, adgangskrav, frakl, tilkl, Ugedag, holdId, fagKode, CONVERT(CHAR(10), StartDato, 105) AS startDate, CONVERT(CHAR(10), slutDato, 105) AS endDate, afdeling, link, navn, deltagerBetaling, bt, niveau,

    MAX(Case when Ugedag = 'Mandag' then frakl + ' - ' + tilkl else null end) as startSlutMandag,

    MAX(Case when Ugedag = 'Tirsdag' then frakl + ' - ' + tilkl else null end) as startSlutTirsdag,

    MAX(Case when Ugedag = 'Onsdag' then frakl + ' - ' + tilkl else null end) as startSlutOnsdag,

    MAX(Case when Ugedag = 'Torsdag' then frakl + ' - ' + tilkl else null end) as startSlutTorsdag,

    MAX(Case when Ugedag = 'Fredag' then frakl + ' - ' + tilkl else null end) as startSlutFredag

    FROM [VisWebHoldSkema] ORDER BY afdeling

    This is because you don't have a group by but you do have aggregate data. It is a bit difficult to say what you need to do here but as a guess you might try putting the aggregate columns and the primary key into a cte then you can join to the cte from your query. Something like this maybe?

    with AggregateData as

    (

    select UserID,

    MAX(Case when Ugedag = 'Mandag' then frakl + ' - ' + tilkl else null end) as startSlutMandag,

    MAX(Case when Ugedag = 'Tirsdag' then frakl + ' - ' + tilkl else null end) as startSlutTirsdag,

    MAX(Case when Ugedag = 'Onsdag' then frakl + ' - ' + tilkl else null end) as startSlutOnsdag,

    MAX(Case when Ugedag = 'Torsdag' then frakl + ' - ' + tilkl else null end) as startSlutTorsdag,

    MAX(Case when Ugedag = 'Fredag' then frakl + ' - ' + tilkl else null end) as startSlutFredag

    from example

    group by UserId

    )

    SELECT FagNavn, adgangskrav, frakl, tilkl, Ugedag, holdId, fagKode, CONVERT(CHAR(10), StartDato, 105) AS startDate,

    CONVERT(CHAR(10), slutDato, 105) AS endDate, afdeling, link, navn, deltagerBetaling, bt, niveau,

    ad.startSlutMandag, ad.startSlutTirsdag, ad.startSlutOnsdag, ad.startSlutTorsdag, ad.startSlutFredag

    from VisWebHoldSkema vwhs

    join AggregateData ad on ad.UserId = vwhs.FagNavn --or whatever you would use to join here

    ORDER BY afdeling

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/