05214M 2 BRAKE PAD METAL EACH 1992 POLARIS 400 XC/ XCR
05214M 2 BRAKE PAD METAL EACH 1991 POLARIS 400 XC/ XCR
您好了上面的数据我试图获取具有相同部件号(05214M)和相同型号(POLARIS 400 XC / XCR)的所有行,并使用年份和型号创建标题。
所以我希望这个的标题是
BRAKE PAD METAL POLARIS 400 XC/ XCR 1991-1992
这可以使用SQL吗?
答案 0 :(得分:2)
使用min max和group by
select model, concat( min(`year`) , '-', max(`year`))
from my_table
group by model
如果你想要一个特定的模型,你可以使用where
select model, concat( min(`year`) , '-', max(`year`))
from my_table
where model = '05214M'
group by model
如果你想要一个具有单行的模型的不同结果,并且模型包含多行,你可以使用这样的联合
select model, concat( min(`year`) , '-', max(`year`))
from my_table
group by model
having count(*)>1
union
select model, `year`
from my_table
group by model
having count(*) = 1
答案 1 :(得分:1)
如果由于某种原因您不能使用组,您也可以使用这样的窗口函数来执行此操作:
SELECT model,
CAST(min(year) over (partition by model order by year) as varchar(4)) + '-' +
CAST(max(year) over (partition by model order by year) as varchar(4)) as Date_Range
FROM atable
答案 2 :(得分:-1)
使用concat和group by
select concat(item,' ',model,' ',Min(year),'-',Max(year)) as title
from Table_name
group by sku;