Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8096377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:26:12+00:00 2026-06-05T21:26:12+00:00

I have two select queries to use in my project for getting data and

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T21:26:13+00:00Added an answer on June 5, 2026 at 9:26 pm

    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:

    /* prepare the storage */
    IF OBJECT_ID('tempdb..#sp_results') IS NOT NULL
      DROP TABLE #sp_results;
    CREATE TABLE #sp_results (
      /* the types may be wrong, please modify as necessary */
      currency_cd int,
      salary decimal(10,2)
    );
    
    /* save the results of the SP */
    INSERT INTO #sp_results
    EXEC SP_name arguments…
    
    /* use the saved results in your query */
    SELECT
      your present columns,
      tmp.currency_cd,
      tmp.salary
    FROM
      your present joins
      LEFT OUTER JOIN #sp_results AS tmp ON a proper join condition
    ;
    

    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_code column 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:

    CREATE VIEW employees_compensation_view
    AS
    SELECT
      employee_code,
      currency_cd,
      SUM(comprate) AS salary
    FROM employees_compensation
    GROUP BY
      employee_code,
      currency_cd
    

    You would then be able just to join the view to your query, like this:

    SELECT
      your present columns,
      ecv.currency_cd,
      ecv.salary
    FROM
      your present joins
      LEFT OUTER JOIN employees_compensation_view AS ecv
        ON b.employee_code = ecv.employee_code
    ;
    

    For better encapsulation of your business logic, you could also change the stored procedure query like this:

    SELECT
      currency_cd,
      salary
    FROM employees_compensation_view
    WHERE employee_code = @emp_code
    

    That is, if you still needed that SP.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two queries use [DatabaseAA] select * ,DATEDIFF(MINUTE,SomeDate,'3/28/2012 12:52:25 PM +00:00') This passes,
I have some different SELECT queries with same values. I want to use something
I have two queries that are basically the same: OLD TRANSACTIONS QUERY SELECT t.payment_method,
I want to execute two select queries in one go, but the data isn't
I have a small sql question for you. I have two queries: SELECT tbl_CostPrevisions.Month,
I have the following two queries: select count(*) from segmentation_cycle_recipients scr , segmentation_instance si
I have two queries, as following: SELECT SQL_CALC_FOUND_ROWS Id, Name FROM my_table WHERE Name
I have two mysql queries: $sql = SELECT * FROM content WHERE threadName LIKE
I have these two queries to test my output SELECT DISTINCT( providerId ), SUM((
I have a query to select from another sub-query select. While the two queries

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.