我正在尝试让此查询填充正在构建的Winform应用程序中的reportviewer(并且在填充要查看的报告之前将查询中的参数更改为在窗体上选择的值)。但是over功能无法正常工作。用户将从单选框,下拉列表等输入参数,单击搜索,然后将打开reportviewer以便进行打印。查询非常丑陋,而且我已经研究了一段时间。这是我以所需格式获取结果集的唯一方法。
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屏幕截图。在回答完以下内容后更新
答案 0 :(得分:1)
我不知道这是否可行,因为我没有要测试的表。 (即,如果出错,则必须决定要怎么做。)
Codes = (t.DeptCode + '-' + t.OpCode)
,然后是:join temptable10 on g.DeptCode = left(codes,2)and g.opcode = RIGHT(codes,2)
实际上,发生了很多dept和op代码的连接,这似乎是不必要的,只需使用两列就可以了。
between
是用于日期范围的狗,我强烈建议使用下面查询中的内容。 >=
和< (+1 day)
的日期范围构造适用于所有日期/时间数据类型
建议查询:
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