简化查询以用作 winform rdlc 报告中的数据集
Simplify Query to be used as dataset in winform rdlc report
我正在尝试让此查询在我正在构建的 winform 应用程序中填充一个报告查看器(并将在填充要查看的报告之前将查询中的参数更改为在表单上选择的值)。但是 over 函数无法工作。用户将从单选框、下拉列表等输入参数,单击搜索,然后将打开报告查看器以便打印。查询非常丑陋,我已经研究了一段时间。这是我获得所需格式结果集的唯一方法。
SQL QUERY 寻求帮助以缩短并得到相同的结果
DECLARE @EndReport DATETIME, @StartReport DATETIME, @Location INT, @Department varchar(50)
SET @EndReport = '10-15-2018' SET @StartReport = '10-15-2018' SET @Department = 'fb' SET @Location = 10
SELECT row_number() over (order by (ai.FirstName + ' ' + ai.LastName)) RowNum
,AssociateName = isnull(upper(ai.FirstName + ' ' + ai.LastName),'**' + cast(t.ID as varchar(30)) + '**')
,ID = t.ID
,Codes = (t.DeptCode + '-' + t.OpCode)
,TimeSUM = cast(SUM(datediff(second,StartTime, FinishTime) /3600.0) as decimal (6,2))
,Units = SUM(Units)
,UPH = cast(isnull(sum(Units) / nullif(sum(datediff(minute,StartTime,FinishTime))*1.0,0),0.0)*60 as decimal(10,0))
into temptable10
FROM TimeLogNEW t LEFT JOIN AssociateInfo ai
ON t.ID = ai.ID
JOIN GoalSetUp g
ON (t.DeptCode + '-' + t.OpCode) = (g.DeptCode + '-' + g.OpCode)
WHERE EventDate between @StartReport and @EndReport
and t.Location = @Location and g.location= @Location and ((t.DeptCode + t.OpCode) in (g.DeptCode + g.OpCode)) and t.DeptCode = @Department
GROUP BY t.DeptCode,t.OpCode,ai.FirstName,ai.LastName, t.ID
SELECT
[Associate Name] = AssociateName
,[Codes] = Codes
,[TimeSUM] = TimeSUM
,[Units] = Units
,[UPH] = UPH
,[UPH Target] = Goal
,[Actual %] = CASE WHEN goal = 0 then '0%'
else convert(varchar,cast(100* (isnull(UPH,0)/nullif(Goal,0)) as decimal(10,0))) + '%' END
FROM goalsetup g join temptable10 on g.DeptCode = left(codes,2)and g.opcode = RIGHT(codes,2)
WHERE g.Location = @Location
ORDER BY Codes, UPH Desc
drop table temptable10
SQL 结果集
Visual Studio错误
正在添加 visual studio 屏幕截图。在下面回答后更新
我不知道这是否有效,因为我没有 table 可以测试。 (即,如果它出错,您将不得不决定要做什么。)
- row_number() 列没有明显的原因,所以我删除了它
Codes = (t.DeptCode + '-' + t.OpCode)
当做table时,后面是:
join temptable10 on g.DeptCode = left(codes,2)and g.opcode = RIGHT(codes,2)
- 那是低效且不必要的,只保留两列
事实上,有很多部门代码和操作代码的串联,这似乎是不必要的,只需使用 2 列就可以了。
between
在用于日期范围时是一只狗,我强烈建议使用我在下面的查询中拥有的内容。此 >=
和 < (+1 day)
的日期范围构造适用于所有 date/time 数据类型
- 请在引用任何列时使用 table 别名,像我这样的 reader 无法知道其中一些 table 列来自哪个 table,这使得 [=41] 变得更加困难=].
建议查询:
DECLARE @EndReport datetime
, @StartReport datetime
, @Location int
, @Department varchar(50)
SET @EndReport = '10-15-2018'
SET @StartReport = '10-15-2018'
SET @Department = 'fb'
SET @Location = 10
SELECT
AssociateName = ISNULL(UPPER(ai.FirstName + ' ' + ai.LastName), '**' + CAST(t.ID AS varchar(30)) + '**')
, ID = t.ID
, Codes = (t.DeptCode + '-' + t.OpCode)
, t.DeptCode
, t.OpCode
, TimeSUM = CAST(SUM(DATEDIFF(SECOND, StartTime, FinishTime) / 3600.0) AS decimal(6, 2))
, Units = SUM(Units)
, UPH = CAST(ISNULL(SUM(Units) / NULLIF(SUM(DATEDIFF(MINUTE, StartTime, FinishTime)) * 1.0, 0), 0.0) * 60 AS decimal(10, 0))
INTO temptable10
FROM TimeLogNEW t
LEFT JOIN AssociateInfo ai
ON t.ID = ai.ID
JOIN GoalSetUp g
ON t.DeptCode = g.DeptCode AND t.OpCode = g.OpCode
WHERE EventDate >= @StartReport AND EventDate < dateadd(day,1,@EndReport)
AND t.Location = @Location
AND g.location = @Location
AND t.DeptCode = @Department
GROUP BY
t.DeptCode
, t.OpCode
, ai.FirstName
, ai.LastName
, t.ID
SELECT
[Associate Name] = t10.AssociateName
, [Codes] = t10.Codes
, [TimeSUM] = t10.TimeSUM
, [Units] = t10.Units
, [UPH] = t10.UPH
, [UPH Target] = g.Goal
, [Actual %] =
CASE
WHEN g.goal = 0 THEN '0%'
ELSE CONVERT(varchar, CAST(100 * (ISNULL(t10.UPH, 0) / NULLIF(g.Goal, 0)) AS decimal(10, 0))) + '%'
END
FROM goalsetup g
JOIN temptable10 t10
ON g.DeptCode = t10.DeptCode
AND g.opcode = t10.opcode
WHERE g.Location = @Location
ORDER BY
t10.Codes
, t10.UPH DESC
DROP TABLE temptable10
我正在尝试让此查询在我正在构建的 winform 应用程序中填充一个报告查看器(并将在填充要查看的报告之前将查询中的参数更改为在表单上选择的值)。但是 over 函数无法工作。用户将从单选框、下拉列表等输入参数,单击搜索,然后将打开报告查看器以便打印。查询非常丑陋,我已经研究了一段时间。这是我获得所需格式结果集的唯一方法。
SQL QUERY 寻求帮助以缩短并得到相同的结果
DECLARE @EndReport DATETIME, @StartReport DATETIME, @Location INT, @Department varchar(50)
SET @EndReport = '10-15-2018' SET @StartReport = '10-15-2018' SET @Department = 'fb' SET @Location = 10
SELECT row_number() over (order by (ai.FirstName + ' ' + ai.LastName)) RowNum
,AssociateName = isnull(upper(ai.FirstName + ' ' + ai.LastName),'**' + cast(t.ID as varchar(30)) + '**')
,ID = t.ID
,Codes = (t.DeptCode + '-' + t.OpCode)
,TimeSUM = cast(SUM(datediff(second,StartTime, FinishTime) /3600.0) as decimal (6,2))
,Units = SUM(Units)
,UPH = cast(isnull(sum(Units) / nullif(sum(datediff(minute,StartTime,FinishTime))*1.0,0),0.0)*60 as decimal(10,0))
into temptable10
FROM TimeLogNEW t LEFT JOIN AssociateInfo ai
ON t.ID = ai.ID
JOIN GoalSetUp g
ON (t.DeptCode + '-' + t.OpCode) = (g.DeptCode + '-' + g.OpCode)
WHERE EventDate between @StartReport and @EndReport
and t.Location = @Location and g.location= @Location and ((t.DeptCode + t.OpCode) in (g.DeptCode + g.OpCode)) and t.DeptCode = @Department
GROUP BY t.DeptCode,t.OpCode,ai.FirstName,ai.LastName, t.ID
SELECT
[Associate Name] = AssociateName
,[Codes] = Codes
,[TimeSUM] = TimeSUM
,[Units] = Units
,[UPH] = UPH
,[UPH Target] = Goal
,[Actual %] = CASE WHEN goal = 0 then '0%'
else convert(varchar,cast(100* (isnull(UPH,0)/nullif(Goal,0)) as decimal(10,0))) + '%' END
FROM goalsetup g join temptable10 on g.DeptCode = left(codes,2)and g.opcode = RIGHT(codes,2)
WHERE g.Location = @Location
ORDER BY Codes, UPH Desc
drop table temptable10
SQL 结果集
Visual Studio错误
正在添加 visual studio 屏幕截图。在下面回答后更新
我不知道这是否有效,因为我没有 table 可以测试。 (即,如果它出错,您将不得不决定要做什么。)
- row_number() 列没有明显的原因,所以我删除了它
Codes = (t.DeptCode + '-' + t.OpCode)
当做table时,后面是:join temptable10 on g.DeptCode = left(codes,2)and g.opcode = RIGHT(codes,2)
- 那是低效且不必要的,只保留两列
事实上,有很多部门代码和操作代码的串联,这似乎是不必要的,只需使用 2 列就可以了。
between
在用于日期范围时是一只狗,我强烈建议使用我在下面的查询中拥有的内容。此>=
和< (+1 day)
的日期范围构造适用于所有 date/time 数据类型- 请在引用任何列时使用 table 别名,像我这样的 reader 无法知道其中一些 table 列来自哪个 table,这使得 [=41] 变得更加困难=].
建议查询:
DECLARE @EndReport datetime
, @StartReport datetime
, @Location int
, @Department varchar(50)
SET @EndReport = '10-15-2018'
SET @StartReport = '10-15-2018'
SET @Department = 'fb'
SET @Location = 10
SELECT
AssociateName = ISNULL(UPPER(ai.FirstName + ' ' + ai.LastName), '**' + CAST(t.ID AS varchar(30)) + '**')
, ID = t.ID
, Codes = (t.DeptCode + '-' + t.OpCode)
, t.DeptCode
, t.OpCode
, TimeSUM = CAST(SUM(DATEDIFF(SECOND, StartTime, FinishTime) / 3600.0) AS decimal(6, 2))
, Units = SUM(Units)
, UPH = CAST(ISNULL(SUM(Units) / NULLIF(SUM(DATEDIFF(MINUTE, StartTime, FinishTime)) * 1.0, 0), 0.0) * 60 AS decimal(10, 0))
INTO temptable10
FROM TimeLogNEW t
LEFT JOIN AssociateInfo ai
ON t.ID = ai.ID
JOIN GoalSetUp g
ON t.DeptCode = g.DeptCode AND t.OpCode = g.OpCode
WHERE EventDate >= @StartReport AND EventDate < dateadd(day,1,@EndReport)
AND t.Location = @Location
AND g.location = @Location
AND t.DeptCode = @Department
GROUP BY
t.DeptCode
, t.OpCode
, ai.FirstName
, ai.LastName
, t.ID
SELECT
[Associate Name] = t10.AssociateName
, [Codes] = t10.Codes
, [TimeSUM] = t10.TimeSUM
, [Units] = t10.Units
, [UPH] = t10.UPH
, [UPH Target] = g.Goal
, [Actual %] =
CASE
WHEN g.goal = 0 THEN '0%'
ELSE CONVERT(varchar, CAST(100 * (ISNULL(t10.UPH, 0) / NULLIF(g.Goal, 0)) AS decimal(10, 0))) + '%'
END
FROM goalsetup g
JOIN temptable10 t10
ON g.DeptCode = t10.DeptCode
AND g.opcode = t10.opcode
WHERE g.Location = @Location
ORDER BY
t10.Codes
, t10.UPH DESC
DROP TABLE temptable10