[英]Compare one table with all tables in database
This is sample table data. 这是样本表数据。 My Database consists of so many tables like this. 我的数据库包含如此多的表。 In all the tables where wv1 is same and rv1 is different 在wv1相同而rv1不同的所有表中
wv1 rv1
341.6 2.48
343.6 2.58
344.7 2.37
346.3 2.32
347.9 2.29
349.5 2.36
351.1 2.23
352.6 2.24
354.2 2.25
355.8 2.29
357.4 2.28
358.9 2.23
For comparing one table with another table, I mentioned another table rv1 as rv2 here. 为了将一个表与另一个表进行比较,我在这里提到了另一个表rv1作为rv2。 I am using the data rv1 from user selected tablename, and rv2 for all the tables in the database The formula is 我正在使用用户选择的表名中的数据rv1和数据库中所有表的rv2
PostgreSQL Query for comparing two tables is shown below, I want to compare one table with all the tables in the database and produce i value for each comparison. 下面显示了用于比较两个表的PostgreSQL查询,我想将一个表与数据库中的所有表进行比较,并为每次比较生成i值。
select
acos(sum(t1.rv1 * t2.rv2) / (
sqrt(sum(power(t1.rv1, 2))) * sqrt(sum(power(t2.rv2, 2)))
)) as i
from
t1
inner join
t2 on t1.wv1 = t2.wv2
This query is joining two tables and applying formula described above, then it shows correct output . 该查询将两个表连接起来并应用上述公式,然后显示正确的输出。 Now I want to compare single table(t1 as user defined table, t2 as all the tables in the database) with all the tables in the database and generate I value for each comparison. 现在,我想将单个表(t1作为用户定义的表,t2作为数据库中的所有表)与数据库中的所有表进行比较,并为每次比较生成I值。 I want to apply formula for all the tables in the database. 我想对数据库中的所有表应用公式。
The output should be like this 输出应该是这样的
Final Output 最终输出
Ivalue
0.3559772512926
0.52684312
.............
.............
I want to write the formula in PostgreSQL query, How to write it. 我想在PostgreSQL查询中编写公式,如何编写。 . 。
However you look at it, it comes down to several selects, one per table, which you can combine with UNION ALL: 无论您如何看,它都归结为几个选择,每个表一个,您可以将其与UNION ALL结合使用:
select acos(...
from t1 inner join t2 on t1.wv1 = t2.wv2
UNION ALL
select acos(...
from t1 inner join t3 on t1.wv1 = t3.wv3
UNION ALL
...
If I understand this correctly, you actually want to compare different sets that all 'look alike' (that is 2 columns, same number of rows, identical wv1 values) and each set is stored in a new table, right ? 如果我正确理解这一点,那么您实际上想比较所有“看起来都一样”的不同集合(即2列,相同的行数,相同的wv1值),并且每个集合都存储在一个新表中,对吗?
My first remark would be: why didn't you store all the sets in the same table and add an extra column (eg. set_id) that points to which set the value-pair belongs. 我的第一句话是:为什么不将所有集合存储在同一张表中,并添加一个额外的列(例如set_id)来指向值对所属的集合。 If that would have been the case you could have created a query like this: 如果是这样的话,您可以创建如下查询:
select t1.set_id as left_set_id,
t2.set_id as right_set_id,
acos(sum(t1.rv1 * t2.rv2) / ( sqrt(sum(power(t1.rv1, 2))) * sqrt(sum(power(t2.rv2, 2))))) as i
from bigtable as t1
inner join bigtable as t2
on t1.wv1 = t2.wv2
AND t1.set_id > t2.set_id
GROUP BY t1.set_id,
t2.set_id
ORDER BY t1.set_id,
t2.set_id
Note: the t1.set_id > t2.set_id
avoids that you compare a set with itself + it seems (to me) that the relationship is symmetrical anyway so you only need to compare sets in one direction. 注意: t1.set_id > t2.set_id
避免了将集合与自身进行比较+(对我而言)该关系无论如何都是对称的,因此您只需要在一个方向上比较集合即可。
Update, if you want to compare just 1 set with all the other sets, you'll have to use it like this : 更新,如果您只想将一个集合与所有其他集合进行比较,则必须像这样使用它:
select t2.set_id,
acos(sum(t1.rv1 * t2.rv2) / ( sqrt(sum(power(t1.rv1, 2))) * sqrt(sum(power(t2.rv2, 2))))) as i
from bigtable as t1
inner join bigtable as t2
on t1.wv1 = t2.wv2
AND t1.set_id <> t2.set_id
WHERE t1.set_id = '<base set identifier>'
GROUP BY t2.set_id
ORDER BY t2.set_id
Now, since we don't have this, we'll have to fake it. 现在,由于我们没有这个,我们将不得不伪造它。 To do so I would suggest to create a view that UNION ALL
s all your tables and then you can run the query above on the view instead of on bigtable
为此,我建议创建一个将所有表都使用UNION ALL
的视图,然后可以在视图上而不是bigtable
上运行上面的查询
CREATE VIEW bigview
AS
SELECT 'table1' as set_id, wv1, rv1 FROM table1
UNION ALL
SELECT 'table2' as set_id, wv1, rv1 FROM table2
UNION ALL
SELECT 'table3' as set_id, wv1, rv1 FROM table3
UNION ALL
etc...
This view can then be re-used regardless for which set (= table) you are comparing, you will have to make sure though that it includes all the tables (= sets) you want to be included in the comparison. 然后,无论您要比较哪个集(=表),都可以重复使用此视图,尽管该视图必须包括要包含在比较中的所有表(=集),但您必须确保此视图。
PS: you might have to work a bit on the syntax, don't have PostgreSQL available right here. PS:您可能需要在语法上做一些工作,这里没有PostgreSQL。 PS: the documentation mentions you can use @
to calculate the absolute value of a number; PS: 文档中提到您可以使用@
来计算数字的绝对值; I'm guessing that would be easier than taking the root of the square. 我想这会比平方根更容易。 It surely will be less prone to overflows. 它肯定会更不容易溢出。
It is made through JSP + PostgreSQL 它是通过JSP + PostgreSQL制作的
String ss1 = "SELECT UPPER(table_name)FROM information_schema.tables where table_schema='public' and table_type='BASE TABLE' ORDER BY table_name ASC;";
stmt = connection.createStatement();
rset=stmt.executeQuery(ss1);
The String ss1 is passed as dbTab 字符串ss1作为dbTab传递
String usTab ="oak";
while (rset.next()) {
String sname1 = rset.getString(1);
String dbTab = sname1.toUpperCase();
try
{
out.println("<tr><td>" + dbTab + "</td><td>"+ usTab + " vs " + dbTab + "</td>");
if(dbTab.equals(usTab)) continue;
Connection connection = DriverManager.getConnection(ss, "postgres", "acheive9");
String ss2 = "select acos(sum("+usTab+".reflectance * "+dbTab+".reflectance) / ( sqrt(sum(power("+usTab+".reflectance, 2))) * sqrt(sum(power("+dbTab+".reflectance, 2))))) as i from "+usTab+" inner join "+dbTab+" on "+usTab+".wavelength = "+dbTab+".wavelength";
stmt2 = connection.createStatement();
rset2=stmt2.executeQuery(ss2);
while (rset2.next()) {
String sname17 = rset2.getString(1);
out.println("<td>"+sname17+"</td></tr>");
}
This Code is working.. 该代码正在工作..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.