简体   繁体   English

通过SQL检索列

[英]retrieve columns with through sql

Here is a sample of my table , no index , no primary key , no nothing and with almost 6000 columns. 这是我的表的一个示例,没有索引,没有主键,没有任何内容,几乎有6000列。

date     col1 col2 col3 ... col6000
jun14     0    .    0       0
may14     1    .    2       2
apr14     1    .    3       1
mar14     1    3    3       0
feb14     1    3    3       5
jan14     1    0    3       8
dec13     1    0    3       1

I want to know , which column has only . 我想知道,哪一列只有 since apr 2014. 自2014年4月起。

Edited 已编辑

My expected results would be to know, in the example of 3 columns , see above, that column 2 has only dots since april 2014 我的预期结果是,在3列示例中,请参见上文,自2014年4月以来,第2列仅包含点

End of Edited 编辑结束

I was thinking of transpose my table but it seems to be a pointless solution. 我当时正在考虑换位,但这似乎是没有意义的解决方案。

I'm stuck on that issue since yesterday and I don't feel I have a solution. 自昨天以来,我一直在解决该问题,但我没有解决方案。 Argh! 啊!

Thanks. 谢谢。

Edited : My query would be the following 编辑 :我的查询将是以下

select * from mytable where date < (input('may14',MONYY5.));

However, it does not give me what I'm expecting as it is retrieving all the rows from all the columns. 但是,它并没有提供我期望的结果,因为它正在从所有列中检索所有行。 I thought about transposing my table but having done that, it does not take me anywhere. 我本来打算换桌子,但这样做并没有带我去任何地方。

End of Edited 编辑结束

I don't think there is much you can do in SQL with 6000 columns. 我认为用6000列在SQL中没有什么可以做的。

I'd download the whole thing to a CSV file (how many rows do you have?) and write some processing code in a programming language that goes through the file. 我将整个内容下载到一个CSV文件(您有多少行?),并使用遍历该文件的编程语言编写一些处理代码。

From the looks of it, it can be a single-pass (make sure you have the file ordered by that date column). 从外观上看,它可以是一次通过(请确保该文件在该日期列之前排序)。

let's say I have only 3 columns. 假设我只有3栏。 Would it be simpler? 会更简单吗?

With three columns, maybe 三列,也许

SELECT max(date) FROM the_table WHERE col1 <> '.' HAVING max(date) < 'may14';
SELECT max(date) FROM the_table WHERE col2 <> '.' HAVING max(date) < 'may14';
SELECT max(date) FROM the_table WHERE col3 <> '.' HAVING max(date) < 'may14';

By the way, do the dates really look like that? 顺便说一句,日期真的看起来像吗? If they cannot be sorted in a meaningful way, you have another big problem. 如果无法以有意义的方式对它们进行排序,那么您将面临另一个大问题。

Here is a dynamic approach, and i assume your date column is in DateTime format, the result is a list of all columns which has only dots from a specific time : 这是一种动态方法,我假设您的日期列为DateTime格式,结果是所有列的列表,其中仅包含特定时间的点:

if object_id('test') is not null drop table test

create table test
(
[date] datetime, 
col1 nvarchar(1), 
col2 nvarchar(1), 
col3 nvarchar(1), 
col4 nvarchar(1), 
col5 nvarchar(1), 
col6 nvarchar(1), 
col7 nvarchar(1)
)

declare @i int, @cols int, @cmd nvarchar(max)

insert into test values('2014-01-01', 1, 2, 3, 4, 5 ,6 ,7)
insert into test values('2014-02-01', 1, 2, 3, 4, 5 ,6 ,7)
insert into test values('2014-03-01', 1, 2, 3, 4, 5 ,6 ,7)
insert into test values('2014-04-01', 1, 2, 3, 4, 5 ,6 ,7)
insert into test values('2014-05-01', 1, '.', '.', 4, 5 ,6 ,7)
insert into test values('2014-06-01', 1, '.', '.', 4, 5 ,6 ,7)
insert into test values('2014-07-01', 1, '.', '.', 4, 5 ,6 ,7)
insert into test values('2014-08-01', 1, '.', 5, 4, 5 ,6 ,7)


if object_id('tempdb..#chk') is not null drop table #chk 
create table #chk
(
    checkValue int
)

declare @result table
(
    ColList nvarchar(64) 
)

declare @dateFrom datetime, @test cursor, @colName nvarchar(64), @returnValue int 

set @dateFrom = '2014-05-01'

set @test = cursor for 
                select name from syscolumns 
                where id = object_id('test') 
                and name <> 'date' 
                order by colorder 

open @test
fetch next from @test into @colName
while @@fetch_status = 0 
begin
    truncate table #chk
    set @cmd = 'select top 1 1 from test where date >= ''' + convert(varchar(32), @dateFrom) + ''' and ' + @colName + ' <> ''.'''

    insert into #chk exec( @cmd)

    if @@rowcount = 0
        insert into @result values(@colName) 

    fetch next from @test into @colName 
end

select * from @result

Please correct me if i misunderstood your need! 如果我误解了您的需求,请纠正我!

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

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