[英]retrieve columns with through sql
這是我的表的一個示例,沒有索引,沒有主鍵,沒有任何內容,幾乎有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
我想知道,哪一列只有。 自2014年4月起。
已編輯
我的預期結果是,在3列示例中,請參見上文,自2014年4月以來,第2列僅包含點
編輯結束
我當時正在考慮換位,但這似乎是沒有意義的解決方案。
自昨天以來,我一直在解決該問題,但我沒有解決方案。 啊!
謝謝。
編輯 :我的查詢將是以下
select * from mytable where date < (input('may14',MONYY5.));
但是,它並沒有提供我期望的結果,因為它正在從所有列中檢索所有行。 我本來打算換桌子,但這樣做並沒有帶我去任何地方。
編輯結束
我認為用6000列在SQL中沒有什么可以做的。
我將整個內容下載到一個CSV文件(您有多少行?),並使用遍歷該文件的編程語言編寫一些處理代碼。
從外觀上看,它可以是一次通過(請確保該文件在該日期列之前排序)。
假設我只有3欄。 會更簡單嗎?
三列,也許
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';
順便說一句,日期真的看起來像嗎? 如果無法以有意義的方式對它們進行排序,那么您將面臨另一個大問題。
這是一種動態方法,我假設您的日期列為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
如果我誤解了您的需求,請糾正我!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.