SQL Server to display all columns, but with separate values ​​in one of the columns (without grouping anything)

I have a table with 106 columns. One of these columns is the Type column with 16 types.

I want 16 lines where the Type is different. So, line 1 is of type "Design", line 2 is of type "Elevator PVT", etc.

Using Navicat.

From what I have found (and understood) so far, I cannot use Distinct (because it applies to all rows), I cannot use Group By (because it is to aggregate data that I don't look to do), therefore I am stuck.

Please be careful - I'm really really new to this.

Below is the portion of the table (how can I share it in normal mode?) - it is really large, so I did not share all this. Below is a partial result I'm looking for, where Violation_Type is unique and the rest of the columns are displayed.

Got it. Sheesh ... (took me forever, but got it ...)

  D_ID   B_ID   V_ID       V_Type      S_ID   c_f   d_y    l_u    p_s   du_p  
 ------ ------ ------- -------------- ------ ----- ------ ------ ----- ------ 
   184    117   V 032   Elevator PVT      2     8      0      0               
     4    140   V 100   Construction      1     8      0      0               
    10    116   V 122   Electric                1      8   2005     0      0  
    11    117   V 033   Boiler Local      1     0   2005      0     0       
+4
source share
1 answer

You can use ROW_NUMBERfor this:

SELECT *
FROM(
    SELECT *,
        rn = ROW_NUMBER() OVER(PARTITION BY V_Type ORDER BY (SELECT NULL))
    FROM tbl
)t
WHERE rn = 1

Change ORDER BYdepending on which line you want to set priority.


From the documentation:

Returns the serial number of a line inside the section of the result set, starting with 1 for the first line in each section.

, ( PARTITION BY) sql-server 1 , ORDER BY.

ROW_NUMBER ORDER BY. SELECT NULL sql-, . .

WHERE rn = 1 ROW_NUMBER 1. V_TYPE.

+3

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


All Articles