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

  • SEARCH
  • Home
  • 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 634199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:16:05+00:00 2026-05-13T20:16:05+00:00

Here is my select statement with the innerjoin of two tables, if not exists(select

  • 0

Here is my select statement with the innerjoin of two tables,

if not exists(select EmpId from SalaryDetails 
              where EmpId in (select Emp_Id 
                              from Employee where Desig_Id=@CategoryId))
begin
  // some statements here
end
else
begin
  SELECT e.Emp_Id, e.Identity_No, e.Emp_Name,
      case WHEN e.SalaryBasis=1 THEN 'Weekly' 
           ELSE 'Monthly' end as SalaryBasis,e.FixedSalary,
      (SELECT TOP 1 RemainingAdvance 
       FROM SalaryDetails 
       ORDER BY CreatedDate DESC) as Advance
    FROM Employee as e inner join Designation as d on e.Desig_Id=d.Desig_Id 
    INNER JOIN SalaryDetails as S on e.Emp_Id=S.EmpId 
End

My results pane,

alt text http://img220.imageshack.us/img220/7774/resultpane.jpg

And My SalaryDetails Table,

alt text http://img28.imageshack.us/img28/770/salarydettable.jpg

EDIT:
My Output must be,

16 CR14 Natarajan Weekly 150.00
354.00
17 cr12333 Pandian Weekly 122.00 0.00

  • 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-05-13T20:16:05+00:00Added an answer on May 13, 2026 at 8:16 pm

    It looks like your join to Designation isn’t even used and you’re also missing your WHERE clause that you used in the IF statement at the top. I’d also move the subquery down into the join like Andy pointed out. Without having the DB to test against this probably won’t be exact but I’d rewrite it to something like;

    SELECT e.Emp_Id, e.Identity_No, e.Emp_Name,
      case WHEN e.SalaryBasis=1 
           THEN 'Weekly' 
           ELSE 'Monthly' end as SalaryBasis,
      e.FixedSalary,S.RemainingAdvance as Advance
    FROM Employee as e 
      INNER JOIN (
       SELECT TOP 1 EmpId, RemainingAdvance 
       FROM SalaryDetails 
       ORDER BY CreatedDate DESC) as S on e.Emp_Id=S.EmpId 
    WHERE e.Desig_Id=@CategoryId
    

    Andy’s suggestion to move the subquery into a view is a good one, much easier to read and probably a lot more efficient if the DB is large.

    EDIT: (ANSWER)

    (SELECT sd.empid,
                   sd.remainingadvance,
                   ROW_NUMBER() OVER (PARTITION BY sd.empid ORDER BY sd.createddate DESC) AS rank
              FROM SALARYDETAILS sd
              JOIN EMPLOYEE e ON e.emp_id = sd.empid
                             AND e.desig_id = @CategoryId) s
                WHERE s.rank = 1
    

    I edited jay’s answer because he came close to my output…

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

Sidebar

Related Questions

Here's my current SQL statement: SEARCH_ALBUMS_SQL = SELECT * FROM albums WHERE title LIKE
Here is my SQL Statement which is not returning DISTINCT Thread Titles. SELECT DISTINCT
I'm joining two tables - Contact and RetailTrainingUserLevelMap in a Select statement. (The common
So here is the original query SELECT SUM(PickQty), SUM(ReqQty), AssignmentID, StopID FROM PickRequest GROUP
Here's my explain plan: SELECT STATEMENT, GOAL = ALL_ROWS 244492 4525870 235345240 SORT ORDER
Here's an example of the query I'm trying to convert to LINQ: SELECT *
So firstly here's my query: ( NOTE:I know SELECT * is bad practice I
What am I missing here? I want to do a simple call to Select()
I have two tables as follows: tblCountry (countryID, countryCode) tblProjectCountry(ProjectID, countryID) The tblCountry table
Here's the query: SELECT COUNT(*) AS c, MAX(`followers_count`) AS max_fc, MIN(`followers_count`) AS min_fc, MAX(`following_count`)

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.