0

Please help me with next problem. I have next table:

|  Name  |  Size  |  DateTime    |
|--------|--------|--------------| 
| Adi    |  120   |  2017-01-26  |
|--------|--------|--------------|
| Mark   | 225    | 2017-01-11   |
|--------|--------|--------------|
| Adi    | 150    | 2017-01-28   |
|--------|--------|--------------|
| Adi    | 115    | 2017-02-01   |
|--------|--------|--------------|
| Adi    | 170    | 2017-02-05   |
|--------|--------|--------------|

I need to put one more column with all registrations of each name(if exist), ordereb by DateTime column like this:

| Name   | Size1 | Size2 | Size3 | 
|--------|-------|-------|-------| 
| Adi    | 120   | 150   | 115   | 
|--------|-------|-------|-------| 

I have tried with:

SELECT DISTINCT table.Name, table.Size
FROM database.table AS table
JOIN database.table AS table1
ON table.Name = table.Name

Thank you for your help!

Adrian
  • 1
  • 1
  • It's unclear what you are asking. Adding a column in the query isn't enough? Are you missing rows (use LEFT JOIN). –  Feb 17 '17 at 08:25
  • I need to put one more column with all registrations of each name(if exist), ordereb by DateTime column – Adrian Feb 17 '17 at 10:17

1 Answers1

1

Group by the column you want to be unique. Aggregate functions like min() apply to each unique group and not the whole result set

select name, 
       min(size) as size1, 
       max(size) as size2
from your_table
group by name
having min(size) <> max(size)
juergen d
  • 201,996
  • 37
  • 293
  • 362