|
|
Question : Returning Max Value T-SQL
|
|
Hi, I have a question based on finding the max entry into a certin table. This seemed obvious at first and i tried the usually, max function in the select statement, subquery in the From /Where/Select statemenet to get the max.however this is not working for me. I have 3 tables. Table A hold the account no. Table b holds job id's and Table c holds jobs detail. My scenario is as follow: One Account holder has 2 jobs scheduled against it and there for two related rows in the Job Details Table. For example Account 12345 has two jobs (258369,369258) and two row of information on jobdetial table. I have created tables as examples below : --Table A CREATE TABLE ACCOUNT ([account_id] [int] NOT NULL); insert into [account]([account_id])values(12345); --Table B CREATE TABLE Job ([account_id] [int] NOT NULL, [job_id] [int] NOT NULL, [date_entered][datetime]NOT NULL); insert into [Job] ([account_id],[job_id],[date_entered]) values (12345,258369,'2007-01-30'); insert into [Job] ([account_id],[job_id],[date_entered]) values (12345,369258,'2007-04-15'); --Table C CREATE TABLE JobDetail ([job_id] [int] NOT NULL, [au_snr] [varchar](50) NOT NULL); insert into [JobDetail] ([job_id],[au_snr]) values (369258,78); insert into [JobDetail] ([job_id],[au_snr]) values (258369,0);
The result i want is to see is: Account 12345, with max id 369258, max date(2007-04-15), jobdetial (78) I would appriciate any help. Kind Regards, MickyNin
|
Answer : Returning Max Value T-SQL
|
|
maybe this one will do:
thanks for posting the create script for the table it is much appreciated!
select
job.account_id , job.job_id , job.date_entered , au_snr
from job job , account acc , jobDetail jdt
where job.account_id = acc.account_id and job.date_entered = ( select max( date_entered ) from job chk where chk.account_id = acc.account_id )
and
job.job_id = jdt.job_id
|
|
|
|
|