基于日期的GridView数据显示 [英] GridView Data Display On The Basis Of Date
问题描述
我有一张表格Ex(ID,一,二,三,四,五,六,七,八,九,十),值的格式如下。 。
I have a table Ex (ID,one,Two,three,Four,five,six,seven,eight,nine,ten) and values are in following format . .
ID one Two three Four five six seven eight nine ten
1 10 11 12 13 14 15 16 17 18 19
2 21 21 22 23 24 25 26 27 28 29
3 13 14 12 13 54 55 46 13 18 19
4 10 11 12 13 14 15 16 17 18 19
5 10 14 124 113 114 115 116 117 118 119
此处列表示月份,Id表示日期。所以我想要一个查询,其中只有三个月的数据将根据月份显示。假设我们有4月1月,2月,3月的记录(它将通过显示当前日期的标签进行比较或获取)Jan Data自动删除。现在记录只有2月,3月,4月的数据(当前)请帮助我如何实现这一点。
Here The Column Represents The Months and Id represents The Date. so I want a query where only three month data will be displayed on the basis of the month. suppose we have records of Jan,Feb,march now as soon as April comes (It will be compared or fetched through label displaying Current Date) Jan Data Automatically be removed. now the record will have only Feb,March,April data (current) To Kindly Help me How To achieve this.
推荐答案
是的,你可以这样做。虽然有点奇怪。看看。
Yes, you can do this. Little weird though. Have a look.
--Get current month
DECLARE @MONTH INT
SELECT @MONTH=DATEPART(MONTH,GETDATE())
--Get first column/month name
DECLARE @MONTH1 VARCHAR(10)
SELECT @MONTH1 = COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'MYTABLE'
AND ORDINAL_POSITION = @MONTH+1 --added 1 to exclude column 'ID'
--get second column/month name
DECLARE @MONTH2 VARCHAR(10)
IF(@MONTH<12) SET @MONTH=@MONTH+1 ELSE SET @MONTH=1
SELECT @MONTH2 = COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'MYTABLE'
AND ORDINAL_POSITION = @MONTH+1
--get third column/month name
DECLARE @MONTH3 VARCHAR(10)
IF(@MONTH<12) SET @MONTH=@MONTH+1 ELSE SET @MONTH=1
SELECT @MONTH3 = COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'MYTABLE'
AND ORDINAL_POSITION = @MONTH+1
DECLARE @SQL AS NVARCHAR(1000)
SET @SQL = 'SELECT ' + @MONTH1 +',' + @MONTH2 +','+@MONTH3 +' FROM MYTABLE'
EXEC(@SQL)
我在当地试过这个,应该做你的工作。如果这不起作用或您希望我解释任何事情,请告诉我:))
I have tried this in my local and should do your job. In case this doesn't work or you want me to explain anything, please let me know :)
这篇关于基于日期的GridView数据显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!