I have table like this:
| ID | Team | User | Try1 | Try2 | Try3 |
| 1 | Black | Chris | 2 | 6 | 4 |
| 2 | Black | Brian | 10 | 8 | 10 |
| 3 | Red | Mark | 6 | 2 | 8 |
| 4 | Red | Andrew | 4 | 10 | 6 |
I needed to count team try points together to get total.
SELECT *, SUM(Try1 + Try2 + Try3) AS total FROM team_pts GROUP BY team ORDER BY total DESC
The question is - how do I output each teams total for each try?
Something like this:
| Pos | Team | Try1 | Try2 | Try3 | Total |
| 1 | Black | 12 | 14 | 14 | 40 |
| 2 | Red | 10 | 12 | 14 | 36 |
Sorry for my English!
You need to sum column before sum total:
SELECT *,
SUM(Try1),
SUM(Try2),
SUM(Try3),
SUM(Try1 + Try2 + Try3) AS total
FROM team_pts
GROUP BY team
ORDER BY total DESC
SELECT ID,Team,SUM(Try1) as Try1,SUM(Try2) as Try2,SUM(Try3) as Try3,SUM(Try1 + Try2 + Try3) AS Total FROM team_pts GROUP BY team ORDER BY total DESC
Related
I'm working on a project where I have to list the solutions with the highest votes per problem.
Every problem has two solutions and the users can vote on one solution per problem. This is my database at the moment.
+----------+------------+---------+
| id | id_problem | vote |
+----------+------------+---------+
| 1 | 1 | 25 |
| 2 | 1 | 10 |
| 3 | 2 | 18 |
| 4 | 2 | 2 |
| 5 | 3 | 6 |
| 6 | 3 | 7 |
| 7 | 4 | 11 |
| 8 | 4 | 4 |
| 9 | 5 | 5 |
| 10 | 5 | 2 |
+----------+------------+---------+
I would like to get this result:
(The row with the highest vote per id_problem)
+----------+------------+---------+
| id | id_problem | vote |
+----------+------------+---------+
| 1 | 1 | 25 |
| 3 | 2 | 18 |
| 6 | 3 | 7 |
| 7 | 4 | 11 |
| 9 | 5 | 5 |
+----------+------------+---------+
SELECT
id,
id_problem,
max(vote)
from
tablename
group by
id_problem
order by
id_problem ASC
The max(vote) determines greater vote, but it aggregate the result, then you need to group by id_problem and then order it asc.
You can use group by clause with max aggregation function to get the expected result, e.g.:
select id, id_problem, max(vote) as vote
from result
group by id_problem
order by id_problem
Here's SQL Fiddle.
I'm trying to add a new page onto an old site, a records page that'd show which players won the most money or won the race when they were a certain age
The user table looks like this
***********************************
| id | name | age | bday |
| 1 | bob | 15 | 2000-07-30 |
| 2 | john | 14 | 2001-07-30 |
| 3 | mary | 13 | 2002-07-30 |
***********************************
the race_results table looks like this
************************************************************
| id | raceid | userid | place | winnings | date |
| 1 | 1 | 1 | 1 | 1000 | 2006-04-10 |
| 2 | 1 | 2 | 5 | 50 | 2005-02-15 |
| 3 | 1 | 3 | 6 | 50 | 2010-06-12 |
| 4 | 2 | 1 | 1 | 1000 | 2009-05-29 |
| 5 | 2 | 2 | 3 | 250 | 2003-01-12 |
************************************************************
What's the most practical approach to a query that'd calculate the year range when Bob was 3 years old and match that with the race results table to see how many times he won 1st place within that particular date range?
SELECT COUNT(*)
FROM 'race_results'
INNER JOIN 'user' on user.id = race_results.userid
WHERE user.name = 'bob'
AND race_results.place = 1
AND race_results.date >= ADDDATE(user.bday, INTERVAL 3 YEAR)
AND race_results.date < ADDDATE(user.bday, INTERVAL 4 YEAR);
To get a list of all 3 yr old 1st placers and how many times they won, not just for 'bob' ...
SELECT user.name, COUNT(*)
FROM 'race_results'
INNER JOIN 'user' on user.id = race_results.userid
WHERE race_results.place = 1
AND race_results.date >= ADDDATE(user.bday, INTERVAL 3 YEAR)
AND race_results.date < ADDDATE(user.bday, INTERVAL 4 YEAR)
GROUP BY user.name;
I have a table (url_analyst) with the following info:
id | user_id | url_id | date | visits
----------------------------------------------------
1 | 2 | 5 | 2015-10-25 | 15
2 | 2 | 6 | 2015-10-25 | 18
3 | 2 | 5 | 2015-10-25 | 11
4 | 3 | 8 | 2015-10-25 | 22
5 | 3 | 9 | 2015-10-27 | 30
6 | 5 | 15 | 2015-10-25 | 15
etc
What I want is a update/insert earning in a new table "earning"
earning calculate 1$/10 visits
id | user_id | total_urls | date | total_visits | earning
-------------------------------------------------------------------
1 | 2 | 3 | 2015-10-25 | 44 | 4.4$
2 | 3 | 1 | 2015-10-25 | 22 | 2.2$
3 | 3 | 1 | 2015-10-27 | 30 | 3.0$
4 | 5 | 1 | 2015-10-25 | 15 | 1.5$
How can I do this with CodeIgniter using the active record query method? Can this be done with COUNT Total URL then GROUP BY user_id AND Date , update earning?
Try this MySQL query:
SELECT
user_id, count(url_id), date, sum(visits) as
total_visits, concat(sum(visits)/10,'$') as earning
FROM
url_analyst
GROUP BY
user_id, date
Considering all group_by columns from url_analyst table,
$this->db->select("SUM(total_url)");//if required select other fields
$this->db->group_by("url_analyst.user_id");
$this->db->group_by("url_analyst.date");
If you want group_by on any column from earning, you can change the table name in group_by clause
Here is my data
cardNo| userName| tablename| hours | date
1 | a | a | 12 | 12-06-2015
1 | a | a | 5 | 11-06-2015
2 | b | b | 3 | 15-06-2015
1 | a | a | 8 | 12-06-2015
2 | b | b | 3 | 21-06-2015
1 | a | a | 12 | 14-06-2015
2 | b | b | 10 | 8-06-2015
cardNo is unique. I need to display all details and total hours for each card, like:
cardNo | userName | tablename | totalhours
1 | a | a | 37
2 | b | b | 16
It's simple SUM() with GROUP BY:
SELECT cardNo,sum(hours)
FROM yourtable
GROUP BY cardNo;
I left it as an exercise for the OP to include userName and tablename columns into the query
SELECT cardNo,userName, tablename, sum(hours) hours
FROM Table_1 GROUP BY cardNo,userName,tablename
I have a database table campaign_data. I need to select the customer_id where in the campaign there is difference in tariff. How can i do that with MySQL query. Here is some sample data.
SQL Fiddle Schema
| CAMPAIGN_ID | CUSTOMER_ID | CAMPAIGN_NAME | TARIFF |
---------------------------------------------------------
| 1 | 1 | Richmond | 100 |
| 2 | 1 | Sutton Coldfield | 75 |
| 3 | 1 | Putney | 100 |
| 4 | 1 | Kentish Town | 100 |
| 5 | 1 | Woking | 100 |
| 6 | 2 | Chiswick | 90 |
| 7 | 2 | Ealing | 100 |
| 8 | 2 | Camden | 100 |
| 9 | 3 | Croydon | 75 |
| 10 | 3 | Croydon1 | 100 |
| 11 | 3 | Archway | 100 |
| 12 | 4 | Ealing0 | 100 |
| 13 | 4 | Ealing01 | 100 |
| 14 | 4 | Ealing02 | 100 |
| 15 | 4 | Chingford | 100 |
| 16 | 4 | chingford01 | 100 |
Now as you can see customer id 1 , and 3 has different tariffs. I want to select them and leave the customer id 4 because it has campaigns with same tariffs.
Desired Output
| CUSTOMER_ID |
---------------
| 1 |
| 2 |
| 3 |
For clearification you can see customer 1 has 5 records. If in his 5 records the tariff is same (100) i want to avoid but if the tariff is not some as 4 records have 100 and one has 75, i want to select.
SELECT customer_id, count(DISTINCT tariff) as tariffs
FROM campaign_data
GROUP BY customer_id
HAVING tariffs > 1
you looking for this maybe
SELECT customer_id
FROM campaign_data
GROUP BY customer_id
HAVING count(DISTINCT tariff) > 1
http://sqlfiddle.com/#!2/48b6e/31
select
customer_id,
tariff
from campaign_data
group by customer_id
having sum(tariff)/count(tariff) <> tariff;