In one table I have
ID, PAGE_ID, DATE
Each time a page is loaded, the DATE, PAGE_ID [from the page table below] are loaded into the table above.
I am trying to calculate and sort pages by popularity. The page table contains:
ID [PAGE_ID], DESCRIPTION, DATE
I have no idea where to start.
select L.PAGE_ID, P.DESCRIPTION, count(L.ID) from LOADED_PAGE L
inner join PAGE P on P.ID = L.PAGE_ID
where L.DATE > :sevenDaysAgo
group by L.PAGE_ID, P.DESCRIPTION
order by count(L.ID) desc
will give you the list of loaded pages, from the most popular to the least one.
select
id_page,
count(*) as popularity
from table
where date >= curdate() - interval 7 day
group by id_page
order by popularity desc
Related
I have been struggling with this problem for about month..
Have been searching and reading many posts, but still can't figure out, how to make this work..
Basically: I got 2 database tables fun_posts and fun_post_upvotes And I want to
SELECT *
FROM fun_posts
ORDER BY (HOTTEST POSTS(MOST UPVOTED THIS WEEK))
This is my latest code, that won't work
SELECT *
FROM fun_posts
ORDER BY (SELECT count(*), image_id, date
FROM fun_post_upvotes
GROUP BY image_id
ORDER BY DATE(date) > (NOW() - INTERVAL 7 DAY) DESC,
count(*) DESC,
date DESC)
If I divide this line into 2 different SELECT functions, they work. I can select simple posts and I can select upvotes count ordered like I want.
But If I make them into one line like that, I get following error:
#1241 - Operand should contain 1 column(s)
EDIT NR 1:
fun_posts table
fun_post_upvotes table
Problem with Answer that I checked:
Here, look how posts are ordered in my select function. (It selects like I want) 10->134->132->2->13
And here with given code (It selects image, but not in that order) 10->122->39->8->110
You can use a join to do this
SELECT fp.*, fpu.`cnt`
FROM fun_posts fp
LEFT JOIN ( SELECT image_id, COUNT(*) AS cnt
FROM fun_post_upvotes
WHERE `date` > (NOW() - INTERVAL 7 day)
GROUP BY image_id
) fpu ON ( fpu.image_id = fp.id )
ORDER BY fpu.cnt DESC, fp.`date` DESC, fp.`id` DESC;
It selects a list from fun_post_upvotes grouped by image_id and counts the amount of rows. That list is returned to the main query and matches (LEFT JOIN) on fp.id. The query will first show the item with the most upvotes in the past 7 days, than the least. If no upvotes are found, the result will still return them, but at the bottom in no specific order.
You can edit the order by, to obtain the items in the order you like.
Here a sqlfiddle.com
I have five tables:
Bill: no(pk),date, time, total
BillOrderRelation: no(fk), order_id(fk)
Order: order_id(pk), menu_id(fk), quantities, total
Menu: menu_id(pk), category_id(fk), menu_name, stock, price
Category: category_id(pk), category_name, colour
In my case, I have to retrieve which menu that has a highest sales in one day range, 7 days range, and 30 days range.
I've already succeed retrieve those information, but i think it's too complicated. First I have to retrieve the date on Bill, and then find the order in BillOrderRelation, and then find the Menu, and find the Category name. It includes a lot of queries and complex way to do the summing stuff for the same menu.
My question is, is that possible to query all those table in one query to retrieve just menu.menu_name, order.quantities, order.total, category.name and it's included the sum stuff for the same menu retrieved?
I've already succeed make a query for three table without using time range like this..
SELECT
menu.menu_name as top_item,
SUM(order.quantities) AS count_sold,
SUM(order.total) AS amount,
category.nama AS categories
FROM
menu, order, category
WHERE
menu.mennu_id = bill.menu_id
AND category.category_id = menu.category_id
GROUP BY
bill.menu_id, menu.menu_name, category.category_name
ORDER BY
count_sold DESC
Is there any tricky way for the case above?
Update for PostgreSQL
I believe you want something like this:
SELECT
m.menu_name AS top_item
, c.name AS category
, SUM(o.quantities) AS sum_quantity
, SUM(o.total) AS sum_total
FROM
menu m
JOIN
category c
ON c.category_id=m.category_id
JOIN
order o
ON o.menu_id=m.menu_id
JOIN
billorderrelation bor
ON bor.order_id=o.order_id
JOIN
bill b
ON b.no=bor.no
WHERE
b.date >= (CURRENT_DATE - INTERVAL '7 days')
GROUP BY
m.menu_id
ORDER BY
sum_quantity DESC
;
CURRENT_DATE allows you to get the current date (according to the timezone specified in your database). Read more:
https://www.postgresql.org/docs/8.2/static/functions-datetime.html#FUNCTIONS-DATETIME-CURRENT
I have 2 tables, Pages and LinkPages.
Within Pages i have the fields:
pageID (the identifier of the page),
startmemberID (the id of the member that created the page),
startDate (date the page was created).
In LinkPages I have:
pageID (to link with the page),
linkmemberID (member linking with the page),
joinDate (date member linked with the page).
What sql query would i use to get all information on the pages with a particular id and then order it by the date the page was started.
I got this far:
SELECT * FROM LinkPages WHERE linkmemberID='MEMBERID' LIMIT 5
but obviously i haven't ordered them here, would i need to use a join?
Many Thanks,
Jai
Try this:
SELECT * FROM LinkPages
INNER JOIN Pages ON Pages.pageID = LinkPages.pageID
WHERE linkmemberID='MEMBERID'
ORDER BY startDate DESC
LIMIT 5
SELECT lp.pageID, lp.linkmemberID, lp.joinDate
FROM LinkPages lp, Pages p
WHERE lp.linkmemberID='MEMBERID' AND lp.pageID = p.pageID
ORDER BY p.startDate DESC
LIMIT 5
You have two options, either you can JOIN or you can use a subquery:
SELECT * FROM LinkPages WHERE linkmemberID='MEMBERID'
ORDER BY
(SELECT startDate FROM Pages WHERE Pages.pageID = LinkPages.PageID) DESC
LIMIT 5
For good measure, here's the join:
-- be sure to use L.* here, otherwise you get all of the columns from
-- pages as well
SELECT L.* FROM LinkPages L
INNER JOIN Pages P ON P.pageID = L.pageID
WHERE linkmemberID='MEMBERID'
ORDER BY P.startDate DESC LIMIT 5
SELECT lp.*
FROM LinkPages lp, Pages p
WHERE lp.pageId = p.pageId
AND lp.linkmemberID='MEMBERID'
ORDER BY p.startDate
LIMIT 5
sorry - forgot the ORDER BY ...
I'm kind of new to SQL and I can't find the solution to my problem. I have two tables. In table A, I'm storing a lot of comments, each with a unique ID.
In table B, I'm storing every vote (like=1 and dislike=0) for every comment with a datetime. There will be an entry for every vote, so there will be tons of rows for each comment in table A.
I need to retrieve all the comments and sort them such that the weekly most liked comments are at the top, but I'm not sure how.
Here's what I have so far, but not sure how to continue:
SELECT * FROM comment INNER JOIN logs ON comment.c_id=logs.c_id WHERE logs.daterate >= DATE_SUB(CURDATE(), INTERVAL 8 DAY) AND logs.rated=1
To clarify, I need to get all entries from logs with rated = 1 in the past week and sort them by the most frequent c_id in descending order, and get distinct c_id for each row... if that makes sense
Please ask questions if I didn't make it clear enough, thanks!!
SELECT *
FROM comment
INNER JOIN (SELECT comment.c_id,
COUNT(*) AS cnt
FROM comment
INNER JOIN logs ON comment.c_id=logs.c_id
WHERE logs.daterate >= DATE_SUB(CURDATE(), INTERVAL 8 DAY)
AND logs.rated=1
GROUP BY comment.c_id) x ON x.c_id = comment.c_id
ORDER BY x.cnt DESC
Try this -
I have first queried all records from logs table which are rated 1 and are from 7 days from current date and also are ordered based on the count of c_id. Then joined this with the COmments table.
SELECT Comment.* FROM comment C
INNER JOIN (SELECT logs.c_id as c_id,count(logs.c_id) as logcount FROM logs
WHERE logs.rated=1
AND logs.daterate BETWEEN GETDATE() AND DATEADD(day,-7,getdate())
Group by logs.c_id
order by count(logs.c_id) desc) X
ON C.c_id = X.c_id
ORDER BY X.logcount DESC
How can I get the top X posts of the past week? I have two tables: td_table - holds info about the post; vote_table - holds the info about the votes that have been made for the posts. The following gives me the top three posts (those that have most votes for), but it gives me the top three of all times.
$query = 'SELECT t.id, t.content, t.userid, t.time FROM tb_table t,
(SELECT *, count(*) AS num FROM vote_table GROUP BY voted_id ORDER BY num desc) u
WHERE u.vote_id=t.id LIMIT 3';
I tried using something like this:
SELECT * FROM vote_table WHERE t.time > date_sub(date(now()), interval 7 day)
But whenever I add this kind of WHERE condition to the above query it either dies (if I add it to the select from vote_table) or it loads an empty page (if it's and AND condition of the last where)...
So... how can I combine those? Call all the information from the query, but only those voted_ids that have been made for the past week?
Figured it out!
using_time >= (UNIX_TIMESTAMP() - ((60*60*24)*7))