oracle sql query

Hi,
 
I need to write a query that will return the table_name, and the difference in number of rows i.e the difference between the maximum and minimum rows recorded for the table which would be from the maxium and minumium dates for the rows - it can be assumed the minimum dates is the minium rows etc

my table columns are:

table_name, monitor_date, num_rows

thanks
H





thanks
H
LVL 1
hraja77Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

slightwv (䄆 Netminder) Commented:
I do not understand the question.

Are you saying you have a lookup type table that tells you how many rows should in in a different table?

I do not understand what defines the min and max rows of a table based on some date.
0
sdstuberCommented:
is this what you mean?

SELECT table_name, min_value, max_value, max_value - min_value difference
  FROM (SELECT DISTINCT
               FIRST_VALUE(
                   num_rows)
               OVER (PARTITION BY table_name
                     ORDER BY monitor_date
                     ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
                    )
                   min_value,
               LAST_VALUE(
                   num_rows)
               OVER (PARTITION BY table_name
                     ORDER BY monitor_date
                     ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
                    )
                   max_value
          FROM your_table)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sdstuberCommented:
or,  even simpler,  if you can reliably assume that your num_rows will grow over time then you don't really need to include the time component at all.


SELECT   table_name,
         MIN(num_rows) min_value,
         MAX(num_rows) max_value,
         MAX(num_rows) - MIN(num_rows) difference
    FROM your_table
GROUP BY table_name


but, again, this assumes the table starts small and the number of rows never decreases, only constant or increasing.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

awking00Commented:
Worse yet, it assumes ALL tables never delete any rows. Are you sure that occurs?
0
sdstuberCommented:
>>> it assumes ALL tables never delete any rows.

no it didn't,  because the group by/partition by makes the values table specific
0
awking00Commented:
I was referring to the possibility that num_rows may not reliably grow over time for any table.
0
sdstuberCommented:
well, that was already mentioned as one of the assumptions


the first query compares the earliest count with the latest count, whether they are truly the smallest and biggest or not.


the second query compares the smallest and biggest counts whether they are truly the earliest and latest or not.


I can't think of any other combintion of early/late, small/big that would have any meaning with with sample structure given.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.