Count with Group By always shows '1' - sql

I'm trying to count the number of articles mentioning Donald Trump exist in a Google BigQuery table.
SELECT
sourcecommonname,
COUNT(DISTINCT sourcecommonname) counter
FROM
`israel_media`
WHERE
persons LIKE '%donald trump%'
GROUP BY
sourcecommonname
Results are always
sourcecommonname
counter
first_newspaper
1
second_newspaper
1
third_newspaper
1
forth_newspaper
1
What am I not seeing?

Of course. You are counting distinct values. But there is only one value per group. That is what the group by does.
Perhaps you just want count() without distinct:
SELECT sourcecommonname,
COUNT(*) as counter
FROM `israel_media`
WHERE persons LIKE '%donald trump%'
GROUP BY sourcecommonname

Related

SQL Query to create a column to determine count from historical data

Given: columns "basket" & "Fruit".
Output: Column "Count present in all the previous basket"
How to check if a fruit in a basket is present in all the preceding baskets and get the total count present?
For ex: Basket 2 contains Berry, Banana and Orange, now i need to check basket 1 to determine the count of these fruits. In the same way, for the fruits in basket 3, basket 1 and basket 2 are checked.
How can i do this using an SQL query? Currently i'm doing this on the application side using loops, rowfilter etc which consumes a lot of times as i've more than million rows.
You can also go with a window function I think. I am subtracting 1 to avoid the first count for each fruit. Maybe someone can provide a more elegant solution.
select *,
(count(*) over (partition by fruit order by basket) - 1)
from t
order by basket, fruit;
It appears you need a simple correlated subquery, such as:
select *, (
select Count(*) from t t2
where t2.basket < t.basket
and t2.fruit = t.fruit
) "Count in prev baskets"
from t;

Select query to fetch required data from SQL table

I have some data like this as shown below:
Acc_Id || Row_No
1 1
2 1
2 2
2 3
3 1
3 2
3 3
3 4
and I need a query to get the results as shown below:
Acc_Id || Row_No
1 1
2 3
3 4
Please consider that I'm a beginner in SQL.
I assume you want the Count of the row
SELECT Acc_Id, COUNT(*)
FROM Table
GROUP BY Acc_Id
Try this:
select Acc_Id, MAX(Row_No)
from table
group by Acc_Id
As a beginner then this is your first exposure to aggregation and grouping. You may want to look at the documentation on group by now that this problem has motivated your interest in a solutions. Grouping operates by looking at rows with common column values, that you specify, and collapsing them into a single row which represents the group. In your case values in Acc_Id are the names for your groups.
The other answers are both correct in the the final two columns are going to be equivalent with your data.
select Acc_Id, count(*), max(Row_No)
from T
group by Acc_Id;
If you have gaps in the numbering then they won't be the same. You'll have to decide whether you're actually looking for a count of rows of a maximum of a value within a column. At this point you can also consider a number of other aggregate functions that will be useful to you in the future. (Note that the actual values here are pretty much meaningless in this context.)
select Acc_Id, min(Row_No), sum(Row_No), avg(Row_No)
from T
group by Acc_Id;

sql count or sum?

I'm a newbie programmer, I want to sum a value of employee's attendance record
Anyway, what should I choose? COUNT or SUM?
I tried to use COUNT functions like this...
SELECT COUNT(jlh_sakit) AS sakit FROM rekap_absen
It shows value changed to "1" for 1 Record only.
And I try to use SUM functions like this...
SELECT SUM(jlh_sakit) AS sakit FROM rekap_absen
It shows all values changed ALL value to "1"
I want to display only 1 person for each sum
(e.g : John (2 sick, 2 permissions, 1 alpha)
Can you help me please?
If you are using any aggregate function like min/max/sum/count you should use group by. Now your question says "what should I choose? COUNT or SUM?" Assuming you have person_name, jlh_sakit which means sick/permission/alpha in your case you could use
select person_name, count(jhl_sakit) as attribute
from rekap_absen
group by person
This will give you output like:
person_name attribute
John 2
King 5
In order to sum by specified column, use group by statement.
SELECT SUM(sick),SUM(alpha),SUM(permissions),person FROM rekap_absen group by person
It will group your sums according to person.
You may name your sums like:
SELECT SUM(sick) as sick,SUM(alpha) as alpha,SUM(permissions) as permissions,person FROM rekap_absen group by person
Assuming that you have table rekap_absen with columns: person,sick,alpha,permissions

SQL select query for ranking

I have this table called logs that logs a who input or output data.
Now I wan't to get the statistics of who has the most contributions and rank them.
Columns are
Occur_Time | iUser_id | iUsername | oUser_id | oUsername
--iUser_id is the input persons index from another table that lists the username.
--iUsername is the input persons name.
--oUser_id is the index of the person who took the input away.
--oUsername is the name of the person who took the input away.
Now I wan't to know who has the most input.
My logic:
Example:
User_id is 1, name is One.
Check how many times 1 is repeated on iUser_id = 100 times.
Check how many times 1 is repeated on oUser_id = 10 times.
User_id=1 has contributed 90 times.
Then sort by who has most contribution.
Thank you.
(untested):
SELECT L.iUsername,
((SELECT COUNT(1) FROM logs WHERE iUsername=L.iUsername) -
(SELECT COUNT(1) FROM logs WHERE oUsername=L.iUsername)) as rank
FROM logs L
GROUP BY L.iUsername
ORDER BY rank ASC
The Rank feature is probably what you are looking for.
http://msdn.microsoft.com/en-us/library/ms176102.aspx
Try that query.
Select user_id, count(user_id) from tablename group by user_id;

Retrieve names by ratio of their occurrence

I'm somewhat new to SQL queries, and I'm struggling with this particular problem.
Let's say I have query that returns the following 3 records (kept to one column for simplicity):
Tom
Jack
Tom
And I want to have those results grouped by the name and also include the fraction (ratio) of the occurrence of that name out of the total records returned.
So, the desired result would be (as two columns):
Tom | 2/3
Jack | 1/3
How would I go about it? Determining the numerator is pretty easy (I can just use COUNT() and GROUP BY name), but I'm having trouble translating that into a ratio out of the total rows returned.
SELECT name, COUNT(name)/(SELECT COUNT(1) FROM names) FROM names GROUP BY name;
Since the denominator is fixed, the "ratio" is directly proportional to the numerator. Unless you really need to show the denominator, it'll be a lot easier to just use something like:
select name, count(*) from your_table_name
group by name
order by count(*) desc
and you'll get the right data in the right order, but the number that's shown will be the count instead of the ratio.
If you really want that denominator, you'd do a count(*) on a non-grouped version of the same select -- but depending on how long the select takes, that could be pretty slow.