2014-01-23 156 views
0

我試着寫一個MySQL的語句,將我帶回來這些結果INT比較:基於多少任務的每一天執行的每個員工沒有WHERE語句

## Name | Day 0 | Day 1 | Day 2 | Day 3 | Day 4 | Day 5 | 
##Jeff | 0 | 3 |  1 |  2 | 1 | 1 | 
##Larry | 1 | 1 |  4 |  4 | 1 | 0 | 

我的數據庫表如下:

員工

id(INT),number(VARCHAR),name(VARCHAR),dateStarted(VARCHAR),

項目

id(INT),number (VARCHAR),dateEnded(DATETIME)

現在,我用這樣的說法:

SELECT 
a.name AS "Name", 
count(abs(datediff(STR_TO_DATE(a.dateStarted, '%Y-%m-%d %H:%i:%s'), b.dateEnded))) AS "Day 0", 
count(abs(datediff(STR_TO_DATE(a.dateStarted, '%Y-%m-%d %H:%i:%s'), b.dateEnded))) AS "Day 1", 
count(abs(datediff(STR_TO_DATE(a.dateStarted, '%Y-%m-%d %H:%i:%s'), b.dateEnded))) AS "Day 2", 
count(abs(datediff(STR_TO_DATE(a.dateStarted, '%Y-%m-%d %H:%i:%s'), b.dateEnded))) AS "Day 3", 
count(abs(datediff(STR_TO_DATE(a.dateStarted, '%Y-%m-%d %H:%i:%s'), b.dateEnded))) AS "Day 4", 
count(abs(datediff(STR_TO_DATE(a.dateStarted, '%Y-%m-%d %H:%i:%s'), b.dateEnded))) AS "Day 5" 
FROM employee a, project b 
WHERE b.number=a.number 
AND "Day 0" = 0 
AND "Day 1" = 1 
AND "Day 2" = 2 
AND "Day 3" = 3 
AND "Day 4" = 4 
AND "Day 5" >= 5 

電流輸出

Current Undesired Output

上述聲明的作品,但由於某種原因,沒有提供上述要求中提到的所需結果。關於如何修復/更改它的任何想法?

編輯

如果我冒了出來:

AND "Day 0" = 0 
AND "Day 1" = 1 
AND "Day 2" = 2 
AND "Day 3" = 3 
AND "Day 4" = 4 
AND "Day 5" >= 5 

然後打印出來:

## Name | Day 0 | Day 1 | Day 2 | Day 3 | Day 4 | Day 5 | 
##Jeff | 9 | 9 |  9 |  9 | 9 | 9 | 
+2

'E之間沒有加入條件mployee'和'Project'? – hashbrown

+0

'STR_TO_DATE(a.dateStarted,'%Y-%m-%d%H:%i:%s')是什麼意思?你的日期存儲爲字符串嗎? –

+0

@hashbrown,如果兩個id是兩個單獨的東西(id爲person,id爲project),加入它們將毫無意義。如果他們提到同樣的事情(名字很差),那麼你可以把它們加入一張表中。 –

回答

1

試試這個:

SELECT 
    emp.name AS '## Name', 
    (SELECT COUNT(*)  
    FROM project p JOIN employee e ON p.number = e.number 
    WHERE e.name = emp.name 
    AND datediff(str_to_date(p.dateEnded, '%Y-%m-%d'), e.dateStarted) = 0 
    ) AS 'Day 0' 
    ,(SELECT COUNT(*) 
    FROM project p JOIN employee e ON p.number = e.number 
    WHERE e.name = emp.name 
     AND datediff(str_to_date(p.dateEnded, '%Y-%m-%d'), e.dateStarted) = 1 
    ) AS 'Day 1' 
    ,(SELECT COUNT(*) 
    FROM project p JOIN employee e ON p.number = e.number 
    WHERE e.name = emp.name 
     AND datediff(str_to_date(p.dateEnded, '%Y-%m-%d'), e.dateStarted) = 2 
    ) AS 'Day 2' 
    ,(SELECT COUNT(*) 
    FROM project p JOIN employee e ON p.number = e.number 
    WHERE e.name = emp.name 
     AND datediff(str_to_date(p.dateEnded, '%Y-%m-%d'), e.dateStarted) = 3 
    ) AS 'Day 3' 
    ,(SELECT COUNT(*) 
    FROM project p JOIN employee e ON p.number = e.number 
    WHERE e.name = emp.name 
     AND datediff(str_to_date(p.dateEnded, '%Y-%m-%d'), e.dateStarted) = 4 
    ) AS 'Day 4' 
    ,(SELECT COUNT(*) 
    FROM project p JOIN employee e ON p.number = e.number 
    WHERE e.name = emp.name 
     AND datediff(str_to_date(p.dateEnded, '%Y-%m-%d'), e.dateStarted) >= 5 
    ) AS 'Day 5' 
FROM employee emp 
GROUP BY emp.name 

硒( - 根據提供的信息對您的數據進行了一些假設)。

1

試試這個:

SELECT a.name AS "Name", 
     SUM(noOfDays = 0) AS "Day 0", SUM(noOfDays = 1) AS "Day 1", 
     SUM(noOfDays = 2) AS "Day 2", SUM(noOfDays = 3) AS "Day 3", 
     SUM(noOfDays = 4) AS "Day 4", SUM(noOfDays >= 5) AS "Day 5", 
     COUNT(1) AS "Total Days" 
FROM (SELECT a.name, DATEDIFF(DATE(b.dateEnded), DATE(a.dateStarted)) noOfDays 
     FROM employee a INNER JOIN project b ON b.number = a.number 
     WHERE b.dateEnded IS NOT NULL 
    ) AS A 
GROUP BY a.name; 

檢查SQL FIDDLE DEMO

| NAME | DAY 0 | DAY 1 | DAY 2 | DAY 3 | DAY 4 | DAY 5 | TOTAL DAYS | 
|---------|-------|-------|-------|-------|-------|-------|------------| 
| ##Jeff |  0 |  3 |  1 |  2 |  1 |  1 |   8 | 
| ##Larry |  1 |  1 |  4 |  4 |  1 |  0 |   11 | 
1

以前的解決方案可以通過以下操作連接語句或者改善..

確保有一個索引(唯一的?)在項目上(編號,日期已結束) 您也可以嘗試在員工上添加索引(編號,dateStarted)

NULL值通常不是任何索引的一部分(因爲它們爲null),您可以嘗試將該字段的默認值更新爲'0000-00-00',這樣就可以獲得索引並可能增加速度。

試試這個:

SELECT a.name AS "Name", 
     SUM(noOfDays = 0) AS "Day 0", SUM(noOfDays = 1) AS "Day 1", 
     SUM(noOfDays = 2) AS "Day 2", SUM(noOfDays = 3) AS "Day 3", 
     SUM(noOfDays = 4) AS "Day 4", SUM(noOfDays >= 5) AS "Day 5" 
FROM (SELECT a.number, a.name, DATEDIFF(DATE(b.dateEnded), DATE(a.dateStarted)) noOfDays 
     FROM employee a INNER JOIN project b ON (b.number = a.number and b.dateEnded>a.dateStarted) 
    ) AS a 
GROUP BY a.name 

檢查SQL FIDDLE DEMO

| NAME | DAY 0 | DAY 1 | DAY 2 | DAY 3 | DAY 4 | DAY 5 | 
|---------|-------|-------|-------|-------|-------|-------| 
| ##Jeff |  0 |  3 |  1 |  2 |  1 |  1 | 
| ##Larry |  1 |  1 |  4 |  4 |  1 |  0 | 
0

從以前的答案的一個使用SQL擺弄數據,我已經試過這樣: -

SELECT Sub0.name AS "Name", 
    SUM(IF(DayDesc = 0 AND DaysCount IS NOT NULL, 1, 0)) AS "Day 0", 
    SUM(IF(DayDesc = 1 AND DaysCount IS NOT NULL, 1, 0)) AS "Day 1", 
    SUM(IF(DayDesc = 2 AND DaysCount IS NOT NULL, 1, 0)) AS "Day 2", 
    SUM(IF(DayDesc = 3 AND DaysCount IS NOT NULL, 1, 0)) AS "Day 3", 
    SUM(IF(DayDesc = 4 AND DaysCount IS NOT NULL, 1, 0)) AS "Day 4", 
    SUM(IF(DayDesc = 5 AND DaysCount IS NOT NULL, 1, 0)) AS "Day 5" 
FROM 
(
    SELECT DISTINCT name FROM employee 
)Sub0 
CROSS JOIN 
(
    SELECT 0 AS DayMin, 0 AS DayMax, 0 AS DayDesc 
    UNION 
    SELECT 1 AS DayMin, 1 AS DayMax, 1 AS DayDesc 
    UNION 
    SELECT 2 AS DayMin, 2 AS DayMax, 2 AS DayDesc 
    UNION 
    SELECT 3 AS DayMin, 3 AS DayMax, 3 AS DayDesc 
    UNION 
    SELECT 4 AS DayMin, 4 AS DayMax, 4 AS DayDesc 
    UNION 
    SELECT 5 AS DayMin, 999999999 AS DayMax, 5 AS DayDesc 
) Sub1 
LEFT OUTER JOIN 
(
    SELECT a.name, ABS(DATEDIFF(STR_TO_DATE(a.dateStarted, '%Y-%m-%d %H:%i:%s'), b.dateEnded)) DaysCount 
    FROM employee a 
    INNER JOIN project b 
    ON b.number = a.number 
) Sub2 
ON Sub2.DaysCount BETWEEN Sub1.DayMin AND Sub1.DayMax 
AND Sub2.name = Sub0.name 
GROUP BY Sub0.name 

似乎給出正確的結果並且相當快。

SQL小提琴的位置: -

http://www.sqlfiddle.com/#!2/db498/84

位更有效(但不與誰沒有項目員工應付): -

SELECT Sub0.name AS "Name", 
    SUM(IF(DayDesc = 0, 1, 0)) AS "Day 0", 
    SUM(IF(DayDesc = 1, 1, 0)) AS "Day 1", 
    SUM(IF(DayDesc = 2, 1, 0)) AS "Day 2", 
    SUM(IF(DayDesc = 3, 1, 0)) AS "Day 3", 
    SUM(IF(DayDesc = 4, 1, 0)) AS "Day 4", 
    SUM(IF(DayDesc = 5, 1, 0)) AS "Day 5" 
FROM 
(
    SELECT a.name, 
     CASE ABS(DATEDIFF(STR_TO_DATE(a.dateStarted, '%Y-%m-%d %H:%i:%s'), b.dateEnded)) 
      WHEN 0 THEN 0 
      WHEN 1 THEN 1 
      WHEN 2 THEN 2 
      WHEN 3 THEN 3 
      WHEN 4 THEN 4 
      ELSE 5 
     END AS DayDesc 
    FROM employee a 
    INNER JOIN project b 
    ON b.number = a.number 
) Sub0 
GROUP BY Sub0.name 

SQL小提琴的位置: -

http://www.sqlfiddle.com/#!2/db498/89