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.
| Author |
Topic |
|
dazz_789
Starting Member
2 Posts |
Posted - 2012-05-29 : 20:21:27
|
| Hi, I have data as follows:owning_acct_no SERV_NO PRODUCT_CODE INST_DATE PROD_STATUS960410282500 4450062 322 11/17/2001 Z960410282500 4450062 322 12/07/2011 W960410282500 4450062 335 10/12/2003 X960410282500 4450062 335 09/06/2011 A960410282500 4450065 332 05/11/2010 W960410282500 4450065 332 01/22/2012 XAnd I want to get the max effected date as follows:owning_acct_no SERV_NO PRODUCT_CODE INST_DATE PROD_STATUS960410282500 4450062 322 12/07/2011 W960410282500 4450062 335 09/06/2011 A960410282500 4450065 332 01/22/2012 XThe following sql just gives me the 3 lines above without the PROD_STATUS:select cp.owning_acct_no,cp.serv_no, cp.product_code ,MAX(cp.inst_date) as dtfrom cis_prod.cst_customer_product cpwhere cp.owning_acct_no ='960410282500' and cp.product_code in (322, 335)group by cp.owning_acct_no,cp.serv_no,cp.product_codeWhat's the best way to bring in the PROD_STATUS to the results?Thanks! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-29 : 21:15:46
|
add it as join to main tableSELECT t.owning_acct_no,t.SERV_NO,t.PRODUCT_CODE,t.INST_DATE,t.PROD_STATUSFROM cis_prod.cst_customer_product tINNER JOIN(select cp.owning_acct_no,cp.serv_no, cp.product_code ,MAX(cp.inst_date) as dtfrom cis_prod.cst_customer_product cpwhere cp.owning_acct_no ='960410282500' and cp.product_code in (322, 335)group by cp.owning_acct_no,cp.serv_no,cp.product_code)t1ON t1.owning_acct_no = t.owning_acct_no AND t1.SERV_NO = t.SERV_NO AND t1.PRODUCT_CODE = t.PRODUCT_CODE AND t1.dt = t.INST_DATE ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
dazz_789
Starting Member
2 Posts |
Posted - 2012-05-30 : 00:45:28
|
| Hi, this works great, thanks! I was actually trying to apply this to the code below because the cst_customer_product table contains multiple rows for each owning_acct_no, serv_no, product_code combination, but I only want it to select the rows with the max instillation datee.g.owning_acct_no, serv_no, product_code inst_date cust_prod_status105036590000 4440270 8641 4/9/2009 W105036590000 4440270 8641 11/21/2000 ZSo I would only want the first row to be extracted in my query results below. Cay you say how I can update the query below.. it returns errors when I try it..Thanks again!select cp.product_code, sp.product_description, cp.unique_serv_id, cp.serv_no,cp.owning_acct_no, cp.personal_rental, cp.cust_prod_no,cp.inst_date, cp.price_var_exist, cp.cust_prod_status, nm.first_name, nm.surname, nm.company_name,fpr.fpr_bill_year, fpr.fpr_bill_month, fpr.fpr_rental_charge from cis_prod.cst_customer_product as cpleft outerjoin cis_prod.cst_name as nmon cp.owning_acct_no = nm.acct_noleft outer join cis_prod.sys_product as spon cp.product_code = sp.product_codeleft outer join (select * from cis_prod.cst_full_period_rentals fp where fp.fpr_bill_year = '2012' and fp.fpr_bill_month = '02' ) fpr on cp.owning_acct_no = fpr.fpr_owning_acct_no and cp.cust_prod_no = fpr.fpr_cust_prod_no and cp.product_code = fpr.fpr_product_codewhere (cp.product_code between 8640 and 8649 or cp.product_code between 8660 and 8669 ) andnm.stop_time_stamp = '31-DEC-9999' |
 |
|
|
|
|
|
|
|