Answer the question
In order to leave comments, you need to log in
How to form a SQL query with grouping by one column?
Welcome all.
I made a query like this:
SELECT
USERINFO.Name,
USERINFO.lastname,
FORMAT(acc_monitor_log.[time], 'dd.MM.yyyy, HH:mm') AS 'Time',
acc_monitor_log.pin,
acc_monitor_log.event_point_name
FROM
acc_monitor_log
INNER JOIN USERINFO
ON USERINFO.USERID = acc_monitor_log.pin
WHERE USERINFO.DEFAULTDEPTID = 15 AND
acc_monitor_log.event_type = 1000 AND
acc_monitor_log.event_point_id >= 1
/*GROUP BY acc_monitor_log.event_point_name*/
ORDER BY acc_monitor_log.[time] DESC
OFFSET 1000 ROWS FETCH NEXT 10 ROWS ONLY
Name lastname Time pin event_point_name
--------------------------------------------------------------------------------
Петров Петр 15.01.2019, 08:25 69 Вход
Андреев Андрей 15.01.2019, 08:16 61 Вход
Андреев Андрей 15.01.2019, 08:16 61 Выход
Олегов Олег 15.01.2019, 08:04 111 Выход
Name lastname Time pin event_point_name
--------------------------------------------------------------------------------
Андреев Андрей 15.01.2019, 08:16 61 Вход
15.01.2019, 08:16 61 Выход
Answer the question
In order to leave comments, you need to log in
You do not show on what grounds [Name] and [lastname] should not be shown.
SQL will not return multidimensional data, after all string records. You can use the concat function , which allows you to "glue" the fields together. Right here description. Group by pin and event_point_name, glue the one you need, parse the output and get the desired format.
"output" is the result of executing the query in SQL Management Studio?
Usually this type of "hiding duplicate values" (by alignment trailer, etc.) is the diocese of reporters, etc.
A SQL honestly gives the table.
Well, or as they wrote about concat from MySQL above, only in MS SQL it will be stub + for xml
Add a row_number() over (partition by pin order by time) as RN column to the select
Wrap your query in a CTE and select from it.
Select iif(RN = 1 ,Name, '' ), iif(RN = 1 ,Last name, '' ) ,Time , pin , event_point_name from cte
This is indicative
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question