[英]alias column name by lookup query
I have a 'Table A' if I query that returns something like this: 如果我查询返回的内容如下,则有一个“表A”:
DPSF0010001 | 4973
DPSF0010002 | 139
DPSF0010003 | 266
DPSF0010004 | 437
DPSF0010005 | 391
DPSF0010006 | 146
DPSF0010007 | 100
DPSF0010008 | 78
DPSF0010009 | 141
DPSF0010010 | 253
DPSF0010011 | 425
DPSF0010012 | 491
DPSF0010013 | 501
DPSF0010014 | 477
DPSF0010015 | 382
...
.
.
The query has multiple columns DPSFxxxx. 该查询具有多个列DPSFxxxx。 The DPSFxxxxx can be referenced another table, where "DPSF0010001" can be decoded to a string: "Total: ". DPSFxxxxx可以引用另一个表,其中“ DPSF0010001”可以解码为字符串:“总计:”。 eg: 例如:
Total: | dpsf0010001
Under 5 years | dpsf0010002
5 to 9 years | dpsf0010003
10 to 14 years | dpsf0010004
15 to 19 years | dpsf0010005
20 to 24 years | dpsf0010006
25 to 29 years | dpsf0010007
30 to 34 years | dpsf0010008
35 to 39 years | dpsf0010009
40 to 44 years | dpsf0010010
45 to 49 years | dpsf0010011
50 to 54 years | dpsf0010012
55 to 59 years | dpsf0010013
60 to 64 years | dpsf0010014
65 to 69 years | dpsf0010015
...
.
.
I want to replace DPSF0010001
in my query w/ it's associated string in Table B. eg: 我想用表B中的关联字符串替换查询中的DPSF0010001
。例如:
BEFORE: 之前:
SELECT "DPSF0010001" from TableA;
DPSF0010001
-------------
4973
(1 row)
AFTER: 后:
SELECT "DPSF0010001" from TableA;
Total:
-------------
4973
(1 row)
How can I replace the column names in my query w/ the lookup value? 如何使用查询值替换查询中的列名? I know I can use AS to rename, how can I extend this to lookup the value in a different table? 我知道我可以使用AS重命名,如何扩展它以在另一个表中查找值?
It's not totally clear what you're asking, but it sounds like you need a pivot query . 目前尚不清楚您要问什么,但是听起来您需要一个透视查询 。
SQL doesn't allow data values to be used as column names or alias names dynamically in the same query that reads the data. SQL不允许在读取数据的同一查询中将数据值动态用作列名或别名。 The column names and alias names must be fixed before the query is parsed, which naturally comes before it reads any data from the tables. 在解析查询之前 ,列名和别名必须固定,这自然是在从表中读取任何数据之前进行的。 So there's no opportunity for data values to override, rename, or extend the set of columns. 因此,数据值没有机会覆盖,重命名或扩展列集。
Doing a pivot query therefore requires that you know the codes and their labels before you write the query. 因此,进行数据透视查询需要在编写查询之前了解代码及其标签。 You can query your decoding table, and from those results, it's pretty simple to generate a series of expressions like the following: 您可以查询您的解码表,然后从这些结果中生成一系列如下所示的表达式非常简单:
SELECT
MAX(CASE code WHEN 'dpsf0010001' THEN value END) AS 'Total',
MAX(CASE code WHEN 'dpsf0010002' THEN value END) AS 'Under 5 years',
MAX(CASE code WHEN 'dpsf0010003' THEN value END) AS '10 to 14 years'
. . .
FROM TableA;
How to solve this problem has to do with what you are doing with the result. 如何解决此问题与您对结果所做的事情有关。 If you have a front end with some ability program you can do a select like this (I'm assuming all column names are the same in both tables) 如果您的前端有一些能力计划,则可以进行这样的选择(我假设两个表中的所有列名都相同)
SELECT "Column Head" as RowType, * FROM TABLEA
UNION ALL
SELECT "Column Value" as RowType, * FROM TABLEB
This will give you something like this: 这会给你这样的东西:
RowType DPSF0010001 DPSF0010002 DPSF0010003 DPSF0010004 DPSF0010005 DPSF0010006 DPSF0010007 DPSF0010008 DPSF0010009 DPSF0010010 DPSF0010011 DPSF0010012 DPSF0010013 DPSF0010014 DPSF0010015
Column Head Total: Under 5 years 5 to 9 years 10 to 14 years 15 to 19 years 20 to 24 years 25 to 29 years 30 to 34 years 35 to 39 years 40 to 44 years 45 to 49 years 50 to 54 years 55 to 59 years 60 to 64 years 65 to 69 years
Column Value 4973 139 266 437 391 146 100 78 141 253 425 491 501 477 382
Which should be easy to display in whatever your front end is. 无论前端是什么,都应该易于显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.