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 |
|
nt4vn
Yak Posting Veteran
98 Posts |
Posted - 2008-06-26 : 22:05:39
|
| How would I write a SQL that joins the following tables together. Below are primary key of those tables that would need to join.1. Table 1: table1.rootkey=table2.cc_file_no table1.corres_name <> '' max(table1.corres_create_dt) as corres_create_dt2. Table 2: table1.rootkey=table2.cc_file_no table2.estate_id=table4.estate_id3. Table 3: table3.case_id=table4.caseid table3.case_type_id=1 table3.case_type='P' 4. Table 4: table4.estate_id=table2.estate_id table4.acn_dln in ('101', '102', '201', '202')I think table 2, 3, 4 need to join toghether as a sub-query under table1. I tried to write the query for above tables, but I got syntax error...don't know how to solve.SELECTA.ROOTKEY,B.LAST_NAME,B.FIRST_NAME,A.CORRES_1ST_ADDR,A.CORRES_2ND_ADDR,A.CITY,A.STATE,A.ZIP_5,A.PHONE_NUM,REPLACE (REPLACE (REPLACE (REPLACE (REPLACE (REPLACE (REPLACE (C.CORRES_NAME, ' ESQ', ''), ' -ESQ', ''), ' ATTY', ''), ' -ATTY', ''), '.', ''), 'JR', ''), 'ATTORNEY', '') AS CORRES_NAME,MAX(CORRES_CREATE_DT) AS CORRES_CREATE_DTFROM DBA.ITCORRES AINNER JOIN DBA.ITGNINFO B ON B.ROOTKEY=B.ROOTKEYLEFT JOIN ( SELECT CC_FILE_NO, ESTATE_ID, E.CASE_ID, E.CASE_TYPE_ID, E.CALCULATED_TAX, FROM DBA.TKESTATE C INNER JOIN DBA.TKCASIT D ON D.ESTATE_ID=C.ESTATE_ID INNER JOIN DBA.TKCASE E ON E.CASE_ID=D.CASE_ID WHERE D.ACN_DLN IN ('101', '102', '201', '202') AND E.STATUS_ID <>2 AND E.CASE_TYPE_ID= 1 GROUP BY CC_FILE_NO, ESTATE_ID, E.CASE_ID, E.CALCULATED_TAX )ZZ ON ZZ.CC_FILE_NO=A.ROOTKEYWHERE A.CORRES_TYPE='P'AND A.CORRES_NAME <> ''ORDERY BY CORRES_NAME, CORRES_CREATE_DT |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-27 : 09:43:16
|
may be this:-SELECT * FROMFROM (SELECT rootkey,MAX(corres_create_dt) as corres_create_dt FROM table1 WHERE corres_name <> '' GROUP BY rootkey) t1INNER JOIN table2 t2ON t1.rootkey=t2.cc_file_noINNER JOIN table4 t4ON t2.estate_id=t4.estate_idINNER JOIN table3 t3ON t3.case_id=t4.caseidWHERE t3.case_type_id=1AND t3.case_type='P'AND t4.acn_dln in ('101', '102', '201', '202')replace * by actual columns you want. |
 |
|
|
nt4vn
Yak Posting Veteran
98 Posts |
Posted - 2008-06-27 : 11:04:27
|
quote: Originally posted by visakh16 may be this:-SELECT * FROMFROM (SELECT rootkey,MAX(corres_create_dt) as corres_create_dt FROM table1 WHERE corres_name <> '' GROUP BY rootkey) t1INNER JOIN table2 t2ON t1.rootkey=t2.cc_file_noINNER JOIN table4 t4ON t2.estate_id=t4.estate_idINNER JOIN table3 t3ON t3.case_id=t4.caseidWHERE t3.case_type_id=1AND t3.case_type='P'AND t4.acn_dln in ('101', '102', '201', '202')replace * by actual columns you want.
I tried this, but still get syntax error near to "FROM":SELECTT1.ROOTKEY,T5.LAST_NAME,T5.FIRST_NAME,CORRES_NAME,T1.CORRES_1ST_ADDR,T1.CORRES_2ND_ADDR,T1.CITY,T1.STATE,T1.ZIP_5,T1.PHONE_NUM,CORRES_CREATE_DTFROM (SELECT rootkey,MAX(corres_create_dt) as corres_create_dt, replace (replace (replace (replace (replace (replace (replace(replace(replace(replace(replace(C.CORRES_NAME, ' ESQ', ''),' ATTY', ''), '-ESQ', ''), '-ATTY',''),'.',''),',', ''),'JR', ''),'ATTORNEY', ''),'-',''), 'III', ''), '&', '')as CORRES_NAME,FROM DBA.table1WHERE corres_name <> ''AND CORRES_TYPE='P'GROUP BY rootkey) t1INNER JOIN DBA.table5 T5 ON T5.ROOTKEY=T1.ROOTKEYINNER JOIN DBA.table2 t2 ON t1.rootkey=t2.cc_file_noINNER JOIN DBA.table4 t4 ON t2.estate_id=t4.estate_idINNER JOIN DBA.table3 t3 ON t3.case_id=t4.caseidWHERE t3.case_type_id=1AND t3.case_type='P'AND t4.acn_dln in ('101', '102', '201', '202') |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-27 : 11:13:29
|
remove the duplicate from & trySELECT * FROMFROM (SELECT rootkey,MAX(corres_create_dt) as corres_create_dt FROM table1 WHERE corres_name <> '' GROUP BY rootkey) t1INNER JOIN table2 t2ON t1.rootkey=t2.cc_file_noINNER JOIN table4 t4ON t2.estate_id=t4.estate_idINNER JOIN table3 t3ON t3.case_id=t4.caseidWHERE t3.case_type_id=1AND t3.case_type='P'AND t4.acn_dln in ('101', '102', '201', '202') |
 |
|
|
nt4vn
Yak Posting Veteran
98 Posts |
Posted - 2008-06-27 : 11:27:29
|
quote: Originally posted by visakh16 remove the duplicate from & trySELECT * FROMFROM (SELECT rootkey,MAX(corres_create_dt) as corres_create_dt FROM table1 WHERE corres_name <> '' GROUP BY rootkey) t1INNER JOIN table2 t2ON t1.rootkey=t2.cc_file_noINNER JOIN table4 t4ON t2.estate_id=t4.estate_idINNER JOIN table3 t3ON t3.case_id=t4.caseidWHERE t3.case_type_id=1AND t3.case_type='P'AND t4.acn_dln in ('101', '102', '201', '202')
Still got an error for syntax...thanks, |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2008-06-27 : 11:57:19
|
| You need to close the parentheses and alias the tableSELECT * FROMFROM (SELECT rootkey,MAX(corres_create_dt) as corres_create_dt FROM table1 WHERE corres_name <> '' GROUP BY rootkey) t1INNER JOIN table2 t2ON t1.rootkey=t2.cc_file_noINNER JOIN table4 t4ON t2.estate_id=t4.estate_idINNER JOIN table3 t3ON t3.case_id=t4.caseidWHERE t3.case_type_id=1AND t3.case_type='P'AND t4.acn_dln in ('101', '102', '201', '202')) AS Table1Jim |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-27 : 12:03:32
|
quote: Originally posted by jimf You need to close the parentheses and alias the tableSELECT * FROMFROM (SELECT rootkey,MAX(corres_create_dt) as corres_create_dt FROM table1 WHERE corres_name <> '' GROUP BY rootkey) t1INNER JOIN table2 t2ON t1.rootkey=t2.cc_file_noINNER JOIN table4 t4ON t2.estate_id=t4.estate_idINNER JOIN table3 t3ON t3.case_id=t4.caseidWHERE t3.case_type_id=1AND t3.case_type='P'AND t4.acn_dln in ('101', '102', '201', '202')) AS Table1Jim
I've already done that. I think you missed it out |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-27 : 12:04:09
|
quote: Originally posted by nt4vn
quote: Originally posted by visakh16 remove the duplicate from & trySELECT * FROMFROM (SELECT rootkey,MAX(corres_create_dt) as corres_create_dt FROM table1 WHERE corres_name <> '' GROUP BY rootkey) t1INNER JOIN table2 t2ON t1.rootkey=t2.cc_file_noINNER JOIN table4 t4ON t2.estate_id=t4.estate_idINNER JOIN table3 t3ON t3.case_id=t4.caseidWHERE t3.case_type_id=1AND t3.case_type='P'AND t4.acn_dln in ('101', '102', '201', '202')
Still got an error for syntax...thanks,
whats the error? |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2008-06-27 : 12:20:30
|
| Oops! misread the query! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-27 : 12:37:10
|
quote: Originally posted by jimf Oops! misread the query!
No probs it happens |
 |
|
|
nt4vn
Yak Posting Veteran
98 Posts |
Posted - 2008-06-27 : 13:09:03
|
quote: Originally posted by visakh16
quote: Originally posted by nt4vn
quote: Originally posted by visakh16 remove the duplicate from & trySELECT * FROMFROM (SELECT rootkey,MAX(corres_create_dt) as corres_create_dt FROM table1 WHERE corres_name <> '' GROUP BY rootkey) t1INNER JOIN table2 t2ON t1.rootkey=t2.cc_file_noINNER JOIN table4 t4ON t2.estate_id=t4.estate_idINNER JOIN table3 t3ON t3.case_id=t4.caseidWHERE t3.case_type_id=1AND t3.case_type='P'AND t4.acn_dln in ('101', '102', '201', '202')
Still got an error for syntax...thanks,
whats the error?
The same syntax error "near a 'FROM. I don't see any wrong with that... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-27 : 13:13:01
|
quote: Originally posted by nt4vn
quote: Originally posted by visakh16
quote: Originally posted by nt4vn
quote: Originally posted by visakh16 remove the duplicate from & trySELECT * FROMFROM (SELECT rootkey,MAX(corres_create_dt) as corres_create_dt FROM table1 WHERE corres_name <> '' GROUP BY rootkey) t1INNER JOIN table2 t2ON t1.rootkey=t2.cc_file_noINNER JOIN table4 t4ON t2.estate_id=t4.estate_idINNER JOIN table3 t3ON t3.case_id=t4.caseidWHERE t3.case_type_id=1AND t3.case_type='P'AND t4.acn_dln in ('101', '102', '201', '202')
Still got an error for syntax...thanks,
whats the error?
The same syntax error "near a 'FROM. I don't see any wrong with that...
can i see full query used by you? i dont think error is in batch given. |
 |
|
|
nt4vn
Yak Posting Veteran
98 Posts |
Posted - 2008-06-27 : 13:45:20
|
quote: Originally posted by visakh16
quote: Originally posted by nt4vn
quote: Originally posted by visakh16
quote: Originally posted by nt4vn
quote: Originally posted by visakh16 remove the duplicate from & trySELECT * FROMFROM (SELECT rootkey,MAX(corres_create_dt) as corres_create_dt FROM table1 WHERE corres_name <> '' GROUP BY rootkey) t1INNER JOIN table2 t2ON t1.rootkey=t2.cc_file_noINNER JOIN table4 t4ON t2.estate_id=t4.estate_idINNER JOIN table3 t3ON t3.case_id=t4.caseidWHERE t3.case_type_id=1AND t3.case_type='P'AND t4.acn_dln in ('101', '102', '201', '202')
Still got an error for syntax...thanks,
whats the error?
The same syntax error "near a 'FROM. I don't see any wrong with that...
can i see full query used by you? i dont think error is in batch given.
---------SELECTT1.ROOTKEY,T5.LAST_NAME,T5.FIRST_NAME,CORRES_NAME,T1.CORRES_1ST_ADDR,T1.CORRES_2ND_ADDR,T1.CITY,T1.STATE,T1.ZIP_5,T1.PHONE_NUM,CORRES_CREATE_DTFROM (SELECT rootkey,MAX(corres_create_dt) as corres_create_dt,replace (replace (replace (replace (replace (replace (replace(replace(replace(replace(replace(C.CORRES_NAME, ' ESQ', ''),' ATTY', ''), '-ESQ', ''), '-ATTY',''),'.',''),',', ''),'JR', ''),'ATTORNEY', ''),'-',''), 'III', ''), '&', '') as CORRES_NAME,FROM DBA.TABLE1 WHERE corres_name <> ''AND CORRES_TYPE='P'GROUP BY rootkey) t1INNER JOIN DBA.TABLE5 T5 ON T5.ROOTKEY=T1.ROOTKEYINNER JOIN DBA.TABLE2 t2 ON t1.rootkey=t2.cc_file_noINNER JOIN DBA.TABLE4 t4 ON t2.estate_id=t4.estate_idINNER JOIN DBA.TABLE3 t3 ON t3.case_id=t4.caseidWHERE t3.case_type_id=1AND t3.case_type='P'AND t4.acn_dln in ('101', '102', '201', '202') |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-28 : 02:13:58
|
quote: Originally posted by nt4vn
quote: Originally posted by visakh16
quote: Originally posted by nt4vn
quote: Originally posted by visakh16
quote: Originally posted by nt4vn
quote: Originally posted by visakh16 remove the duplicate from & trySELECT * FROMFROM (SELECT rootkey,MAX(corres_create_dt) as corres_create_dt FROM table1 WHERE corres_name <> '' GROUP BY rootkey) t1INNER JOIN table2 t2ON t1.rootkey=t2.cc_file_noINNER JOIN table4 t4ON t2.estate_id=t4.estate_idINNER JOIN table3 t3ON t3.case_id=t4.caseidWHERE t3.case_type_id=1AND t3.case_type='P'AND t4.acn_dln in ('101', '102', '201', '202')
Still got an error for syntax...thanks,
whats the error?
The same syntax error "near a 'FROM. I don't see any wrong with that...
can i see full query used by you? i dont think error is in batch given.
---------SELECTT1.ROOTKEY,T5.LAST_NAME,T5.FIRST_NAME,CORRES_NAME,T1.CORRES_1ST_ADDR,T1.CORRES_2ND_ADDR,T1.CITY,T1.STATE,T1.ZIP_5,T1.PHONE_NUM,CORRES_CREATE_DTFROM (SELECT rootkey,MAX(corres_create_dt) as corres_create_dt,replace (replace (replace (replace (replace (replace (replace(replace(replace(replace(replace(C.CORRES_NAME, ' ESQ', ''),' ATTY', ''), '-ESQ', ''), '-ATTY',''),'.',''),',', ''),'JR', ''),'ATTORNEY', ''),'-',''), 'III', ''), '&', '') as CORRES_NAME,FROM DBA.TABLE1 WHERE corres_name <> ''AND CORRES_TYPE='P'GROUP BY rootkey) t1INNER JOIN DBA.TABLE5 T5 ON T5.ROOTKEY=T1.ROOTKEYINNER JOIN DBA.TABLE2 t2 ON t1.rootkey=t2.cc_file_noINNER JOIN DBA.TABLE4 t4 ON t2.estate_id=t4.estate_idINNER JOIN DBA.TABLE3 t3 ON t3.case_id=t4.caseidWHERE t3.case_type_id=1AND t3.case_type='P'AND t4.acn_dln in ('101', '102', '201', '202')
Couple of errors. You dont need , before FROM which is syntactically wrong. ALso you're you are selecting CORRES_NAME in select list after grouping by rootkey. This is not possible unless you apply some aggregate function on CORRES_NAME like MIN or MAX or include it also in your group by. the decision depends on your requirement. If you want help on it please post what exactly are you trying to get inside the subquery. |
 |
|
|
|
|
|
|
|