I have two select queries to use in my project for getting data and displaying it in my web page. I want to join them so that I need to access data only once. But I don’t know how to join them as one of the query contains group function. Could you help me? Thanks in Advance.
Share
One way to solve this would be to do the job in several steps. Before joining the result set to the query, you’ll need to save it in a temporary table or table variable. (A temporary table may be preferable if you are expecting a large result set.)
Basically, your query would change to something like this:
The problem is, though, your SP is designed to return results for an individual employee while the query seems to be retrieving data for multiple employees. To resolve the issue, you could add an
employee_codecolumn to the temporary table and get/save the results for every employee one by one, in a cursor, using a loop.But that would probably be one of the worst examples of using a cursor. A better alternative might be to create a view returning the same data as your SP, but for all employees:
You would then be able just to join the view to your query, like this:
For better encapsulation of your business logic, you could also change the stored procedure query like this:
That is, if you still needed that SP.