I have a table recommendation with fields recommender, attractionid
I want to count the how many attractionid existed in the table group by the attractionid but if there are same pairs of recommender and attractionid they are counted as 1.
For example,
attractionid recommender
1 1
1 2
1 1
2 3
2 1
2 2
2 2
2 2
expected result :
attractionid count
1 2
2 3
the rows below should be counted as 1
attractionid recommender
1 1
1 1
2 2
2 2
2 2
Use distinct attractionid,recommender inside count function.
Query
select attractionid,
count(distinct attractionid,recommender) as `count`
from recommendation
group by attractionid;
Try:
select attractionid, count(recommender) cnt
from (
select distinct attractionid, recommender
from recommendation
) x
group by attractionid
Related
can anyone help me to group every 3 counts rows???
I have data like this
num
score
1
3
1
3
1
3
1
3
1
3
1
3
4
3
4
3
4
3
2
3
2
3
2
3
and i want result like this
num
count(num)
1
3
1
3
2
3
4
3
You want to group every 3 rows. But without giving supplementary conditions, we can only take assumptions on how you would like to group. Supposing each row in the 3-rows group has the same content and the score value from the base table has no influence on grouping, I guess you just want to get the num once for every 3 rows as the 3 rows have the same num. Besides, we use a constant value 3 for count(num) column. If all my assumptions come true, try this:
select num, 3 as 'count(num)'
from (select num,#row_id:=#row_id+1 as row_id
from test,(select #row_id:=0) t1 ) t2
where row_id mod 3 =0
order by num
;
Initially we have such a table
contact_id
group_id
1
1
2
1
2
3
3
1
3
3
3
2
1
2
After that I make a query to search for groups containing the values of contacts 1 and 3
SELECT `group_id` ,COUNT(DISTINCT(`contact_id`)) AS `variants`
FROM `TaskTeam_member`
WHERE `contact_id`='1' OR `contact_id`='3'
GROUP BY `group_id`
HAVING `variants`='2'
it turns out that such a table (correct)
contact_id
variants
1
2
2
2
And now I need to add in addition to searching for values 1 and 3 in the group to check the total number of elements in it (I need 2), that is, if presumably there are elements 1 and 3 in group 1, but the total number of elements is 3 and not 2 as in the example above, then this group should not be output
the result should be like this
contact_id
variants
2
2
help me complete my request!
If you want to check for other contact_ids you need to include all records, but only count the ones you want:
SELECT `group_id`
FROM `TaskTeam_member`
GROUP BY `group_id`
HAVING COUNT(DISTINCT contact_id)=2 AND COUNT(DISTINCT CASE contact_id WHEN 1 THEN 1 WHEN 3 THEN 3 END)=2
How I would add 2 specific rows in an SQL table together?
For example:
ID Quantity
1 1
1 3
1 2
2 3
2 4
I want to be able to add together ID 1 and 2 so the table becomes
ID Quantity
1 6
2 7
You need to use the SUM aggregate function and GROUP BY the ID.
SELECT Id, SUM(Quantity)
FROM yourtable
GROUP BY ID
Output:
ID Quantity
1 6
2 7
SQL Fiddle:
Further Reading.
I have a "resources" table that contains information about how resources of a specific weight are placed inside a territory by an user.
territory_id user_id weight
1 1 1
1 1 4
1 1 2
1 2 2
2 3 2
2 2 3
2 2 3
3 1 1
4 1 1
4 1 1
4 2 2
4 3 3
4 3 1
4 3 2
5 3 2
5 3 3
5 2 1
4 3 1
I want to calculate, for each existing territory, which user has the highest total weight of resources (and what is this value).
So this should be an expected outcome for the previous data:
territory_id best_user_id best_user_total_weight_of_resources
1 1 7
2 2 6
3 1 1
4 3 6
5 3 5
I have already tried several nested queries with SUM, MAX, GROUP BY but I really didn't find the proper way to calculate this.
I have found a lot of similiar question, but not solving this exact problem.
Any help? Thanks in advance!
EDIT:
I found out right now that the double GROUP BY (i.e. "GROUP BY territory_id, user_id") with double ORDER BY partially solves my problem, but it shows also information that I don't want (not only the best user, but each single user that placed at least one resource).
SELECT territory_id, user_id AS best_user_id, SUM( weight ) AS best_user_total_weight
FROM resources
GROUP BY territory_id, user_id
ORDER BY territory_id ASC, best_user_total_weight DESC;
You can run a first query to determine SUM(weight) for each couple (territory_id,user_id) and then run a second SELECT query on that result set to retrieve the row corresponding to max summ value:
SELECT territory_id, user_id, MAX(summ)
FROM (
SELECT territory_id, user_id, SUM(weight) AS summ
FROM resources
GROUP BY territory_id, user_id
) AS t1
GROUP BY territory_id
I am looking to do the following:
tblOne:
-page_id
-split
tblMany
-view_id
-page_id
I want to order tblOne by the number of related page_id in tblMany.
Then divide the number of rows in tblOne by 5 and update tblOne.split to a number between 1 - 5 into which split it falls... e.g if there are 50 rows... row 0 - 9 are split 1, 10 - 19 split 2...etc
I am sure I can do the 'count' part... but havn't a clue how I would update the 'split' row
This query will give you the distinct page id and their count ordered by page_id count in descendant order (so max count first) :
SELECT page_id, count(1)
FROM tblMany
GROUP BY 1
ORDER BY 2 DESC
So for a tblMany like this:
view_id | page_id
--------+---------
1 1
2 1
3 2
4 2
5 2
6 3
You will get
page_id | count(1)
--------+---------
2 3
1 2
3 1