How to compare two different rows of two different tables? - 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.

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

Related

using list as an argument in groupby() in pandas and none of the key elements match column or index names

So I have a random values of dataframe as below and a book I am studying uses a list was groupby key (key_list). How is the dataframe grouped in this case since none of list values match column or index names? So, the last two lines are confusing to me.
people = pd.DataFrame(np.random.randn(5,5), columns = ['a','b','c','d','e'], index=['Joe','Steve','Wes','Jim','Travis'])
key_list = ['one','one','one','two','two']
people.groupby(key_list).min()
people.groupby([len, key_list]).min()
Thank you in advance!
The user guide on groupby explains a lot and I suggest you have a look at it. I'll explain as much as I understand for your use case.
You can verify the groups created using the group method:
people.groupby(key_list).groups
{'one': Index(['Joe', 'Steve', 'Wes'], dtype='object'),
'two': Index(['Jim', 'Travis'], dtype='object')}
You have your dictionary with the keys 'one' and two' being the groups from the key_list list. As such when you ask for the 'min', it looks at each group and picks out the minimum, indexed from the first column. Let's inspect group 'one' using the get_group method:
people.groupby(key_list).get_group('one')
a b c d e
Joe -0.702122 0.277164 1.017261 -1.664974 -1.852730
Steve -0.866450 -0.373737 1.964857 -1.123291 1.251595
Wes -0.043835 -0.011108 0.214802 0.065022 -1.335713
You can see that Steve has the lowest value from column 'a'. when you run the next line it should give you that:
people.groupby(key_list).get_group('one').min()
a -0.866450
b -0.373737
c 0.214802
d -1.664974
e -1.852730
dtype: float64
The same concept applies when you run it on the second group 'two'. As such, when you run the first part of your groupby code:
people.groupby(key_list).min()
You get the minimum row indexed at 'a' for each group:
a b c d e
one -0.866450 -0.373737 0.214802 -1.664974 -1.852730
two -1.074355 -0.098190 -0.595726 -2.194481 0.232505
The second part of your code, which involves the len applies the same grouping concept. In this case, it groups the dataframe according to the length of the strings in its index: (Jim, Joe, Wes) - 3 letters, (Steve) - 5 letters, (Travis) - 6 letters, and then groups with the key_list to give the final output:
a b c d e
3 one -0.702122 -0.011108 0.214802 -1.664974 -1.852730
two -0.928987 -0.098190 3.025985 0.702471 0.232505
5 one -0.866450 -0.373737 1.964857 -1.123291 1.251595
6 two -1.074355 1.110879 -0.595726 -2.194481 0.394216
Note that for 3 it spills out 'one' and 'two' because 'Joe' and 'Wes' are in group 'one' but the lowest is 'Joe', while 'Jim' is the only three letter word in group 'two'. The same concept goes for 5 letter and 6 letter words.

How to condition on odd or even across rows in dataframe

I am looking to create a new variable based on the nature of other variables in the same observation
Column C should be "Keep" if all values in Columns A and B are not each Even or not each Odd, otherwise value in Column C should be remove. Ideally, I'd just like the result to retain only the "Keeps"
I've typed out what I want to achieve programmatically in Column C
b<-data.frame(matrix(c(1, 3, 4, 5, 2, 8), nrow=3, ncol=2))
c<-data.frame(c("remove","keep","remove"))
x<-cbind(b,c)
names(x)<-c("A","B","C")
x
Ideally, I'd just like the result to retain only the "Keeps"

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)

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 and update a third table?

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
;