SELECT e.firstName,t.setDate, w.wageAmount
FROM TimeSheet t
INNER JOIN Employee e
ON e.employeeID = t.employeeID
INNER JOIN Salary s
ON s.salaryID = t.salaryID
INNER JOIN Wage w
ON w.wageID = s.wageID
INNER JOIN EmpHiringInfo ehf
ON ehf.EmpHiringInfoID = s.EmpHiringInfoID
INNER JOIN WageType wt
ON wt.wageTypeID = w.wageTypeID
WHERE (wt.wageTypeID = 19) AND (ehf.isActive = 1 AND s.isActive = 1
AND ehf.employeeID = 300) AND (CONVERT(varchar, t.setDate, 23) BETWEEN '2000-08-02' AND '2020-08-04')
顺便说一下,工资每个月只有一个,所以工资是重复的。所以对于2010年11月,无论11,2010多少天,工资是3600 ......
以上示例生成以下示例代码:
我需要将所有类似的月/年收集到一个单元格中,丢弃日期和时间。例如 2010-11-12和2010-11-26必须按照2010-11(任何格式)签约一个单元格。
感谢任何帮助。
答案 0 :(得分:2)
要将所有日期统一到几个月,您可以执行以下操作:
dateadd(mm,datediff(mm,0,t.setDate),0)
但是你需要分组才能进行聚合:
select e.firstName,dateadd(mm,datediff(mm,0,t.setDate),0), sum(w.wageAmount)
from ...
group by e.firstName, dateadd(mm,datediff(mm,0,t.setDate),0)
答案 1 :(得分:0)
您可以尝试以下内容:
SELECT e.firstName
,CONVERT(CHAR(7),t.setDate,102)
,SUM(w.wageAmount)
FROM ...
GROUP BY e.firstName,
,CONVERT(CHAR(7),t.setDate,102)
答案 2 :(得分:0)
T-SQL中有DatePart function能够检索特定日期的日期和年份。
您的查询可能如下所示:
SELECT e.firstName,
DATEPART(yy, t.setDate) AS Year,
DATEPART(mm, t.setDate) AS Month,
MAX(w.wageAmount) AS wageAmount
FROM TimeSheet t
INNER JOIN Employee e
ON e.employeeID = t.employeeID
INNER JOIN Salary s
ON s.salaryID = t.salaryID
INNER JOIN Wage w
ON w.wageID = s.wageID
INNER JOIN EmpHiringInfo ehf
ON ehf.EmpHiringInfoID = s.EmpHiringInfoID
INNER JOIN WageType wt
ON wt.wageTypeID = w.wageTypeID
WHERE (wt.wageTypeID = 19) AND (ehf.isActive = 1 AND s.isActive = 1
AND ehf.employeeID = 300) AND
(CONVERT(varchar, t.setDate, 23) BETWEEN '2000-08-02' AND '2020-08-04')
GROUP BY e.firstName, DATEPART(yy, t.setDate), DATEPART(mm, t.setDate)