2017-04-19 104 views
0

「我們有兩個表,一個是發送給供應商(主)的小時,另一個是我們將要發送(加載)的小時數。總結基於月份和年份與分類彙總每個人的時間。我們希望找到那些誰擁有300小時以上特定月份的個人。試圖爲每個月/每年的個人添加總計

CREATE TABLE MasterTabletesting(
    ID   CHAR(9) NOT NULL 
    ,Workdate DATE NOT NULL 
    ,Emp   CHAR(30) NOT NULL 
    ,HoursWorked DECIMAL(18,2) NOT NULL); 
    INSERT INTO MasterTabletesting(ID,Workdate,Emp,HoursWorked) VALUES ('25','20160731','7502',24); 
    INSERT INTO MasterTabletesting(ID,Workdate,Emp,HoursWorked) VALUES ('25','20160731','21874',128); 
    INSERT INTO MasterTabletesting(ID,Workdate,Emp,HoursWorked) VALUES ('25','20160731','7502',166); 
    INSERT INTO MasterTabletesting(ID,Workdate,Emp,HoursWorked) VALUES ('72','20160831','7508',180); 
    INSERT INTO MasterTabletesting(ID,Workdate,Emp,HoursWorked) VALUES ('72','20160831','5501',180); 
    INSERT INTO MasterTabletesting(ID,Workdate,Emp,HoursWorked) VALUES ('72','20160831','21037',23); 
    INSERT INTO MasterTabletesting(ID,Workdate,Emp,HoursWorked) VALUES ('66','20160831','83641',22); 



    CREATE TABLE LoadTabletesting(
    ID   CHAR(9) NOT NULL 
    ,Workdate CHAR(8) NOT NULL 
    ,Emp   CHAR(30) NOT NULL 
    ,HoursWorked DECIMAL(18,2) NOT NULL); 
    INSERT INTO LoadTabletesting(ID,Workdate,Emp,HoursWorked) VALUES ('8','07312016','7500',24); 
    INSERT INTO LoadTabletesting(ID,Workdate,Emp,HoursWorked) VALUES ('6','07312016','21974',128); 
    INSERT INTO LoadTabletesting(ID,Workdate,Emp,HoursWorked) VALUES ('25','07312016','7500',166); 
    INSERT INTO LoadTabletesting(ID,Workdate,Emp,HoursWorked) VALUES ('72','08312016','7500',180); 
    INSERT INTO LoadTabletesting(ID,Workdate,Emp,HoursWorked) VALUES ('88','08312016','5507',180); 
    INSERT INTO LoadTabletesting(ID,Workdate,Emp,HoursWorked) VALUES ('23','08312016','21012',23); 
    INSERT INTO LoadTabletesting(ID,Workdate,Emp,HoursWorked) VALUES ('55','08312016','83667',22);} 

負載表的日期是char變量類型我的方法是將兩個表中的必要數據放在標題爲「Over300agtest:

SELECT ID AS 'ID' 
,Employer AS 'Emp' 
,month(WorkDate) AS 'Mnth' 
,year(WorkDate) AS 'Yr' 
,HoursworkedAS 'Hrs' 
,’amt’ as ‘Table’ 
FROM Mastertabletesting 
WHERE HoursWorked IS NOT NULL 
GROUP BY ID, Employer, month(WorkDate), year(WorkDate) 

UNION all 

SELECT ID AS 'ID' 
,Employer AS 'Emp' 
,month(CAST((RIGHT(workdate, 4) + LEFT(workdate, 4)) AS DATE)) AS 'Mnth' 
,YEAR(CAST((RIGHT(workdate, 4) + LEFT(workdate, 4)) AS DATE)) AS 'Yr' 
,hoursworked AS 'Hrs' 
,‘alt’ as ‘Table’ 
FROM Loadtabletesting 
WHERE HoursWorked IS NOT NULL 
GROUP BY ID, Employer, month(CAST((RIGHT(workdate, 4) +LEFT workdate,4)) AS DATE)), year(CAST((RIGHT(workdate, 4) + LEFT(workdate, 4)) AS DATE)) 

然後,我使用一個公用表表達式來查找那些在特定月份中有超過300小時的表達式,然後使用查詢加入CTE以添加他們工作的僱主。我們將不勝感激任何關於如何在每個月的時間欄下添加一行的建議。

with monthsum as(
    Select ID as 'ID' 
    , mnth as 'mnth' 
    , yr as 'yr' 
    ,SUM(hrs)as 'TotalHrs' 
    From over300agtest 
    Group by ID, mnth, yr 
    having SUM(hrs) > 300) 

Select ms.ID 
,ms.mnth 
,ms.yr 
,emp 
,hrs 
,o3.[table] 
,totalhrs 
From monthsum ms left outer join over300agtest o3 
on ms.ID = o3.ID and ms.mnth = o3.mnth and ms.yr =o3.yr 
Order by ms.ID, ms.yr asc, ms.mnth asc 

下面是電流輸出:

+----+------+------+-------+-----+-------+----------+ 
| ID | mnth | yr | emp | hrs | table | totalhrs | 
+----+------+------+-------+-----+-------+----------+ 
| 25 | 7 | 2016 | 7502 | 24 | AMT |  484 | 
| 25 | 7 | 2016 | 21874 | 128 | AMT |  484 | 
| 25 | 7 | 2016 | 7502 | 166 | AMT |  484 | 
| 25 | 7 | 2016 | 7500 | 166 | ALT |  484 | 
| 72 | 8 | 2016 | 7508 | 180 | AMT |  563 | 
| 72 | 8 | 2016 | 5501 | 180 | AMT |  563 | 
| 72 | 8 | 2016 | 21037 | 23 | AMT |  563 | 
| 72 | 8 | 2016 | 7500 | 180 | ALT |  563 | 
+----+------+------+-------+-----+-------+----------+ 

這裏是輸出我打算:

+-------+------+------+-------+-----+-------+ 
| ID | mnth | yr | emp | hrs | table | 
+-------+------+------+-------+-----+-------+ 
| 25 | 7 | 2016 | 7502 | 24 | AMT | 
| 25 | 7 | 2016 | 21874 | 128 | AMT | 
| 25 | 7 | 2016 | 7502 | 166 | AMT | 
| 25 | 7 | 2016 | 7500 | 166 | ALT | 
| Total |  |  |  | 484 |  | 
| 72 | 8 | 2016 | 7508 | 180 | AMT | 
| 72 | 8 | 2016 | 5501 | 180 | AMT | 
| 72 | 8 | 2016 | 21037 | 23 | AMT | 
| 72 | 8 | 2016 | 7500 | 180 | ALT | 
| Total |  |  |  | 563 |  | 
+-------+------+------+-------+-----+-------+ 
+1

如果您可以爲'Load'和'Master'表格以及樣本數據提供表格模式,將會有所幫助。更重要的是,您似乎在兩個表中都有「ID」,當您將記錄推入單個視圖時,這些ID可能會重疊。 – bc004346

+0

請閱讀[this](http://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/)以獲取關於改善問題的一些提示。 – HABO

+0

我不確定你可以用普通的SQL來做你想做的事情。您可以通過分區獲得小時數,但我認爲您必須以編程方式獲取您要查找的顯示輸出。 – Shawn

回答

0

我建議這種方法。你應該能夠制定出具體的細節。

select convert(char(6), cast(workdate as date), 120) yearMonth 
, sum(hrs) totalHours 
from etc 
group by convert(char(6), cast(workdate as date), 120) 
having sum(hrs) > 300 
+0

我正在使用SQL Server 2008.嘗試使用上面的代碼。收到消息:消息241,級別16,狀態1,行1 轉換日期和/或時間從字符串轉換失敗。 – Michael