last day (15 days later) » 

9:21 AM
0
Q: How to update column in table from two different tables USING DML Command

Mahmoud Al-HaroonI have three different tables here: df_umts_relation table: |---------------------|------------------|---------------------|------------------|------------------| | cell_name | n_cell_name | technology | source_ops_num | target_ops_num | |---------------------|-...

 
Please add database tag. Thanks!
 
@VBoka Edited thanks you for your interests :)
 
You are fast, that is nice. But what I wanted is : Oracle, MySQL, Postgresql, SQLServer....One of this
 
@VBoka updated it's sql-server express :)
 
Is there any date in df_umts_carrier and in df_gsm_carrier ? Please update one or two values to see ? Thanks!
 
9:21 AM
@VBoka no there's no date in the tables
 
But you want to update your df_umts_relation table "So All I need I want to update [MyNewDatabase].[dbo].[df_umts_relation].[target_ops_num]" with the data from the df_umts_carrier and df_gsm_carrier tables and there is no data in this two tables ?
 
@VBoka no there's data in those other tables an integer data
Hello :)
 
Hello
 
I am glad for helping me btw :) thanks alot :)
 
So, lets go step by step ok?
 
9:28 AM
Ok sure
 
df_umts_carrier this is a name of a table ? Yes ?
 
This are two columns that this table has: opsnum_umts cell_name_umts
?
 
yes exactly
 
Ok
Now do you understand a term row of data ?
One row of data , two rows...
?
 
9:29 AM
Not really I got confused of this, but what imagine it's two rows data, UMTS and GSM related for the technology column
 
no no lets go back one step.
 
This are two columns that this table has: opsnum_umts cell_name_umts
you said yes
 
so lets continue there
in this table df_umts_carrier , do you have any data ?
 
9:31 AM
this is the query is have in this table
SELECT TOP 1000 [id]
,[opsnum_umts]
,[cell_name_umts]
,[band]
,[cell_status]
,[rnc]
,[rnc_id]
FROM [MyNewDatabase].[dbo].[df_umts_carrier]
 
yes, but data ?
 
those are the columns which I have in this table ,[opsnum_umts]
,[cell_name_umts]
,[band]
,[cell_status]
,[rnc]
,[rnc_id]
yes it contains 28998 rows
 
Yes yes but this are columns
not data
cell_name | n_cell_name | technology | source_ops_num | target_ops_num |
|---------------------|------------------|---------------------|------------------|------------------|
| 121 | 221 | UMTS | 1 | | |
|---------------------|------------------|---------------------|------------------|------------------|
| 122 | 222 | GSM | 2 | |
do you understand ?
 
opsnum_umts cell_name_umts band cell_status rnc rnc_id
7352 15000 2100 ID ASDTUR16 172
3092 15003 2100 ID ASDTUR16 172
3092 15004 2100 ID ASDTUR16 172
3092 15005 2100 ID ASDTUR16 172
941 15010 2100 ID AHPTUR16 173
941 15011 2100 ID AHPTUR16 173
941 15012 2100 ID AHPTUR16 173
3130 15013 2100 ID AHPTUR16 173
the data looks like this in this table
 
in the table df_umts_relation column name is technology and data in this table column is 'UMTS', 'GSM' and so on...
Ok, this is the data. Great!
Theer is your problem
 
9:34 AM
actually this is all of data looks like
id opsnum_umts cell_name_umts band cell_status rnc rnc_id
1 7352 15000 2100 ID ASDTUR16 172
2 3092 15003 2100 ID ASDTUR16 172
3 3092 15004 2100 ID ASDTUR16 172
4 3092 15005 2100 ID ASDTUR16 172
5 941 15010 2100 ID AHPTUR16 173
6 941 15011 2100 ID AHPTUR16 173
7 941 15012 2100 ID AHPTUR16 173
8 3130 15013 2100 ID AHPTUR16 173
 
You see, there is no value of 221 or 222 in the column opsnum_umts
and you use this column opsnum_umts to join two tables
 
you are joining based on wrong columns
 
this is the data looks like, on other way the data I posted in the question is fake just an example
 
As far as I can see
 
9:37 AM
hold a moment I'll send you the other tables
 
O I see
thanks
I will post an answer and then go to luch... cheers
will be back in one hour
 
Also, please do accept my answer and vote it up if it helps you. If you do not know how to do that please ask and I will help you.
 
this table related for df_umts_relation
yes sure I'll accept answer tyt :)
and this is the df_gsm_carrier
the df_umts_relation
/****** Script for SelectTopNRows command from SSMS ******/
SELECT TOP 1000 [id]
,[opsnum_gsm]
,[cellname]
FROM [MyNewDatabase].[dbo].[df_gsm_carrier]


/****** Script for SelectTopNRows command from SSMS ******/
SELECT TOP 1000 [id]
,[opsnum_umts]
,[cell_name_umts]
,[band]
,[cell_status]
,[rnc]
,[rnc_id]
FROM [MyNewDatabase].[dbo].[df_umts_carrier]

/****** Script for SelectTopNRows command from SSMS ******/
SELECT TOP 1000 [id]
,[cell_name]
,[n_cell_name]
,[technology]
,[source_vendor]
,[target_vendor]
Well When I tried to execute the below query
UPDATE [MyNewDatabase].[dbo].[df_umts_relation]
SET [MyNewDatabase].[dbo].[df_umts_relation].[target_ops_num] = ( SELECT CASE WHEN dur.technology = 'UMTS' then du.cell_name_umts
when dur.technology = 'GSM' then dg.cellname
end
FROM [MyNewDatabase].[dbo].[df_umts_relation] dur
left join [MyNewDatabase].[dbo].[df_umts_carrier] du on dur.n_cell_name = du.opsnum_umts
left join [MyNewDatabase].[dbo].[df_gsm_carrier] dg on dur.n_cell_name = dg.opsnum_gsm
where dur.n_cell_name = [MyNewDatabase].[dbo].[df_umts_relation].[n_cell_name])
I foun this error `Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.`
 
10:28 AM
Hello, what is the unique identifier for your table df_umts_relation ?
Also, please do not accept the answer if it is not correct and I see you have problems with my answer.
I will explain to you what "unique identifier" means: it is a column or group of columns that uniquely identifies a row
For example:
1 Mark
2 Ann
3 Tom
4 Mark
In this example the unique identifier is column that has values 1 2 3 and 4
 
the unique identifier is the any cell_name in any table fore example....
 
no it is not
cell_name column has same values
 
[MyNewDatabase].[dbo].[df_umts_carrier].[cell_name_umts] is unique..
 
Yes, but I need it in the table df_umts_relation ?
 
any column contains cell_ name or cell it's a unique because ops_num contains more than one cell
 
10:37 AM
so, I have updated my answer please try
 
the column key which concatnate the cell_name and n_cell_name is columns called [df_st_umts_relation_key]
ok I'll check this and come back
Okay Well I think I have some problem with the query I'll solve it, but I am sorry I'll have to go to my launch and I'll back to you after an hour... I am really sorry... I'll back to you to comlete our discussion and explain to you more... I hope you use python pandas btw to explain to you the whole idea
 
Sorry, I do not :)
But the query should work regardless to that...
 
Ok let me see.. I am just re importing the data again as you know I am working on huge data contains millions and thousands of rows it take about 30 min for importing the data to the sql-server :(
 
but, always try with small amount
not all :)
 
So.. I will back to you after one hour.. thanks a lot for your support :)... I appretiate that :)
 
10:50 AM
for example you can also buld your small test example here
You are welcome
 
Ahaaa Okay I got you :)
that's a very good idea :)
 
If I manage to find solution for you do accept my answer :)
If not, thanks for the up vote !
 
I'll accept your answer btw if not solved :), I appritiate that you are helping me :)
 
:)
Well, you do not need to because it will maybe be a reference to someone else in he future :) So we need a correct answer...
 
 
1 hour later…
12:20 PM
well I am back sorry for the late :)
Now I created something like this query
```
UPDATE [MyNewDatabase].[dbo].[df_umts_relation]
SET [MyNewDatabase].[dbo].[df_umts_relation].[target_ops_num] = ( SELECT CASE WHEN dur.technology = 'UMTS' then du.cell_name_umts
when dur.technology = 'GSM' then dg.cellname_gsm
end
FROM [MyNewDatabase].[dbo].[df_umts_relation] dur
LEFT join [MyNewDatabase].[dbo].[df_umts_carrier] du on dur.n_cell_name = du.opsnum_umts
LEFT join [MyNewDatabase].[dbo].[df_gsm_carrier] dg on dur.n_cell_name = dg.opsnum_gsm
WHERE dur.id = [MyNewDatabase].[dbo].[df_umts_relation].[id])
related to your query
but I think it take much time I am just waiting to see the result
 
iT IS WOKRING ?
No errors ?
Just working which means he is updating
great :)
 
it takes till now 7 min and no result I think it may be work I hope :)
 
Well... you said it is a lot of data ...
 
but there's other way to escape this delay as to make query faster or it's related to the capacity of the data?
 
:)
There is always a way. for example adding index
 
12:27 PM
ummmmm I don't know what do you mean adding index exactly :) but I'll google it btw :)
you mean something like this...
as this note
Note: Updating a table with indexes takes more time than updating a table without (because the indexes also need an update). So, only create indexes on columns that will be frequently searched against.
I am sorry I am not a familiar with SQL I have just a basic back ground :)
 
yes, I know, I only told you : for example adding index
You will add index on the column you are using for join and not for update
You are updating column df_umts_relation
and columns n_cell_name and opsnum_umts and opsnum_gsm are the ones you can index
because you use them for join and not for update
 
ahhhh Okay I got you now... I got the idea :) thank you I'll work on this, btw the query still executing :)... I'll gie you feedback about this
 
No problem.
 
12:50 PM
can I ask you a question?
for my case I'll use the non-clustered indexes right?
 
I am not sure...
 
1:26 PM
Òkay thank you bro alot, I know how now what can I do Clustered Index Support using python and MS SQL with this link as I created but without indexes :) following this link
 
nice researching
continue the good work
did my update query worked ?
 
it's still running :(
1:12 hour till now
the problem was I am using a large data I am trying to handle this with python pandas function and sql queries
it's very starnge to take this time right?
by the way I didn't create any schema or any relations or indexes it's normal database
may be that was the problem
as this is my python part to create a table with MetaData()
```
dataf_umts_carrier_cols_meta = MetaData()
dataf_umts_cols_carrier = Table(
DATAF_UMTS_CARRIER, dataf_umts_carrier_cols_meta,
Column('id', Integer, primary_key=True),
Column(dataf_umts_carrier_cols[0], Integer),
Column(dataf_umts_carrier_cols[1], Integer),
Column(dataf_umts_carrier_cols[2], Integer),
Column(dataf_umts_carrier_cols[3], String),
Column(dataf_umts_carrier_cols[4], String),
Column(dataf_umts_carrier_cols[5], Integer),
)
```
you see I didn't add any schema or indexes when I am creating the table using python
So there's a function I have to add to this code as this function
```
PrimaryKeyConstraint("id", "cell_name_umts", mssql_clustered=True)
```
I am sorry for sending you I know it's not you scope, but I am trying to explain to you my case in brief
but for my cas as my database is not clustured I'll make mssql_clustered=False instead true
 
 
2 hours later…
4:07 PM
Huh It's doesn't work also :(
Ok look is there's a problem if the n_cell_name have duplicated values?
 
4:24 PM
Yes, it is a problem
This select needs to return one value for every id
select case when dur.technology = 'UMTS' then du.cell_name_umts
when dur.technology = 'GSM' then dg.cellname
end
from df_umts_relation dur
left join df_umts_carrier du on dur.n_cell_name = du.opsnum_umts
left join df_gsm_carrier dg on dur.n_cell_name = dg.opsnum_umts
where dur.id= df_umts_relation.id
for every id in table df_umts_relation
you now have to adjust this select query so it returns only one value per id
play little bit with that select
try different joins
left join, inner join, right join, full outer join
 
Ok I'll try this one on ther way it's works when I create two different queries one for the GSM and one for the UMTS
 
and is this ok for you?
to do it two times ?
 
On other side I am working on the NON Clustered index because most commands I used is INSERT, and UPDATE
actually I have just run it now I am waiting for execution but it works as the below queries
 
good luck
 
```
dataf_umts_relations_target_opsnum_val_query_umts = \
"UPDATE [MyNewDatabase].[dbo].[df_umts_relation] \n" \
"SET [MyNewDatabase].[dbo].[df_umts_relation].[target_ops_num] = [MyNewDatabase].[dbo].[df_umts_carrier].[opsnum_umts] \n" \
"FROM [MyNewDatabase].[dbo].[df_umts_relation] \n" \
"INNER JOIN [MyNewDatabase].[dbo].[df_umts_carrier] \n" \
"ON [n_cell_name] = [cell_name_umts] \n"\
"WHERE [MyNewDatabase].[dbo].[df_umts_relation].[technology] = 'UMTS'"

dataf_umts_relations_target_opsnum_val_query_gsm = \
duplicate where here sorry
 
4:38 PM
and this works ?
 
It should work but I am waiting for the result
 
4:57 PM
I am waiting too
 
5:11 PM
Finally It works Bro with two queries
 
Great
It can be done with one too but I would need some data example to work with in the demo fiddle
I hope I was able to help you
 
Okay look if you are still interested I can send you templates but with excel files
 
I do not have the conditions to do it like that
I am an oracle developer that works only with fiddle when other database is in question
fiddle is the demo link I have sent you before
it simulates different databases
 
Ahaaaa Okay I got you
 
Well the most important thing is that you have resolved your problem right ?
if you think it is deserved you can mark my answer as correct. Cheers and best of luck!
Going to go and help others if I can :)
 
5:17 PM
So sure
it's my please I am glad for that
 

last day (15 days later) »