Complex SQL Query (sum entries) [closed] - mysql

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am wondering if something like this could be implemented in SQL query.
Let say I have these tables:
Table Orders
id tax
01 800
02 255
Table DetailOrders
id price itemName
01 700 Book
01 500 Umbrella
01 100 Jacket
02 1000 Piano
Basically single entry of one table Orders corresponds to multiple entries in DetailOrders.
Is there are any way to write SQL query that would return something like this:
id tax sum-price all-names
01 800 1300 Book, Umbrella, Jacket
02 255 1000 Piano
It would sum the price of items with the same id, and somehow merge the names of the items with same id.
Could something like this be achieved?

How about something like
SELECT o.id,
o.tax,
sum(od.price) sum_price,
group_concat(itemName) all_names
FROM Orders o INNER JOIN
DetailOrders do ON o.id = do.id
GROUP BY o.id,
o.tax
Have a look at GROUP_CONCAT(expr)
This function returns a string result with the concatenated non-NULL
values from a group. It returns NULL if there are no non-NULL values.

It isn't hard:
select
o.id, o.tax,
sum(d.price),
group_concat(d.itemName)
from
orders as o
inner join detailOrders as d on o.id = d.id
group by
o.id

Related

Finding First Instance of Multiple Variable in Single Column in SQL [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 3 days ago.
Improve this question
I've been trying to write some queries and subqueries but have struggled with one in particular.
Using the database at the below link, I am trying to find a way to separate multiple variables from a single column in to two.
https://www.w3resource.com/sql-exercises/soccer-database-exercise/subqueries-exercises-on-soccer-database.php
I can return the first match with a player scoring a goal from a penalty
`
SELECT a.player_id,MIN(b.play_date) AS firstmatchscoringpenalty FROM goal_details a
JOIN match_mast b ON a.match_no = b.match_no
WHERE a.goal_type = 'P'
GROUP BY a.player_id`
Player_ID
Firstmatchscoringpenalty
160333
2016-06-26
160235
2016-07-03
and the first match in which a player scored a "normal" goal`
`SELECT a.player_id,MIN(b.play_date) AS firstmatchscoringnormal FROM goal_details a
JOIN match_mast b ON a.match_no = b.match_no
WHERE a.goal_type = 'N'
GROUP BY a.player_id`
Player_ID
firstmatchscoringnormal
160236
2016-06-27
160316
2016-07-01
I would, however, like to return a result for all players who scored both a penalty and normal goal that includes both results across three columns, so something like:
Player_ID
Firstmatchscoringpenalty
firstmatchscoringnormal
Cell 1
Cell 2
Cell 3
Cell 4
Cell 5
Cell 6
Any ideas on how to do this would be much appreciated.

Why is my average query showing incorrect values? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I asked a question a few hours ago that was marked as closed and referred me a link that did not clear up my confusion.
I am trying to query a sports database in MySQL to list the names of players who are above average age compared to their teammates. Ideally, I want to group by team, find the average of each team, and compare that to each respective player on that team.
My results from this query seem to be comparing players to the entire databases' average, rather than the average of the team. Can anyone correct my query or propose an alternate query to get the correct data? A friend of mine suggested perhaps using two copies of the tables, but that is beyond the scope of my limited MySQL skills.
My relational schema are as follows:
player(player_name, age, position)
plays_for (player_name, team_name)
SELECT player.player_name, player.age
FROM
plays_for
INNER JOIN player ON player.player_name=plays_for.player_name
WHERE (SELECT AVG(age) FROM player
GROUP BY plays_for.team_name1)< player.age
Your WHERE statement does not include the team grouping. I personally like WITH statements which seems to be the direction your friend was going.
> WITH average_ages AS ( SELECT AVG(p.age) AS average_age, pf.team_name
> FROM player p join plays_for pf on p.player_name = pf.player_name
> GROUP BY pf.team_name) aa
> SELECT player.player_name, player.age
> FROM plays_for
> INNER JOIN player ON player.player_name=plays_for.player_name
> INNER JOIN average_ages ON plays_for.team_name = average_ages.team_name
> WHERE player.age > average_ages.average_age;
The WITH statement at the top creates a temporary table of average ages and then joins it to the plays_for table.
The first few rows of the entire SELECT query before the WHERE statement would look like this
Name Age Team Average_age
Tara 51 KOs 25
Bomb 45 KOs 25
Jess 20 BES 30
Buster 40 BES 30

Mysql query with pivot table and multiple joins [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a sales table, a sale_staff table, a staff table and an offices table.
We are selling properties, and I want to find out the numbers of sales per seller for X month and per office.
The pivot table looks like this
sale_id , staff_id , type
type can be either seller or lister, so I need a where clause for this.
The sales table has a FK to the offices table; office_id
What I have so far is this, its TOTALLY wrong I know, but that's why i'm here - i need to fix the sums and include the office name from the office table, so
select st.first_name, st.last_name, office, count(*) as sold
from sales s, sale_staff ss
left join staff st
on st.id = ss.staff_id
left join offices off
on off.id = s.office_id
where ss.`type` = 'lister' and
year(s.sale_date) = 2017 and
month(s.sale_date) = 12
group by st.id
Sales table is simply a property sale item, price, address, office_id.
Besides the error unknown column s.office_id, as I said, the sum value is incorrect anyway. I'm really not experienced enough to understand this level of relationship joins and aggregating, any pointers please.
Basically I would like to simply see a resultset like
staff(seller) , count , office
Mike , 12 , West
Jim , 7 , East
Fred , 3 , East
Edit: SQLFiddle in case that helps :) Will add some sample test data.
Never use commas in the FROM clause. Always use proper, explicit JOIN syntax. Your problem is because of the scoping rules around commas.
I would recommend:
select st.first_name, st.last_name, o.office, count(*) as sold
from staff st left join
sale_staff ss
on st.id = ss.staff_id join
sales sa
on sa.sale_id = ss.sale_id join
offices o
on o.id = s.office_id
where ss.`type` = 'lister' and
s.sale_date >= '2017-12-01' and
s.sale_date < '2018-01-01'
group by st.first_name, st.last_name, o.office;
I think this has the join condition correctly laid out, but it is hard to be sure without sample data and desired results.
Notes:
left join is probably not necessary. If it is, you should probably be starting with the staff table (to keep all staff).
Qualify all column names.
The group by includes all the non-aggregated columns in the from. This is a good habit if you are learning SQL.
The date comparisons are direct, without the use of functions.

Mysql GROUP_CONCAT and IN query [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a table EMPDetails like
EmpID EmpName EmpFriendsID
1 Hari 2,3
2 Ramesh
3 Suresh
I would like to have a query to retrieve EmpFriends name if i give an EmpID.
example if EmpID 1 is provided,result should be
1 Hari 2,3 Ramesh,Suresh
Thanks.
To Join tables use FIND_IN_SET() and then group recors and use GROUP_CONCAT() to concatenate friends names
SELECT t.EmpID,t.EmpName,t.EmpFriendsID,
GROUP_CONCAT(t1.EmpName)
FROM t
LEFT JOIN t as T1 on FIND_IN_SET(t1.EmpID,t.EmpFriendsID)
WHERE t.EmpID=1
GROUP BY (t.EmpID)
SQLFiddle demo
Use FIND_IN_SET() function
Try this:
SELECT E1.EmpID, E1.EmpName, GROUP_CONCAT(E2.EmpFriendsID)
FROM EMPDetails E1
LEFT JOIN EMPDetails E2 ON FIND_IN_SET(E2.EmpID, E1.EmpFriendsID)
GROUP BY E1.EmpID

Mysql query - grouping results [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have 2 tables and basically what I like to do is group the results or counts together for display. Tried different version of mysql statement but not getting anywhere.
The 2 example tables are:
tbl_One
index O_priority
1 low
2 medium
3 high
tbl_Two
t_priority
2
1
3
3
2
3
1
1
1
expected results:
low = 4
medium = 2
high = 3
SELECT T1.O_priority,T2.c FROM tbl_One as T1 LEFT JOIN (SELECT count(*) as c,t_priority FROM tbl_Two GROUP BY t_priority) as T2 ON T1.index = T2.t_priority;
Join the tables, then group the results:
SELECT tbl_One.O_priority, COUNT(*)
FROM tbl_One JOIN tbl_Two ON tbl_Two.t_priority = tbl_One.index
GROUP BY tbl_One.index
See it on sqlfiddle.
Simple as much as you can, try this:
SELECT count(o.index) as `index`, o.O_priority
FROM tbl_One o join tbl_two t on t.t_priority = o.index
group by t.t_priority;
SQL Fiddle