I am under a situation where I have to update the rows of two column and I am not able to find the correct solution.
I have table like this:
table1 :
nid listName ltitle
1 lsn1 lst1
2 lsn2 lst2
and now this nid is foreign key(listid) for table2
table2
nid listid listcol1 listcol2
1 1 "lstxt1" "lscol1" //belongs to lsn1
2 1 "lstxt2" "lscol2" //belongs to lsn1
3 1 "lstxt3" "lscol3" //belongs to lsn1
3 2 "lstxt4" "lscol4" //belongs to lsn2
For better understanding there are two list named lsn1 and lsn2 and first one has 2 columns listcol1 and listcol2 and listcol1 contains the data which come
through a string which I split through "###" as delimiter something like this "lstxt1###lstxt2###lstxt3###" similarly for listcol2
My try to update both table in stored prcodure on data changed by user is :
update table1 set listName=#listName ,ltitle=#ltitle //table 1 update and it works well
where nid=#nid
update table2 set listId=#nid, listcol1=a.part,listcol2= b.part from //table2 update , problem is here
dbo.splitstring(#listcol1,'###') a inner join
dbo.splitstring(#listcol2,'###') b on a.id=b.id where table2.listid=#nid
problem here is :
(1) table1 is updated properly but table2 updated output is wrong (lets say i tried to update for nid=1 in table1) :
listcol1 listcol2
"lstxt5" "lscol5" //belongs to lsn1
"lstxt5" "lscol5" //belongs to lsn1
"lstxt5" "lscol5"
I mean it updates the first row to all rows. Where as expected output was :
listcol1 listcol2
"lstxt5" "lscol5" //belongs to lsn1
"lstxt6" "lscol6" //belongs to lsn1
"lstxt7" "lscol7"
(2) when user add new row it do not show the update in that row. It must show taht as well.
This part of the code (as far as I understand your logic) for sure does not make any sense: listcol1=a.part,listcol1= b.part
That should give you an error like this:
Msg 264, Level 16, State 1, Line 3
The column name 'listcol1' is specified more than once in the SET clause or column list of an INSERT. A column cannot be assigned more than one value in the same clause. Modify the clause to make sure that a column is updated only once. If this statement updates or inserts columns into a view, column aliasing can conceal the duplication in your code."
update table1 set listName=#listName ,ltitle=#ltitle //table 1 update and it works well
where nid=#nid
update table2 set listId=#nid, **listcol1=a.part,listcol1= b.part** from //table2 update , problem is here
dbo.splitstring(#listcol1,'###') a inner join
dbo.splitstring(#listcol2,'###') b on a.id=b.id where table2.listid=#nid
Related
I am trying to check if the values from Table1 exist in Table2.
The thing is that the values are comma separated in Table1
Table 1
ID
TXT
1
129(a),P24
2
P112
3
P24,XX
4
135(a),135(b)
Table 2
ID
P24
P112
P129(a)
135(a)
135(b)
The following only works if the complete cell value exists in both tables:
SELECT Table1.ID, Table1.TXT
FROM Table1 LEFT JOIN Table2 ON Table1.[TXT] = Table2.[ID]
WHERE (((Table2.ID) Is Null));
MY QUESTION IS:
Is there a way to check each comma separated value and return those that do not exists in Table 2.
In above example the value XX should end up in the result.
Not sure why you store your data in that way (which is bad practice as sos mentioned above), but you need to mimic the temp table like in SQL server.
Select from table1 and create different txt rows per id.
Insert the results from section 1 into the table3.
Select from table3 and join it to table2.
Delete table 3.
Table3 the temp table
ID
TXT
1
129(a)
1
P24
2
P112
3
P24
3
XX
4
135(a)
4
135(b)
Here is some explanation MS Access database (2010) how to create temporary table/procedure/view from Query Designer
Using the query wizard, I built two different queries doing similar functionalities that I am trying to combine into one query. I have two tables (same structure) that I am matching to find duplicates:
Query #1 is as follows (Include ALL records from Table 1 and only those records from Table 2 where the joined fields are equal are applied to all the columns below):
Match Table 1 Column 3 to Table 2 Column 3
Match Table 1 Column 4 to Table 2 Column 4
Match Table 1 Column 5 to Table 2 Column 5
Match Table 1 Column 7 to Table 2 Column 7
If all of those columns from Table 1 match what’s in Table 2, it will identify the duplicates (I bring in Table 2 Column 7 which will show the duplicates I am looking for).
Query #2 is as follows (Include ALL records from Table 1 and only those records from Table 2 where the joined fields are equal are applied to all the columns below):
Match Table 1 Column 3 to Table 2 Column 3
Match Table 1 Column 4 to Table 2 Column 4
Match Table 1 Column 5 to Table 2 Column 5
Match Table 1 Column 8 to Table 2 Column 8
My second query has the same 3 columns, except the last column is different.
If all of those columns from Table 1, match what’s in Table 2, it will identify the duplicates (I bring in Table 2 Column 7/8 which will show the duplicates I am looking for).
What I am trying to do:
Add an OR statement for the query to show both duplicates on matches for Columns 8 and Columns 7. Such as if Table 1 Column 7 matches Table 2 Column 7 OR Table 1 Column 8 matches Table 2 Column 8, show the duplicates.
Would this require a UNION query?
Here is the query for one of them:
SELECT TABLE1.COLUMN_3, TABLE1.COLUMN_4, TABLE1. COLUMN_7,
TABLE2.COLUMN_7, TABLE1.COLUMN_5
FROM TABLE1
LEFT JOIN TABLE2
ON (TABLE1.COLUMN_7 = TABLE2.COLUMN_7)
AND (TABLE1.COLUMN_3 = TABLE2.COLUMN_3)
AND (TABLE1.COLUMN_4 = TABLE2.COLUMN_4)
AND (TABLE1.COLUMN_5 = TABLE2.COLUMN_5);
The given SQL will not run because of spaces in table and column names.
This example eliminates those spaces.
SELECT
Table1.Column3 , Table2.Column3
,Table1.Column4 , Table2.Column4
,Table1.Column5 , Table2.Column5
,Table1.Column7 , Table2.Column7
,Table1.Column8 , Table2.Column8
From Table1
Left Join Table2
On
( Table1.Column3 = Table2.Column3
AND Table1.Column4 = Table2.Column4
AND Table1.Column5 = Table2.Column5
AND ( Table1.Column7 = Table2.Column7
OR Table1.Column8 = Table2.Column8
)
)
Create query 3; add Q1 and Q2 - join them on F7. This will result in all matches of that field.
Create query 4; add Q1 and Q2 - join them on F8. This will result in all matches of that field.
You now have 2 data sets with those matches (you call duplicates). Then the decision is how to present/display. If you need them in a single record set - write those into a single common temp table. But otherwise you can easily present them together as sub reports/forms.
I have a query like this:
DELETE A
FROM A
LEFT JOIN B ON
A.ID=B.ID
WHERE B.Status='OK'
The records from these join might resulted in more than one rows, for example:
Table A
ID
1
2
Table B
ID Status
1 OK
1 OK
2 OK
Do I have to make sure my record only match with one row? Because in these example, ID 1 will have 2 rows.
Apologize for bad english.
In SQL Server you don't have to ensure a 1 row result per row to delete. The engine will delete all rows from the deleting table that matches your joining or where conditions, even if they are being selected for more than 1 row.
The important part is which table are your deleting, make sure not to delete the wrong one!
I have two tables.Table A has 4 columns. And table B has two columns.I want to insert value of one column of tabel A from one column of table B based on condtion if id matches.
how i can do this ? For example if [Movieid] in 1st table =[IMDBid] in second table then insert [count] of table 1=[CB] in table 2.
i want to do it once for full table.
[column] -> these are colums
i m using sql server.
Tabel 1 : Movieid,count,
Tabel 2: IMDBid, CB
Results which i want: i want to insert values of CB column in count where Movieid=IMDBid
Tabel 1 :
(Nick Id,MovieId,Rating,MovId)-> (1,4972,6.25,?)(1,24216,7.25,?)
Tabel 2 :
(Imdbid,Title,ImdbPyId,Id)-> (4972,hello,32450,1)(24216,hi,62450,2)
Insert /fill value of MovId(tabel1) using values Id(tabel2)where MovieId(tabel1)==Imdbid(tabel2)
You can do it like,
UPDATE tabl1
SET count = (SELECT CB FROM tabl2 WHERE IMDBid = Movieid)
But it is gonna blow up if you have multiple values returning from the subquery.
So make sure to use the appropriate function to get the single value from that subquery whichever meets your requirements.
If your tables have 1:1 relationship then it should be fine. But if it is 1:n then you need to use either aggragate functions or the TOP 1 clause in that subqyery.
solution is UPDATE tabl1
SET count = (SELECT CB FROM tabl2 WHERE IMDBid = Movieid)
Currently struggling with finding a way to validate 2 tables (efficiently lots of rows for Table A)
I have two tables
Table A
ID
A
B
C
Table matched
ID Number
A 1
A 2
A 9
B 1
B 9
C 2
I am trying to write a SQL Server query that basically checks to make sure for every value in Table A there exists a row for a variable set of values ( 1, 2,9)
The example above is incorrect because t should have for every record in A a corresponding record in Table matched for each value (1,2,9). The end goal is:
Table matched
ID Number
A 1
A 2
A 9
B 1
B 2
B 9
C 1
C 2
C 9
I know its confusing, but in general for every X in ( some set ) there should be a corresponding record in Table matched. I have obviously simplified things.
Please let me know if you all need clarification.
Use:
SELECT a.id
FROM TABLE_A a
JOIN TABLE_B b ON b.id = a.id
WHERE b.number IN (1, 2, 9)
GROUP BY a.id
HAVING COUNT(DISTINCT b.number) = 3
The DISTINCT in the COUNT ensures that duplicates (IE: A having two records in TABLE_B with the value "2") from being falsely considered a correct record. It can be omitted if the number column either has a unique or primary key constraint on it.
The HAVING COUNT(...) must equal the number of values provided in the IN clause.
Create a temp table of values you want. You can do this dynamically if the values 1, 2 and 9 are in some table you can query from.
Then, SELECT FROM tempTable WHERE NOT IN (SELECT * FROM TableMatched)
I had this situation one time. My solution was as follows.
In addition to TableA and TableMatched, there was a table that defined the rows that should exist in TableMatched for each row in TableA. Let’s call it TableMatchedDomain.
The application then accessed TableMatched through a view that controlled the returned rows, like this:
create view TableMatchedView
select a.ID,
d.Number,
m.OtherValues
from TableA a
join TableMatchedDomain d
left join TableMatched m on m.ID = a.ID and m.Number = d.Number
This way, the rows returned were always correct. If there were missing rows from TableMatched, then the Numbers were still returned but with OtherValues as null. If there were extra values in TableMatched, then they were not returned at all, as though they didn't exist. By changing the rows in TableMatchedDomain, this behavior could be controlled very easily. If a value were removed TableMatchedDomain, then it would disappear from the view. If it were added back again in the future, then the corresponding OtherValues would appear again as they were before.
The reason I designed it this way was that I felt that establishing an invarient on the row configuration in TableMatched was too brittle and, even worse, introduced redundancy. So I removed the restriction from groups of rows (in TableMatched) and instead made the entire contents of another table (TableMatchedDomain) define the correct form of the data.