SQL — Объединение записей по полю идентификатора

#sql #sql-server

#sql #sql-сервер

Вопрос:

У меня есть оператор select, в котором я получаю

 ProductID  Gender Color Type     AgeRange
344842     Boys   NULL  NULL     NULL       
344842     NULL   Black NULL     NULL
344842     NULL   NULL  Leggings NULL   
344842     NULL   NULL  NULL     2 to 4 Years
 

Я хочу получить объединенную строку следующим образом

 ProductID  Gender Color Type     AgeRange
344842     Boys   Black Leggings 2 to 4 Years
 

Комментарии:

1. Можете ли вы просто выбрать ProductID, Max (Пол), max (Цвет), max (тип), max (возрастной диапазон) из Таблицы1, где ProductID = 344842 группа по ProductID

2. Вы что-нибудь пробовали?

Ответ №1:

 select 
productid, max(Gender) as Gender, 
max(Color) as Color, max(Type) as Type, max(AgeRange) as AgeRange
from
table
group by 
productid
 

Ответ №2:

Вы можете попробовать так

 select productid, max(gender) as gender, max(color) as color, max(type) as Type, max(AgeRange) as AgeRange 
from yourtable group by ProductId 
 

Ответ №3:

 select id,max(gender) gender, max(color) color, max(Type) Type, max(AgeRange) AgeRange from yourtable group by id