Hi I need a help regarding this problem. I want to create a view where it displays the summary of purchased and sold items.
Theses are my tables:
**tblfruit**
ID Name
1 Apple
2 Orange
3 mango
**tblpurchaseditems**
ID fruit_id qty amount
1 1 3 75
2 1 2 50
3 2 1 10
4 3 3 30
**tblsolditems**
ID fruit_id qty amount
1 1 2 150
2 1 2 350
3 2 1 50
4 3 2 230
5 3 1 120
I want the output :
**tblsummary**
ID fruit_id totalqtypurchased totalamountpurchased totalqtysold totalamountsold
1 1 5 125 4 500
2 2 1 10 1 50
3 3 3 30 3 350
So there are a couple of ways of doing this, either online or using a join.
Inline:
Select name, (select sum(qty) from tableb) as totalqty
From tablea
Join:
Select a.name, sum(b.qty) as totalqty
From tablea a
Join tableb b
On b.itemid = a.itemid
Group by a.name
For multiple tables things can get trickier using joins due to duplication so inline queries can work better here, for smaller queries.
select a.fruit_id,sum(b.qty),sum(b.amount),sum(c.qty),sum(c.amount)
from tblfruit a,tblpurchaseditems b,tblsolditems c
where a.fruit_id=b.fruit_id and a.fruit_id=c.fruit_id
group by a.fruit_id
From your sample data and expected result
you can try to UNION ALL to combine tblpurchaseditems and tblpurchaseditems table and make grp to spite two result set. then use condition aggregate function to get your expect result.
CREATE TABLE tblpurchaseditems(
ID INT,
fruit_id INT,
qty INT,
amount INT
);
INSERT INTO tblpurchaseditems VALUES (1,1,3,75);
INSERT INTO tblpurchaseditems VALUES (2,1,2,50);
INSERT INTO tblpurchaseditems VALUES (3,2,1,10);
INSERT INTO tblpurchaseditems VALUES (4,3,3,30);
CREATE TABLE tblsolditems(
ID INT,
fruit_id INT,
qty INT,
amount INT
);
INSERT INTO tblsolditems VALUES (1,1,2,150);
INSERT INTO tblsolditems VALUES (2,1,2,350);
INSERT INTO tblsolditems VALUES (3,2,1,50);
INSERT INTO tblsolditems VALUES (4,3,2,230);
INSERT INTO tblsolditems VALUES (5,3,1,120);
Query 1:
SELECT (#RN:=#RN+1) ID,
fruit_id,
SUM(CASE WHEN grp = 1 THEN qty END) totalqtypurchased ,
SUM(CASE WHEN grp = 1 THEN amount END) totalamountpurchased ,
SUM(CASE WHEN grp = 2 THEN qty END) totalqtysold,
SUM(CASE WHEN grp = 2 THEN amount END) totalamountsold
FROM (
select 1 grp,fruit_id,qty,amount from tblpurchaseditems
UNION ALL
SELECT 2,fruit_id,qty,amount FROM tblpurchaseditems
)t1 CROSS JOIN (SELECT #RN:=0) v
GROUP BY fruit_id
Results:
| ID | fruit_id | totalqtypurchased | totalamountpurchased | totalqtysold | totalamountsold |
|----|----------|-------------------|----------------------|--------------|-----------------|
| 1 | 1 | 5 | 125 | 5 | 125 |
| 2 | 2 | 1 | 10 | 1 | 10 |
| 3 | 3 | 3 | 30 | 3 | 30 |
NOTE
I would use a column itemGroup in table, which can represent which type.
1 mean purchaseditems
2 mean solditems
That would not need to use UNION ALL to combine two table, more make sense.
So in the table schema will look like.
CREATE TABLE tblitems(
ID INT,
fruit_id INT,
qty INT,
amount INT,
itemGroup INT
);
INSERT INTO tblitems VALUES (1,1,3,75,1);
INSERT INTO tblitems VALUES (2,1,2,50,1);
INSERT INTO tblitems VALUES (3,2,1,10,1);
INSERT INTO tblitems VALUES (4,3,3,30,1);
INSERT INTO tblitems VALUES (1,1,2,150,2);
INSERT INTO tblitems VALUES (2,1,2,350,2);
INSERT INTO tblitems VALUES (3,2,1,50 ,2);
INSERT INTO tblitems VALUES (4,3,2,230,2);
INSERT INTO tblitems VALUES (5,3,1,120,2);
Query 1:
SELECT (#RN:=#RN+1) ID,
fruit_id,
SUM(CASE WHEN itemGroup = 1 THEN qty END) totalqtypurchased ,
SUM(CASE WHEN itemGroup = 1 THEN amount END) totalamountpurchased ,
SUM(CASE WHEN itemGroup = 2 THEN qty END) totalqtysold,
SUM(CASE WHEN itemGroup = 2 THEN amount END) totalamountsold
FROM tblitems t1 CROSS JOIN (SELECT #RN:=0) v
GROUP BY fruit_id
Results:
| ID | fruit_id | totalqtypurchased | totalamountpurchased | totalqtysold | totalamountsold |
|----|----------|-------------------|----------------------|--------------|-----------------|
| 1 | 1 | 5 | 125 | 4 | 500 |
| 2 | 2 | 1 | 10 | 1 | 50 |
| 3 | 3 | 3 | 30 | 3 | 350 |
Related
After the last update of Mailster (email marketing plugin for wordpress), they have changed the way they store the information about opens, clicks, unsubscribes...
Until now, everything was stored in two databases:
bao_posts: Like any other wordpress post, the information of the
email that is sent was there. (When the post_type = 'newsletter')
bao_mailster_actions: This is where the user's actions with the
email were stored. 1 when it was sent to a person, 2 when they
opened it, 3 when they clicked on it and 4 when they unsubscribed.
And with this query, I could get a table with all the emails and the information of their openings, clicks, unsubscribed...
SELECT bao_posts.post_modified,
bao_posts.ID,
bao_posts.post_title,
COUNT(CASE WHEN bao_mailster_actions.type = 1 then 1 ELSE NULL END) AS Number_People_Reached,
COUNT(CASE WHEN bao_mailster_actions.type = 2 then 1 ELSE NULL END) AS Opens,
COUNT(CASE WHEN bao_mailster_actions.type = 3 then 1 ELSE NULL END) AS Clicks,
COUNT(CASE WHEN bao_mailster_actions.type = 4 then 1 ELSE NULL END) AS Unsubs
FROM bao_posts
LEFT JOIN bao_mailster_actions ON bao_mailster_actions.campaign_id = bao_posts.ID
WHERE bao_posts.post_type = 'newsletter'
GROUP BY bao_posts.ID ;
*Expected result of this query at the end of the post.
Now the problem is that this setting is kept for emails before the update, but it has changed for new ones and now bao_mailster_actions is separated into:
bao_mailster_action_sent
bao_mailster_action_opens
bao_mailster_action_clicks
bao_mailster_action_unsubscribes
I know how to get the count of each of these tables like this:
SELECT bao_mailster_action_sent.campaign_id,
COUNT(bao_mailster_action_sent.count) AS Number_People_Reached
FROM bao_mailster_action_sent
GROUP BY bao_mailster_action_sent.campaign_id;
To get:
campaign_id
Number_People_Reached
9785
300
9786
305
(And so on with each of these 4 new tables).
So what I would like to do would be to join these 4 new queries to the original one. I've been trying to combine different JOINs, but I don't quite understand how to do it.
*Bearing in mind that if an email ID matches in both, I would need it to add up their clicks, opens (or whatever).
The expected outcome would be something like this (the same as the first query but with the aggregate data):
post_modified
ID
post_title
Number_People_Reached
Opens
Clicks
Unsubs
2021-04-29 13:13:03
9785
Prueba email
300
102
30
1
2021-04-30 15:12:01
9786
Segundo email
305
97
56
0
Thanks in advance!
I suggest that you use UNION ALL to join all the tables in a CTE.You can then use this in your query. I have modified the name because we cannot have to records with the same name.
> create table if not exists bao_mailster_action_sent
( campaign_id int,count int);
create table if not exists bao_mailster_action_opens
( campaign_id int,count int);
create table if not exists bao_mailster_action_clicks
( campaign_id int,count int);
create table if not exists bao_mailster_action_unsubscribes
( campaign_id int,count int);
CREATE TABLE if not exists bao_posts(
post_modified date,
ID int,
post_title varchar(50) );
insert into bao_mailster_action_sent values
(1,88),(2,4),(4,6);
insert into bao_mailster_action_opens values
(2,4),(3,5),(4,10);
insert into bao_mailster_action_clicks values
(1,3),(2,3),(4,6);
insert into bao_mailster_action_unsubscribes values
(1,4),(3,5),(4,5);
INSERT INTO bao_posts values
( '2021-03-01',1,'first post'),
( '2021-06-01',2,'second opion'),
( '2021-09-01',3,'third way'),
( '2021-12-01',4,'last post');
WITH bao_mailster_actionsent AS
( SELECT campaign_id,count, 1 type FROM
bao_mailster_action_sent
UNION ALL
SELECT campaign_id,count,2 FROM
bao_mailster_action_opens
UNION ALL
SELECT campaign_id,count,3 FROM
bao_mailster_action_clicks
UNION ALL
SELECT campaign_id,count,4 FROM
bao_mailster_action_unsubscribes)
SELECT bao_mailster_actionsent.campaign_id,
COUNT(bao_mailster_actionsent.count) AS TotalCount,
SUM(bao_mailster_actionsent.count) AS TotalNumber,
'type'
FROM bao_mailster_actionsent
GROUP BY bao_mailster_actionsent.campaign_id,'type' ;
WITH baoMailsterAction AS
( SELECT campaign_id,count, 1 type FROM
bao_mailster_action_sent
UNION ALL
SELECT campaign_id,count,2 FROM
bao_mailster_action_opens
UNION ALL
SELECT campaign_id,count,3 FROM
bao_mailster_action_clicks
UNION ALL
SELECT campaign_id,count,4 FROM
bao_mailster_action_unsubscribes)
SELECT bao_posts.post_modified,
bao_posts.ID,
bao_posts.post_title,
COUNT(CASE WHEN bao_mailster_actions.type = 1 then 1 ELSE NULL END) AS Number_People_Reached,
COUNT(CASE WHEN bao_mailster_actions.type = 2 then 1 ELSE NULL END) AS Opens,
COUNT(CASE WHEN bao_mailster_actions.type = 3 then 1 ELSE NULL END) AS Clicks,
COUNT(CASE WHEN bao_mailster_actions.type = 4 then 1 ELSE NULL END) AS Unsubs
FROM bao_posts
campaign_id | TotalCount | TotalNumber | type
----------: | ---------: | ----------: | ---:
1 | 1 | 88 | 1
2 | 1 | 4 | 1
4 | 1 | 6 | 1
2 | 1 | 4 | 2
3 | 1 | 5 | 2
4 | 1 | 10 | 2
1 | 1 | 3 | 3
2 | 1 | 3 | 3
4 | 1 | 6 | 3
1 | 1 | 4 | 4
3 | 1 | 5 | 4
4 | 1 | 5 | 4
post_modified | ID | post_title | Number_People_Reached | Opens | Clicks | Unsubs
:------------ | -: | :----------- | --------------------: | ----: | -----: | -----:
2021-03-01 | 1 | first post | 1 | 0 | 1 | 1
2021-06-01 | 2 | second opion | 1 | 1 | 1 | 0
2021-09-01 | 3 | third way | 0 | 1 | 0 | 1
2021-12-01 | 4 | last post | 1 | 1 | 1 | 1
db<>fiddle here
I finally got it to work using only the new tables that Mailster created (it seems that finally they did move all the info to the new tables with the update) and with 4 LEFT JOINS.
I leave the code in case someone else finds it useful:
SELECT P.post_modified,
P.ID,
P.post_title,
IFNULL(S.count,0) as 'Total',
IFNULL(O.count,0) as 'Aperturas',
IFNULL(C.count,0) as 'Clicks',
IFNULL(U.count,0) as 'Bajas' from bao_posts as P
LEFT JOIN (select campaign_id, count(DISTINCT subscriber_id) as count from bao_mailster_action_clicks group by campaign_id) as C ON C.campaign_id = P.ID
LEFT JOIN (select campaign_id, count(DISTINCT subscriber_id) as count from bao_mailster_action_opens group by campaign_id) as O ON O.campaign_id = P.ID
LEFT JOIN (select campaign_id, count(DISTINCT subscriber_id) as count from bao_mailster_action_sent group by campaign_id) as S ON S.campaign_id = P.ID
LEFT JOIN (select campaign_id, count(DISTINCT subscriber_id) as count from bao_mailster_action_unsubs group by campaign_id) as U ON U.campaign_id = P.ID
WHERE P.post_type = 'newsletter'
ORDER BY P.post_modified ASC ;
P.S: As I expected, Mailster's support has not helped at all :'(
How would I select a query like this? I am a bit confused because this would return nothing since any of the rows do not fulfill any of the "where clauses"..
What I would like is the answer to the query to produce the count of 1. And that the productId is "1"... Thanks for help!
SELECT productId where carId=12 and objectiveId=15 and locationId=11
+----+-----------+------------+-------------+------------+
| id | productId | carId | objectiveId | locationId |
+----+-----------+------------+-------------+------------+
| 1 | 1 | 12 | 0 | 0 |
| 2 | 1 | 0 | 15 | 0 |
| 3 | 1 | 0 | 0 | 11 |
| 4 | 2 | 11 | 0 | 0 |
| 5 | 2 | 0 | 10 | 0 |
| 6 | 2 | 0 | 0 | 14 |
+----+-----------+------------+-------------+------------+
One approach here would be to aggregate by product and then assert the presence of the various IDs:
SELECT productId
FROM yourTable
GROUP BY productId
HAVING SUM(carId = 12) > 0 AND SUM(objectiveId = 15) > 0 AND SUM(locationId = 11) > 0;
For an explanation to the above, the HAVING clause evaluates after the GROUP BY aggregation has taken place. That is, whatever logic which you see in the HAVING clause deals with the groups of records for each product (and not the individual product records themselves). The expression SUM(carId = 12) basically counts the number of times for which cardId = 12. You want this condition to be true at least once, for each matching product (as well as the other two conditions).
Use or instead of and with Distinct to select unique productid.
SELECT distinct productId from mytable where carId=12 or objectiveId=15 or locationId=11
Example with link:
create table mytable ( id int , productId int, carId int, objectiveId int, locationId int);
insert into mytable value( 1 ,1 ,12 , 0 , 0 );
insert into mytable value( 2 ,1 , 0 , 15 , 0 );
insert into mytable value( 3 ,1 , 0 , 0 ,11 );
insert into mytable value( 4 ,2 ,11 , 0 , 0 );
insert into mytable value( 5 ,2 , 0 , 10 , 0 );
insert into mytable value( 6 ,2 , 0 , 0 ,14 );
Query:
SELECT distinct productId from mytable where carId=12 or objectiveId=15 or locationId=11
productId
1
db<>fiddle here
I am working on a tool to administer customer and payment data.
I use MySQL and have the following tables: customers and payments:
customers:
ID | invoiceID | supreme_invoiceID
1 123 a123
2 124 a123
3 103 a103
4 110 a110
payments:
ID | supreme_invoiceID | amount | date
1 a123 10 10.10.2010
2 a103 105 10.11.2017
3 a123 5 11.10.2010
And my result should look like this:
view_complete:
ID | supreme_invoideID | number_invoices | GROUP_CONCAT(invoiceID) | SUM(payments.amount) | GROUP_CONCAT(payments.amount)
1 a123 2 123;124 15 10;15
Unfortunately, I cannot get it directly into one table. Instead I create 2 views and query the payments table separately for aggregate data on payments.
First, I create an auxiliary view:
CREATE VIEW precomplete as
SELECT *, COUNT(supreme_invoiceID) as number_invoices FROM customers
GROUP BY supreme_invoiceID;
Then, a second one:
Then I take a second VIEW
CREATE VIEW complete AS
SELECT precomplete.*, SUM(payments.amount)
LEFT JOIN payments p ON precomplete.supreme_invoiceID = p.supreme_invoiceID
GROUP BY precomplete.supreme_invoiceID;
And the concatenated Values I receive in an additional query. But I would like to receive my data all in one query and hopefully, without such view hierarchy. PhpMyAdmin is already pretty slow in loading my views even with few entries.
Any help is greatly appreciated.
Thanks!
The db design forces an approach which builds the aggregates separately to avoid duplicates before joining on a common field for example
drop table if exists c,p;
create table c(ID int, invoiceID int, supreme_invoiceID varchar(4));
insert into c values
(1 , 123 , 'a123'),
(2 , 124 , 'a123'),
(3, 103 , 'a103'),
(4 , 110 , 'a110');
create table p(ID int, supreme_invoiceID varchar(4), amount int, date varchar(10));
insert into p values
(1 , 'a123' , 10 , '10.10.2010'),
(2 , 'a103' , 105 , '10.11.2017'),
(3 , 'a123' , 5 , '11.10.2010');
select c.*,p.*
from
(select min(c.id) minid,count(*) nofinvoices,group_concat(c.invoiceid) gciid, max(supreme_invoiceid) maxsid
from c
group by supreme_invoiceid
) c
join
(select group_concat(supreme_invoiceid) gcsid, sum(amount),group_concat(amount),max(supreme_invoiceid) maxsid
from p
group by supreme_invoiceid
) p
on p.maxsid = c.maxsid
order by minid
;
+-------+-------------+---------+--------+-----------+-------------+----------------------+--------+
| minid | nofinvoices | gciid | maxsid | gcsid | sum(amount) | group_concat(amount) | maxsid |
+-------+-------------+---------+--------+-----------+-------------+----------------------+--------+
| 1 | 2 | 123,124 | a123 | a123,a123 | 15 | 10,5 | a123 |
| 3 | 1 | 103 | a103 | a103 | 105 | 105 | a103 |
+-------+-------------+---------+--------+-----------+-------------+----------------------+--------+
2 rows in set (0.15 sec)
Much like your view approach. Note there doesn't appear to be a customer in the customer table
I have three tables in database:
Table: article
id | code | name | quantity | stock_date
--------------------------------------------------
1 1dfod Article name 10 2016-04-01
Table: selling
id | client_id | selling_type_id | selling_date | selling_status
----------------------------------------------------------------
1 1 1 2016-04-02 1
2 1 1 2016-04-03 1
3 1 1 2016-04-04 1
Table: selling_detail
id | selling_id | article_id | quantity
-------------------------------------
1 1 1 2
2 1 1 3
3 1 1 1
4 2 1 3
5 3 1 1
at the end I would have a stock record for this article like this:
date | in_stock (item in stock) | out_stock (sum of item sold)
----------------------------------------------------------------------
2016-04-01 10 0
2016-04-02 0 6
2016-04-03 0 3
2016-04-04 0 1
All mysql queries to my knowledge do not give me this result.
Here is my code:
SELECT SUM(sd.quantity) out_stock, s.search_date, ifnull(ss.quantity, 0) in_stock
FROM selling_detail sd JOIN selling s ON (sd.selling_id = s.id)
LEFT JOIN shop_stock ss ON (ss.search_date = s.search_date) WHERE (sd.shop_stock_id = 1)
GROUP BY s.search_date;
SELECT date,SUM(in_stock) in_stock,SUM(out_stock) out_stock FROM
(
SELECT stock_date date,quantity in_stock,0 out_stock FROM article
UNION
SELECT selling_date,0,quantity FROM selling JOIN selling_detail ON selling_detail.selling_id = selling.id
) x
GROUP BY date;
As you are trying to combine similar data from two very different tables, you'll probably be staring down the barrel of a UNION ALL.
Something along these lines should get you started:
SELECT *
FROM (
SELECT a.stock_date `date`,
SUM(a.quantity) `in_stock (item in stock)`,
0 `out_stock (sum of item sold)`
FROM article a
WHERE a.id = :article_id
GROUP BY `date`
UNION ALL
SELECT s.selling_date,
0,
SUM(sd.quantity)
FROM selling s
JOIN selling_detail sd
ON sd.selling_id = s.id
AND sd.article_id = :article_id
/* WHERE s.selling_type = ??
AND s.selling_status = ?? /* If necessary */
GROUP BY `date`
) sr
ORDER BY `date`
I will give a small exemple for my problem.
I have two tables in my database.
"Car" with the rows: "id","name".
"Seats" with the rows: "cid","weight".
The weight for the seats referees to the order of the seats.A car can have from 0 to n seats.
My problem is that i have seats with the same weight on a car.
I need to do an update for every car where two or more seats have same weight.
CAR
--------------------------
id | name
--------------------------
1 | ford
--------------------------
SEATS
-------------------------------
cid | name | weight
-------------------------------
1 Seat1 | 7
1 Seat2 | 1
1 Seat3 | 7
1 Seat4 | 3
1 Seat5 | 2
1 Seat6 | 3
1 Seat N | N
-------------------------------
And i need to have:
CID is the CAR id
SEATS
-------------------------------
cid | name | weight
-------------------------------
1 Seat1 | 0
1 Seat2 | 1
1 Seat3 | 2
1 Seat4 | 3
1 Seat5 | 4
1 Seat6 | 5
1 Seat N | N
-------------------------------
The query will run on a very big database and will affect many rows, so it is need to be fast.
I have done the join on the tables, but i don`t know how to make the complete update method.
The INNER JOIN is needed for this.
You can use user-defined variables in order to achieve a serial no for your weight column
update t
join (
select `cid`, `name`,#rank:= #rank + 1 rank
from t join
(select #rank:=-1) t1
order by `cid`, `name`
) t2
using(`cid`, `name`)
set weight = t2.rank
Demo
or if you need to a serial weight no for each car group then you can do so
update t
join (
select `cid`, `name`,
#rank:=case when #group = cid
then #rank + 1
else 0 end rank,
#group:=cid
from t join
(select #rank:=-1,#group:= 0) t1
order by `cid`, `name`
) t2
using(`cid`, `name`)
set weight = t2.rank
Demo serial per group