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 2000 Forums
 Transact-SQL (2000)
 Retrieve only certain records

Author  Topic 

sqllearner
Aged Yak Warrior

639 Posts

Posted - 2004-06-11 : 16:15:19
Table 1

emp_id
1235
1246
1231


Table 2

emp_id address contact entry_time contacted
1235 werwer 56767 2003-02-14 10:29:00 Y
1235 yuiio 66767 2003-02-16 10:40:00 N
1235 vbnghf 89089 2003-02-16 10:40:00 Y
1246 qeqwre 76543 2003-02-19 10:29:00 Y
1246 vbnbvn 78902 2003-02-19 10:49:00 Y
1239 qwewer 56767 2003-02-14 10:29:00 Y
1231 jkllkj 56767 2003-02-14 10:29:00 Y
1242 qweert 56767 2003-02-14 10:29:00 Y

I have table 1 with the list of emp_id and table 2 with emp_id details records.Now
I want to retrieve the records from table 2 which has a match in table 1 and this conditions should be satisfied as well

1.Now I have certain emp_id repeated.
2.I want only the latest emp_id record with the latest entry time with contacted 'Y'

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-06-11 : 16:20:55
To get the matches between table1 and table2, use a JOIN. To get the latest emp_id record, use MAX(entry_time) with a GROUP BY and a WHERE clause for contacted 'Y'.

Tara
Go to Top of Page

sqllearner
Aged Yak Warrior

639 Posts

Posted - 2004-06-11 : 17:18:16
When I use group by the max(entry_time) iam getting this error.

Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-06-11 : 17:20:45
What is your query?

It would be easier if you posted your question with DDL and DML. DDL is CREATE TABLE statements for all tables, DML would INSERT INTO statements for your sample data. Then we would need to know what the expected result set would be using the sample data. We can quickly answer your questions when posted in this format as we can copy the information into Query Analyzer on our machines and test it out.

Tara
Go to Top of Page
   

- Advertisement -