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 |
|
amitranjan
Starting Member
45 Posts |
Posted - 2010-03-29 : 03:04:28
|
| I have two tables ProspectImport and Prospects. ProspectImports is a physical table but holds data temporarily. What our application is doing , is that its importing a XL sheet into ProspectImports table. Further if a user wants to import it for use, with some import button on some form he can import those on records of ProspectImport table to Prospect. So what I a want to say from above is that ProspectImport is also a table that will hold values to them until user imports. It could be a day, a week or a month. But thats not an issue.Here comes a issue. I have to import records of ProspectImport to Prospect as said earlier. In the XL sheet there is a column name ProspectNo whose datatype is character but is storing a integer value. So as exists in ProspectImport and Prospect tables. ProspectNo is not a mandatory so it could be null too. What I want to do is that before importing i want to make them unique. See below table with sample dataProspectImport Prospect100 101102 6112125 325400 215100 156102 10121021 6121245 3275215100 2185101 1056nullnullIn above prospectimport has data that is to be imported to prospect table. But before that I need a check. If u match the two tables you can easily find some matching records. for example take first record 100 from prospectimport now search it in same table if it exists add a suffix -1 to matched one. Here 100 matches two so our new table will become. If another match found increment the suffix , -2, -3 and so on both the tables so that we have unique records. But the change shud be reflected only to ProspectImport table so that the prospectno in prospect table will be unique. No repeatation.ProspectImport Prospect100 100102 6112125 325400 215100-1 156102 10121021 6121245 3275215100-2 2185101 1056nullnullin case of null find the max value of column in prospectimport table and also match that value with existing prospectno in prospect table, if matches found add the same suffix to number and replace null with that number.Hope it will be clear.amit Ranjan |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-29 : 05:26:17
|
| do you mean you need to export only distinct values of column to prospect? or is your reqmnt to make repeated values unique before export by adding the suffix part?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
amitranjan
Starting Member
45 Posts |
Posted - 2010-03-29 : 05:29:35
|
quote: Originally posted by visakh16 do you mean you need to export only distinct values of column to prospect? or is your reqmnt to make repeated values unique before export by adding the suffix part?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
reqmnt to make repeated values unique before export by adding the suffix partamit Ranjan |
 |
|
|
amitranjan
Starting Member
45 Posts |
Posted - 2010-03-29 : 05:32:01
|
quote: Originally posted by amitranjan [quote]Originally posted by visakh16 do you mean you need to export only distinct values of column to prospect? or is your reqmnt to make repeated values unique before export by adding the suffix part?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/reqmnt to make repeated values unique before export by adding the suffix partamit Ranjan
Please help, i need it asap with 1-2 hrs.. It would be a gr8 help. thanks in advanceamit Ranjan |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-03-29 : 06:00:26
|
Here is an approach but I think I have not complete checked what you want.declare @temp table (ProspectImport varchar(255), Prospect varchar(255))insert @tempselect '100', '101' union allselect '102', '6112' union allselect '125', '325' union allselect '400', '215' union allselect '100', '156' union allselect '102', '1012' union allselect '1021', '612' union allselect '1245', '3275' union allselect '215','' union allselect '100', '2185' union allselect '101', '1056' union allselect null,'' union allselect null,''select * from @tempselect case when rownum > 1 then ProspectImport + '-' + convert(varchar(10),rownum-1) else ProspectImport end as ProspectImport,Prospectfrom(select *, row_number() over (partition by ProspectImport order by Prospect) as rownumfrom @temp)dtwhere ProspectImport is not null No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
amitranjan
Starting Member
45 Posts |
Posted - 2010-03-29 : 06:35:29
|
quote: Originally posted by webfred Here is an approach but I think I have not complete checked what you want.declare @temp table (ProspectImport varchar(255), Prospect varchar(255))insert @tempselect '100', '101' union allselect '102', '6112' union allselect '125', '325' union allselect '400', '215' union allselect '100', '156' union allselect '102', '1012' union allselect '1021', '612' union allselect '1245', '3275' union allselect '215','' union allselect '100', '2185' union allselect '101', '1056' union allselect null,'' union allselect null,''select * from @tempselect case when rownum > 1 then ProspectImport + '-' + convert(varchar(10),rownum-1) else ProspectImport end as ProspectImport,Prospectfrom(select *, row_number() over (partition by ProspectImport order by Prospect) as rownumfrom @temp)dtwhere ProspectImport is not null No, you're never too old to Yak'n'Roll if you're too young to die.
It worked but, i think its selecting, i need to update the existing prospectimport table data with newly generated prospectnos.amit Ranjan |
 |
|
|
amitranjan
Starting Member
45 Posts |
Posted - 2010-03-29 : 06:42:40
|
quote: Originally posted by amitranjan
quote: Originally posted by webfred Here is an approach but I think I have not complete checked what you want.declare @temp table (ProspectImport varchar(255), Prospect varchar(255))insert @tempselect '100', '101' union allselect '102', '6112' union allselect '125', '325' union allselect '400', '215' union allselect '100', '156' union allselect '102', '1012' union allselect '1021', '612' union allselect '1245', '3275' union allselect '215','' union allselect '100', '2185' union allselect '101', '1056' union allselect null,'' union allselect null,''select * from @tempselect case when rownum > 1 then ProspectImport + '-' + convert(varchar(10),rownum-1) else ProspectImport end as ProspectImport,Prospectfrom(select *, row_number() over (partition by ProspectImport order by Prospect) as rownumfrom @temp)dtwhere ProspectImport is not null No, you're never too old to Yak'n'Roll if you're too young to die.
It worked but, i think its selecting, i need to update the existing prospectimport table data with newly generated prospectnos.amit Ranjan
can you provide me an example that will use only the two tables not the temp table. Or temp table is mandatory. Thanks in advanceamit Ranjan |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-03-29 : 07:18:20
|
quote: Originally posted by amitranjan
quote: Originally posted by amitranjan
quote: Originally posted by webfred Here is an approach but I think I have not complete checked what you want.declare @temp table (ProspectImport varchar(255), Prospect varchar(255))insert @tempselect '100', '101' union allselect '102', '6112' union allselect '125', '325' union allselect '400', '215' union allselect '100', '156' union allselect '102', '1012' union allselect '1021', '612' union allselect '1245', '3275' union allselect '215','' union allselect '100', '2185' union allselect '101', '1056' union allselect null,'' union allselect null,''select * from @tempselect case when rownum > 1 then ProspectImport + '-' + convert(varchar(10),rownum-1) else ProspectImport end as ProspectImport,Prospectfrom(select *, row_number() over (partition by ProspectImport order by Prospect) as rownumfrom @temp)dtwhere ProspectImport is not null No, you're never too old to Yak'n'Roll if you're too young to die.
It worked but, i think its selecting, i need to update the existing prospectimport table data with newly generated prospectnos.amit Ranjan
can you provide me an example that will use only the two tables not the temp table. Or temp table is mandatory. Thanks in advanceamit Ranjan
It is just for clear example Use only SELECT statement and replace @temp with actual table nameMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|