按年份连接SQL结果(年份,品牌,型号)

时间:2015-11-17 20:20:10

标签: mysql sql concatenation

我有一个相当简单的查询,我用它来获取车辆属性,如年份,品牌和模型。数据目前看起来类似于......:

+------+------+-----------+-------+----------+------------------------------------------+
| SKU  | Year |   Make    | Model | Submodel |                  Notes                   |
+------+------+-----------+-------+----------+------------------------------------------+
| 0001 | 1995 | Chevrolet | Astro | Base     | Clear Lens; Chrome Housing; Pair; 1 pc.; |
| 0001 | 1995 | Chevrolet | Astro | CL       | Clear Lens; Chrome Housing; Pair; 1 pc.; |
| 0001 | 1995 | Chevrolet | Astro | LS       | Clear Lens; Chrome Housing; Pair; 1 pc.; |
| 0001 | 1996 | Chevrolet | Astro | Base     | Clear Lens; Chrome Housing; Pair; 1 pc.; |
| 0001 | 1996 | Chevrolet | Astro | CL       | Clear Lens; Chrome Housing; Pair; 1 pc.; |
| 0001 | 1996 | Chevrolet | Astro | LS       | Clear Lens; Chrome Housing; Pair; 1 pc.; |
| 0001 | 1997 | Chevrolet | Astro | Base     | Clear Lens; Chrome Housing; Pair; 1 pc.; |
| 0001 | 1997 | Chevrolet | Astro | LT       | Clear Lens; Chrome Housing; Pair; 1 pc.; |
| 0001 | 2001 | Chevrolet | Astro | Base     | Clear Lens; Chrome Housing; Pair; 1 pc.; |
+------+------+-----------+-------+----------+------------------------------------------+

我希望连接多年,当其他数据在行内匹配时(SKU,Make,Model,Submodel,Notes)。最终结果的一个例子是......:

+------+-------------+-----------+-------+----------+------------------------------------------+
| SKU  |    Year     |   Make    | Model | Submodel |                  Notes                   |
+------+-------------+-----------+-------+----------+------------------------------------------+
| 0001 | 1995 - 1997 | Chevrolet | Astro | Base     | Clear Lens; Chrome Housing; Pair; 1 pc.; |
| 0001 | 1995 - 1996 | Chevrolet | Astro | CL       | Clear Lens; Chrome Housing; Pair; 1 pc.; |
| 0001 | 1995 - 1996 | Chevrolet | Astro | LS       | Clear Lens; Chrome Housing; Pair; 1 pc.; |
| 0001 | 1997        | Chevrolet | Astro | LT       | Clear Lens; Chrome Housing; Pair; 1 pc.; |
| 0001 | 2001        | Chevrolet | Astro | Base     | Clear Lens; Chrome Housing; Pair; 1 pc.; |
+------+-------------+-----------+-------+----------+------------------------------------------+

查询目前看起来像......:

SELECT DISTINCT
sku,
YearID,
MakeName,
modelname,
SubmodelName,
notes
FROM
TableName
WHERE 1

在我的类似示例中,向用户提供了指向查询中“CASE”用法的答案。提供的示例如下,但它抓住了整个表格的最小年份和最长年份。最终结果是这样的,只有一个结果......

+------+-------------+-----------+-------+----------+------------------------------------------+
| SKU  |    Year     |   Make    | Model | Submodel |                  Notes                   |
+------+-------------+-----------+-------+----------+------------------------------------------+
| 0001 | 1950 - 2015 | Chevrolet | Astro | Base     | Clear Lens; Chrome Housing; Pair; 1 pc.; |
+------+-------------+-----------+-------+----------+------------------------------------------+

示例查询(包含CASE)......:

SELECT DISTINCT
sku,
CASE
WHEN MIN(YearID) = MAX(YearID) 
    THEN MIN(YearID)
ELSE CONCAT(MIN(YearID), '-', MAX(YearID))
END As Year,
MakeName,
modelname,
SubmodelName,
notes
FROM
TableName
WHERE 1

非常感谢您帮助我获得所需的结果。我一直难以理解如何通过SQL来做到这一点。

2 个答案:

答案 0 :(得分:0)

我再次改变它。请测试它,让我知道它是否有效。对不起以前的错误SQL。我忘记了第一次初始化变量。

  SELECT
    sku,
    CASE
      WHEN MIN(YEAR) = MAX(YEAR) THEN MIN(YEAR)
      ELSE CONCAT(MIN(YEAR), '-', MAX(YEAR))
     END AS YEAR,
     make, model, submodel, notes
  FROM
    (SELECT @ldfnr:= IF((@old_make = tab.make
      AND @old_model = tab.model
      AND @old_submodel = tab.submodel
      AND @old_notes = tab.notes
      AND (@old_year = tab.`year`
      OR @old_year = tab.`year`-1)) , @ldfnr, @ldfnr+1) AS nr, tab.* ,
      @old_make := tab.make , @old_model := tab.model ,
      @old_submodel := tab.submodel , @old_notes := tab.notes ,
      @old_year := tab.`year`
    FROM TableName AS tab,
      (SELECT @ldfnr:=0, @old_model:='', @old_submodel:='', @old_notes:='', @old_year:='', @old_make:=''  ) AS tmp
    ORDER BY make, model, submodel, notes, `YEAR` ASC) AS mytab
  GROUP BY nr
  ORDER BY nr;

+------+-----------+-----------+-------+----------+------------------------------------------+
| sku  | YEAR      | make      | model | submodel | notes                                    |
+------+-----------+-----------+-------+----------+------------------------------------------+
|    1 | 1995-1997 | Chevrolet | Astro | Base     | Clear Lens; Chrome Housing; Pair; 1 pc.; |
|    1 | 2001      | Chevrolet | Astro | Base     | Clear Lens; Chrome Housing; Pair; 1 pc.; |
|    1 | 1995-1996 | Chevrolet | Astro | CL       | Clear Lens; Chrome Housing; Pair; 1 pc.; |
|    1 | 1995-1996 | Chevrolet | Astro | LS       | Clear Lens; Chrome Housing; Pair; 1 pc.; |
|    1 | 1997      | Chevrolet | Astro | LT       | Clear Lens; Chrome Housing; Pair; 1 pc.; |
+------+-----------+-----------+-------+----------+------------------------------------------+
5 rows in set (0.44 sec)

答案 1 :(得分:0)

您可能希望将STUFF函数与xml子查询一起使用。这将允许您选择同一行的信息并将其连接起来。我用它来进行类似的查询。

https://sqlwhisper.wordpress.com/2013/03/24/stuff-and-for-xml-path-for-string-concatenation/