| Author |
Topic |
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2007-02-14 : 09:58:22
|
| Please help me in writing the query. Thanks for the help in advance..Table1------accid state County cp----- ----- ----- ---804102 AR 338 TA804101 AZ 339 TBtable2------accid item1 item2 PE----- ----- ----- ---804102 11 03 1804102 01 04 1804101 11 03 0 Output:accid state County cp item1 item2 PE----- ----- ----- --- ----- ----- ---804102 AR 338 TA 11 03 0804101 AZ 339 TB 01 04 1 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-02-14 : 10:00:37
|
1.what is your criteria for choosing804102 11 03 1 and not 804102 01 04 1 2.How do you get PE = 0 for 804102 ? KH |
 |
|
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2007-02-14 : 10:14:03
|
| Oops..The criteria is selecting the max of createddate....Table 2-------accid item1 item2 PE createddate ----- ----- ----- --- -----------804102 11 03 1 2007-02-02 14:13:43.000804102 01 04 1 2007-02-02 14:13:31.000 804101 11 03 0 2007-02-02 14:13:31.000I need the Output:accid state County cp item1 item2 PE----- ----- ----- --- ----- ----- ---804102 AR 338 TA 11 03 1804101 AZ 339 TB 11 03 0 |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-02-14 : 10:34:51
|
[code]declare @Table1 table( accid int, state varchar(2), County int, cp varchar(2))insert into @Table1select 804102, 'AR', 338, 'TA' union allselect 804101, 'AZ', 339, 'TB'declare @Table2 table( accid int, item1 varchar(2), item2 varchar(2), PE int, createddate datetime)insert into @Table2select 804102, '11', '03', 1, '2007-02-02 14:13:43.000' union allselect 804102, '01', '04', 1, '2007-02-02 14:13:31.000' union allselect 804101, '11', '03', 0, '2007-02-02 14:13:31.000'select t1.accid, t1.state, t1.County, t1.cp, t2.item1, t2.item2, t2.PEfrom @Table1 t1 inner join @Table2 t2 on t1.accid = t2.accid and t2.createddate = (select max(createddate) from @Table2 x where x.accid = t2.accid)[/code] KH |
 |
|
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2007-02-15 : 11:58:45
|
| Thanks for your help KH!My requirements has changed.declare @Table1 table( accid int, sid bigint, state varchar(2), County int, cp varchar(2))insert into @Table1select 804102,103, 'AR', 338, 'TA' union allselect 804101,110, 'AZ', 339, 'TB' union allselect 804102,106, 'AR', 338, 'TA' declare @Table2 table( accid int, sid bigint, item1 varchar(2), item2 varchar(2), PE int, createddate datetime)insert into @Table2select 804102,103, '11', '03', 1, '2007-02-02 14:13:43.000' union allselect 804102,106, '01', '04', 2, '2007-02-02 14:13:31.000' union allselect 804101,110, '11', '03', 0, '2007-02-02 14:13:31.000'select t1.accid,t2.sid, t1.state, t1.County, t1.cp, t2.item1, t2.item2, t2.PE,t2.createddatefrom @Table1 t1 inner join @Table2 t2 on t1.accid = t2.accid and t2.createddate = (select max(createddate) from @Table2 x where x.accid = t2.accid)Output:accid sid state County cp item1 item2 PE createddate ----------- -------------------- ----- ----------- ---- ----- ----- ----------- -----------------------804102 103 AR 338 TA 11 03 1 2007-02-02 14:13:43.000804101 110 AZ 339 TB 11 03 0 2007-02-02 14:13:31.000 |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-02-15 : 17:19:31
|
this ?select distinctt1.accid, t2.sid, t1.state, t1.County, t1.cp, t2.item1, t2.item2, t2.PE, t2.createddatefrom @Table1 t1 inner join @Table2 t2on t1.accid = t2.accidand t2.createddate = (select max(createddate) from @Table2 x where x.accid = t2.accid) if not, please explain what is your new requirement. KH |
 |
|
|
sqldba2k6
Posting Yak Master
176 Posts |
Posted - 2007-02-16 : 09:14:54
|
| Thanks thanks for your help..My reqs are prod2 table can have two rows with different Csid's for the Custid.I should only pull the latest created_dt record from @prod table.Finally i am able to get the results with the below querydeclare @Cust1 table( Custid int, Csid bigint, name1 varchar(5), state varchar(4), zip varchar(8))insert into @Cust1select 4102,103, 'Sam', 'NY', '2356' union allselect 4101,110, 'Pat', 'CA', '9526' union allselect 4102,106, 'Rob', 'NY', '2356' declare @Prod2 table( Csid bigint, Prod1 varchar(4), Prod2 varchar(4), EPRT int, createddate datetime)insert into @Prod2select 103, '11', '03', 1, '2007-02-02 14:13:43.000' union allselect 106, '01', '04', 2, '2007-02-02 14:13:31.000' union allselect 110, '11', '03', 0, '2007-02-02 14:13:31.000'--With the below query i am able to pull the desired results..select t1.Custid,t2.csid, t1.state, t1.name1, t1.zip, t2.prod1, t2.prod2, t2.EPRT,t2.createddatefrom @Cust1 t1 inner join @Prod2 t2 on t1.Csid = t2.Csid inner join(select custid, max(createddate) as CreatedDatefrom @cust1 a inner join @prod2 bon a.csid = b.csidgroup by a.custid) t3on t1.Custid = t3.Custidand t2.CreatedDate = t3.CreatedDate |
 |
|
|
|
|
|