[英]Select multiple rows into one row from one table
I have a table with related data across multiple rows that I need to query as one row. 我有一张表,其中包含跨多行的相关数据,我需要将其查询为一行。
string_value | def_id | location | model | asset_num | exp_date |
-------------+--------+----------+-------+-----------+------------+
null | 16 | A | CR35 | 1 | 2015-02-01 |
SWIT: C | 25 | A | CR35 | 1 | null |
null | 16 | B | CR85 | 2 | 2015-07-28 |
SWIT: D | 25 | B | CR85 | 2 | null |
What I am looking to end up with is a query that gives me results: 我希望最终得到的查询可以给我结果:
string_value | location | model | asset_num | exp_date |
-------------+----------+-------+-----------+------------+
SWIT: C | A | CR35 | 1 | 2015-02-01 |
SWIT: D | B | CR85 | 2 | 2015-07-28 |
You can Try below - using aggregation and group by 您可以在下面尝试-使用汇总和分组依据
select location, model, asset_num,max(string_value),max(exp_date)
from tablename
group by location, model, asset_num
Using aggregate function MAX()
with GROUP BY
return your expected result: 将聚合函数MAX()
与GROUP BY
返回预期结果:
SELECT MAX(string_value) AS string_value ,
location,
MAX(model) AS model,
MAX(asset_num) AS asset_num,
MAX(exp_date) AS exp_date
FROM TableName
GROUP BY location
I am guessing that the triple location, model, asset_num defines the row in the result set. 我猜三重位置,模型,asset_num定义了结果集中的行。 If so, use aggregation: 如果是这样,请使用聚合:
select location, model, asset_num,
max(string_value) as string_value,
max(exp_date) as exp_date
from t
group by location, model, asset_num;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.