[英]break out each unique value of a column into multiple columns then display in single row
In my actual data I have the following query在我的实际数据中,我有以下查询
SELECT Sales Person,
(CASE WHEN [ProductType] = 'car' THEN [GOAL_VOLUME]
END) AS 'car Goal'
, (CASE WHEN [ProductType] = 'boat' THEN [GOAL_VOLUME]
END) AS 'boat Goal'
, (CASE WHEN [ProductType] IN ('bike', 'scooter') THEN [GOAL_VOLUME]
END) AS 'bike/scooter Goal'
FROM PRODUCT_GOALS
My current results are each Sales Person's name broken out 3 times into 3 rows with each row having only one non-NULL value for the Goal_Volume... I need everything on one row... Might need multiple subqueries to join against for each product?我目前的结果是每个销售人员的名字被分成 3 行,每行只有一个非空值的 Goal_Volume ......我需要一行中的所有内容......可能需要多个子查询来加入每个产品? See image below.
见下图。
enter image description here在此处输入图片说明
Thanks for the help谢谢您的帮助
You can use conditional aggregation:您可以使用条件聚合:
SELECT [Sales Person]
, MAX(CASE WHEN [ProductType] = 'car' THEN [GOAL_VOLUME]
END) AS 'car Goal'
, MAX(CASE WHEN [ProductType] = 'boat' THEN [GOAL_VOLUME]
END) AS 'boat Goal'
, MAX(CASE WHEN [ProductType] IN ('bike', 'scooter') THEN [GOAL_VOLUME]
END) AS 'bike/scooter Goal'
FROM PRODUCT_GOALS
GROUP BY [Sales Person]
Almost the same query as yours, just added MAX
and GROUP BY
.与您的查询几乎相同,只是添加了
MAX
和GROUP BY
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.