Select total members and amount paid - mysql

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

Related

mysql - WHERE records does not have records in another table

I have some tables from which I need to get data.
Here is my structure:
employees
| id | name |
+----+---------+
| 1 | Michael |
| 2 | Sarah |
reports
| id | employee_id | month | year | value | group_id |
+----+-------------+-------+------+-------+----------+
| 1 | 1 | 01 | 2018 | 35 | 1 |
| 2 | 1 | 02 | 2018 | 12 | 1 |
| 3 | 2 | 02 | 2018 | 2 | 2 |
groups
| id | name | employee_id |
+----+------+-------------+
| 1 | G11 | 1 |
| 2 | Z15 | 2 |
Now I need to get groups with employee WHERE employee with group_id AND month AND year DON'T HAVE REPORT, eg.
When I look for 01.2018, it should returns me only Z15 but when I look for 04.2018 it should return Z15 and G11.
How can I do this? At this moment I have sth like this:
SELECT
groups.*,
employees.*,
-- all fields from reports
FROM
groups
INNER JOIN
employees
ON
employees.id = groups.employee_id
My column names are slightly different from yours. That's deliberate...
SELECT g.*
FROM groups g
LEFT
JOIN reports r
ON r.group_id = g.group_id
AND r.yearmonth = 201801
WHERE r.report_id IS NULL;

Query is selecting count from joined table

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;

MySQL join on three tables EAV model

Kindly consider the following tables:
invoices
+-----------+----+------------+--------+---------+
| accountid | id | customerid | total | balance |
+-----------+----+------------+--------+---------+
| 1 | 2 | 167909 | 120060 | 120060 |
+-----------+----+------------+--------+---------+
invoices_attributes
+-----------+----+--------------+
| accountid | id | name |
+-----------+----+--------------+
| 1 | 1 | registration |
+-----------+----+--------------+
| 1 | 2 | claimnumber |
+-----------+----+--------------+
| 1 | 3 | jobid |
+-----------+----+--------------+
invoices_attributes_values
+------------------+-------------+-----------+---------------+
| attributevalueid | attributeid | invoiceid | value |
+------------------+-------------+-----------+---------------+
| 1 | 1 | 2 | ABC 126L |
+------------------+-------------+-----------+---------------+
| 2 | 2 | 2 | ABZ123 |
+------------------+-------------+-----------+---------------+
| 3 | 3 | 2 | MARY DOE |
+------------------+-------------+-----------+---------------+
Through the help of Eugen Rieck's original answer I was able to make the following query
SELECT
invoices.accountid,
invoices.id AS invoiceid,
invoices.customerid,
invoices.total,
registration.value AS registration,
claimnumber.value AS claimnumber,
jobid.value as jobid
FROM
invoices
LEFT JOIN invoice_attributes ON invoices.accountid=invoice_attributes.accountid
LEFT JOIN invoice_attribute_values AS registration ON registration.attributeid = invoice_attributes.id AND invoices.id = registration.invoiceid AND invoice_attributes.name = 'registration'
LEFT JOIN invoice_attribute_values AS claimnumber ON claimnumber.attributeid = invoice_attributes.id AND invoices.id = claimnumber.invoiceid AND invoice_attributes.name = 'claimnumber'
LEFT JOIN invoice_attribute_values AS jobid ON jobid.attributeid = invoice_attributes.id AND invoices.id = jobid.invoiceid AND invoice_attributes.name = 'jobid'
Which gave the following result
+-----------+-----------+------------+--------+--------------+-------------+----------+
| accountid | invoiceid | customerid | total | registration | claimnumber | jobid |
+-----------+-----------+------------+--------+--------------+-------------+----------+
| 1 | 2 | 167909 | 120060 | NULL | NULL | MARY DOE |
+-----------+-----------+------------+--------+--------------+-------------+----------+
| 1 | 2 | 167909 | 120060 | NULL | ABZ123 | NULL |
+-----------+-----------+------------+--------+--------------+-------------+----------+
| 1 | 2 | 167909 | 120060 | ABC 126L | NULL | NULL |
+-----------+-----------+------------+--------+--------------+-------------+----------+
When I GROUP BY invoices.id some of the columns (registration, claimnumner or job) will become NULL. I desire the result to be as:
+-----------+-----------+------------+--------+--------------+-------------+----------+
| accountid | invoiceid | customerid | total | registration | claimnumber | jobid |
+-----------+-----------+------------+--------+--------------+-------------+----------+
| 1 | 2 | 167909 | 120060 | ABC 126L | ABZ123 | MARY DOE |
+-----------+-----------+------------+--------+--------------+-------------+----------+
How can the query be modified to get the result above?
SQL has no provision to make the columns dependant on the data. You could however create a query with ALL possible attributes along the lines of
Basically you want to renormalize an EVA structure - this is of course possible:
SELECT
invoices.accountid,
invoices.id AS invoiceid
invoices.customerid,
invoices.total,
jobids.value AS jobid -- one of these lines per attriubute
FROM
invoices
LEFT JOIN invoices_attributes ON invoices.accountid=invoices_attributes.accountid
-- One of the following joins per attribute
LEFT JOIN invoices_attributes_values AS jobids
ON jobids.attr_id=invoices_attributes.attr_id
AND jobids.accountid=invoices.accountid
AND jobids.invoiceid=invoices.id
AND invoices_attributes.attr_name='jobid'

Calculate sum for group records in MySQL

I have this table of orders
| ORDER_ID | PRODUCT | CUSTOMER | QTY | DATE
---------------------------------------------
| 1 | shoes | Nick | 1 | 01/01/2016
| 2 | shirts | Nick | 5 | 02/02/2016
| 3 | shoes | Paul | 10 | 03/03/2016
| 4 | shirts | Paul | 20 | 04/04/2016
So, How can I achieve this report result with ONE Select Statement?
| Date_of_Order | Customer | Quantity | PRODUCT_TOTAL_SALES |
-----------------------------------------------------------------
| 01/01/2016 | Nick | 1 | shoes : 11 |
| 02/02/2016 | Nick | 10 | shirts : 25 |
| 03/03/2016 | Paul | 5 | shoes : 11 |
| 04/04/2016 | Paul | 20 | shirts : 25 |
I know how to use concat(column1, ' ', column2) to create a combined column but I haven't succeed to add a sum for a grouped item there. When I try with left join I get the sum for a product ...BUT its always the whole sum and its not related to the dates of the order so when I try to filter the results on my query for a certain period I still get 11 for shoes and 25 for shirts...
You can group by multiple columns and get the sum for the smallest group.
If you want the daily sales, then instead of GROUP BY product use GROUP BY product, date
SELECT
o.`date` AS Date_of_Order,
SUM(o.qty) as Total_Quantity,
CONCAT(o.product, ':', SUM(o.qty))
FROM
orders o
GROUP BY product, `date`
ORDER BY `date`
Simple additional SELECT from same table can do that for entire period:
SELECT
o.`date` AS Date_of_Order,
o.Customer,
o.qty as Quantity,
(SELECT
CONCAT(oo.product, ':', SUM(oo.qty))
FROM
orders oo
WHERE
oo.product = o.product
) PRODUCT_TOTAL_SALES
FROM
orders o
Output:
+---------------+----------+----------+---------------------+
| Date_of_Order | Customer | Quantity | PRODUCT_TOTAL_SALES |
+---------------+----------+----------+---------------------+
| 01/01/2016 | Nick | 1 | shoes:11 |
| 02/02/2016 | Nick | 5 | shirts:25 |
| 03/03/2016 | Paul | 10 | shoes:11 |
| 04/04/2016 | Paul | 20 | shirts:25 |
+---------------+----------+----------+---------------------+
4 rows in set
If you want to filter by certain period, you must include it in both:
SELECT
o.`date` AS Date_of_Order,
o.Customer,
o.qty as Quantity,
(SELECT
CONCAT(oo.product, ':', sum(oo.qty))
FROM
orders oo
WHERE
oo.product = o.product
AND STR_TO_DATE(oo.`date`,'%d/%m/%Y') BETWEEN '2016-01-01' AND '2016-03-03'
) PRODUCT_TOTAL_SALES
FROM
orders o
WHERE
STR_TO_DATE(o.`date`,'%d/%m/%Y') BETWEEN '2016-01-01' AND '2016-03-03'
Output:
+---------------+----------+----------+---------------------+
| Date_of_Order | customer | Quantity | PRODUCT_TOTAL_SALES |
+---------------+----------+----------+---------------------+
| 01/01/2016 | Nick | 1 | shoes:11 |
| 02/02/2016 | Nick | 5 | shirts:5 |
| 03/03/2016 | Paul | 10 | shoes:11 |
+---------------+----------+----------+---------------------+
3 rows in set

Get available stocks to sell

What I have until now: http://sqlfiddle.com/#!2/bbfec/6
I want to get the quanity of shares for a given stock, that a given company has available to sell - grouped by price. For example, for company number 9 and stock number 1, I want the data like this:
| id | name | price | date | quantity | total |
------------------------------------------------------------------
| 3 | ALTR | 2.240 | 2015-05-12 04:29:29 | 50 | 112.00 |
| 7 | ALTR | 2.449 | 2014-06-10 18:21:02 | 50 | 122.45 |
Because company 9 bought 200 stocks on 2015-05-12 04:29:29, sold 100 on 2014-06-10 15:50:17, more 50 on 2014-06-10 17:06:18 and bought 50 on 2014-06-10 18:21:02.
I don't want the total of all shares, because they have different prices when a company acquires them. The price and the date are the purchasing price and date but the quantity is what is left from a certain purchasing.
Thanks in advance.
Strawberry, the desired result:
| id | price | date | quantity |
-----------------------------------------------
| 3 | 2.240 | 12-05-2015 | 50 |
| 7 | 2.449 | 10-06-2014 | 50 |
Start with:
select id_acao, id_empresa, ifnull(bought,0) - ifnull(sold,0) as stock
from
(
select id_acao, id_empresa,
(select sum(quantidade) from acao_trans where tipo='C' and id_acao=a.id_acao and id_empresa=a.id_empresa) as bought,
(select sum(quantidade) from acao_trans where tipo='V' and id_acao=a.id_acao and id_empresa=a.id_empresa) as sold
from acao_trans a group by id_acao,id_empresa
) x
;
+---------+------------+-------+
| id_acao | id_empresa | stock |
+---------+------------+-------+
| 1 | 4 | 1500 |
| 1 | 9 | 100 |
| 8 | 9 | 3500 |
| 13 | 9 | 5000 |
+---------+------------+-------+
And join this query to your base acao and empresa tables.
Remark: For statistics etc. it would be easier to use negative quantities for selling transactions instead of a transaction type 'C' and 'V'.
For ease of (my) comprehension, I translated and adjusted your stock table slightly...
SELECT a.stock_id
, a.company_id
, a.transaction_date
, a.price
, COALESCE(a.quantity - SUM(b.quantity),a.quantity) quantity
, COALESCE(a.quantity - SUM(b.quantity),a.quantity) * a.price subtotal
FROM stock_company a
LEFT
JOIN
( SELECT x.stock_id
, x.company_id
, MAX(x.transaction_date) min_transaction_date
, y.quantity
FROM stock_company x
JOIN stock_company y
ON y.stock_id = x.stock_id
AND y.company_id = x.company_id
AND y.transaction_date <= x.transaction_date
AND y.transaction_type <> x.transaction_type
WHERE y.transaction_type = 'SELL'
GROUP
BY x.stock_id
, x.company_id
, y.quantity
) b
ON b.stock_id = a.stock_id
AND b.company_id = a.company_id
AND b.min_transaction_date = a.transaction_date
WHERE a.stock_id = 1
AND a.company_id = 9
AND a.transaction_type = 'BUY'
GROUP
BY stock_id
, company_id
, transaction_date;
+----------+------------+---------------------+-------+----------+----------+
| stock_id | company_id | transaction_date | price | quantity | subtotal |
+----------+------------+---------------------+-------+----------+----------+
| 1 | 9 | 2014-06-10 18:21:02 | 2.449 | 50 | 122.450 |
| 1 | 9 | 2015-05-12 04:29:29 | 2.240 | 50 | 112.000 |
+----------+------------+---------------------+-------+----------+----------+
http://www.sqlfiddle.com/#!2/cfa4d/1
Note that this hasn't been tested extensively so there may be a flaw (or perhaps several flaws!) in my logic, but it seems to work well enough on the data set provided.
EDIT: I made a slight adjustment - still not sure if it's enough. Let me know.
Maybe now I've understood it. How about this:
select c.id,c.id_empresa,c.id_acao,c.data as c_data,c.quantidade as c_quantidade,v.preco,v.id as v_id,v.data as v_data,ifnull(v.quantidade,0) as v_quantidade, c.preco*v.quantidade as bought, v.preco*v.quantidade as sold
from acao_trans c
left join acao_trans v
on c.id=v.parent
order by id_empresa, id_acao,c_data,v_data
which results in
+----+------------+---------+---------------------+--------------+-------+------+---------------------+--------------+----------+----------+
| id | id_empresa | id_acao | c_data | c_quantidade | preco | v_id | v_data | v_quantidade | bought | sold |
+----+------------+---------+---------------------+--------------+-------+------+---------------------+--------------+----------+----------+
| 4 | 4 | 1 | 2014-06-10 08:59:09 | 2000 | 2.385 | 8 | 2014-06-11 10:39:48 | 500 | 1184.000 | 1192.500 |
| 8 | 4 | 1 | 2014-06-11 10:39:48 | 500 | NULL | NULL | NULL | 0 | NULL | NULL |
| 5 | 9 | 1 | 2014-06-10 15:50:17 | 100 | NULL | NULL | NULL | 0 | NULL | NULL |
| 6 | 9 | 1 | 2014-06-10 17:06:18 | 50 | NULL | NULL | NULL | 0 | NULL | NULL |
| 7 | 9 | 1 | 2014-06-10 18:21:02 | 50 | NULL | NULL | NULL | 0 | NULL | NULL |
| 3 | 9 | 1 | 2015-05-12 04:29:29 | 200 | 2.430 | 5 | 2014-06-10 15:50:17 | 100 | 224.000 | 243.000 |
| 3 | 9 | 1 | 2015-05-12 04:29:29 | 200 | 2.449 | 6 | 2014-06-10 17:06:18 | 50 | 112.000 | 122.450 |
| 2 | 9 | 8 | 2015-05-12 04:27:56 | 3500 | NULL | NULL | NULL | 0 | NULL | NULL |
| 1 | 9 | 13 | 2015-05-12 04:25:52 | 5000 | NULL | NULL | NULL | 0 | NULL | NULL |
+----+------------+---------+---------------------+--------------+-------+------+---------------------+--------------+----------+----------+
Can you go on from there?
Ok, now I think I got it.
Here's the query resulting what I wanted:
SELECT p.id
, a.nome
, p.preco
, date_format(p.`data`,'%m/%d/%Y') AS `data`
, COALESCE(p.quantidade-SUM(f.quantidade), p.quantidade) AS quantidade
, p.preco*COALESCE(p.quantidade-SUM(f.quantidade), p.quantidade) AS total
FROM acao_trans p
LEFT JOIN acao_trans f
ON p.id=f.parent
INNER JOIN acao a
ON p.id_acao=a.id
WHERE p.parent IS NULL
AND p.id_acao=1
AND p.id_empresa=9
GROUP BY p.id
Fiddle: http://sqlfiddle.com/#!2/bbfec/64.
What I did: I joined the table that matters (acao_trans "p") with itself ("f") and I used Sum function to aggregate all the quantities of second argument, giving me the total of all sold shares. If there are records on "f" I want to subtract that total of the quantity of purchasing shares ("p"). If there is not a correspondence it will show null fields and I show purchased quantity. After it's done it's kind of simple. Quantity was what really mattered here, with that I was able to reach other things easily.