我想能夠輸出從下面的查詢中的以下字段:MySQL的派生表版本
AddedById,AddedByName,HoursWorked,CurrentYearlyFlexiAvailable
然而,當我有WHERE addedby = 1,我想和替換此字段名稱AddedById因爲我不想硬編碼此值作爲整體查詢應該會返回多個人,我想從rpt_timesheet_data視圖得到這個值,這是在那裏。該CurrentYearlyFlexiAvailable場應該告訴我,他們有多少時間留給本年度迄今爲止通過執行SELECT SUM(TTL)的計算從FROM(SELECT SUM(工作)-420爲TTL
SELECT AddedById,AddedByName,SUM(HoursWorked) AS HoursWorked
,(SELECT SUM(ttl) - (
SELECT SUM(worked)
FROM vwtimesheet
WHERE addedby=AddedById
AND entrydate BETWEEN '2017-01-01' AND '2017-04-13'
AND activityid=3192
GROUP BY addedby) AS flexihours
FROM (
SELECT SUM(worked)-420 AS ttl
FROM vwtimesheet
WHERE addedby=1 <!--HERE IS THE ISSUE
AND entrydate BETWEEN '2017-01-01' AND '2017-04-13'
AND projectid<>113 AND activityid<>3192
GROUP BY entrydate
HAVING SUM(worked)>420
) AS s) AS CurrentYearlyFlexiAvailable
FROM rpt_timesheet_data
WHERE entrydate BETWEEN '2017-04-02' AND '2017-04-13 23:59:59'
AND ActivityId=3192
GROUP BY AddedById,AddedByName
ORDER BY AddedByName
,但我不斷收到:
錯誤代碼:1054在未知列'AddedById 'where子句'
剛在那一個地方。我已經嘗試過各種查詢來排序,但只是無法弄清楚。對不起不擅長解釋這一點,可以看到它在我的腦袋我想做...
這裏是做在它返回的結果爲單個用戶,非常類似的查詢,其中的一個以上是通過所有的用戶意味着環和給我的結果: -
SELECT addedbyname, SUM(ttl) -
(SELECT SUM(worked)
FROM vwtimesheet
WHERE addedby=1
AND entrydate BETWEEN '2017-01-01' AND '2017-04-13'
AND activityid=3192
GROUP BY addedby) AS CurrentYearlyFlexiAvailable
,(SELECT SUM(worked) FROM vwtimesheet
WHERE addedby=1
AND entrydate BETWEEN '2017-01-01' AND '2017-04-13'
AND activityid=3192
GROUP BY addedby) AS flexiused
,(SELECT sum(worked) FROM vwtimesheet
WHERE addedby=1
AND entrydate BETWEEN DATE_FORMAT(NOW() ,'%Y-%m-01') AND curdate()
AND activityid=3192
GROUP BY addedby) as fleximonthused
FROM (SELECT entrydate,addedbyname,SUM(worked)-420 AS ttl FROM vwtimesheet
WHERE addedby=1
AND entrydate BETWEEN '2017-01-01' AND '2017-04-13'
AND projectid<>113
AND activityid<>3192
GROUP BY entrydate,addedbyname
HAVING SUM(worked)>420
) AS s
也許你應該問另外一個問題。或者至少認真編輯這一個。提供樣本數據,期望的結果,並描述您嘗試實施的邏輯。 –
在您的內部查詢中,您需要包含您正在應用「GROUP BY」的字段。我將嘗試更正您的代碼並在短時間內發佈答案。 – toonice
您可能會發現它有助於閱讀本https://meta.stackoverflow.com/questions/271055/tips-for-asking-a-good-structured-query-language-sql-question/271056#271056 –