I am completely new to SQL and reading StackOverflow messages in SQL to try to figure this out and other sources and could not do it in SQL. Here goes ...
I have a table of 3 columns and thousands of rows with data for the first 2 columns. The third column is currently empty, and I need to populate the third column based on the data already in the first and second columns.
Let's say that I have states in the first column and fruit records in the second column. I need to write an SQL statement (s) that calculates the number of different states in which each fetus comes from , and then inserts this popularity number in the third column for each row. The popularity number 1 on this line means that the fruit comes from only one state, the popularity number 4 means that the fruit comes from 4 states. So my table is currently similar:
state fruit popularity
hawaii apple
hawaii apple
hawaii banana
hawaii kiwi
hawaii kiwi
hawaii mango
florida apple
florida apple
florida apple
florida orange
michigan apple
michigan apple
michigan apricot
michigan orange
michigan pear
michigan pear
michigan pear
texas apple
texas banana
texas banana
texas banana
texas grape
, , , , . , ( ) , "" 4 , , , , 1 , , .
state fruit popularity
hawaii apple 4
hawaii apple 4
hawaii banana 2
hawaii kiwi 1
hawaii kiwi 1
hawaii mango 1
florida apple 4
florida apple 4
florida apple 4
florida orange 2
michigan apple 4
michigan apple 4
michigan apricot 1
michigan orange 2
michigan pear 1
michigan pear 1
michigan pear 1
texas apple 4
texas banana 2
texas banana 2
texas banana 2
texas grape 1
- , - script, SQL , , SQL. , ? / SQL.
- , (-) SQL , , ? , .
SQL- , , , :
SELECT fruit, COUNT(*)
FROM table
GROUP BY fruit
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC
SELECT fruit, COUNT(*)
FROM table
GROUP BY fruit
HAVING COUNT(*) = 1
SELECT COUNT (DISTINCT(fruit))
FROM table