How to group with a query?

How do you group lines (i.e. purchase lines) based on fields such as location code, posting group using a query? These lines would be grouped later when the document is posted and the grouping would be split into the separate documents

Answers