简体   繁体   English

如何返回 SQL 中值大于 0 的行名和列名列表?

[英]How can I return a list of row names and column names where the value is greater than 0 in SQL?

I've put together a reconciliation tool in SQL Server which identifies the number of record breaks by field (col 2 - col 4) between two identical (data types/structure) sources.我在 SQL 服务器中组装了一个协调工具,它可以识别两个相同(数据类型/结构)源之间的字段(第 2 列 - 第 4 列)的记录中断数。 The output returned is in the format below, grouped on col 1.返回的 output 采用以下格式,按列 1 分组。

Col1  Col2  Col3  Col4
X     0     0     1
Y     0     1     1   
Z     1     0     1

I am trying to manipulate the output so that it provides a list of the Col 1 identifier and the name of any column names (col 2 - col 4) which have breaks (value > 0).我正在尝试操作 output 以便它提供 Col 1 标识符的列表和任何具有中断(值 > 0)的列名(col 2 - col 4)的名称。

The expected output based on the above data would look like this.基于上述数据的预期 output 将如下所示。

Col1  FieldBreak
X     Col2
Y     Col3
Y     Col4
Z     Col2
Z     Col4

I'm newer to SQL (6 months of professional experience) and am stuck.我对 SQL (6 个月的专业经验)较新,并且被卡住了。 Any help would be much appreciated!任何帮助将非常感激!

In any database, you can use:在任何数据库中,您都可以使用:

select col1, 'col2' as col
from t
where col2 = 1
union all
select col1, 'col3' as col
from t
where col3 = 1
union all
select col1, 'col4' as col
from t
where col4 = 1;

There are probably more efficient methods, but those depend on the database.可能有更有效的方法,但这些方法取决于数据库。 And for a small table efficiency may not be a concern.而对于小桌子来说,效率可能不是问题。

In SQL Server, you would unpivot using apply :在 SQL 服务器中,您将使用apply取消透视:

select t.col1, v.*
from t cross apply
     (values ('col2', t.col2), ('col3', t.col3) . . . 
     ) v(col, val)
where v.val is not null;

If you have a lot of columns, you can construct the expression using a SQL statement (from INFORMATION_SCHEMA.COLUMNS ) and/or using a spreadsheet.如果您有很多列,您可以使用 SQL 语句(来自INFORMATION_SCHEMA.COLUMNS )和/或使用电子表格来构造表达式。

暂无
暂无

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

相关问题 SQL中如果列中的值大于1,则多次返回一行数据 - Return a row of data mutiple times if a value in a column is greater than 1 in SQL SQL:返回列名,其中列包含给定值 - SQL: Return Column names where column contains a given Value SQL查找表中所有列的SUM大于零的所有列的名称 - SQL to find the names of all columns in a table where the SUM of the column is greater than zero 如果列的值大于0,则拆分Sql行 - Split Sql Row if Value of a Column is greater than 0 我如何 select 一个 SQL 数据集,其中第一行中的值是列名? - How do I select a SQL dataset where values in the first row are the column names? 我想列出所有薪水高于平均薪水的导师姓名,并显示薪水比 - I am trying to list all the tutor names whose salary is greater than the average salary and show how much the salary is greater than by 使用列名作为 SQL 中行的值 - Using column names as value for row in SQL 如何解决我的列名需要长于SQL允许的长度的问题? - How can I work around my column names needing to be longer than what's allowed in SQL? 如何包含列值大于使用内部连接的 SQL 查询中返回的值的行? - How can I include rows with a column value greater than what is returned in a SQL query that utilizes inner joins? 如何在开始日期和结束日期差大于14的表中选择某人的姓名 - how can I select the someone names in a table whose start date and finish date difference is greater than 14
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM