Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Look, i have a problem thats making my head explode.

lets start from database tables.
table 1: users (Im putting only the fields im using in the query)
Fields:
id
name
lastname

table 2: contracts
Fields:
user_id
department
salary
type /the type is 1=eventual or 2=permanent
status / 1=active, 2=inactive, 3=vacations

table 3: departments
Fields:
id
name


Ok. these are the tables i'm joining in the next query

i must say the a user can have multiples contracts but he can only have one active
and in the query thats what i do, i get only the active contract

SQL

SQL
SELECT * FROM users
   INNER JOIN contracts 
  ON contracts.id = (SELECT TOP 1 contracts.id FROM contracts 
       WHERE contracts.user_id = users.id AND (contracts.status=1 OR contracts.status=3) ORDER BY contracts.id DESC) 
  INNER JOIN departments  
  ON contracts.department = departments.id
WHERE contracts.type=2


What my query does is to get the permanent users with a active contract and the query works fine. for example: if i have 5 users but only two of them have one active contract the query returns the two rows.

VB
ds is a dataset where i have the rows returned from query
and this is where i send the rows to the report.
VB
crD.SetDataSource(ds.Tables(0))
                CrystalReportViewer1.RefreshReport()


as i said before the query returns only two rows of users
but when i open the report in the viewer it shows more data
for example

if one user in the database has 3 contracts but only one of them active the crystal reports show this user 3 times with his contracts no matter if the contracts are inactive. they shouldn be shown because the inactive contracts are not in the data returned by the query.

Can someone helpe me out with this..

What I have tried:

all i wrote in the description
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900