This Online Tech Support tutorial page is based on examples to make it easier to follow. Function Oracle REGR_COUNT allows counting the non-null number pairs and to fit them to the regression line. The REGR_COUNT function returns an integer value. The definition in short is that you can count two number pairs where values are not empty. The Oracle Count function allows counting only one number group but with REGR_COUNT you can do with 2 number groups and keep your SQL query smaller and simpler. The Oracle REGR_COUNT syntax of an aggregate function is following:
REGR_COUNT(<number>,<number>)
And the next syntax is the REGR_COUNT analytic function. This version does calculate on-fly and doesn’t need Group By clause.
REGR_COUNT(<number>,<number>) OVER ([<analytic_clause>])
To go through the examples with Oracle REGR_COUNT we will need some lines and a table. To save the time we will do it all dynamically generating the rows using the Oracle Dual table and CONNECT BY clause. All you have to do is copy and paste the SQL query and all the following rows will appear on your screen.
SELECT MOD(ROWNUM,4) product_Id, DECODE(ROWNUM,2,NULL,ROWNUM) AS Bought_From_Id, DECODE(rownum,8,NULL,rownum) AS Sold_To_Id FROM dual CONNECT BY ROWNUM < 11;
Image may be NSFW.
Clik here to view.
The SQL output describes product movements in a warehouse. The first column is a PRODUCT_ID it is an ID value from the PRODUCTS table. For testing purpose we left in zero values that are not so common in a real system. The second column BOUGHT_FROM_ID is an ID column that points to source from where the product is bought. The third column describes the location we did sell the product to. Take a look at the row there is a source missing on the second line and we haven’t sold a product on the 7th line (Product_Id is 0 and Bought_From_Id is 8). The meaning of this data doesn’t play much role for those examples but this description is done do give more sense to the numbers.
The first example is done with Oracle Count and the query above. Take a look at the output it returned 4 product Ids and the function did count 3,3,2,2 rows. The output is correct if you would need to count them only.
SELECT PRODUCT_ID, COUNT(*) FROM ( SELECT MOD(ROWNUM,4) PRODUCT_ID, DECODE(ROWNUM,2,NULL,ROWNUM) AS BOUGHT_FROM_ID, DECODE(ROWNUM,8,NULL,ROWNUM) AS SOLD_TO_ID FROM DUAL CONNECT BY ROWNUM < 11) GROUP BY product_id;
Image may be NSFW.
Clik here to view.
The same query as the last one only with the Oracle REGR_COUNT function returns 4 product Id lines but the count returns 3,2,2,1. The Oracle Count has star (*) in the function and that condition doesn’t check anything beside the product Id value, so the function returned all available rows per product Id. When using the Oracle REGR_COUNT we need to declare the columns in the function and the REGR_COUNT function counts only non-null number pairs. On the first query you could see 2 rows where one of the values was NULL – those rows are left out from the count and that made the difference in output.
SELECT PRODUCT_ID, REGR_COUNT(BOUGHT_FROM_ID, SOLD_TO_ID) FROM ( SELECT MOD(ROWNUM,4) PRODUCT_ID, DECODE(ROWNUM,2,NULL,ROWNUM) AS BOUGHT_FROM_ID, DECODE(ROWNUM,8,NULL,ROWNUM) AS SOLD_TO_ID FROM DUAL CONNECT BY ROWNUM < 11) GROUP BY PRODUCT_ID;
Image may be NSFW.
Clik here to view.
To see how will work the Oracle Count when you do apply on every column separately take a look at the query below. The query demonstrates that REGR_COUNT works as Oracle Count only it does the same per pair.
SELECT PRODUCT_ID, COUNT(BOUGHT_FROM_ID), COUNT(SOLD_TO_ID) FROM ( SELECT MOD(ROWNUM,4) PRODUCT_ID, DECODE(ROWNUM,2,NULL,ROWNUM) AS BOUGHT_FROM_ID, DECODE(ROWNUM,8,NULL,ROWNUM) AS SOLD_TO_ID FROM DUAL CONNECT BY ROWNUM < 11) GROUP BY PRODUCT_ID;
Image may be NSFW.
Clik here to view.
The output above shows on the second column COUNT(BOUGH_FROM_ID) has left out a row for the second line product with Id 2. The third column has missing row for Product_Id 0. The both rows were left out because they had null values in it and that proves the both functions are working with the same logic.
The 5th query is done using Oracle Count and using the additional restriction the WHERE clause we can get the same output as with Oracle REGR_COUNT. The down side for this is that the query is much more complicated now.
SELECT PRODUCT_ID, COUNT(*) FROM ( SELECT MOD(ROWNUM,4) PRODUCT_ID, DECODE(ROWNUM,2,NULL,ROWNUM) AS BOUGHT_FROM_ID, DECODE(ROWNUM,8,NULL,ROWNUM) AS SOLD_TO_ID FROM DUAL CONNECT BY ROWNUM < 11) WHERE BOUGHT_FROM_ID IS NOT NULL AND SOLD_TO_ID IS NOT NULL GROUP BY PRODUCT_ID;
Image may be NSFW.
Clik here to view.
The summary would be that Oracle REGR_COUNT makes your SQL query smaller but there is always work around to get the same result using Oracle Count function.
See Also:
Oracle Select Oracle Count Oracle Group By Oracle Having Online Tech Support Home