Oracle 8i中的rowcount查询

时间:2015-10-30 21:17:38

标签: sql oracle oracle8i

我有一个表Department_history,这是每个部门的记录数。

select department_name, Id, count(1) total_rows_COUNT from Department_history
where 
group by Department_history,id
order by 2 desc

结果:

department_name         ID     total_rows_COUNT 
Accounting            4564     556
Finance               3434     671
Marketing             4353  234
IT                  1233    454

我想只保留表格中每个部门的10条记录。

如果对此类每个部门运行以下查询,则可以正常工作。

delete from Department_history a1 
where 
and a1.report_runtime NOT IN
(
   select report_runtime
   from (
       select a.*, rank() over ( partition by department_name, id  order by report_runtime desc) r
       from Department_history a
    ) rs
     where r <= 10 and department_name = 'Accounting'

)
and department_name = 'Accounting'

但我不想为每个部门单独运行此删除操作。 如何运行一个删除每个department_name数据的查询(如果它是&gt; 10条记录)。我试试这个。但它不起作用。

delete from Department_history a1 
where 
and a1.report_runtime NOT IN
(
   select report_runtime
   from (
       select a.*, rank() over ( partition by department_name, id  order by report_runtime desc) r
       from Department_history a
    ) rs
     where r <= 10 

)

有人可以提出建议吗?

2 个答案:

答案 0 :(得分:0)

test sample:

create table tst as select owner, view_name from all_views;

delete from tst
  where owner||'::'||view_name in
(  
select owner||'::'||view_name from (
select owner, view_name, rank() over (partition by owner order by view_name) r from tst
) where r > 10
)

your updated sql:

delete from Department_history
where id NOT IN
(
   select id
   from (
       select id, rank() over ( partition by department_name  
order by report_runtime desc) rnk
from Department_history
    ) s
     where s.rnk <= 10    
)

答案 1 :(得分:0)

试试这个。它与您已经尝试过的类似,但using System; namespace IntegerStatistics { class Program { static void Main() { Console.Clear(); // Declaring variables int[] userArray = FillArray(); int ArrayHighest = 0; int ArrayLowest = 0; int ArraySum = 0; int ArrayAverage = 0; Calculations(userArray, out ArrayHighest, out ArrayLowest, out ArraySum, out ArrayAverage); Console.WriteLine("The lowest value in the array is {0}, while the highest is {1}.", ArrayLowest, ArrayHighest); Console.WriteLine("The array has a sum of {0} and averages out to {1}.", ArraySum, ArrayAverage); Console.ReadLine(); } private static int[] FillArray() { int[] intArray = new int[10]; int numbersEntered = 0; int intTemp = 0; string strTemp = ""; for(int k = 0; k < 10; ++k) { Console.WriteLine("Enter a whole number or 999 to quit: "); strTemp = Console.ReadLine(); while(!int.TryParse(strTemp, out intTemp)) { Console.WriteLine("Input was not in the correct format"); Console.Write("Please enter a valid number: "); strTemp = Console.ReadLine(); } } if(intTemp != 999) { intArray[k] = intTemp; ++numbersEntered; } else { k = 10; } Array.Resize(ref intArray, numbersEntered); return intArray; } private static void Calculations(int[] intArray, out int Highest, out int Lowest, out int intSum, out int average) { intSum = 0; Array.Sort(intArray); Lowest = intArray[0]; Array.Reverse(intArray); Highest = intArray[0]; Array.Reverse(intArray); for(int k = 0; k < intArray.Length; ++k) { intSum += intArray[k]; } average = intSum / intArray.Length; } } } 子句应该检查所有3个标识列if (intTemp != 999) { intArray[k] = intTemp; ++numbersEntered; } else { k = 10; } ,而不仅仅是not in。否则,您可能会意外删除错误的行。

我还使用department_name, id, report_runtime代替report_runtime。我认为这是满足您需求的更好选择。虽然根据您对数据的描述,我认为在这种情况下它实际上没有什么区别。

row_number