SQL 查询函数
SQL query with function
我有 table 数据,我正在使用计数语句获取提交日期的记录数量
例子
AuditId Date Crew Shift Cast ObservedBy 2ndObserver AuditType Product
16 2017-06-27 3 Day B1974, B1975 Glen Mason NULL Identification Billet
20 2017-06-29 1 Day 9879 Corey Lundy NULL Identification Billet
21 2017-06-29 4 Day T9627, T9625 Joshua Dwyer NULL ShippingPad Tee
22 2017-06-29 4 Day NULL Joshua Dwyer NULL Identification Billet
23 2017-06-29 4 Day S9874 Joshua Dwyer NULL ShippingPad Slab
24 2017-06-29 4 Day Bay 40 Joshua Dwyer NULL Identification Billet
基本上我使用下面的代码来得到我的结果
SELECT YEAR([Date]) as YEAR, CAST([Date] as nvarchar(25)) AS [Date], COUNT(*) as "Audit Count"
FROM AuditResults
where AuditType = 'Identification' AND Product = 'Billet'
group by Date
这个return的例子
YEAR Date Audit Count
2017 2017-06-27 1
2017 2017-06-29 3
现在我希望能够检索所有日期,即使日期为空
所以我希望 return 成为
YEAR Date Audit Count
2017 2017-06-27 1
2017 2017-06-28 0
2017 2017-06-29 3
我正在尝试使用以下功能:
ALTER FUNCTION [dbo].[fnGetDatesInRange]
(
@FromDate datetime,
@ToDate datetime
)
RETURNS @DateList TABLE (Dt date)
AS
BEGIN
DECLARE @TotalDays int, @DaysCount int
SET @TotalDays = DATEDIFF(dd,@FromDate,@ToDate)
SET @DaysCount = 0
WHILE @TotalDays >= @DaysCount
BEGIN
INSERT INTO @DateList
SELECT (@ToDate - @DaysCount) AS DAT
SET @DaysCount = @DaysCount + 1
END
RETURN
END
如何将我的 select 语句与此函数一起使用?或者,还有更好的方法?
干杯
您必须创建另一个 table calendar
作为 (Mysql)- 所有 RDBMS 的想法都是相同的-
CREATE TABLE `calendar` (
`dt` DATE NOT NULL,
UNIQUE INDEX `calendar_dt_unique` (`dt`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
;
并填写日期数据。
试试这个;
ALTER FUNCTION [dbo].[fnGetDatesInRange]
(
@FromDate datetime,
@ToDate datetime
)
RETURNS @YourData TABLE ([Year] int, DateText nvarchar(25),[Audit Count] int)
AS
begin
insert into @YourData
SELECT
YEAR(allDates.[Date]) as YEAR,
CAST(allDates.[Date] as nvarchar(25)) AS [Date],
COUNT(r.Product) as "Audit Count"
from
(
SELECT
[date]=convert(datetime, CONVERT(float,d.Seq))
FROM
(
select top 100000 row_number() over(partition by 1 order by A.name) as Seq
from syscolumns A, syscolumns B
)d
)allDates
left join
AuditResults r on r.[Date]=allDates.[date] and r.AuditType = 'Identification' AND r.Product = 'Billet'
where
allDates.[Date]>=@FromDate and allDates.[Date]<=@ToDate
group by
allDates.[Date]
return
end
关键是 'allDates' 部分;
SELECT
[date]=convert(datetime, CONVERT(float,d.Seq))
FROM
(
select top 100000 row_number() over(partition by 1 order by A.name) as Seq
from syscolumns A, syscolumns B
)d
这将 return 1900 到 2173 之间的所有日期(在此示例中)。根据需要限制它,但这是一个不错的选择。有很多不同的方法可以清楚地解决这个问题
我有 table 数据,我正在使用计数语句获取提交日期的记录数量 例子
AuditId Date Crew Shift Cast ObservedBy 2ndObserver AuditType Product
16 2017-06-27 3 Day B1974, B1975 Glen Mason NULL Identification Billet
20 2017-06-29 1 Day 9879 Corey Lundy NULL Identification Billet
21 2017-06-29 4 Day T9627, T9625 Joshua Dwyer NULL ShippingPad Tee
22 2017-06-29 4 Day NULL Joshua Dwyer NULL Identification Billet
23 2017-06-29 4 Day S9874 Joshua Dwyer NULL ShippingPad Slab
24 2017-06-29 4 Day Bay 40 Joshua Dwyer NULL Identification Billet
基本上我使用下面的代码来得到我的结果
SELECT YEAR([Date]) as YEAR, CAST([Date] as nvarchar(25)) AS [Date], COUNT(*) as "Audit Count"
FROM AuditResults
where AuditType = 'Identification' AND Product = 'Billet'
group by Date
这个return的例子
YEAR Date Audit Count
2017 2017-06-27 1
2017 2017-06-29 3
现在我希望能够检索所有日期,即使日期为空 所以我希望 return 成为
YEAR Date Audit Count
2017 2017-06-27 1
2017 2017-06-28 0
2017 2017-06-29 3
我正在尝试使用以下功能:
ALTER FUNCTION [dbo].[fnGetDatesInRange]
(
@FromDate datetime,
@ToDate datetime
)
RETURNS @DateList TABLE (Dt date)
AS
BEGIN
DECLARE @TotalDays int, @DaysCount int
SET @TotalDays = DATEDIFF(dd,@FromDate,@ToDate)
SET @DaysCount = 0
WHILE @TotalDays >= @DaysCount
BEGIN
INSERT INTO @DateList
SELECT (@ToDate - @DaysCount) AS DAT
SET @DaysCount = @DaysCount + 1
END
RETURN
END
如何将我的 select 语句与此函数一起使用?或者,还有更好的方法? 干杯
您必须创建另一个 table calendar
作为 (Mysql)- 所有 RDBMS 的想法都是相同的-
CREATE TABLE `calendar` (
`dt` DATE NOT NULL,
UNIQUE INDEX `calendar_dt_unique` (`dt`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
;
并填写日期数据。
试试这个;
ALTER FUNCTION [dbo].[fnGetDatesInRange]
(
@FromDate datetime,
@ToDate datetime
)
RETURNS @YourData TABLE ([Year] int, DateText nvarchar(25),[Audit Count] int)
AS
begin
insert into @YourData
SELECT
YEAR(allDates.[Date]) as YEAR,
CAST(allDates.[Date] as nvarchar(25)) AS [Date],
COUNT(r.Product) as "Audit Count"
from
(
SELECT
[date]=convert(datetime, CONVERT(float,d.Seq))
FROM
(
select top 100000 row_number() over(partition by 1 order by A.name) as Seq
from syscolumns A, syscolumns B
)d
)allDates
left join
AuditResults r on r.[Date]=allDates.[date] and r.AuditType = 'Identification' AND r.Product = 'Billet'
where
allDates.[Date]>=@FromDate and allDates.[Date]<=@ToDate
group by
allDates.[Date]
return
end
关键是 'allDates' 部分;
SELECT
[date]=convert(datetime, CONVERT(float,d.Seq))
FROM
(
select top 100000 row_number() over(partition by 1 order by A.name) as Seq
from syscolumns A, syscolumns B
)d
这将 return 1900 到 2173 之间的所有日期(在此示例中)。根据需要限制它,但这是一个不错的选择。有很多不同的方法可以清楚地解决这个问题