Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 I am having problem displaying data correctly.

Author  Topic 

simflex
Constraint Violating Yak Guru

327 Posts

Posted - 2012-11-23 : 18:04:01
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

52326 Posts

Posted - 2012-11-25 : 09:45:09
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

327 Posts

Posted - 2012-11-25 : 10:18:49
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

52326 Posts

Posted - 2012-11-25 : 11:16:49
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

327 Posts

Posted - 2012-11-25 : 12:34:54
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
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2012-11-26 : 00:29:37
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

327 Posts

Posted - 2012-11-26 : 08:53:06
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

52326 Posts

Posted - 2012-12-02 : 02:36:15
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
   

- Advertisement -