How to multiply 2 table/list in MySQL? - mysql

In MySql I obtained 2 list/tables from these 2 queries. Table 1 contains Quantities for a type of ticket, Table 2 contains Prices of a type of ticket.
Table 1:
SELECT Count(`ticket`.`Ticket_type`) AS Counter
FROM `ticket`
WHERE ((`ticket`.`Ticket_type` = 'Adult') OR (`ticket`.`Ticket_type` = 'Senior'))
GROUP BY `ticket`.`Ticket_type`
Table2 :
SELECT `ticketprice`.`price`
FROM `ticketprice`
WHERE ((`ticketprice`.`Ticket_type` = 'Adult') OR (`ticketprice`.`Ticket_type` = 'Senior'))
My question is how do I being to multiply these two tables? (Qunatity * Price) = Total
Will Appreciate any help!

Join the tables and multiply:
SELECT t.ticket_type, COUNT(*) AS quantity, p.price, p.price * COUNT(*) AS total
FROM ticket AS t
JOIN ticketprice AS p ON t.ticket_type = p.ticket_type
WHERE t.ticket_type IN ('Adult', 'Senior')
GROUP BY t.ticket_type

Related

SUM(DISTINCT) on the same table?

I currently have table as below:
By executing the code as:
SELECT FromLedger, SUM(TransactionVal)
FROM Journal
WHERE FromGroup = 'Asset'
GROUP BY FromLedger
I get the result as:
Simlarly, I get the another set by executing:
SELECT ToLedger, SUM(TransactionVal)
FROM Journal
WHERE ToGroup = 'Asset'
GROUP BY ToLedger
I want to get a single table combining the both table with Group By Ledger Id and another column as the difference between the SUM of the above 2 table. In other words, I am looking for table as below
How do I get it please?
You can join the 2 queries like this:
SELECT t.ToLedger UniqueLedgerId, t.total - f.total ClosingBalance
FROM (SELECT ToLedger, SUM(TransactionVal) total FROM Journal WHERE ToGroup = 'Asset' GROUP BY ToLedger) t
INNER JOIN (SELECT FromLedger, SUM(TransactionVal) total FROM Journal WHERE FromGroup = 'Asset' GROUP BY FromLedger) f
ON f.FromLedger = t.ToLedger
or use UNION ALL and then aggregate:
SELECT t.UniqueLedgerId, SUM(t.TransactionVal) ClosingBalance
FROM (
SELECT ToLedger UniqueLedgerId, TransactionVal FROM Journal WHERE ToGroup = 'Asset'
UNION ALL
SELECT FromLedger, -TransactionVal FROM Journal WHERE FromGroup = 'Asset'
) t
GROUP BY t.UniqueLedgerId

Inner join and left join not working as expected in mysql

I have 3 tables
table_supplier_bills - bill_id, supplier_id, date
table_supplier_bill_details - bill_id, product_id, quantity, rate
table_supplier_bill_payment_details - id, bill_id, payment_date, amount
I want to get all the bills with their bill_amount and paid_amount.
This is my query.
select
SB.bill_id,
SB.date, SB.supplier_id,
SUM(SBD.quantity * SBD.rate) as bill_amount,
COALESCE(SUM(SBPD.payment_amount), 0.00) as paid_amount
from table_supplier_bills SB
INNER JOIN
table_supplier_bill_details SBD
ON SB.bill_id = SBD.bill_id
LEFT JOIN table_supplier_bill_payment_details SBPD
ON SBD.bill_id = SBPD.bill_id
group by SBD.bill_id;
But this query doesn't give correct paid_amount if there are multiple rows in table_supplier_bill_details for a bill. in case of multiple rows query gives the paid_amount multiplied by as many rows are in table_supplier_bill_details for that table.
Can anyone help me what is wrong here?
Use a correlated query instead :
SELECT SB.bill_id,
SB.date,
SB.supplier_id,
SUM(SBD.quantity * SBD.rate) as bill_amount,
COALESCE((SELECT SUM(SBPD.payment_amount)
FROM table_supplier_bill_payment_details SBPD
WHERE SBD.bill_id = SBPD.bill_id ),0.00) as paid_amount
FROM table_supplier_bills SB
INNER JOIN table_supplier_bill_details SBD
ON SB.bill_id = SBD.bill_id
GROUP BY SBD.bill_id;

Select SUM multiply field for another field in different tables and this is equal

I explain through the example:
First table is products:
id|price|
1 30
Second table is warehouse:
id|id_product|amount|sell_price|
1 1 1 50
I will want SUM of multiply amount (warehouse table) * price (products table) where id (warehouse table) = id (products table)
$result_sum_ricavo = mysqli_query($link,"SELECT SUM(total_sum) FROM (p.prezzo * w.quantita as total_sum FROM prodotti p JOIN storico_magazzino w on p.id = w.id_collo AND w.causale='consegna' AND w.tipo='prodotto' AND w.importo_documento>0)");
$row_sum_ricavo = mysqli_fetch_assoc($result_sum_ricavo);
$guadagno = $row_sum_ricavo["total_sum"];
I have this error:
mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean
given in
SELECT
SUM(total_sum)
FROM (
SELECT p.price * w.amount as total sum
FROM products p JOIN warehouse w on p.id = w.id
)

MySQL query joining 3 tables and counting

I've been stuck on this problem for far too long.
I have to merge 3 tables and do some counting of distinct values.
I have 3 tables
1.User_me
profileId( String )
responded( int 1 or 0)
2.Profiles
profileId ( String )
idLocation ( int )
3.lookup_location
id ( int )
location (String )
I can join User_me and Profiles ON User_me.profileId = Profiles.profileId
I can join Profiles and lookup_location ON Profiles.idLocation = lookup_location.id
Under Profiles I need to count the number of distinct values for idLocation where User_me.profileId = Profiles.profileId
I also need to count the number of Profiles.idLocation that have User_me.responded = 1
I have this:
SELECT lookup.location, count(*) as total
FROM User_me user
JOIN Profiles
ON user.profileId= profiles.profileId
JOIN lookup_location lookup
ON profiles.idLocation = lookup.id
GROUP BY profiles.idLocation
but I still need to have the column giving me the count where User_me.responded = 1
Something like:
SELECT lookup.location, count(*) as total, count(*) responded
If I'm understanding your question correctly, you can you a case statement in the count aggregate:
SELECT lookup.location, count(*) as total,
count(case when user.responded = 1 then 1 end) as responded
FROM User_me user
JOIN Profiles
ON user.profileId= profiles.profileId
JOIN lookup_location lookup
ON profiles.idLocation = lookup.id
GROUP BY profiles.idLocation
Since you're using MySQL, you can also use something like sum(user.responded = 1).

Inner query is difficult to write

I have two tables:
customer with schema_id
Schema table has: schema_id, period, amt, updated_date
I need to take join of customer and schema but only retrieve the latest record joined and not the others.
customer table
cust_id name schema_id
1 ABC 1
Schema table
schema_id period amt updated_date
1 1 100 2010-4-1
1 2 150 2011-4-1
If you need the max(updated_date) for each schema_id, then you can use an subquery:
select c.cust_id, c.name, c.schema_id, s.period, s.amt, s.updated_date
from customer c
inner join
(
select s1.schema_id, s1.period, s1.amt, s1.updated_date
from `schemas` s1
inner join
(
select schema_id, max(updated_date) MaxDate
from `schemas`
group by schema_id
) s2
on s1.schema_id = s2.schema_id
and s1.updated_date = s2.maxdate
) s
on c.schema_id = s.schema_id
See SQL Fiddle with Demo
The subquery is then used in a join back to your table to return the rows that have the matching date and schema_id.
If I understood your problem, you need to take lastest register of the "schema".
I think you need to use max() function. So, try the query below:
select *
from customer c,
schema s
where c.schema_id = s.schema_id
and s.updated_date = ( select max(s2.updated_date)
from schema s2
where s2.schema_id = s.schema_id
)
Regards!
Edmilton