arianhojat Posted June 21, 2006 Share Posted June 21, 2006 Hello,had a specific SQL Query question.have rows like thisscanID | status | empID 1 1 80 1 2 221 3 34 3791 4 222 1 732 2 222 3 92 379now i want to return all scanIDs in stage 3... which is done with..."WHERE scanID in (SELECT scanID as SumStatus FROM scan_status sls WHERE sls.status=3 )"return their sum of what status they are in... which is done with..."SUM(status) as SumStatus" and "GROUP BY scanID"so far query looks like:[code]SELECT scanID, SUM(status) as SumStatusFROM scan_status slsWHERE scanID in (SELECT scanID as SumStatus FROM scan_status sls WHERE sls.status=3 )GROUP BY scanID[/code]now this will bring back this dataset:scanID | SumStatus1 10 2 6Now i want to add one more bit of info. In the row that has the stage 3, has info on who authored the document in this 'status stage' and also scanned it (which is usually same person but still could be different), hence the 2 numbers. I would like to take this 2nd number of who scanned it, and show their fullname by joining it with employee table... but since its a Group By Query, the info for 3rd stage is lost.Not sure, what to do. heres an example of how i would guess it to be done, but looks inefficient and... doesnt work hehe.[code]SELECT scanID, SUM(status) as SumStatus, ( SELECT concat(e.emplname , ', ', e.empfname) as empnameFROM intranet.employee e WHERE e.ID=(SELECT SUBSTRING( sls.empID , (LOCATE(' ', sss.empID)+1) ) FROM scan_status sss WHERE sss.status=3 AND sss.scanID=???getCurrentIDFromRowBeingLookedAt???) )WHERE scanID in (SELECT scanID as SumStatus FROM scan_status sls WHERE sls.status=3 )GROUP BY scanID[/code]and if anyone has better ideas just let me know, at worst i can always like do a second lookup in my php code (by getting the scanID while looping rows in php, and then look up up that '2nd name' in employee table) Quote Link to comment https://forums.phpfreaks.com/topic/12544-question-about-a-sql-query-i-am-making/ Share on other sites More sharing options...
arianhojat Posted June 21, 2006 Author Share Posted June 21, 2006 yay actually figured a way to do it without 2nd query.added this...(SELECT SUBSTRING(sss.empID , (LOCATE(' ', sss.empID)+1)) FROM scan_status sss WHERE sss.scanID=sls.scanID AND sss.status=3)=e.ID[code]SELECT scanID, c.Cltsort, SUM(status) as SumStatus, concat(e.emplname , ', ', e.empfname) as empnameFROM scan_status slsINNER JOIN scanlog sl ON sls.scanID=sl.IDINNER JOIN intranet.employee e ON(SELECT SUBSTRING(sss.empID , (LOCATE(' ', sss.empID)+1)) FROM scan_status sss WHERE sss.scanID=sls.scanID AND sss.status=3)=e.IDINNER JOIN intranet.clients c ON sl.ClientID=c.IDWHEREscanID in (SELECT scanID as SumStatus FROM scan_status sls WHERE sls.status=3 )GROUP BY scanID[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12544-question-about-a-sql-query-i-am-making/#findComment-48060 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.