mysql join and sum problems - php

Im having some trouble with this.
Here's an example of my tables.
Booking
id zone_id name excursion_id
1 2 1
2 1 1
3 2 1
The table where I have the quantities
booking_price
id_booking id_price quantity
1 1 2
1 2 3
2 1 1
2 2 0
3 1 2
3 2 3
Here the zone table
Zone
id Name
1 a
2 b
3 c
So I want to have a table like that
Zone_id Quantity
1 1
2 10
3 0
The problem is when im joining tables and filtering by excursion_id im not getting ALL the zones.
I want to know how many people goes in each zone.

I think better way of doing it is
select z.id, coalesce(sum(bp.quantity),0) as quantity
from Booking b
right join Zone z on z.id = b.zone_id AND b.excursion_id = 1
left join booking_price bp on bp.id_booking = b.id
group by z.id
demo http://sqlfiddle.com/#!2/771f5/13

You could use a query like:
SELECT zone.id as zone_id,
sum(quantity) as Quantity
FROM zone
LEFT JOIN Booking on Booking.zone_id = zone.id
LEFT JOIN Excursion on Excursion.id_booking = Booking.id
GROUP BY zone.id

Related

codeigniter: get percentage in multiple table by data category

I have tables like this,
table_a
PID Name Type
1 a selling
2 b sampling
3 c selling
4 d sampling
5 e selling
And,
table_b
ID PID Qty
1 2 3
2 2 2
3 1 1
4 3 1
5 1 3
6 5 3
7 4 3
8 3 2
I need to show like this,
output_selling
Name Total_qty Percentage
a 4 40%
c 3 30%
e 3 30%
And,
output_sampling
Name Total_qty Percentage
b 5 62,5%
d 3 37,5%
How would I go about this in CodeIgniter?
You can use below in your function and change 'sampling' and 'selling' as you need it:
$this->db->query("
SELECT dd.NAME,
dd.total_qty,
( dd.total_qty / dd.totals ) * 100 AS Percentage
FROM (SELECT a.NAME,
Sum(b.qty) AS Total_qty,
(SELECT Sum(b1.qty) AS total
FROM table_b b1
JOIN table_a a1
ON a1.pid = b1.pid
AND a1.type = 'selling') AS totals
FROM table_a a
JOIN table_b b
ON b.pid = a.pid
AND a.type = 'selling'
GROUP BY a.pid) dd ");
Please see below query output:

Mysql join two tables with sum of quantity

Q : I am trouble to do following task in mysql query.
Task is get all products (If product is duplicate than sum of it's qty) and deduct wastage stock (If wastage product is duplicate than sum of it's qty).
I have two tables like,
1) manage_stock
2) manage_wastage
manage_stock
=> This table has p_id(Product ID) and many rows with product duplication's.
p_id p_name p_qty
1 Pro-1 10
2 Pro-2 15
3 Pro-3 8
1 Pro-1 15
manage_wastage
=> This table has p_id(Product ID) of manage_stock table. It is also many rows with product duplication's.
p_id w_qty
1 2
1 4
3 5
Desired Output
p_id p_name p_qty w_qty final_qty
1 Pro-1 20 6 14
2 Pro-2 15 0 15
3 Pro-3 8 5 3
Thank you very much.
You just have to compute the difference between the stock quantity and the wastage
SELECT s.p_id, s.p_name, SUM(p_qty), SUM(w_qty), SUM(p_qty) - SUM(w_qty) as final_qty
FROM manage_stock s
LEFT OUTER JOIN manage_wastage w
ON s.p_id = w.p_id
GROUP BY s.p_id, s.p_name
try this one
SELECT s.p_id, s.p_name, SUM(p_qty),SUM(ifnull(w_qty, 0)),SUM(p_qty - ifnull(w_qty, 0)) as total
FROM manage_stock s
left outer JOIN manage_wastage w
ON s.p_id = w.p_id
GROUP BY s.p_name
its work
SELECT ms.p_id ,GROUP_CONCAT(ms.p_name)p_name ,SUM(ms.p_qty) p_qty ,SUM(mw.w_qty) w_qty,SUM(ms.p_qty)-SUM(mw.w_qty) final_qty FROM manage_stock ms
INNER JOIN manage_wastage mw on ms.p_id =mw.p_id
GROUP BY ms.p_id
Try above code.
As p_name always unique with p_id GROUP_CONCAT() only returns single name.

Mysql: Duplicate and incorrect output when using join

I have 4 tables on which I need to use a join. The query is returning duplicate rows. However for simplicity I will post my question with just 2 tables as the problem still exits even with 2 tables. Tables are called as product_offer and promo. The structures are as below
product_offer table:
id product_id offer_id store_id validity
1 1 1 1 2016-12-12 00:00:00
2 2 2 1 2016-12-12 00:00:00
3 3 3 1 2016-12-12 00:00:00
4 1 4 1 2016-12-12 00:00:00
promo table:
id product_id store_id name
1 1 1 Buy 1 Get 1 free
2 2 1 10% off
3 3 1 $10 off
4 1 1 Get Chips Free
I am trying to fetch all the offers for a particular product-store combination. I am expecting to get this
product_id offer_id store_id name
1 1 1 Buy 1 Get 1 Free
1 4 1 Get Chips Free
But When I run this query I am getting the below output which is incorrect and with duplicate rows
SELECT po.product_id, po.offer_id,po.store_id,o.name
from product_offer po
left join promo o on po.product_id = o.product_id
where po.product_id = 1 and po.store_id = 1
product_id offer_id store_id name
1 1 1 Buy 1 Get 1 Free
1 4 1 Buy 1 Get 1 Free
1 1 1 Get Chips Free
1 4 1 Get Chips Free
I also want to improve the query and use concat(using ~ as separator) to only return one row per product_id as below:
product_id offer_id store_id name
1 1~4 1 Buy 1 Get 1 free~Get Chips Free
You want to use a GROUP BY, what does it, it takes the column you chose puts them together to avoid duplicate results where data in that column matches
SELECT po.product_id, po.offer_id,po.store_id,o.name
from product_offer po
left join promo o on po.product_id = o.product_id
where po.product_id = 1 and po.store_id = 1
GROUP BY product_id
In order to concatenate them, there is the option of GROUP_CONCAT, you can read more at http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat
use Group by with GROUP_CONCAT
SELECT po.product_id,GROUP_CONCAT(DISTINCT po.offer_id SEPARATOR '~') offer_id
,po.store_id,GROUP_CONCAT(DISTINCT o.name SEPARATOR '~') name
from product_offer po
left join promo o on po.product_id = o.product_id
where po.product_id = 1 and po.store_id = 1
Group by product_id
I realized I was doing the Join ON the wrong column. When I changed the join on offer_id from product_offer and id on promo it worked nicely.
SELECT po.product_id, group_concat(po.offer_id SEPARATOR '~') offer_id, po.store_id ,group_concat(o.name SEPARATOR '~') offer
FROM product_offer po
LEFT JOIN
promo o ON po.offer_id = o.id
WHERE
po.product_id = 1 and po.store_id = 1
I think you need to group by offer_id
SELECT po.product_id,GROUP_CONCAT(po.offer_id SEPARATOR '~') offer_id,po.store_id,o.name
from product_offer po
left join promo o on po.product_id = o.product_id
where po.product_id = 1 and po.store_id = 1
Group by offer_id

Query and table - MAX and Join

Still very new to all of this so bear with me.
Have 3 tables
table 1: member
Mem_index, Mem_name
1 joe
2 Mark
Table 2: Course
Course_index, Course_Name
1 Math
2 Reading
Table 3 : data
Data index,Member,Course,Score
1 1 1 85
2 1 2 75
3 2 1 95
4 1 2 65
SO what I would like to do is create a table:
Do a query and gather all of the courses, find the max score for each course and attribute the member name to it.
Table result should look like:
Course, Max score,name
Math 95 Mark
Reading 75 Mark
I can do the query individually but unsure of how to loop it and then propogate the data into the table.
How about this query for SQL?
SELECT c.course_name, MAX( d.score ), m.mem_name
FROM members m
JOIN data d on m.mem_id = d.member
JOIN course c on c.course_id = d.course
GROUP BY d.course
ORDER BY d.score, m.mem_name, c.course_name
Not sure if the field names match up but you get the idea - tested this in sql with some dummy data.
Data
Index Member Course Score
1 1 1 60
1 1 1 85
Course
course_id course_name
1 Math
2 English
3 Science
Members
mem_id mem_name
1 Mark
2 James
You will get the following
Course Name Score Member
Math 85 Mark
Try this query :
SELECT c.course_Name , MAX(d.score),m.mem_name
FROM data d
JOIN course c ON d.course=c.course_index
JOIN members m ON m.mem_index = d.member
GROUP BY d.course
ORDER by MAX(d.score) DESC

get SUM of another row of GROUP BY'd rows

I have this table:
This selection is is duplicated many times for different var_lines (which pretty much work as one row of data, or respondent for a survey) and set_codes (different survey codes).
With this query:
SELECT
*, COUNT(*) AS total
FROM
`data`
WHERE
`var_name` = 'GND.NEWS.INT'
AND(
`set_code` = 'BAN11A-GND'
OR `set_code` = 'BAN09A-GND'
OR `set_code` = 'ALG11A-GND'
)
AND `country_id` = '5'
GROUP BY
`data_content`,
`set_code`
ORDER BY
`set_code`,
`data_content`
The query basically counts the number of answers for a specific question. Then groups them survey (set_code).
What I need is for each of the grouped data_content answers for GND.NEWS.INT to also show the SUM of all the corresponding GND_WT with the same var_line.
For example if I had this:
data_id data_content var_name var_line
1 2 GND.NEW.INT 1
2 1.4 GND_WT 1
3 2 GND.NEW.INT 2
4 1.6 GND_WT 2
5 3 GND.NEW.INT 3
6 0.6 GND_WT 3
I would get something like this:
data_id data_content var_name var_line total weight
1 2 GND.NEW.INT 1 2 3
5 3 GND.NEW.INT 3 1 0.6
Thanks for any help.
Your requirements are not exactly clear, but I think the following gives you what you want:
select d1.data_id,
d1.data_content,
d1.var_name,
d1.var_line,
t.total,
w.weight
from data d1
inner join
(
select data_content,
count(data_content) Total
from data
group by data_content
) t
on d1.data_content = t.data_content
inner join
(
select var_line,
sum(case when var_name = 'GND_WT' then data_content end) weight
from data
group by var_line
) w
on d1.var_line = w.var_line
where d1.var_name = 'GND.NEW.INT'
See SQL Fiddle with Demo
This Query can be suitable for your specific example:
select st.data_id,
st.data_content,
st.var_name,
st.var_line,
count(st.data_id) as total,
sum(st1.data_content) as weight
from data st
left join data st1 on st1.var_name = 'GND_WT' AND st1.var_line=st.var_line
where st.var_name='GND.NEW.INT'
group by st.data_content
Regards,
Luis.

Categories