I have these two table.
-----------------------------------------------
|order_id | cust_id| order_detail| order_price|
|101 | 203 | Canon-XL | 4500.00 |
|102 | 201 | Canon-XL | 4500.00 |
|103 | 201 | Battery-L | 850.00 |
|104 | 207 | EPSONL 120 | 5100.00 |
|105 | 205 | EPSONL 120 | 5100.00 |
|106 | 203 | Battery-S | 5100.00 |
|107 | 204 | HP DESK-230 | 520.00 |
|108 | 206 | TRIPOD-XL | 1550.00 |
-----------------------------------------------
------------------------------
|cust_id| salary_id| salary |
|201 | 101 | 3200.00 |
|202 | 102 | 4100.00 |
|203 | 103 | 2000.00 |
|204 | 104 | 5100.00 |
|205 | 105 | 5100.00 |
|206 | 106 | 2500.00 |
|207 | 107 | 2700.00 |
------------------------------
Now I want to sum the order_price on tb1 where salary on tb2 is less than 3000..
How can I do this?
------------
|total_order |
|------------|
|11150 |
------------
You could join the two tables according to the cust_id:
SELECT SUM(order_price)
FROM tb1
JOIN tb2 ON tb1.cust_id = tb2.cust_id
WHERE salary < 3000
Related
Bus
+----------+-------------+-------------+
| route_no | Start_point | destination |
+----------+-------------+-------------+
| 123 | ABC | BCD |
| 234 | DEF | EFG |
| 345 | GHI | HIJ |
| 445 | JKL | KLM |
| 546 | MNO | NOP |
+----------+-------------+-------------+
Passenger
+-----+--------+--------+------+
| pid | pname | gender | age |
+-----+--------+--------+------+
| 111 | David | m | 30 |
| 222 | Andy | f | 20 |
| 333 | kat | f | 27 |
| 444 | viki | m | 32 |
| 555 | rob | m | 52 |
+-----+--------+--------+------+
Booking
+-----+----------+------------+---------+
| pid | route_no | jrny_date | seat_no |
+-----+----------+------------+---------+
| 111 | 123 | 2019-05-14 | 57 |
| 222 | 234 | 2019-06-11 | 3 |
| 333 | 345 | 2019-07-20 | 33 |
| 444 | 445 | 2018-08-22 | 14 |
| 555 | 546 | 2018-11-17 | 19 |
+-----+----------+------------+---------+`
1)I am trying to display all passengers on route_no=123 traveling from ABC to BCD.
2) Displaying all pid and gender of all passengers traveling on route_no=345 on the current date.
select passenger.*
from passeger
, bus
, booking
where passenger.pid = booking.pid;
ERROR 1146 (42S02): Table 'travel_agency.passenger' doesn't exist`
mysql> select passenger.* from passeger,bus,booking where passenger.pid=booking.
pid;
Is passenger ? probably this mistake.
To your answer query here:
1)
select passenger.pname, passenger.gender, passenger.age, bus.Start_point, bus.destination from passenger,bus,booking where passenger.pid=booking.pid and booking.route_no= bus.route_no and bus.route_no=123;
2)
select passenger.pname, passenger.gender, booking.jrny_date from passenger,booking where passenger.pid=booking.pid and booking.route_no=345;
I have two tables orders and customers:
select * from orders;
+------+---------------------+-------------+--------+
| oid | date | customer_id | amount |
+------+---------------------+-------------+--------+
| 102 | 2009-10-08 00:00:00 | 4 | 300 |
| 100 | 2009-10-08 00:00:00 | 3 | 15000 |
| 101 | 2008-10-08 00:00:00 | 2 | 1300 |
| 105 | 2010-10-08 00:00:00 | 1 | 400 |
| 106 | 2014-12-23 00:00:00 | 3 | 300 |
+------+---------------------+-------------+--------+
select * from customers;
+------+--------+------+-----------+----------+
| id | name | age | address | salary |
+------+--------+------+-----------+----------+
| 1 | ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | khilan | 25 | delhi | 1500.00 |
| 3 | muffy | 22 | bhopal | 8500.00 |
| 4 | suresh | 48 | mumbai | 24000.00 |
| 1 | ramesh | 32 | Ahmedabad | 300.00 |
| 5 | akil | 21 | madurai | 1000.00 |
| 6 | rajesh | 22 | delhi | 5000.00 |
+------+--------+------+-----------+----------+
What I'm trying to do is to do SUM(salary) from customers and subtract it with the SUM(amount) from orders table. I have tried with this query:
SELECT id ,NAME,SUM(salary),SUM(amount),SUM(salary)-SUM(amount)
FROM customers a LEFT JOIN orders b ON a.id=b.customer_id
GROUP BY NAME;
This will return the following result, which some of them return incorrect value:
+------+--------+-------------+-------------+-------------------------+
| id | name | SUM(salary) | SUM(amount) | SUM(salary)-SUM(amount) |
+------+--------+-------------+-------------+-------------------------+
| 5 | akil | 1000.00 | NULL | NULL |
| 2 | khilan | 1500.00 | 1300 | 200.00 |
| 3 | muffy | 17000.00 | 15300 | 1700.00 |
| 6 | rajesh | 5000.00 | NULL | NULL |
| 1 | ramesh | 2300.00 | 800 | 1500.00 |
| 4 | suresh | 24000.00 | 300 | 23700.00 |
+------+--------+-------------+-------------+-------------------------+
My expected output is as following:
+------+--------+-------------+-------------+-------------------------+
| id | name | SUM(salary) | SUM(amount) | SUM(salary)-SUM(amount) |
+------+--------+-------------+-------------+-------------------------+
| 5 | akil | 1000.00 | NULL | 1000 |
| 2 | khilan | 1500.00 | 1300 | 200.00 |
| 3 | muffy | 8500 | 15300 | -6800 |
| 6 | rajesh | 5000.00 | NULL | 5000 |
| 1 | ramesh | 2300.00 | 400 | 1900.00 |
| 4 | suresh | 24000.00 | 300 | 23700.00 |
+------+--------+-------------+-------------+-------------------------+
One way is to make calculation on orders into sub-query.
To cater for NULL value, you can use IFNULL(value,0).
SELECT id,NAME,SUM(salary),amt,SUM(salary)-IFNULL(amt,0)
FROM customers a LEFT JOIN
(SELECT customer_id, SUM(amount) amt FROM orders GROUP BY customer_id) b
ON a.id=b.customer_id
GROUP BY NAME;
+----------------+
| Tables_in_test |
+----------------+
| contribution |
| expectedamount |
| registration |
+----------------+
registration table
+----+--------+-------------+
| id | reg_no | fullname |
+----+--------+-------------+
| 1 | TTI001 | JOHN JAMES |
| 2 | TTI002 | DAVID CERES |
| 3 | TTI003 | JOYCE LEE |
| 4 | TTI004 | JOEL MARTIN |
+----+--------+-------------+
espectedamount
+----+--------+---------+---------+---------+
| id | reg_no | number1 | number2 | number3 |
+----+--------+---------+---------+---------+
| 1 | TTI001 | 500 | 500 | 500 |
| 2 | TTI002 | 500 | 500 | 500 |
| 3 | TTI003 | 500 | 500 | 500 |
| 4 | TTI004 | 500 | 500 | 500 |
| 5 | TTI001 | 400 | 400 | 400 |
| 6 | TTI001 | 1000 | 1000 | 1000 |
| 7 | TTI002 | 1000 | 1000 | 1000 |
| 8 | TTI003 | 1000 | 1000 | 1000 |
| 9 | TTI004 | 1000 | 1000 | 1000 |
+----+--------+---------+---------+---------+
contribution table
+----+--------+---------+---------+---------+
| id | reg_no | number1 | number2 | number3 |
+----+--------+---------+---------+---------+
| 1 | TTI001 | 200 | 400 | 600 |
| 2 | TTI002 | 100 | 50 | 250 |
| 3 | TTI001 | 100 | 400 | 400 |
| 4 | TTI002 | 300 | 400 | 600 |
| 5 | TTI003 | 300 | 100 | 50 |
| 6 | TTI004 | 50 | 60 | 40 |
| 7 | TTI004 | 500 | 300 | 400 |
+----+--------+---------+---------+---------+
I created the following Query to join tables registration,expectedamaount,and contribution where i WANT TO SUBTRACT SUM FORM amount table from sum contribution table but am getting Wrong result
select registration.reg_no
,registration.fullname
,sum(expectedamount.number1-contribution.number1) as contribution1
,sum(expectedamount.number2-contribution.number2) as contribution2
,sum(expectedamount.number3-contribution.number3) as contribution3
FROM registration
INNER JOIN expectedamount ON registration.reg_no = expectedamount.reg_no
INNER JOIN contribution ON expectedamount.reg_no = contribution.reg_no
GROUP BY reg_no;
+--------+-------------+---------------+---------------+---------------+
| reg_no | fullname | contribution1 | contribution2 | contribution3 |
+--------+-------------+---------------+---------------+---------------+
| TTI001 | JOHN JAMES | 700 | 200 | 0 |
| TTI002 | DAVID CERES | 600 | 550 | 150 |
| TTI003 | JOYCE LEE | 200 | 400 | 450 |
| TTI004 | JOEL MARTIN | 450 | 640 | 560 |
+--------+-------------+---------------+---------------+---------------+
expected result
+-------+---------------+---------------+------------------+--------------+
|reg_no | fullname | contribution1 | contribution2 | contribution3|
+-------+---------------+---------------+------------------+--------------+
|TTI001 | JOHN JAMES | 1600 | 1100 | 900 |
|TTI002 | DAVID CERES | 1000 | 950 | 550 |
|TTI003 | JOYCE LEE | 1200 | 1400 | 1450 |
|TTI004 | JOEL MARTIN | 950 | 1140 | 1060 |
+-------+---------------+---------------+------------------+--------------+
Kindly guys help.
I think you need this -
select registration.reg_no
,registration.fullname
,sum(expected.num1-contrib.num1) as contribution1
,sum(expected.num2-contrib.num2) as contribution2
,sum(expected.num3-contrib.num3) as contribution3
FROM registration
INNER JOIN (SELECT reg_no, SUM(number1) num1, SUM(number2) num2, SUM(number3) num3
FROM expectedamount
GROUP BY reg_no) expected ON registration.reg_no = expected.reg_no
INNER JOIN (SELECT reg_no, SUM(number1) num1, SUM(number2) num2, SUM(number3) num3
FROM contribution
GROUP BY reg_no) contrib ON expected.reg_no = contrib.reg_no
GROUP BY reg_no;
i have two tables called
1 table smartpos.pos_order_Id
+---------+--------------+---------+--------+--------------+----------------+------------+
| orderId | restaurantId | tableId | closed | customerName | customerNumber | dateorderd |
+---------+--------------+---------+--------+--------------+----------------+------------+
| 7 | 14 | 0 | yes | | | 21/03/2018 |
| 8 | 14 | 0 | yes | | | 21/03/2018 |
| 9 | 14 | 0 | no | | | 20/03/2018 |
| 10 | 14 | 0 | yes | soumya | 1234567890 | 21/03/2018 |
| 11 | 14 | 0 | yes | | | 21/03/2018 |
| 12 | 14 | 0 | yes | | | 21/03/2018 |
| 13 | 14 | 0 | yes | | | 21/03/2018 |
| 14 | 14 | 0 | yes | | | 20/03/2018 |
| 15 | 14 | 0 | no | | | 22/03/2018 |
+---------+--------------+---------+--------+--------------+----------------+------------+
2smartpos.pos_invoice
+---------------+---------+----------+-------------+-------------+------------+-------------+---------------+
| invoiceNumber | orderId | totalAmt | discountAmt | totalTaxAmt | grandTotal | paymentmode | paymentrefNum |
+---------------+---------+----------+-------------+-------------+------------+-------------+---------------+
| 1 | 7 | 200 | 34 | 46 | 212 | Cash | |
| 2 | 10 | 1200 | 200 | 280 | 1280 | Cash | |
| 3 | 1 | 720 | 34 | 120 | 806 | Cash | |
| 4 | 12 | 240 | 34 | 58 | 264 | Cash | |
| 5 | 13 | 330 | 32 | 83 | 381 | Cash | |
| 6 | 14 | 80 | 2 | 22 | 100 | Cash | |
+---------------+---------+----------+-------------+-------------+------------+-------------+---------------+
i want to fetch invoice details using restaurantId and two dates by providing restaurantId and dates as follows
select inv.invoiceNumber ,inv.totalAmt,inv.discountAmt,inv.totalTaxAmt,inv.grandTotal,i.dateorderd from smartpos.pos_invoice inv,smartpos.pos_order_Id i where inv.invoiceNumber in (select invv.invoiceNumber from smartpos.pos_invoice invv where invv.orderId in(select ii.orderId from smartpos.pos_order_Id ii where ii.closed='yes' and ii.restaurantId=14 and STR_TO_DATE(dateorderd,'%d/%m/%Y') between STR_TO_DATE('20/03/2018','%d/%m/%Y') and STR_TO_DATE('21/03/2018','%d/%m/%Y'))) group by inv.invoiceNumber ;
out put:
+---------------+----------+-------------+-------------+------------+------------+
| invoiceNumber | totalAmt | discountAmt | totalTaxAmt | grandTotal | dateorderd |
+---------------+----------+-------------+-------------+------------+------------+
| 1 | 200 | 34 | 46 | 212 | NULL |
| 2 | 1200 | 200 | 280 | 1280 | NULL |
| 4 | 240 | 34 | 58 | 264 | NULL |
| 5 | 330 | 32 | 83 | 381 | NULL |
| 6 | 80 | 2 | 22 | 100 | NULL |
+---------------+----------+-------------+-------------+------------+------------+
but when i run above query it gives null values , how to fetch the date as well?
Its difficult to understand what you wrote but i think you query is much simple than it seems, try to use this approach :
select
inv.invoiceNumber ,
inv.totalAmt,
inv.discountAmt,
inv.totalTaxAmt,
inv.grandTotal,
i.dateorderd
from
smartpos.pos_invoice inv,
smartpos.pos_order_Id i
where
inv.orderId = i.orderId
and
i.closed='yes'
and
i.restaurantId=14
and
STR_TO_DATE(dateorderd,'%d/%m/%Y') between STR_TO_DATE('20/03/2018','%d/%m/%Y') and STR_TO_DATE('21/03/2018','%d/%m/%Y')
group by
inv.invoiceNumber;
Given the following three tables:
Table: A Table: B Table: C
+----+-----+ +-----+--------+ +--------+----------+
| id | cid | | cid | cat_id | | cat_id | cat_name |
+----+-----+ +-----+--------+ +--------+----------+
| 1 | 33 | | 33 | 6 | | 1 | AAAAA |
| 2 | 33 | | 33 | 20 | | 2 | BBBBB |
| 3 | 33 | | 33 | 59 | | 3 | CCCCC |
| 4 | 33 | | 33 | 88 | | 4 | DDDDD |
| 5 | 33 | | 35 | 8 | | 5 | EEEEE |
| 6 | 86 | | 86 | 1 | | 6 | FFFFF |
| 7 | 86 | | 86 | 14 | | 7 | GGGGG |
| 8 | 99 | | 86 | 45 | | 14 | HHHHH |
| 9 | 100 | | 86 | 57 | | 20 | IIIII |
+----+-----+ | 86 | 59 | | 42 | JJJJJ |
| 86 | 94 | | 45 | KKKKK |
| 98 | 5 | | 57 | MMMMM |
| 99 | 42 | | 58 | NNNNN |
| 100 | 75 | | 59 | OOOOO |
+-----+--------+ | 75 | PPPPP |
| 88 | QQQQQ |
| 89 | RRRRR |
| 90 | SSSSS |
| 91 | TTTTT |
| 92 | UUUUU |
| 93 | VVVVV |
| 94 | WWWWW |
| 95 | XXXXX |
| 96 | YYYYY |
| 97 | ZZZZZ |
+--------+----------+
I would like to get:
Result 1:
+-----+--------+----------+-------------+
| cid | cat_id | cat_name | Total_Click | Total_Click = count(cid) from table A
+-----+--------+----------+-------------+
| 33 | 6 | FFFFF | 5 |
| 33 | 20 | IIIII | 5 |
| 33 | 59 | OOOOO | 5 |
| 33 | 88 | QQQQQ | 5 |
| 86 | 1 | AAAAA | 2 |
| 86 | 14 | HHHHH | 2 |
| 86 | 45 | KKKKK | 2 |
| 86 | 57 | MMMMM | 2 |
| 86 | 59 | OOOOO | 2 |
| 86 | 94 | WWWWW | 2 |
| 99 | 42 | JJJJJ | 1 |
| 100 | 75 | PPPPP | 1 |
+-----+--------+----------+-------------+
Ultimately, I would like to know the number of clicks each category received.
Result 2:
+----------+-------------+
| cat_name | Total_Click |
+----------+-------------+
| AAAAA | 2 |
| BBBBB | 0 |
| CCCCC | 0 |
| DDDDD | 0 |
| EEEEE | 0 |
| FFFFF | 5 |
| GGGGG | 0 |
| HHHHH | 2 |
| IIIII | 5 |
| JJJJJ | 1 |
| KKKKK | 2 |
| LLLLL | 0 |
| MMMMM | 2 |
| NNNNN | 0 |
| OOOOO | 7 |
| PPPPP | 1 |
| QQQQQ | 5 |
| RRRRR | 0 |
| SSSSS | 0 |
| TTTTT | 0 |
| UUUUU | 0 |
| VVVVV | 0 |
| WWWWW | 2 |
| XXXXX | 0 |
| YYYYY | 0 |
| ZZZZZ | 0 |
+----------+-------------+
Thanks!
Your first result can be obtained by doing a JOIN
SELECT a.cid, b.cat_id, c.cat_name, COUNT(a.id) as Click
FROM Table_c c
JOIN Table_b b ON c.cat_id = b.cat_id
JOIN Table_a a ON a.cid = b.cid
GROUP BY c.cat_name, a.cid, b.cat_id
Try the following query for your second result
SELECT c.cat_name, IFNULL(COUNT(a.id), 0) as Total_Click
FROM Table_c c
LEFT OUTER JOIN Table_b b ON c.cat_id = b.cat_id
LEFT OUTER JOIN Table_a a ON a.cid = b.cid
GROUP BY c.cat_name