以下查询工作正常,但它返回两行小时,我不想
SELECT
USERINFO.name, USERINFO.BADGENUMBER,
departments.deptname, APPROVEDHRS.hours,
sum(workingdays) as workingdays,TotalWorkingDays
FROM
(SELECT DISTINCT
(DATEDIFF(DAY, '2014-06-01', '2014-06-30') + 1) -
DATEDIFF(WEEK, '2014-06-01', '2014-06-30') * 2 -
(CASE WHEN DATEPART(WEEKDAY, '2014-06-01') = 5 THEN 1 ELSE 0 END) -
(CASE WHEN DATEPART(WEEKDAY, '2014-06-30') = 6 THEN 1 ELSE 0 END) AS TotalWorkingDays,
COUNT(DISTINCT DATEADD(d, 0,DATEDIFF(d, 0, CHECKINOUT.CHECKTIME))) AS workingdays,
USERINFO.BADGENUMBER, USERINFO.NAME, hours
FROM
USERINFO
LEFT JOIN
CHECKINOUT ON USERINFO.USERID = CHECKINOUT.USERID
LEFT JOIN
departments ON departments.deptid = userinfo.DEFAULTDEPTID
left join APPROVEDHRS on APPROVEDHRS.userid = userinfo.userid AND
(APPROVEDHRS.DATE >='2014-06-01') AND (APPROVEDHRS.DATE <='2014-06-30')
WHERE
(DEPARTMENTS.DEPTNAME = 'xyz')
AND (CHECKINOUT.CHECKTIME >= '2014-06-01')
AND (CHECKINOUT.CHECKTIME <= '2014-06-30')
GROUP BY
hours, USERINFO.BADGENUMBER, deptname, USERINFO.NAME,
CONVERT(VARCHAR(10), CHECKINOUT.CHECKTIME, 103)) blue
GROUP BY
name, BADGENUMBER, workingdays, TotalWorkingDays, deptname, hours
以上查询的输出:
name BADGENUMBER deptname hours
---------------------------------------------------
abc 1111 xyz 00:07:59
abc 1111 xyz 00:08:00
pqr 2222 qwe NULL
现在表中的总小时数(APPROVEDHRS
表)是:
BADGENUMBER NAME DATE HOURS
-------------------------------------------------
1111 xyz 2014-06-15 00:07:59
1111 xyz 2014-06-14 00:08:00
1111 xyz 2014-07-14 00:10:00
我正在从2014-06-01到2014-06-30获取记录
所以我想要以下输出:
name BADGENUMBER deptname hours
--------------------------------------------------------
abc 1111 xyz 00:15:59
pqr 2222 qwe NULL
帮助我获得所需的输出。
谢谢
答案 0 :(得分:0)
您必须GROUP BY name, badgenumber, deptname
。
答案 1 :(得分:0)
试试这个
Select
name,badgenumber,deptname,
Hours=
Convert(varchar,SUM(convert(int,PARSENAME(replace(hours,':','.'),3))) +SUM(convert(int,PARSENAME(replace(hours,':','.'),2))) /60)+':'+
Convert(varchar,SUM(convert(int,PARSENAME(replace(hours,':','.'),2))) %60+SUM(convert(int,PARSENAME(replace(hours,':','.'),1))) /60)+':'+
convert(varchar,SUM(convert(int,PARSENAME(replace(hours,':','.'),1))) %60)
from table -- Or joining Criteria
where isnull(date,'2014-06-01') between '2014-06-01' and '2014-06-30'
group by name,badgenumber,deptname