如何从table1计数row1 /从table2计数row1返回的结果是按天分组

时间:2014-08-29 05:44:07

标签: sql oracle division

我知道这一点,但我知道它不起作用。

select date_decision,
      (select count(*) from table2)/
      (select count(*) from table1)
from dual
group by date_decision

Date_decision来自table2,table1和table2之间有一个公共行,即Case_number

但是当它来自双表时我无法分组。我试着从table1中获得1天内的计数除以table2中1天的计数。

表1

 Case_number       Date_application
  39123             01-Jan-14
  17724             05-Jan-14
  13455             01-Jan-14
  42314             03-Jan-14
  10485             02-Jan-14
  38762             02-Jan-14
  93974             03-Jan-14
  17388             01-Jan-14
  29384             03-jan-14
  65438             05-Jan-14
  22312             04-Jan-14

表2

CASE_NUMBER     DATE_DECISION
  39123         01-Jan-14
  17724         05-Jan-14
  13455         01-Jan-14
  42314         03-Jan-14
  10485         02-Jan-14
  38762         02-Jan-14
  93974         03-Jan-14

我希望结果像

Date              Ratio
01-JAN-2014       85.84%
02-JAN-2014       75.33%
03-JAN-2014       82.35%
04-JAN-2014       84.93%
...               ...

2 个答案:

答案 0 :(得分:0)

您可以在以下所有日期找到两个表格中的案例数量的比率。我认为它效率不高,但它确实有用。

SELECT A.Date_application ALLDATES, ( (A.CNT / B.CNT) * 100) || '%' RATIO
  FROM    (  SELECT t1.Date_application, COUNT (*) CNT
               FROM table1 t1
           GROUP BY t1.Date_application) A
       JOIN
          (  SELECT t2.DATE_DECISION, COUNT (*) CNT
               FROM table2 t2
           GROUP BY t2.DATE_DECISION) B
       ON A.Date_application= B.DATE_DECISION

答案 1 :(得分:0)

您想找出决定的案件数量与特定日期的案件数量的比率吗?

您可以在日期列上加入这两个表,按日期分组,计算不同的案例数。将这两个计数除以你的比率。

SQL Fiddle

  SELECT table1.date_application "date",
         COUNT (DISTINCT table1.case_number) cases_applied,
         COUNT (DISTINCT table2.case_number) cases_decided,
            ROUND (
                 COUNT (DISTINCT table2.case_number)
               * 100
               / COUNT (DISTINCT table1.case_number),
               2)
         || '%'
            percentage_cases_decided
    FROM    table1
         LEFT OUTER JOIN
            table2
         ON table1.date_application = table2.date_decision
GROUP BY table1.date_application
ORDER BY table1.date_application

<强> Results

|                           DATE | CASES_APPLIED | CASES_DECIDED | PERCENTAGE_CASES_DECIDED |
|--------------------------------|---------------|---------------|--------------------------|
| January, 01 2104 00:00:00+0000 |             3 |             2 |                   66.67% |
| January, 02 2104 00:00:00+0000 |             2 |             1 |                      50% |
| January, 03 2104 00:00:00+0000 |             3 |             3 |                     100% |
| January, 04 2104 00:00:00+0000 |             1 |             0 |                       0% |
| January, 05 2104 00:00:00+0000 |             2 |             1 |                      50% |