SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 I am having problem displaying data correctly.
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

simflex
Constraint Violating Yak Guru

307 Posts

Posted - 11/23/2012 :  18:04:01  Show Profile  Reply with Quote
Greetings all,

Per record this query below, if you run it as is, it displays records where Active is only Yes and each department sees its own department records for a given year (date_timestamp).

The issue we are having is that we would like to display all records whether active is yes or No.

The active fieldname is on the table called tbl_emp.

Any ideas how to modify this query?

Thanks a lot in advance.

SELECT distinct tbl_emp.lastname, tbl_emp.firstname, CASE active WHEN 1 then 'Yes'WHEN 0 then 'No' else 'other'END
FROM (dept_table INNER JOIN ((contributions INNER JOIN charity_table ON contributions.charity_code = charity_table.charity_code) 
                 INNER JOIN tbl_emp ON contributions.employee_ID = tbl_emp.employee_id) ON dept_table.dept_code = tbl_emp.dept_code) 
WHERE Year(contributions.date_stamp)=Year(getDate())
GROUP BY tbl_emp.lastname, tbl_emp.firstname, tbl_emp.employee_id, dept_table.dept_name, charity_table.ded_code,active;

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 11/25/2012 :  09:45:09  Show Profile  Reply with Quote
As per posted code you're not applying any filter based on active field so it should list records with all values of active field not only 1. It may be that you dont have records existing in table for current year with active <> 1 and also having matches in other tables used in the join

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

simflex
Constraint Violating Yak Guru

307 Posts

Posted - 11/25/2012 :  10:18:49  Show Profile  Reply with Quote
I think you are correct but how come LEFT JOIN didn't work?

A LEFT JOIN to return those that match and everything in tbl_emp that didn't match.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 11/25/2012 :  11:16:49  Show Profile  Reply with Quote
where's left join? i can see only INNER JOINs in your posted query

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

simflex
Constraint Violating Yak Guru

307 Posts

Posted - 11/25/2012 :  12:34:54  Show Profile  Reply with Quote
No, I didn't post it here.

I was merely indicating that I had tried lEFT JOIN on this line:

INNER JOIN tbl_emp ON contributions.employee_ID = tbl_emp.employee_id)

before but it didn't change the result outcome.

Active fieldname is on tbl_emp. Am I doing the join incorrectly?
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2168 Posts

Posted - 11/26/2012 :  00:29:37  Show Profile  Reply with Quote
Try this...........


SELECT distinct tbl_emp.lastname, tbl_emp.firstname, 
	CASE active WHEN 1 then 'Yes'WHEN 0 then 'No' else 'other'END
FROM contributions c
INNER JOIN charity_table ct ON c.charity_code = ct.charity_code
LEFT JOIN tbl_emp e ON e.employee_id = c.employee_id
INNER JOIN dept_table d ON d.dept_code = e.dept_code
WHERE Year(c.date_stamp)=Year(getDate())
GROUP BY e.lastname, e.firstname, e.employee_id, d.dept_name, ct.ded_code, active;


--
Chandu
Go to Top of Page

simflex
Constraint Violating Yak Guru

307 Posts

Posted - 11/26/2012 :  08:53:06  Show Profile  Reply with Quote
Thank you Bandi,

That's exactly how I did the LEFT JOIN earlier.

This time, however, I decided to remove the WHERE clause:

WHERE Year(c.date_stamp)=Year(getDate()) and it worked.

Also, when I used the WHERE clause for a previous year

WHERE Year(c.date_stamp)='2011', it still worked.

The question is why isn't it working when date_stamp = current year?

After all, the idea of LEFT JOIN is to show what matter between 2 tables, and everything else that didn't match on the left table, no?

In this case, show what matched between various joins and everything else on the tbl_emp table that didn't match.

I am really baffled.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 12/02/2012 :  02:36:15  Show Profile  Reply with Quote
quote:
Originally posted by simflex

Thank you Bandi,

That's exactly how I did the LEFT JOIN earlier.

This time, however, I decided to remove the WHERE clause:

WHERE Year(c.date_stamp)=Year(getDate()) and it worked.

Also, when I used the WHERE clause for a previous year

WHERE Year(c.date_stamp)='2011', it still worked.

The question is why isn't it working when date_stamp = current year?

After all, the idea of LEFT JOIN is to show what matter between 2 tables, and everything else that didn't match on the left table, no?

In this case, show what matched between various joins and everything else on the tbl_emp table that didn't match.

I am really baffled.


i doubt whether the order of tables in LEFT JOIN where correct otherwise this may be the reason

http://weblogs.sqlteam.com/jeffs/archive/2007/05/14/criteria-on-outer-joined-tables.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000