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
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
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
Suppose I have a table like so,
unqiue_data int(10),
not_unique_data int (10)
unique_data not_unique_data
1 1
2 1
3 2
4 2
5 2
select * from some_table order by not_unique_data DESC;
What I need to do, is randomize this SELECT query, but in a very two particular ways that I just can't figure out how to do. Firstly, I want unique_data randomized, so that the SELECT query could return something like (randomly):
unique_data not_unique_data
2 1
1 1
4 2
3 2
5 2
The second requirement I have is that, unique_data appears multiple times, but in a very specific order.
In an ideal world, I need is so that it could return something like
unique_data not_unique_data
4 2
3 2
5 2
1 1
2 1
3 2
5 2
4 2
2 1
1 1
5 2
4 2
3 2
What I mean by this is, I need it so that each unique_data (4,3,5), (3,5,4), (5,4,3) The first number of each set appears only once while still being ordered by not_unique_data.
How to do this?
Well for this problem you have to make sure that 100 products related to a product
how many of them have appeared for that product
how many of them will be appeared for that product
We can use a temporary table to do so
SELECT unique_data, not_unique_data, 0
INTO temp_newtable
FROM some_table
ORDER BY RAND()
Now we will get a randomly organized table and by default seen=0 (seen to know it has been appeared for that product or not)
unique_data not_unique_data seen
4 2 1
3 2 1
5 2 0
1 1 0
2 1 0
3 2 1
So whenever some product related to product appear on page you need to update seen column to 1, when you are out of this table truncate and generate random data for usage again
I think you are looking for this https://stackoverflow.com/a/3990479/2552551
SELECT * FROM
(
SELECT * FROM some_table
ORDER BY not_unique_data DESC
LIMIT 100
) T1
ORDER BY RAND()
I have a table of data like this:
id user_id A B C
=====================
1 15 1 2 3
2 15 1 2 5
3 20 1 3 9
4 20 1 3 7
I need to remove duplicate user ids and keep the record that sorts lowest when sorting by A then B then C. So using the above table, I set up a temp query (qry_temp) that simply does the sort--first on user_id, then on A, then on B, then on C. It returns the following:
id user_id A B C
====================
1 15 1 2 3
2 15 1 2 5
4 20 1 3 7
3 20 1 3 9
Then I wrote a Totals Query based on qry_temp that just had user_id (Group By) and then id (First), and I assumed this would return the following:
user_id id
===========
15 1
20 4
But it doesn't seem to do that--instead it appears to be just returning the lowest id in a group of duplicate user ids (so I get 1 and 3 instead of 1 and 4). Shouldn't the Totals query use the order of the query it's based upon? Is there a property setting in the query that might impact this or another way to get what I need? If it helps, here is the SQL:
SELECT qry_temp.user_id, First(qry_temp.ID) AS FirstOfID
FROM qry_temp
GROUP BY qry_temp.user_id;
You need a different type of query, for example:
SELECT tmp.id,
tmp.user_id,
tmp.a,
tmp.b,
tmp.c
FROM tmp
WHERE (( ( tmp.id ) IN (SELECT TOP 1 id
FROM tmp t
WHERE t.user_id = tmp.user_id
ORDER BY t.a,
t.b,
t.c,
t.id) ));
Where tmp is the name of your table. First, Last, Min and Max are not dependent on a sort order. In relational databases, sort orders are quite ephemeral.