如何根据列值创建视图

发布于 2025-01-19 16:53:25 字数 774 浏览 0 评论 0原文

我有一个表:

+------+----------+----------+-------+
| id   | location | variable | value |
+------+----------+----------+-------+
| 1    | loc1     | outside  | 30.6  |
| 2    | loc1     | inside   | 22.3  |
| 3    | loc2     | outside  | 31.1  |
| 4    | loc2     | inside   | 22.2  |
| 5    | loc3     | outside  | 34.7  |
| 6    | loc3     | inside   | 21.9  |
+------+----------+----------+-------+

是否可以创建以这种方式显示的视图:

+----------+----------+-------+
| location | outside  | inside|
+----------+----------+-------+
| loc1     | 30.6     | 22.3  |
| loc2     | 31.1     | 22.2  |
| loc3     | 34.7     | 21.9  |
+----------+----------+-------+

我尝试为每个列“位置”和“位置”和“位置”获得不同的值,但不知道如何创建一个这些值的列正确填充它们。

I have a table:

+------+----------+----------+-------+
| id   | location | variable | value |
+------+----------+----------+-------+
| 1    | loc1     | outside  | 30.6  |
| 2    | loc1     | inside   | 22.3  |
| 3    | loc2     | outside  | 31.1  |
| 4    | loc2     | inside   | 22.2  |
| 5    | loc3     | outside  | 34.7  |
| 6    | loc3     | inside   | 21.9  |
+------+----------+----------+-------+

Is it possible to create view that shows this in this way:

+----------+----------+-------+
| location | outside  | inside|
+----------+----------+-------+
| loc1     | 30.6     | 22.3  |
| loc2     | 31.1     | 22.2  |
| loc3     | 34.7     | 21.9  |
+----------+----------+-------+

I tried to Get DISTINCT values for each column "location" and "location" but don't know how to create a columns with these values populate them correctly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏末染殇 2025-01-26 16:53:25

您可以使用条件聚合(取决于当前模型代表整个数据集)

SELECT location, 
       MAX(CASE WHEN variable='outside' THEN value END) AS outside,
       MAX(CASE WHEN variable='inside' THEN value END) AS inside
  FROM [tab]
 GROUP BY location 

,也可以使用max sum> sum ,取决于每个位置< /代码>和变量组合。

You can use conditional aggregation (depending on the current model represents the whole dataset) like

SELECT location, 
       MAX(CASE WHEN variable='outside' THEN value END) AS outside,
       MAX(CASE WHEN variable='inside' THEN value END) AS inside
  FROM [tab]
 GROUP BY location 

or replace presumably MAX aggregation by SUM depending whether multiple rows exist for each location and variable combination.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文