G
G
German Jet2019-01-31 11:55:34
SQL
German Jet, 2019-01-31 11:55:34

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

I get as output:
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       Выход

I need the employee with pin=61, Andrey, to be displayed in one line, but in the Time and
event_point_name columns so that there are several values:
Name           lastname      Time                   pin        event_point_name
--------------------------------------------------------------------------------
Андреев        Андрей        15.01.2019, 08:16       61        Вход
                             15.01.2019, 08:16       61        Выход

Those. need grouping by unique pin. Please help me complete the request

Answer the question

In order to leave comments, you need to log in

4 answer(s)
K
Konstantin Tsvetkov, 2019-01-31
@tsklab

You do not show on what grounds [Name] and [lastname] should not be shown.

S
Sergei S., 2019-01-31
@seganim

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.

D
d-stream, 2019-01-31
@d-stream

"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

1
180Ringing, 2019-02-01
@180Ringing

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 question

Ask a Question

731 491 924 answers to any question