1

我有以下问题:我想计算一个表中的数据,计算第二个表中的数据并比较具有子句中的计数,并且只显示具有相同计数的行类似这样的东西:

SELECT bla
FROM T1 t1 JOIN T2 t2
ON t1.id = t2.id
HAVING COUNT(counted data from table1) = COUNT(counted data from table2)

你有什么主意吗?干杯

4

2 回答 2

1

标准 SQL:

SELECT t1.bla, t1.id, t1.counter, t2.counter
  FROM (SELECT t1.bla, t1.id, COUNT(counted_data_from_t1) AS counter
          FROM t1
         GROUP BY t1.bla, t1.id
       ) AS t1
  JOIN (SELECT t2.id, COUNT(counted_data_from_t2) AS counter
          FROM t2
         GROUP BY t2.id
       ) AS t2
    ON t1.id = t2.id AND t1.counter = t2.counter

Oracle SQL(因为 Oracle 在表别名之前不喜欢 AS):

SELECT t1.bla, t1.id, t1.counter, t2.counter
  FROM (SELECT t1.bla, t1.id, COUNT(counted_data_from_t1) AS counter
          FROM t1
         GROUP BY t1.bla, t1.id
       ) t1
  JOIN (SELECT t2.id, COUNT(counted_data_from_t2) AS counter
          FROM t2
         GROUP BY t2.id
       ) t2
    ON t1.id = t2.id AND t1.counter = t2.counter

你只需要决定bla从哪里来;我提名了t1。我假设对于任何给定的 值t1.id,都有一个 的值t1.bla。如果没有,那么您需要更清楚地解释您在数什么,各个列在哪里,以及表的键是什么。

更新:很抱歉没有注意到 Oracle 标签并给出了无效的 Oracle 语法。

于 2012-04-10T16:19:08.690 回答
0
WITH jezyki as 
          (SELECT pseudo_wampira, COUNT(*) AS counter
            FROM Jezyki_obce_w
            GROUP BY pseudo_wampira
          )
    ,sprawnosc as 
         (SELECT pseudo_wampira, sprawnosc, COUNT(*) AS counter
          FROM Sprawnosci_w
         GROUP BY pseudo_wampira, sprawnosc 
         )
      SELECT jezyki.pseudo_wampira, sprawnosc.counter
        FROM jezyki,sprawnosc
       WHERE  jezyki.pseudo_wampira = sprawnosc.pseudo_wampira 
         AND  jezyki.counter = sprawnosc.counter
于 2012-04-10T17:41:13.927 回答