How to compare two different rows of two different tables and update a third table? - sql

Im having two different tables Value(queid, m) and Ans(queid1, an). I want to compare queid and queid1 and if they are same then m and an's values and has to update the third table with correct values. Thanx a tons.
Table structures are
Value table will have two attributes queid and m. queid will have data like 3, 4, 5, 6, and m will have a, v, d, e
Ans table will have attributes queid1 and an. queid1 attributes will have data like 3, 4, 3, 4, 3, 3, 3, 2, 3, 4 and an will have data like a, v, a, a, a, c, e, r, e, d.
Now what i want is that it should compare the values of queid with queid1. so if we consider 3 ie first value of queid in value table, then it should find all the 3's in ans table and then is should compare a (ie the row corresponding to 3 in value table) with all the 3's in ans. And the corresponding right comparison of a's is to be stored in some third table.

This can be done by joining both tables on the queid & queid1 columns then filtering any results where the m & an columns are equal:
INSERT INTO NewTable (col1, col2)
SELECT V.queid, V.m
FROM Value V
JOIN Ans A
ON V.queid = A.queid1
WHERE V.m = A.an
;

Related

Query function to pull data based on multiple values within one cell

I would like to ask how to use Query function where one of the cell contains multiple comma separated values
For example:-
To pull col2 values when Col 1 is B, it would be straightforward.
Col 1
Col 2
A
1
B
2
C
3
D
4
=QUERY(A1:B3,"select B where A = '"&D3&"' ",0)
D3 cell value is B
Similar to how we have IN clause in SQL, I would like to pull data from col 2 when col1 values are B,C,D.
Would it be possible to concatenate the results in one row as well?
Try this one:
=join(", ", QUERY(A1:B4,"select B where "&CONCATENATE("A = '", join("' or A = '", SPLIT(D4, ",")), "'"),0))
Where D4 = B,C,D
Sample Output:
Note:
The values on column B below are the value of the formula:
CONCATENATE("A = '", join("' or A = '", SPLIT(D4, ",")), "'"),0))
Since there was no in statement in sheets query (not that I have encountered), what I did was split those said values in column D and have them format like the multiple or statements which are equal to a single in statement. This is a workaround and should do what you wish to achieve.

How to find the row and column number of a specific cell in sql?

I have a table in SQL database and I want to find the location of a cell like a coordinate and vice versa. Here is an example:
0 1 2 3
1 a b c
2 g h i
3 n o j
When I ask for i, I want to get row=2 and column=3. When I ask for a cell of row=2 and column=3, I want to get i.
You need to store your matrix in table specifying the columns and rows like this
create table matrix (
row int,
column int,
value varchar2(20)
);
Then you insert your data like this
insert into matrix values (1, 1, 'a');
insert into matrix values (1, 2, 'b');
//and so on.
And then you can simply find what you need using two queries
select column, row from matrix where value = 'i';
select value from matrix where column = 2 and row = 3;
In Oracle, you would do:
select "3"
from t
where "0" = 2;
Naming columns as numbers is not recommended. Your whole data model is strange for SQL. A better representation would be:
row col val
1 1 a
1 2 b
1 3 c
2 1 g
. . .
Then you could do:
select val
from grid
where row = 2 and col = 3;
Create a primary key column such as 'id' and for example, the related row is 'col'
select col from db where id = 2;
this returns you a specific cell (x,2)

How can I compare two sets of data having two columns in excel? Picture below will elaborate

Below are two sets of data. Each has two columns. I want that that the similar data comes in front of each other.
This is a manual solution with formulas and sorting.
Imagine the following data in columns A to E:
Enter the following formulas into columns G to K
Column G: =IFERROR(IF(VLOOKUP(D:D,A:B,2,FALSE)=E:E,1,2),3)
Column H: =IF(G:G<3,D:D,"")
Column I: =IFERROR(VLOOKUP(H:H,A:B,2,FALSE),"")
Column J: =D:D
Column K: =IFERROR(VLOOKUP(J:J,D:E,2,FALSE),"")
The column G sort by now shows:
1 if part and quantity matched
2 if only part matched
3 if nothing matched
So if you now select data from A3:K10 and sort by column G (sort by) then it will result in this:

Using pig, How do I parse and comapre a grouped item

I have
A B
a, d
a, e
a, y
z, v
z, k
z, o
and so on.
Column B is of type cararray and contains key value pairs separated by &.
For example - d = 'abc=1&c=1&p=success'
What I want to figure out --
Suppose -
d = 'abc=1&c=1&xyz=23423423'
e = 'xyz=1&it=ssd'
y = 'abc=1&c=1&p=success'
For every 'a' I want to figure out if it has column b which contains the same value of abc and have c=1 and p = success. I also want to extract the value of abc and c from d and y.
For instance lets take the above example -
d contains abc=1 and c=1
y contains abc=1 and p= success
So this satisfies what I am looking for i.e for a given 'a' i have same value of abc and c=1 and p =success.
I started with grouping my data :
grouped = group data BY (A, B);
which gives me
a, (a,b)(a,e)(a,y)
z, (z,v)(z,k)(z,o)
But after this I am clueless on how to compare data within each group so that the above condition is satisfied.
Any help on this is appreciated.
Please let me know if you want me to clarify further on my question.
Since you are only concerned with some of the fields in the query string (I assume that's what it is), you will want to split the data with a FOREACH and STRSPLIT. Flatten it so you have something that looks like this
(a, b) where b would be a single key/value from the query ex: abc=1
Filter out the key/value pairs you don't care about, join them back together and then group by the combined key/value pairs. That will give you a list of every a with the same b where b only contains abc=X, c=1 and p=success

How to compare two different rows of two different tables?

Im having two different tables Value(queid, m) and Ans(queid1, an). I want to compare queid and queid1 and if they are same then m and an's values and has to update the third table with correct values. Thanx a tons. Table structures are Value table will have two attributes queid and m. queid will have data like 3, 4, 5, 6, and m will have a, v, d, e Ans table will have attributes queid1 and an. queid1 attributes will have data like 3, 4, 3, 4, 3, 3, 3, 2, 3, 4 and an will have data like a, v, a, a, a, c, e, r, e, d. Now what i want is that it should compare the values of queid with queid1. so if we consider 3 ie first value of queid in value table, then it should find all the 3's in ans table and then is should compare a (ie the row corresponding to 3 in value table) with all the 3's in ans. And the corresponding right comparison of a's is to be stored in some third table.
Not entirely sure I understand the question, but I'll take a shot:
Update PsychiciallyDiscernedThirdTable
set m=b.m, an=c.an
from value b
join ans c
on b.queid=c.queid1