A
A
Andrey Kuznetsov2021-06-24 15:49:44
Transact SQL
Andrey Kuznetsov, 2021-06-24 15:49:44

What does MAX(type) mean in a short TSQL query?

Can you tell me what MAX(type) means?
MAX - returns the maximum value.
But it is not clear why MAX is used in a particular query:

SELECT maker, MAX(type)
FROM product
GROUP BY maker
HAVING COUNT(DISTINCT type) = 1 AND COUNT(model) > 1

Table Product:

maker model type
A 1232 PC
A 1233 PC
A 1276 Printer
A 1298 Laptop
A 1401 Printer
A 1408 Printer
A 1752 Laptop
B 1121 PC
B 1750 Laptop
C 1321 Laptop
D 1288 Printer

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
d-stream, 2021-06-24
@Andryushok

TSQL is strict enough that it does not allow fields that do not participate in either grouping or aggregation.
Which is actually logical.
To "bypass" this severity, the user's email dangling next to him when grouping by id - you have to either include it in the grouping or ask for min / max (knowing that this is supposedly superfluous)
Well, you can try to imagine what to show as a result of a simple request id-user-role table when grouped by role )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question