简体   繁体   English

SQL DB2 - 如何根据名称选择或比较列?

[英]SQL DB2 - How to SELECT or compare columns based on their name?

Thank you for checking my question out!感谢您检查我的问题!

I'm trying to write a query for a very specific problem we're having at my workplace and I can't seem to get my head around it.我正在尝试为我们在工作场所遇到的一个非常具体的问题编写一个查询,但我似乎无法理解它。

Short version : I need to be able to target columns by their name, and more specifically by a part of their name that will be consistent throughout all the columns I need to combine or compare.简短版本:我需要能够通过名称来定位列,更具体地说,通过名称的一部分将在我需要组合或比较的所有列中保持一致。

More details : We have (for example), 5 different surveys.更多细节:我们有(例如)5 个不同的调查。 They have many questions each, but SOME of the questions are part of the same metric, and we need to create a generic field that keeps it.他们每个人都有很多问题,但有些问题是同一指标的一部分,我们需要创建一个通用字段来保留它。 There's more background to the "why" of that, but it's pretty important for us at this point.关于“为什么”的更多背景,但在这一点上对我们来说非常重要。

We were able to kind of solve this with either COALESCE() or CASE statements but the challenge is that, as more surveys/survey versions continue to grow, our vendor inevitably generates new columns for each survey and its questions.我们能够使用 COALESCE() 或 CASE 语句解决这个问题,但挑战在于,随着更多调查/调查版本的不断增长,我们的供应商不可避免地为每个调查及其问题生成新列。

Take this example, which is what we do currently and works well enough:以这个例子为例,这是我们目前所做的并且运行良好:

CASE
   WHEN SURVEY_NAME = 'Service1' THEN SERV1_REC
   WHEN SURVEY_NAME = 'Notice1' THEN FNOL1_REC
   WHEN SURVEY_NAME = 'Status1' THEN STAT1_REC
   WHEN SURVEY_NAME = 'Sales1' THEN SALE1_REC
   WHEN SURVEY_NAME = 'Transfer1' THEN Null
   ELSE Null
END REC

And also this alternative which works well:还有这个效果很好的替代方案:

COALESCE(SERV1_REC, FNOL1_REC, STAT1_REC, SALE1_REC) as REC

But as I mentioned, eventually we will have a "SALE2_REC" for example, and we'll need them BOTH on this same statement.但正如我所提到的,最终我们将有一个“SALE2_REC”,例如,我们将在同一个语句中同时需要它们。 I want to create something where having to come into the SQL and make changes isn't needed.我想创建一些不需要进入 SQL 并进行更改的东西。 Given that the columns will ALWAYS be named " something# _REC" for this specific metric, is there any way to achieve something like:鉴于此特定指标的列将始终命名为“ something# _REC”,是否有任何方法可以实现以下目标:

COALESCE(all columns named LIKE '%_REC') as REC

Bonus!奖金! Related, might be another way around this same problem: Would there also be a way to achieve this?相关,可能是解决同一问题的另一种方法:是否还有一种方法可以实现这一目标?

SELECT (columns named LIKE '%_REC') FROM ...

Thank you very much in advance for all your time and attention.非常感谢您的时间和关注。

-Kendall -肯德尔

Table and column information in Db2 are managed in the system catalog. Db2 中的表和列信息在系统目录中进行管理。 The relevant views are SYSCAT.TABLES and SYSCAT.COLUMNS .相关视图是SYSCAT.TABLESSYSCAT.COLUMNS You could write:你可以写:

select colname, tabname from syscat.tables
where colname like some_expression
and syscat.tabname='MYTABLE

Note that the LIKE predicate supports expressions based on a variable or the result of a scalar function.请注意, LIKE 谓词支持基于变量或标量函数结果的表达式。 So you could match it against some dynamic input.所以你可以将它与一些动态输入进行匹配。

Have you considered storing the more complicated properties in JSON or XML values?您是否考虑过将更复杂的属性存储在JSON或 XML 值中? Db2 supports both and you can query those values with regular SQL statements. Db2 支持两者,您可以使用常规 SQL 语句查询这些值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM