Simple, pure Sql Query (getting rid of duplicate results)

时间:2017-08-30 20:08:35

标签: sql sql-server

If anyone know a better title please, go ahead and change it.

I know that my attempt is mediacore at best so if you know a better aproach please share. I will try to keep it clear and simple:

My database table:

--debug-brk

What I need as output:

--inspect-brk

My attempt:

"npm-run-all": "node $NODE_DEBUG_OPTION myapp.js" 

My result:

  ID  |      Category    |     Number   |      NumberInCategory    |

   1             A                31                101
   2             B                31                107
   3             C                31                121
   4             A                32                191
   5             A                33                165
   6             B                32                156
   7             C                32                127

I just cant seem to find a way to get this working.

I anyone wants to help here is full script for https://www.tutorialspoint.com/execute_sql_online.php

 Number  |         A         |        B        |         C       

   31             101                107                121
   32             191                156                127
   33             165                NULL               NULL

EDIT Simple GROUP BY at the end does the job .____.

SELECT (SELECT DISTINCT(Number) FROM MYDB V WHERE F.ID = V.ID) AS Id, 
        (SELECT NumberInCategory FROM MYDB  V WHERE F.Number = V.Number AND Category =  'A' ) AS CategoryA  ,
        (SELECT NumberInCategory FROM MYDB V WHERE F.Number = V.Number AND Category =  'B' ) AS CategoryB,
        (SELECT NumberInCategory FROM MYDB  V WHERE F.Number = V.Number AND Category =  'C')  AS CategoryC
FROM MYDB F;

Result:

31|101|107|121
31|101|107|121
31|101|107|121
32|191|156|127
33|165||
32|191|156|127
32|191|156|127  

Although the aproach from Accepted Answer (by @Gordon Linoff) is better.

2 个答案:

答案 0 :(得分:3)

You can use conditional aggregation:

Origin

答案 1 :(得分:1)

Sql server supports a syntax called PIVOT:

newbutton.focus()

The basic format is:

form1.focus()