delphi 按天统计
最近在学习delphi和SQL,目前发现一个难题,就是按天统计数据库中的东西。数据库如下名称日期A20090101B20090103C20090104…………可是我想要的...
最近在学习delphi和SQL,目前发现一个难题,就是按天统计数据库中的东西。
数据库如下
名称 日期
A 20090101
B 20090103
C 20090104
…… ……
可是我想要的结果是
名称 日期
A 20090101
(空) 20090102
B 20090103
C 20090104
…… ……
(空) 20090130
(空) 20090131
这样子要怎么实现啊? 展开
数据库如下
名称 日期
A 20090101
B 20090103
C 20090104
…… ……
可是我想要的结果是
名称 日期
A 20090101
(空) 20090102
B 20090103
C 20090104
…… ……
(空) 20090130
(空) 20090131
这样子要怎么实现啊? 展开
2个回答
展开全部
如果你用的是SQLServer,那你有答案了。(别嫌长,答案其实很短)
1.先给你两个函数,蛮有用的东西,你就当收藏吧。
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_getdate]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_getdate]
GO
/*--生成日期列表
生成指定年份的工作日/休息日列表
--邹建 2003.12(引用请保留此信息)--*/
/*--调用示例
--查询 2003 年的工作日列表
SELECT * FROM dbo.f_getdate(2003,0)
--查询 2003 年的休息日列表
SELECT * FROM dbo.f_getdate(2003,1)
--查询 2003 年全部日期列表
SELECT * FROM dbo.f_getdate(2003,NULL)
--*/
CREATE FUNCTION dbo.f_getdate(
@year int, --要查询的年份
@bz bit --@bz=0 查询工作日,@bz=1 查询休息日,@bz IS NULL 查询全部日期
)RETURNS @re TABLE(id int identity(1,1),Date datetime,Weekday nvarchar(3))
AS
BEGIN
DECLARE @tb TABLE(ID int IDENTITY(0,1),Date datetime)
INSERT INTO @tb(Date) SELECT TOP 366 DATEADD(Year,@YEAR-1900,'1900-1-1')
FROM sysobjects a ,sysobjects b
UPDATE @tb SET Date=DATEADD(DAY,id,Date)
DELETE FROM @tb WHERE Date>DATEADD(Year,@YEAR-1900,'1900-12-31')
IF @bz=0
INSERT INTO @re(Date,Weekday)
SELECT Date,DATENAME(Weekday,Date)
FROM @tb
WHERE (DATEPART(Weekday,Date)+@@DATEFIRST-1)%7 BETWEEN 1 AND 5
ELSE IF @bz=1
INSERT INTO @re(Date,Weekday)
SELECT Date,DATENAME(Weekday,Date)
FROM @tb
WHERE (DATEPART(Weekday,Date)+@@DATEFIRST-1)%7 IN (0,6)
ELSE
INSERT INTO @re(Date,Weekday)
SELECT Date,DATENAME(Weekday,Date)
FROM @tb
RETURN
END
GO
/*====================================================================*/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_getdate]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_getdate]
GO
/*--生成列表
生成指定日期段的日期列表
--邹建 2005.03(引用请保留此信息)--*/
/*--调用示例
--查询工作日
SELECT * FROM dbo.f_getdate('2005-1-3','2005-4-5',0)
--查询休息日
SELECT * FROM dbo.f_getdate('2005-1-3','2005-4-5',1)
--查询全部日期
SELECT * FROM dbo.f_getdate('2005-1-3','2005-4-5',NULL)
--*/
CREATE FUNCTION dbo.f_getdate(
@begin_date Datetime, --要查询的开始日期
@end_date Datetime, --要查询的结束日期
@bz bit --@bz=0 查询工作日,@bz=1 查询休息日,@bz IS NULL 查询全部日期
)RETURNS @re TABLE(id int identity(1,1),Date datetime,Weekday nvarchar(3))
AS
BEGIN
DECLARE @tb TABLE(ID int IDENTITY(0,1),a bit)
INSERT INTO @tb(a) SELECT TOP 366 0
FROM sysobjects a ,sysobjects b
IF @bz=0
WHILE @begin_date<=@end_date
BEGIN
INSERT INTO @re(Date,Weekday)
SELECT Date,DATENAME(Weekday,Date)
FROM(
SELECT Date=DATEADD(Day,ID,@begin_date)
FROM @tb
)a WHERE Date<=@end_date
AND (DATEPART(Weekday,Date)+@@DATEFIRST-1)%7 BETWEEN 1 AND 5
SET @begin_date=DATEADD(Day,366,@begin_date)
END
ELSE IF @bz=1
WHILE @begin_date<=@end_date
BEGIN
INSERT INTO @re(Date,Weekday)
SELECT Date,DATENAME(Weekday,Date)
FROM(
SELECT Date=DATEADD(Day,ID,@begin_date)
FROM @tb
)a WHERE Date<=@end_date
AND (DATEPART(Weekday,Date)+@@DATEFIRST-1)%7 in(0,6)
SET @begin_date=DATEADD(Day,366,@begin_date)
END
ELSE
WHILE @begin_date<=@end_date
BEGIN
INSERT INTO @re(Date,Weekday)
SELECT Date,DATENAME(Weekday,Date)
FROM(
SELECT Date=DATEADD(Day,ID,@begin_date)
FROM @tb
)a WHERE Date<=@end_date
SET @begin_date=DATEADD(Day,366,@begin_date)
END
RETURN
END
GO
Create function FormatDateTime(@Date datetime,@formatStr varchar(20))
returns varchar(16)
as
begin
declare @tempstr varchar(20),@index int,@retStr varchar(20),@formatLen int,@str1 varchar(6),@str2 varchar(6),@str3 varchar(6),@j int
declare @tempformat varchar(20)
select @tempformat=@formatStr,@formatStr = Upper(@formatStr),@index=-1,@retstr=''
if @formatStr='MM/DD/YYYY'
set @retstr= convert(varchar(10),@date,101)
else if @formatstr='YYYY-MM-DD'
set @retstr = Convert(char(10),@Date,20)
else if @formatStr='YYYY.MM.DD'
set @retstr= Convert(varchar(10),@Date,102)
else if @formatStr='YYYY/MM/DD'
set @retstr= Convert(varchar(10),@Date,111)
else if @formatStr='DD/MM/YYYY'
set @retstr= Convert(varchar(10),@Date,103)
else if @formatStr='DD.MM.YYYY'
set @retstr= Convert(varchar(10),@Date,104)
else if @formatStr='DD-MM-YYYY'
set @retstr= Convert(varchar(10),@Date,105)
else if @formatStr='YYYYMMDD'
set @retstr= Convert(varchar(10),@Date,112)
else
begin
select @tempformat=@formatStr,@formatLen = len(@formatStr)
if @formatLen>8
begin
set @index=charindex('M',@tempformat)
select @str1=right(left(@tempformat,@index-1),@index-5),@str2=right(@tempformat,@formatLen-@index-1)
select @index=charindex('D',@str2),@str3=@str2
set @str2=left(@str2,@index-1)
set @str3=right(@str3,len(@str3)-@index-1)
end
select @tempstr = Convert(char(10),@Date,20),@str1=isnull(@str1,''),@str2=isnull(@str2,''),@str3=isnull(@str3,''),@j=0
while @index <> 0
begin
set @index = charindex('-',@tempstr)
if @j=0
select @retstr=left(@tempstr,@index-1)+@str1,@j=@j+1
else set @retstr=@retstr+left(@tempstr,@index-1)+@str2
select @tempstr=right(@tempstr,len(@tempstr)-@index)
set @index= charindex('-',@tempstr)
end
set @retstr=@retstr+@tempstr+@str3
end
return @retstr
end
---把上面的代码在查询分析器里执行一下。
2.然后执行下面的语句,就是你需要的。
Select isNull(B.名称,'(空)') as 名称, A.UseDate as 日期 from
(select dbo.FormatDatetime([Date],'YYYYMMDD') as UseDate from f_getdate('2009-01-01','2009-01-31',null))A
left join
(Select 名称,日期 from YourTable)B
on
A.UseDate = B.日期
1.先给你两个函数,蛮有用的东西,你就当收藏吧。
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_getdate]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_getdate]
GO
/*--生成日期列表
生成指定年份的工作日/休息日列表
--邹建 2003.12(引用请保留此信息)--*/
/*--调用示例
--查询 2003 年的工作日列表
SELECT * FROM dbo.f_getdate(2003,0)
--查询 2003 年的休息日列表
SELECT * FROM dbo.f_getdate(2003,1)
--查询 2003 年全部日期列表
SELECT * FROM dbo.f_getdate(2003,NULL)
--*/
CREATE FUNCTION dbo.f_getdate(
@year int, --要查询的年份
@bz bit --@bz=0 查询工作日,@bz=1 查询休息日,@bz IS NULL 查询全部日期
)RETURNS @re TABLE(id int identity(1,1),Date datetime,Weekday nvarchar(3))
AS
BEGIN
DECLARE @tb TABLE(ID int IDENTITY(0,1),Date datetime)
INSERT INTO @tb(Date) SELECT TOP 366 DATEADD(Year,@YEAR-1900,'1900-1-1')
FROM sysobjects a ,sysobjects b
UPDATE @tb SET Date=DATEADD(DAY,id,Date)
DELETE FROM @tb WHERE Date>DATEADD(Year,@YEAR-1900,'1900-12-31')
IF @bz=0
INSERT INTO @re(Date,Weekday)
SELECT Date,DATENAME(Weekday,Date)
FROM @tb
WHERE (DATEPART(Weekday,Date)+@@DATEFIRST-1)%7 BETWEEN 1 AND 5
ELSE IF @bz=1
INSERT INTO @re(Date,Weekday)
SELECT Date,DATENAME(Weekday,Date)
FROM @tb
WHERE (DATEPART(Weekday,Date)+@@DATEFIRST-1)%7 IN (0,6)
ELSE
INSERT INTO @re(Date,Weekday)
SELECT Date,DATENAME(Weekday,Date)
FROM @tb
RETURN
END
GO
/*====================================================================*/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_getdate]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_getdate]
GO
/*--生成列表
生成指定日期段的日期列表
--邹建 2005.03(引用请保留此信息)--*/
/*--调用示例
--查询工作日
SELECT * FROM dbo.f_getdate('2005-1-3','2005-4-5',0)
--查询休息日
SELECT * FROM dbo.f_getdate('2005-1-3','2005-4-5',1)
--查询全部日期
SELECT * FROM dbo.f_getdate('2005-1-3','2005-4-5',NULL)
--*/
CREATE FUNCTION dbo.f_getdate(
@begin_date Datetime, --要查询的开始日期
@end_date Datetime, --要查询的结束日期
@bz bit --@bz=0 查询工作日,@bz=1 查询休息日,@bz IS NULL 查询全部日期
)RETURNS @re TABLE(id int identity(1,1),Date datetime,Weekday nvarchar(3))
AS
BEGIN
DECLARE @tb TABLE(ID int IDENTITY(0,1),a bit)
INSERT INTO @tb(a) SELECT TOP 366 0
FROM sysobjects a ,sysobjects b
IF @bz=0
WHILE @begin_date<=@end_date
BEGIN
INSERT INTO @re(Date,Weekday)
SELECT Date,DATENAME(Weekday,Date)
FROM(
SELECT Date=DATEADD(Day,ID,@begin_date)
FROM @tb
)a WHERE Date<=@end_date
AND (DATEPART(Weekday,Date)+@@DATEFIRST-1)%7 BETWEEN 1 AND 5
SET @begin_date=DATEADD(Day,366,@begin_date)
END
ELSE IF @bz=1
WHILE @begin_date<=@end_date
BEGIN
INSERT INTO @re(Date,Weekday)
SELECT Date,DATENAME(Weekday,Date)
FROM(
SELECT Date=DATEADD(Day,ID,@begin_date)
FROM @tb
)a WHERE Date<=@end_date
AND (DATEPART(Weekday,Date)+@@DATEFIRST-1)%7 in(0,6)
SET @begin_date=DATEADD(Day,366,@begin_date)
END
ELSE
WHILE @begin_date<=@end_date
BEGIN
INSERT INTO @re(Date,Weekday)
SELECT Date,DATENAME(Weekday,Date)
FROM(
SELECT Date=DATEADD(Day,ID,@begin_date)
FROM @tb
)a WHERE Date<=@end_date
SET @begin_date=DATEADD(Day,366,@begin_date)
END
RETURN
END
GO
Create function FormatDateTime(@Date datetime,@formatStr varchar(20))
returns varchar(16)
as
begin
declare @tempstr varchar(20),@index int,@retStr varchar(20),@formatLen int,@str1 varchar(6),@str2 varchar(6),@str3 varchar(6),@j int
declare @tempformat varchar(20)
select @tempformat=@formatStr,@formatStr = Upper(@formatStr),@index=-1,@retstr=''
if @formatStr='MM/DD/YYYY'
set @retstr= convert(varchar(10),@date,101)
else if @formatstr='YYYY-MM-DD'
set @retstr = Convert(char(10),@Date,20)
else if @formatStr='YYYY.MM.DD'
set @retstr= Convert(varchar(10),@Date,102)
else if @formatStr='YYYY/MM/DD'
set @retstr= Convert(varchar(10),@Date,111)
else if @formatStr='DD/MM/YYYY'
set @retstr= Convert(varchar(10),@Date,103)
else if @formatStr='DD.MM.YYYY'
set @retstr= Convert(varchar(10),@Date,104)
else if @formatStr='DD-MM-YYYY'
set @retstr= Convert(varchar(10),@Date,105)
else if @formatStr='YYYYMMDD'
set @retstr= Convert(varchar(10),@Date,112)
else
begin
select @tempformat=@formatStr,@formatLen = len(@formatStr)
if @formatLen>8
begin
set @index=charindex('M',@tempformat)
select @str1=right(left(@tempformat,@index-1),@index-5),@str2=right(@tempformat,@formatLen-@index-1)
select @index=charindex('D',@str2),@str3=@str2
set @str2=left(@str2,@index-1)
set @str3=right(@str3,len(@str3)-@index-1)
end
select @tempstr = Convert(char(10),@Date,20),@str1=isnull(@str1,''),@str2=isnull(@str2,''),@str3=isnull(@str3,''),@j=0
while @index <> 0
begin
set @index = charindex('-',@tempstr)
if @j=0
select @retstr=left(@tempstr,@index-1)+@str1,@j=@j+1
else set @retstr=@retstr+left(@tempstr,@index-1)+@str2
select @tempstr=right(@tempstr,len(@tempstr)-@index)
set @index= charindex('-',@tempstr)
end
set @retstr=@retstr+@tempstr+@str3
end
return @retstr
end
---把上面的代码在查询分析器里执行一下。
2.然后执行下面的语句,就是你需要的。
Select isNull(B.名称,'(空)') as 名称, A.UseDate as 日期 from
(select dbo.FormatDatetime([Date],'YYYYMMDD') as UseDate from f_getdate('2009-01-01','2009-01-31',null))A
left join
(Select 名称,日期 from YourTable)B
on
A.UseDate = B.日期
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询