Sum top three totals and group result by [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Here is my problem...
table looks like this:
|id |coutry |sum
| 0 | USA | 30
| 1 | USA | 60
| 2 | USA | 90
| 3 | USA | 80
| 4 | GER | 40
| 5 | GER | 90
| 6 | SWE | 30
| 7 | SWE | 10
| 8 | SWE | 90
| 9 | SWE | 40
and result (top three scores if exists) should be like this:
|coutry |total
| USA | 230
| GER | 130
| SWE | 160
Any change to solve this by sql only? I'll fetch final result using php foreach..

The limit keyword should do the trick:
SELECT country, SUM(`sum`)
FROM my_table
GROUP BY country
ORDER BY 2 DESC
LIMIT 3

You need to establish a row number to get the top 3 sums per country. To do this with MySql, you have to use user-defined variables.
SELECT country, SUM(`sum`)
FROM (
SELECT *,
#rn:=IF(#prevCountry=country,#rn+1,1) rn,
#prevCountry:=country
FROM yourtable, (SELECT #rn:=0, #prevCountry:='') t
ORDER BY country, `sum` DESC
) t
WHERE rn <= 3
GROUP BY country
ORDER BY country
SQL Fiddle Demo

Try it here because #Mureinik have right.
SQL Fiddle
Order by country DESC solve "TOP" problem.

Related

mysql - many to many query from bridge table [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
i have a task to complete. there is a many to many relationship. the bridge table has been made which looks like
left id right id
+----------+---------+
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 2 |
| 2 | 8 |
| 3 | 1 |
| 3 | 2 |
| 3 | 4 |
| 4 | 1 |
| 4 | 2 |
| 4 | 3 |
| 4 | 5 |
| 5 | 1 |
| 5 | 2 |
| 5 | 4 |
| 5 | 6 |
| 5 | 7 |
+----------+---------+
i have to display the left id = right id in one row
for example
for left id 1
left1 | right1 righ 2
for left id 3
left3 | right1 right2 right 4
how do i do this ? i have tried joining table , doesn't work
I think you can use a simple query to acheive this using GROUP BY and GROUP_CONCAT()
SELECT left_id, GROUP_CONCAT(right_id SEPARATOR ' ') as rigth_id
FROM left-right
GROUP BY left_id;
This is a reasonably straightforward application of GROUP_CONCAT() and GROUP BY. (http://sqlfiddle.com/#!9/ed7e1/2/0)
SELECT leftId,
GROUP_CONCAT(rightId ORDER BY rightId) rightIds
FROM bridge
GROUP BY leftId

Get two different limits from mysql left join [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I want to display up to 3 posts form each category. Also I want to check each category have at least 3 posts (get the post count). Please see the table stretcher below
Category table
+---------+---------------+
| cat_id | cat_name |
+---------+---------------+
| 1 | cat name 1 |
| 2 | cat name 2 |
| 3 | cat name 3 |
+---------+---------------+
Posts table
+------+--------+-------+
| p_id | post | c_id |
+------+--------+-------+
| 1 | post 1 | 1 |
| 2 | post 2 | 1 |
| 3 | post 3 | 2 |
| 4 | post 1 | 2 |
| 5 | post 2 | 1 |
| 6 | post 3 | 3 |
| 6 | post 3 | 1 |
+------+--------+-------+
Query
if($results=$mysqli->query( SELECT * FROM categories LEFT JOIN posts ON posts.p_id= categories.cat_id WHERE posts.p_id= categories.cat_id ORDER BY cat_id LIMIT 0, 10")){
while($row = mysqli_fecth_array($results)){
//Do stuff
}
$results ->close();
}
Any example or comments are appreciated.
It is not possible to get up to 3 posts from each category in MySQL and sane query. You need support for window functions for that. I would preform a separate query for each category. You can read short info in this tag: https://stackoverflow.com/tags/window-functions/info
For the second part:
SELECT c_id, COUNT(*) FROM posts GROUP BY c_id;
select distinct (c.postid) , c.postname, s.category_name from posts c inner join category s on s.cat_id = c.cat_id group by c.cat_id having COUNT(c.cat_id) > 3 limit 3

Clubbed total sale for month using PHP query [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I need to get a total sale from the given table. Where showing total daily sale and the grand total at the end. My table is like:
Date | Bill No. | Product | Amount | Total Bill Amt. |
01/04/2015 | 001 | A | 150 | 650 << |
01/04/2015 | 001 | B | 300 | 650 |
01/04/2015 | 001 | C | 200 | 650 |
01/04/2015 | 002 | B | 10 | 80 << |
01/04/2015 | 002 | D | 70 | 80 |
02/04/2015 | 003 | Z | 60 | 110 << |
02/04/2015 | 003 | Y | 50 | 110 |
Where results should be:
Date | Total Sale |
01/04/2015 | 730 |
02/04/2015 | 190 |
For 02/04/2015, it should be 110 and not 190.
SELECT `Date`,SUM(`Amount`) AS daily_sale FROM `table` GROUP BY `Date`.
To get the different days dates, depending on the structure of your table, I assume you have a column for id which is a primary key to help track all those table rows by id.
To get the daily Total Bill Amt, apply same query format used by #web-normad annd you'll get your total.
SELECT DATE(Date),SUM(Total Bill Amt) AS daily_sale FROM `table` GROUP BY `Date`.
Test it out and see if it works.

Compare three tables for one answers in MySQL [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have been working with MySQL for a while now, and just recently found the need to manage my data better (MOAR DATA!)...
The problem I am having is this:
Table1: users
- id
Table2: companies
- companyid
- companyname
Table3: customers
- customerid
- companyid
I am trying to query the following.
I have the users ID, I need to use that to get the companyid from customers using the customerid, and return companyname based of the assigned companyid in customers.
It is very possible I am going about this very wrong. I understand that eventually the data is going to get very hard to read by eye as the data starts to grow. My concern is having the ability to associate and disassociate customers from businesses.
If you have any tips, or have a better strategy, or think I should just add this information into the users tables please let me know.
First off, you need some understandings on what Normalization is: http://support.microsoft.com/kb/283878
The database tables are not meant to be "read by eye". I'm pretty sure you are dealing with a very small database now, but imagine in the future you're dealing with thousands of tables with millions of rows, "visual inspection" is not going to work anymore.
A simple join would have given what you need:
SELECT t2.companyname
FROM table1 t1, table2 t2, table3 t3
WHERE t1.id = t3.customerid
AND t3.companyid = t2.company id
AND t3.customerid = (some id) //Depends on what your purpose is,
//this line can also be replaced by
//AND t1.id = (some id)
In your case, it is possible to combine User table and Customer table into one ONLY if all users are customers too. But it is definitely a NO to have company information in either User or Customer tables.
Assuming customers.customerid and users.id will be the same value, this should suffice:
SELECT companies.companyname
FROM customers
LEFT JOIN companies ON customers.companyid = companies.companyid
WHERE customers.customerid = 5
Here is a fiddle
Schema is on the left, sql is on the right.
Tables:
users
+--------+
| ID |
+--------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
| 11 |
| 12 |
| 13 |
+--------+
companies
+------------------------------------------+
| COMPANYID COMPANYNAME |
+------------------------------------------+
| 5 CompAlumpany |
| 9 Dergy Hergins LLC |
| 3 Smergy Berg Inc. |
| 23 Hergin Derz |
| 7 Comperation corpany |
| 11 Contagion Engine |
| 31 AEther Vial |
| 66 Necropotence |
| 90 Lord of Atlantis |
| 65 Snoogins |
| 51 Nickty-Schnickty-Schnoine |
| 58 Take a knee |
| 59 Coorprate |
+------------------------------------------+
customers
+--------------------------+
| CUSTOMERID COMPANYID |
+--------------------------+
| 1 5 |
| 2 9 |
| 3 3 |
| 4 23 |
| 5 7 |
| 6 11 |
| 7 31 |
| 8 66 |
| 9 90 |
| 10 65 |
| 11 51 |
| 12 58 |
| 13 59 |
+--------------------------+
Query Returns:
+---------------------+
| COMPANYNAME |
+---------------------+
| Comperation corpany |
+---------------------+
Posted on behalf of the OP.
I got the results I was looking for with the following:
SELECT t2.companyname FROM companies t2,customers t3 WHERE t3.companyid = t2.companyid AND t3.customerid=?
I was unaware I could create references, this will become very useful for me. Thank you!

How do I sort data from a mysql db according to a unique and predetermined order, NOT asc or desc

I need to show 1000 test questions to a student, 10 per page.
The questions are in a mysql table, the answers will go in another table.
I need each students questions to appear in a different predetermined order than any other students. The sort order is predetermined when they register and placed in a usermeta table.
In the usermeta table there is a column that lists the order in which the questions should be shown. The order in that column is unique to each student and looks like this example: 8|14|97|54|21|37|54|61 ...etc.
The first question to be shown to the student would be question #8, and then question #14, and then question #97, and so on, listing 10 per page.
I don't need to sort the questions asc or desc. Also, I can change the db structure if that would help find a solution.
Also, I can change the db structure if that would help find a
solution.
If changing the db structure is possible, then instead of storing the sorting order as a pipe separated string, store it in a separate table that maps each question to the order it should appear in for a given student. i.e.
student_id, sort_order, question_id
1 1 8
1 2 2
1 3 97
Then join on your sorting table when selecting your questions for a particular student.
SELECT q.* FROM
questions q
JOIN questions_sorting_order qso
ON q.id = qso.question_id
ORDER BY qso.sort_order
WHERE qso.student_id = :student_id
SELECT * FROM ints;
+---+
| i |
+---+
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
+---+
SELECT i2.i*10+i1.i x
, FIND_IN_SET(i2.i*10+i1.i,'8,14,97,54,21,37,54,61') y
FROM ints i1
, ints i2
HAVING y > 0
ORDER
BY y;
+----+---+
| x | y |
+----+---+
| 8 | 1 |
| 14 | 2 |
| 97 | 3 |
| 54 | 4 |
| 21 | 5 |
| 37 | 6 |
| 61 | 8 |
+----+---+
Note that 54 is ignored second time around

Categories