Want to include columns without using group....

  • Team,

    I would like to include few columns (varchar and int data types) from same table in below query without adding in group clause, i do not want these column to be group but want to include, please advise.

    ---------------------------------------------------------------

    SELECT count(*) as Qty, CustomerName, [SalesOrder], CONVERT(VARCHAR(10),LogDate,110) FROM [MyLocalDB]

    group by Customer , [Sales Order], LogDate

    order by LogDate

    ----------------------------------------------------------------

    Thank You

  • ghitpro (2/21/2014)


    Team,

    I would like to include few columns (varchar and int data types) from same table in below query without adding in group clause, i do not want these column to be group but want to include, please advise.

    ---------------------------------------------------------------

    SELECT count(*) as Qty, CustomerName, [SalesOrder], CONVERT(VARCHAR(10),LogDate,110) FROM [MyLocalDB]

    group by Customer , [Sales Order], LogDate

    order by LogDate

    ----------------------------------------------------------------

    Thank You

    What do you mean you don't want to group? Are you saying you don't want to group by those columns? That is what aggregate data does.

    _______________________________________________________________

    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/

  • You might want to take a look at the OVER Clause.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • ghitpro (2/21/2014)


    Team,

    I would like to include few columns (varchar and int data types) from same table in below query without adding in group clause, i do not want these column to be group but want to include, please advise.

    Why? What's the final objective you're trying to arrive at that Group By is something to avoid?


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • Team,

    if i include column without group clause im getting below error...

    i want to add one column without using aggregate function but want column data beside aggregate columns....

    ____________________________________________

    Column 'station' is invalid in the select list because it is not

    contained in either an aggregate function or the GROUP BY clause

    ___________________________________________________

    is their any way i can add column without using aggregate function in aggregate query...

    Thank You Guys

  • Two options:

    Create another query based on the summary query and the original table. include the column from the original table that you couldn't use in the summary query.

    Or

    Build the query using windowing functions.

Viewing 6 posts - 1 through 5 (of 5 total)

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