I have a shipments table, and a shipments detail table. A shipment generally ships multiple cartons. I am trying to select a count of the shipment table and sum the quantity from the detail table. But my values are being selected from the joined table.
ex. count = 7, when count should be 4 from my shipment table
SELECT ss.tenant_id,
ss.order_id,
COUNT(ss.shipment_number),
SUM(sd.qty_shipped)
FROM shipment ss
LEFT JOIN detail sd
ON ss.id = sd.shipment_id
GROUP BY
ss.order_id,
ss.tenant_id;
output -->
tenant_id | order_id | count | sum
-----------+----------+-------+------
1 | 2573 | 7 | 1350
Data set -->
shipment
id | shipment_number | shipment_status | tracking_number | shipping_cost
------+-----------------+----------------+----------------+---------------
8332 | 1000048 | confirmed | 123 | 10.00
8333 | 1000049 | confirmed | 123 | 10.00
8334 | 1000050 | confirmed | 123 | 10.00
8335 | 1000051 | confirmed | 123 | 10.00
detail
id | carton_number | qty_shipped | order_id | shipment_id
-------+---------------+-------------+----------+------------
14654 | 1 | 200 | 2573 | 8332
14655 | 2 | 200 | 2573 | 8332
14656 | 1 | 200 | 2573 | 8333
14657 | 1 | 200 | 2573 | 8334
14658 | 2 | 200 | 2573 | 8334
14659 | 1 | 150 | 2573 | 8335
14660 | 2 | 200 | 2573 | 8335
I had to add distinct in my count.
select ss.tenant_id,
ss.order_id,
count(distinct ss.shipment_number),
sum(sd.qty_shipped)
from shipping_shipment ss
left join shipping_shipmentdetail sd
on ss.id = sd.shipment_id
GROUP BY
ss.order_id,
ss.tenant_id;
Related
I have two tables and I want one query that gets sum of table 2 (addonAmount) column and combine it with table 1
table 1: subscriptions
+-------+--------+-------------+
| subId | userId | subDuration |
+-------+--------+-------------+
| 80 | 4607 | 6 |
| 81 | 4607 | 12 |
| 82 | 4608 | 18 |
+-------+--------+-------------+
table 2: subscriptionAddons
+---------+-------+-------------+
| addonId | subId | addonAmount |
+---------+-------+-------------+
| 15 | 80 | 4 |
| 16 | 80 | 2 |
+---------+-------+-------------+
Query I used:
SELECT subscriptions.*, subscriptionAddons.addonAmount
FROM subscriptions LEFT JOIN subscriptionAddons
ON subscriptions.subId = subscriptionAddons.subId;
what I want
+-------+--------+-------------+------------+
| subId | userId | subDuration |addonAmount |
+-------+--------+-------------+------------+
| 80 | 4607 | 6 |6 |
| 81 | 4607 | 12 |NULL |
| 82 | 4608 | 18 |NULL |
+-------+--------+-------------+------------+
You need to group by the columns that define a record and sum the column you want as result.
SELECT s.subId, s.userId, s.subDuration, sum(sa.addonAmount) as addonAmount
FROM subscriptions s LEFT JOIN subscriptionAddons sa
ON s.subId = sa.subId
GROUP BY s.subId, s.userId, s.subDuration;
I have the following data in my database:
scu_banks:
---------------------------------
| id | type | name |
|-------------------------------|
| 1 | 1 | One |
| 2 | 1 | Two |
| 3 | 2 | Three |
| 4 | 3 | Four |
---------------------------------
scu_statement:
---------------------------------
| id | code | mutation |
|-----------------------------------|
| 1 | 1 | 100 |
| 2 | 1 | 200 |
| 3 | 2 | 500 |
| 4 | 1 | 500 |
-------------------------------------
What I want to do is I want to select all the rows in table scu_banks and show the total sum of mutations. The data should be represented like:
--------------------------------------------------------------
| scu_banks.type | scu_banks.name | total | scu_banks.id |
--------------------------------------------------------------
| 1 | One | € 800.00 | 1 |
| 1 | Two | € 500.00 | 2 |
| 2 | Three | € 0.00 | 3 |
| 3 | Four | € 0.00 | 4 |
--------------------------------------------------------------
When I run my sql statement I get the following data:
---------------------------------------------------------------
| scu_banks.type | scu_banks.name | total | scu_banks.id |
--------------------------------------------------------------
| 1 | One | € 1300.00 | 1 |
---------------------------------------------------------------
The data I get in this case is not correct. € 1300.00 it the total of all the mutations in table scu_statement. The statement also dont shows the other rows in the database.
Does someone know what is wrong with my sql statement?
Here is my sql statement:
SELECT scu_banks.type,
scu_banks.name,
CONCAT('€ ', FORMAT(IFNULL(SUM(scu_statement.mutations), 0),2)) AS total,
scu_banks.id
FROM scu_banks
INNER JOIN scu_statement
ON scu_banks.id = scu_statement.code
Do the aggregation in a subquery and left join it to the banks.
SELECT b.type "scu_banks.type",
b.name "scu_banks.name",
concat('€ ', format(coalesce(x.mutation, 0), 2)) "total",
b.id "scu_banks.id"
FROM scu_banks b
LEFT JOIN (SELECT s.code,
sum(s.mutation) mutation
FROM scu_statement s
GROUP BY s.code) x
ON x.code = b.id;
I have following tables that manage revenue
revenue Table
+------------+--------+--------+
| revenue_id | amount | status |
+------------+--------+--------+
| 1 | 45000 | 1 |
| 2 | 25000 | 1 |
| 3 | 67000 | 1 |
| 4 | 22000 | 1 |
| 5 | 32000 | 0 |
+------------+--------+--------+
bank Table
+---------+--------+-------------+-------------+
| bank_id | ref_no | bank_amount | bank_status |
+---------+--------+-------------+-------------+
| 1 | 2 | 23000 | Pending |
| 2 | 3 | 67000 | Confirmed |
| 3 | 4 | 22000 | Confirmed |
+---------+--------+-------------+-------------+
02) If a revenue as mentioned in the revenue table has banked, it is recorded in the bank table. After that, the amounts in two tables are equal, the bank status may be into "Confirmed".
03) So, I need to get Confirmed records only as following by joining above two tables
Desired Output
+------------+--------+-------------+-------------+
| revenue_id | amount | bank_amount | bank_status |
+------------+--------+-------------+-------------+
| 3 | 67000 | 67000 | Confirmed |
| 4 | 22000 | 22000 | Confirmed |
+------------+--------+-------------+-------------+
Desired Output-02
+------------+--------+-------------+-------------+
| revenue_id | amount | bank_amount | bank_status |
+------------+--------+-------------+-------------+
| 1 | 45000 | | |
| 2 | 25000 | 23000 | Pending |
| 3 | 67000 | 67000 | Confirmed |
| 4 | 22000 | 22000 | Confirmed |
+------------+--------+-------------+-------------+
Desired Output-03
+------------+--------+-------------+-------------+
| revenue_id | amount | bank_amount | bank_status |
+------------+--------+-------------+-------------+
| 1 | 45000 | | |
| 2 | 25000 | 23000 | Pending |
+------------+--------+-------------+-------------+
04) To get the desired output I used the following query
select revenue.revenue_id, revenue.amount, bank.bank_amount, bank.bank_status
from revenue
left join bank on bank.ref_no = revenue.revenue_id
where revenue.status = 1 and bank.bank_status = "Confirmed"
05) But did't get the expected result. It generated only the empty result. I can not understand what I am going wrong. Can any one help me ?
You have to use Inner Join instead of Left JOIN for your expected output.
SELECT revenue.revenue_id,
revenue.amount,
bank.bank_amount,
bank.bank_status
FROM revenue
INNER JOIN bank ON bank.ref_no = revenue.revenue_id
WHERE revenue.status = 1
AND bank.bank_status = "Confirmed"
DEMO
select revenue.revenue_id, revenue.amount, bank.bank_amount, bank.bank_status
from revenue
Inner join (select *
From bank
Where bank_status = "Confirmed") bank
on bank.ref_no = revenue.revenue_id
You have to use the first bank table and then revenue table for left join .
Because in left join all rows will be returned even without matches.
So, use query like this.
select revenue.revenue_id, revenue.amount, bank.bank_amount, bank.bank_status
from bank
left join revenue on bank.ref_no = revenue.revenue_id
where revenue.status = 1 and bank.bank_status = "Confirmed"
i try select all columns from two different tables WHERE active = 1
i have 2 tables table_pro and table_basic,
sql:"select * from table_basic,table.name";
and 2 condition:
WHERE active = 1
WHERE table_pro.id = table_basic.name.id
how to make it correctly
Here is table_pro
+----+--------+---------+-----------+
| id | people | rooms | active |
+----+--------+---------+-----------+
| 1 | 5 | 10 | 0 |
| 2 | 12 | 17 | 0 |
| 3 | 21 | 38 | 1 |
+----+--------+---------+-----------+
Here is table_basic
+---------+-------+---------+------------+----------+
| name_id | name | balance | title | time |
+---------+-------+---------+------------+----------+
| 1 |shop | 100 | failed | 15:10:20 |
| 2 |factory| 75 | error | 15:10:20 |
| 3 |studio | 25 | timed_out | 15:10:20 |
+---------+-------+---------+------------+----------+
I'd like to have this output result only rows (from of all columns) with status active = 1
+-----+-------+----- --+--------+-------+----------+---------+--------+
| id | people| rooms | name |balance| title | time | active |
+-----+-------+--------+--------+-------+----------+---------+--------+
| 3 | 21 | 38 | studio |25 | timed_out| 15:10:20| 1 |
+-----+-------+--------+--------+-------+----------+---------+--------+
Thanks
SELECT A.id, A.people, A.rooms, B.name, B.balance, B.title, B.time, A.active
FROM
table_pro AS A
JOIN
table_basic AS B
ON
A.id = B.name_id
WHERE
A.id = 3
SELECT table_pro.*, table_basic.*
FROM table_pro
INNER JOIN table_basic
ON table_basic.name_id = table_pro.id
WHERE table_pro.active = 1
I need help generating SQL for MySQL database.
I have three tables:
Organisations
Members
Payments
Organisations table:
+------------+---------+--------+
| id | name |website |
+------------+---------+--------+
| 1 | AAA | a.com |
|-------------------------------+
| 2 | BBB | b.com |
+------------+---------+--------+
Members table:
+------------+-------------------+--------+-----------------+-----------+
| id | organisation_id |name | Payment_confirm | join_date |
+------------+-------------------+--------+-----------------+-----------+
| 1 | 1 | james | 1 | 2013-8-02 |
|-----------------------------------------+-----------------+-----------+
| 2 | 1 | Jimmy | 0 | 2013-6-25 |
+------------+-------------------+--------+-----------------+-----------+
| 3 | 2 | Manny | 1 | 2013-07-02|
|-----------------------------------------+-----------------+-----------+
| 4 | 1 | Kim | 1 | 2013-09-02|
+------------+-------------------+--------+-----------------+-----------+
Payments table:
+------------+-------------------+--------+-----------------+----------------+
| id | member_id |amount | transaction_id | transferred_at |
+------------+-------------------+--------+-----------------+----------------+
| 1 | 1 | 100 | T1001 | 2013-8-03 |
|-----------------------------------------+-----------------+--------------- +
| 2 | 2 | 0 | null | Null |
+------------+-------------------+--------+-----------------+----------------+
| 3 | 3 | 200 | T1002 | Null |
|-----------------------------------------+-----------------+----------------+
| 4 | 4 | 50 | T1005 | 2013-09-05 |
+------------+-------------------+--------+-----------------+----------------+
How can I select the following?
Expecting the following output:
+------------+-------------------+--------+-----------------+---------------+--------------+
| Org name | Revenue |untransferred amount | Total members | last 30 days |
+------------+-------------------+--------------------------+---------------+--------------+
| AAA | 150 | 0 | 3 | 2 |
|-----------------------------------------------------------+---------------+--------------+
| BBB | 200 | 200 | 1 | 0 |
+------------+-------------------+--------------------------+---------------+--------------+
Org name = organisation name
Revenue = Total amount received
untransferred amount = transferred_at is null (payments table)
Total members = total members joined till today
last 30 days = total members joined last 30 days
You need to join your tables, group the results and select the desired logic:
SELECT org.name,
SUM(pmt.amount) AS revenue,
SUM(IF(pmt.transferred_at IS NULL, pmt.amount, 0)) AS untransferred
FROM Organisations org
JOIN Members mem ON mem.organisation_id = org.id
JOIN Payments pmt ON pmt.member_id = mem.id
GROUP BY org.id
See it on sqlfiddle.
select o.name,
sum(amount) as Revenue,
sum(if(transferred_at is null, amount, 0)) as untransfered_ammt,
sum(if(join_date>=curdate() - interval 30 day, 1, 0)) as last_30_d
from organisations o
inner join members m on o.id=m.organisation_id
inner join payments p on p.member_id=m.member_id
group by 1