Reduction of the GROUP BY clause

Is it possible to shorten the group by clause so that you do not have to repeat the fields specified in the select adause clause? For example:

SELECT
   field1,
   field2,
   field3,
   field4
FROM table
GROUP BY
   field1,
   field2,
   field3,
   field4

 to:

SELECT
 field1,
 field2,
 field3,
 field4
FROM table
GROUP BY
 SELECT.*

... or something like that. I am writing a query that will use the sp_executesql () stored procedure, and I have run out of free space in my variable. Thank you very much.

+3
source share
4 answers

are you looking for SELECT DISTINCTor SELECT DISTINCTROW?

+11
source
SELECT
   field1,
   field2,
   field3,
   field4
FROM table
GROUP BY 1,2,3,4

If the numbers represent the colum position in the selected part.

+8
source

DISTINCT GROUP BY. , , , GROUP BY .

, , DISTINCT DISTINCT, (, StartDate DATEADD (, 1, StartDate)), , , DATEADD DISTINCT. GROUP BY .

... , GROUP BY -, (, ), . , , GROUP BY.

, :

GROUP BY t.id, u.id
/* All these other fields are ignored but here because they're 
   in the HAVING/SELECT/ORDER BY clauses
*/
, t.TransactionValue, u.Username, ....

, , GROUP BY t.*, , Microsoft, https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=124654

+1

:

  • SELECT = output
  • GROUP BY =

SELECT GROUP BY. GROUP BY , SELECT.

, / . CTE "".

SELECT 
   T1.field1, T1.field2, foo.field3, foo.field4, foo.AggField5
FROM
   table T1
   JOIN
   (SELECT
        T2.keycol, T2.field3, T2.field4, SUM(T2.field5) AS AggField5
   FROM 
        table T2
   GROUP BY 
        T2.keycolT2.field3, T2.field4
   ) foo ON T1.Keycol = foo.keycol

, , DISTINCT GROUP BY...

0

Source: https://habr.com/ru/post/1720463/


All Articles