Calculate average of values between 2 columns sql

Bobbzorzen

I have a table called validation_errors that looks like this:

+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| link        | varchar(200) | NO   | MUL | NULL    |                |
| message     | varchar(500) | NO   |     |         |                |
| explanation | mediumtext   | NO   |     | NULL    |                |
| type        | varchar(50)  | NO   |     |         |                |
| subtype     | varchar(50)  | NO   |     |         |                |
| message_id  | varchar(50)  | NO   |     |         |                |
+-------------+--------------+------+-----+---------+----------------+

Link table looks like this:

+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| link      | varchar(200) | NO   | PRI | NULL    |       |
| visited   | tinyint(1)   | NO   |     | 0       |       |
| validated | tinyint(1)   | NO   |     | 0       |       |
+-----------+--------------+------+-----+---------+-------+

I wish to calculate the average number of validation errors per page per topdomain. I have a query that can fetch the amount of pages per topdomain:

    SELECT substr(link, - instr(reverse(link), '.')) as domain , count(*) as count
    FROM links
    GROUP BY domain
    ORDER BY count desc
    limit 30;

And have a sql query that can fetch the amount of validation errors per top domain:

    SELECT substr(link, - instr(reverse(link), '.')) as domain ,count(*) as count
    FROM validation_errors
    GROUP BY domain
    ORDER BY count desc
    limit 30;

What i now need to do is combine them into a query and divise the results of one column with the other and i can't figure out how to do it.

Any help would be greatly apriciated.

Gordon Linoff

First, use substring_index(), rather than your construct. Here is the query to join them together:

select domain, sum(numviews) as numviews, sum(numerrors) as numerrors,
       sum(numerrors) / nullif(sum(numviews), 0) as error_rate
from ((SELECT substring_index(link, '.', -1) as domain , count(*) as numviews, 0 as numerrors
       FROM links
       GROUP BY domain
      ) UNION ALL
      (SELECT substring_index(link, '.', -1) as domain , 0, count(*)
       FROM validation_errors
       GROUP BY domain
      )
     ) d
GROUP BY domain;

With both variables, I don't know which 30 you want to choose, so I haven't included an order by.

Note that this doesn't use a join, it uses union all with aggregation. This ensures that you will get all domains, even those with no views and those with no errors.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Calculate average days between events?

분류에서Dev

How to calculate the difference between two java.sql.Time values?

분류에서Dev

Method to calculate the average without storing individual values

분류에서Dev

Calculate average values for rows with different ids in MS Excel

분류에서Dev

SQL - difference and average between more records

분류에서Dev

Calculating Average in Google doc based on values in other columns

분류에서Dev

Calculate average for a sequence of strings, then remove anything greater than 2SD of the average in R

분류에서Dev

Difference between this 2 values?

분류에서Dev

Format the average price to 2 decimal places in SQL

분류에서Dev

Trying to calculate the average in an ArrayList

분류에서Dev

Calculate average with xslt

분류에서Dev

What is the best way to calculate difference between TIMESTAMP values in Ruby on Rails

분류에서Dev

Calculate age when date month and year are in different columns in sql server

분류에서Dev

SQL rows to columns (Pivot with just bit values)

분류에서Dev

Calculate Median with SQL (DB2)

분류에서Dev

Average of 4 slider values

분류에서Dev

mySQL: Finding the most common values between two columns

분류에서Dev

How to calculate angle between two 2D matrix transform?

분류에서Dev

SQL - Difference between SPACE(2) + '|' + SPACE(2) and ‘ | ‘?

분류에서Dev

How to calculate an average value based on duplicate groups?

분류에서Dev

Case when in where clause to choose between two columns ,SQL Server

분류에서Dev

Inserting Multiple values into a single null columns using sql

분류에서Dev

SQL copy an existing columns values into a custom column in a select statement

분류에서Dev

Remove columns from SQL output where all values are 0

분류에서Dev

SQL query to return maximum values from multiple columns

분류에서Dev

Remove items with values beyond average

분류에서Dev

Comparison between the first columns of 2 different files with awk

분류에서Dev

SQL where exists while preserving values between them

분류에서Dev

Calculate the 2 columns from table and insert the result as other column in the same table

Related 관련 기사

  1. 1

    Calculate average days between events?

  2. 2

    How to calculate the difference between two java.sql.Time values?

  3. 3

    Method to calculate the average without storing individual values

  4. 4

    Calculate average values for rows with different ids in MS Excel

  5. 5

    SQL - difference and average between more records

  6. 6

    Calculating Average in Google doc based on values in other columns

  7. 7

    Calculate average for a sequence of strings, then remove anything greater than 2SD of the average in R

  8. 8

    Difference between this 2 values?

  9. 9

    Format the average price to 2 decimal places in SQL

  10. 10

    Trying to calculate the average in an ArrayList

  11. 11

    Calculate average with xslt

  12. 12

    What is the best way to calculate difference between TIMESTAMP values in Ruby on Rails

  13. 13

    Calculate age when date month and year are in different columns in sql server

  14. 14

    SQL rows to columns (Pivot with just bit values)

  15. 15

    Calculate Median with SQL (DB2)

  16. 16

    Average of 4 slider values

  17. 17

    mySQL: Finding the most common values between two columns

  18. 18

    How to calculate angle between two 2D matrix transform?

  19. 19

    SQL - Difference between SPACE(2) + '|' + SPACE(2) and ‘ | ‘?

  20. 20

    How to calculate an average value based on duplicate groups?

  21. 21

    Case when in where clause to choose between two columns ,SQL Server

  22. 22

    Inserting Multiple values into a single null columns using sql

  23. 23

    SQL copy an existing columns values into a custom column in a select statement

  24. 24

    Remove columns from SQL output where all values are 0

  25. 25

    SQL query to return maximum values from multiple columns

  26. 26

    Remove items with values beyond average

  27. 27

    Comparison between the first columns of 2 different files with awk

  28. 28

    SQL where exists while preserving values between them

  29. 29

    Calculate the 2 columns from table and insert the result as other column in the same table

뜨겁다태그

보관