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 |
|
baaul
Starting Member
16 Posts |
Posted - 2006-10-24 : 16:35:54
|
| Hi,I need to INSERT rows into #final table from #tmp with groups of 'grp_code' and 'bap_id' combination rows.Create Table #tmp ( sort_id int default 0, grp_Code int default 1, bap_id INT, value1_tx varchar(500) default NULL, value2_tx varchar(500) default NULL)Data in the #tmp table:Sort_id grp_code bap_id Value1_tx Value2_tx1 1 1 Alabama NULL1 1 1 Alaska NULL1 1 1 Arizona NULL1 1 1 California NULL2 1 2 Alaska NULL2 1 2 Arizona NULL2 1 2 Arkansas NULL2 1 2 California NULL3 1 3 Alaska NULL3 1 3 Arizona NULL3 1 3 Arkansas NULL1 2 1 ACS Image Solutions NULL1 2 1 ABC Virtual Communications, Inc NULL1 2 1 ACS Unclaimed Propery Clearinghouse, Inc. NULL1 2 1 ABC Virtual Communications, Inc NULL1 2 1 ABC Virtual Communications, Inc NULL1 2 1 Acxiom NULL1 2 1 ABC Virtual Communications, Inc NULL2 2 2 ABC Virtual Communications, Inc NULL2 2 2 ACS Unclaimed Propery Clearinghouse, Inc. NULL2 2 2 #NAME? NULL2 2 2 ACS Image Solutions NULL2 2 2 Acxiom NULL2 2 2 ADP NULL2 2 2 ADP Investor Communication NULL2 2 2 Bancomer Transfer Services NULL3 2 3 ABC Virtual Communications, Inc NULL3 2 3 ACS Image Solutions NULL3 2 3 ACS Unclaimed Propery Clearinghouse, Inc. NULL3 2 3 Acxiom NULL3 2 3 ABC Virtual Communications, Inc NULL3 2 3 ACS Image Solutions NULL1 3 1 STATE AND LOCAL LAWS AND REGULATIONS Insurance Laws & Regulations - Various States1 3 1 STATE AND LOCAL LAWS AND REGULATIONS Escheat Laws1 3 1 STATE AND LOCAL LAWS AND REGULATIONS State Credit Law Requirements1 3 1 STATE AND LOCAL LAWS AND REGULATIONS State Deceptive Practices Laws1 3 1 OTHER FEDERAL LAWS AND REGULATIONS American's with Disability Act Public Law 101-336 - Abstract2 3 2 OTHER FEDERAL LAWS AND REGULATIONS Bankruptcy Laws2 3 2 OTHER FEDERAL LAWS AND REGULATIONS COBRA2 3 2 OTHER FEDERAL LAWS AND REGULATIONS Department of Labor RulesCreate Table #final ( sort_id INT, bap_id INT, geography varchar(120) NULL, vendor_nm varchar(250) NULL, regulator varchar(100) NULL, regulation varchar(500) NULL)This is how it needs to be inserted into #final table:rows from value1_tx inserted into Column 'geography' WHERE grp_code =1 and bap_id = 1 rows from value1_tx inserted into Column 'vendor_nm' WHERE grp_code =2 and bap_id = 1 rows from value1_tx inserted into Column 'regulator'WHERE grp_code =3 and bap_id = 1 rows from value2_tx inserted into Column 'regulation' WHERE grp_code =3 and bap_id = 1 |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2006-10-24 : 17:33:35
|
| [code]INSERT #finalSELECT sort_id, bap_id, value1_tx, NULL, NULL, NULLFROM #tmpWHERE grp_code =1 and bap_id = 1 INSERT #finalSELECT sort_id, bap_id, NULL, value1_tx, NULL, NULLFROM #tmpWHERE grp_code =2 and bap_id = 1[/code] Not sure for the last one if you want separate rows or single rowsThese two for separate[code]INSERT #finalSELECT sort_id, bap_id, NULL, NULL, value1_tx, NULLFROM #tmpWHERE grp_code =3 and bap_id = 1 INSERT #finalSELECT sort_id, bap_id, NULL, NULL, NULL, value2_txFROM #tmpWHERE grp_code =3 and bap_id = 1[/code]This one for single[code]INSERT #finalSELECT sort_id, bap_id, NULL, NULL, value1_tx, value2_txFROM #tmpWHERE grp_code =3 and bap_id = 1[/code] |
 |
|
|
baaul
Starting Member
16 Posts |
Posted - 2006-10-24 : 18:30:06
|
| Thanks for replying so quickly. Currently, after the Insert I'm getting the data in #final table as:Sort_id bap_id Vendor_nm Vendor_nm Regulator Regulation 1 1 Alabama NULL NULL NULL 1 1 Alaska NULL NULL NULL 1 1 Arizona NULL NULL NULL 1 1 California NULL NULL NULL 1 1 NULL ACS Image Solutions NULL NULL 1 1 NULL ABC Virtual Communications, Inc NULL NULL 1 1 NULL ACS Unclaimed Propery Clearinghouse, Inc. NULL NULL 1 1 NULL ABC Virtual Communications, Inc NULL NULL 1 1 NULL ABC Virtual Communications, Inc NULL NULL 1 1 NULL Acxiom NULL NULL 1 1 NULL ABC Virtual Communications, Inc NULL NULL 1 1 NULL NULL FEDERAL RESERVE REGULATIONS Reg. AA - 12 CFR 227 - Unfair or Deceptive Acts 1 1 NULL NULL FEDERAL RESERVE REGULATIONS Reg. B - 12 CFR 202 - Equal Credit Opportunity 1 1 NULL NULL FEDERAL RESERVE REGULATIONS Reg. B - 12 CFR 202 - Equal Credit Opportunity ===================================================However, I'd like the 'vendor_nm', 'regulator' and 'regulation' groups moved up to the top. Below is the desired result.----------------------------------------------------------Sort_id bap_id Vendor_nm Vendor_nm Regulator Regulation 1 1 Alabama ACS Image Solutions FEDERAL RESERVE REGULATIONS Reg. AA - 12 CFR 227 - Unfair or Deceptive Acts1 1 Alaska ABC Virtual Communications, Inc FEDERAL RESERVE REGULATIONS Reg. B - 12 CFR 202 - Equal Credit Opportunity1 1 Arizona ACS Unclaimed Propery Clearinghouse, Inc. FEDERAL RESERVE REGULATIONS Reg. B - 12 CFR 202 - Equal Credit Opportunity1 1 California ABC Virtual Communications, Inc NULL NULL1 1 NULL ABC Virtual Communications, Inc NULL NULL1 1 NULL Acxiom NULL NULL1 1 NULL ABC Virtual Communications, Inc NULL NULL1 1 NULL NULL NULL NULL1 1 NULL NULL NULL NULL1 1 NULL NULL NULL NULL1 1 NULL NULL NULL NULL1 1 NULL NULL NULL NULL1 1 NULL NULL NULL NULL1 1 NULL NULL NULL NULL |
 |
|
|
baaul
Starting Member
16 Posts |
Posted - 2006-10-24 : 19:25:49
|
| More info: I may have more than ONE bap_id(variable)... could be 50 bap_ids and 4 grp_code(constant). =========================================Dataset I'm getting after running the code below for 2 bap_ids: INSERT #finalSELECT sort_id, bap_id,null, value1_tx, NULL, NULL, NULLFROM #tmpWHERE grp_code =1 and bap_id = 1 INSERT #finalSELECT sort_id, bap_id,null,null, value1_tx, NULL, NULLFROM #tmpWHERE grp_code =2 and bap_id = 1INSERT #finalSELECT sort_id,bap_id, NULL, NULL, NULL, value1_tx, value2_txFROM #tmpWHERE grp_code =3 and bap_id = 1INSERT #finalSELECT sort_id, bap_id,null, value1_tx, NULL, NULL, NULLFROM #tmpWHERE grp_code =1 and bap_id = 2INSERT #finalSELECT sort_id, bap_id,null,null, value1_tx, NULL, NULLFROM #tmpWHERE grp_code =2 and bap_id = 2INSERT #finalSELECT sort_id,bap_id, NULL, NULL, NULL, value1_tx, value2_txFROM #tmpWHERE grp_code =3 and bap_id = 2Sort_id bap_id Geography Vendor_nm Regulator Regulation1 1 Alabama NULL NULL NULL1 1 Alaska NULL NULL NULL1 1 Arizona NULL NULL NULL1 1 California NULL NULL NULL1 1 NULL ACS Image Solutions NULL NULL1 1 NULL ABC Virtual Communications, Inc NULL NULL1 1 NULL ACS Unclaimed Propery Clearinghouse, Inc. NULL NULL1 1 NULL ABC Virtual Communications, Inc NULL NULL1 1 NULL ABC Virtual Communications, Inc NULL NULL1 1 NULL Acxiom NULL NULL1 1 NULL ABC Virtual Communications, Inc NULL NULL1 1 NULL NULL FEDERAL RESERVE REGULATIONS Reg. AA - 12 CFR 227 - Unfair or Deceptive Acts1 1 NULL NULL FEDERAL RESERVE REGULATIONS Reg. B - 12 CFR 202 - Equal Credit Opportunity1 1 NULL NULL FEDERAL RESERVE REGULATIONS Reg. B - 12 CFR 202 - Equal Credit Opportunity1 1 NULL NULL FEDERAL RESERVE REGULATIONS Reg. C - 12 CFR 203 - Home Mortgage Disclosure1 1 NULL NULL FEDERAL RESERVE REGULATIONS Reg. CC - 12 CFR 229 - Availability of Funds & Collection of Checks1 1 NULL NULL FEDERAL RESERVE REGULATIONS Reg. D - 12 CFR 204 - Reserve Requirements of Depository Institutions2 2 Alaska NULL NULL NULL2 2 Arizona NULL NULL NULL2 2 Arkansas NULL NULL NULL2 2 California NULL NULL NULL2 2 NULL ABC Virtual Communications, Inc NULL NULL2 2 NULL ACS Unclaimed Propery Clearinghouse, Inc. NULL NULL2 2 NULL #NAME? NULL NULL2 2 NULL ACS Image Solutions NULL NULL2 2 NULL Acxiom NULL NULL2 2 NULL ADP NULL NULL2 2 NULL ADP Investor Communication NULL NULL2 2 NULL Bancomer Transfer Services NULL NULL2 2 NULL Bank of New York NULL NULL2 2 NULL First American Commercial Real Estate Tax Services Inc. NULL NULL2 2 NULL First American Default Management Solutions NULL NULL2 2 NULL First American Default Technologies NULL NULL2 2 NULL First American eAppraiseIT NULL NULL2 2 NULL First American Field Services NULL NULL2 2 NULL First American Flood Data Services (FAFDS) NULL NULL2 2 NULL First American National Default Title Services NULL NULL2 2 NULL First American Nationwide Documents (FAND) NULL NULL2 2 NULL First American Real Estate Solutions (FARES) NULL NULL2 2 NULL First American Real Estate Tax Services NULL NULL2 2 NULL First American Title-Lenders Advantage NULL NULL2 2 NULL First Canadian Title NULL NULL2 2 NULL First Data Corp NULL NULL2 2 NULL ABC Virtual Communications, Inc NULL NULL2 2 NULL ACS Image Solutions NULL NULL2 2 NULL ACS Unclaimed Propery Clearinghouse, Inc. NULL NULL2 2 NULL Platinum Direct NULL NULL2 2 NULL POS Portal NULL NULL2 2 NULL NULL FEDERAL RESERVE REGULATIONS Reg S Subpart B - 12 CFR 219 - Record Keeping Requirements for Certain Financial Records2 2 NULL NULL FDIC REGULATIONS 12 CFR 330 FDIC Deposit Insurance2 2 NULL NULL STATE AND LOCAL LAWS AND REGULATIONS UCC - Uniform Commercial Code============================================================MY DESIRED DATASET should look like:------>Sort_id bap_id Geography Vendor_nm Regulator Regulation1 1 Alabama ACS Image Solutions FEDERAL RESERVE REGULATIONS Reg. AA - 12 CFR 227 - Unfair or Deceptive Acts1 1 Alaska ABC Virtual Communications, Inc FEDERAL RESERVE REGULATIONS Reg. B - 12 CFR 202 - Equal Credit Opportunity1 1 Arizona ACS Unclaimed Propery Clearinghouse, Inc. FEDERAL RESERVE REGULATIONS Reg. B - 12 CFR 202 - Equal Credit Opportunity1 1 California ABC Virtual Communications, Inc FEDERAL RESERVE REGULATIONS Reg. C - 12 CFR 203 - Home Mortgage Disclosure1 1 NULL ABC Virtual Communications, Inc FEDERAL RESERVE REGULATIONS Reg. CC - 12 CFR 229 - Availability of Funds & Collection of Checks1 1 NULL Acxiom FEDERAL RESERVE REGULATIONS Reg. D - 12 CFR 204 - Reserve Requirements of Depository Institutions1 1 NULL ABC Virtual Communications, Inc NULL NULL1 1 NULL NULL NULL NULL1 1 NULL NULL NULL NULL1 1 NULL NULL NULL NULL1 1 NULL NULL NULL NULL1 1 NULL NULL NULL NULL1 1 NULL NULL NULL NULL1 1 NULL NULL NULL NULL1 1 NULL NULL NULL NULL1 1 NULL NULL NULL NULL1 1 NULL NULL NULL NULL2 2 Alaska ABC Virtual Communications, Inc FEDERAL RESERVE REGULATIONS Reg S Subpart B - 12 CFR 219 - Record Keeping Requirements for Certain Financial Records2 2 Arizona ACS Unclaimed Propery Clearinghouse, Inc. FDIC REGULATIONS 12 CFR 330 FDIC Deposit Insurance2 2 Arkansas #NAME? STATE AND LOCAL LAWS AND REGULATIONS UCC - Uniform Commercial Code2 2 California ACS Image Solutions NULL NULL2 2 NULL Acxiom NULL NULL2 2 NULL ADP NULL NULL2 2 NULL ADP Investor Communication NULL NULL2 2 NULL Bancomer Transfer Services NULL NULL2 2 NULL Bank of New York NULL NULL2 2 NULL First American Commercial Real Estate Tax Services Inc. NULL NULL2 2 NULL First American Default Management Solutions NULL NULL2 2 NULL First American Default Technologies NULL NULL2 2 NULL First American eAppraiseIT NULL NULL2 2 NULL First American Field Services NULL NULL2 2 NULL First American Flood Data Services (FAFDS) NULL NULL2 2 NULL First American National Default Title Services NULL NULL2 2 NULL First American Nationwide Documents (FAND) NULL NULL2 2 NULL First American Real Estate Solutions (FARES) NULL NULL2 2 NULL First American Real Estate Tax Services NULL NULL2 2 NULL First American Title-Lenders Advantage NULL NULL2 2 NULL First Canadian Title NULL NULL2 2 NULL First Data Corp NULL NULL2 2 NULL ABC Virtual Communications, Inc NULL NULL2 2 NULL ACS Image Solutions NULL NULL2 2 NULL ACS Unclaimed Propery Clearinghouse, Inc. NULL NULL2 2 NULL Platinum Direct NULL NULL2 2 NULL POS Portal NULL NULL2 2 NULL NULL NULL NULL2 2 NULL NULL NULL NULL2 2 NULL NULL NULL NULL2 2 NULL NULL NULL NULL2 2 NULL NULL NULL NULL2 2 NULL NULL NULL NULL2 2 NULL NULL NULL NULL |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-10-24 : 19:26:32
|
How do you associate 'Alabama' with 'ACS Image Solutions' and not 'ABC Virtual Communications, Inc' ? KH |
 |
|
|
baaul
Starting Member
16 Posts |
Posted - 2006-10-24 : 23:59:05
|
| The group data is SORTED in the #tmp table. So, all i need is to INSERT the grouped rows into #final table in that order. Is that possibile? Thanks in advance.-baaul |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2006-10-25 : 08:48:12
|
| order means nothing in the database. you order it when you retrieve it. You do NOT insert by order, you insert by relation![Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQLhttp://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
|
|
|
|
|