I have 3 database tables as follows:
projects:
id
project_name
project_location
status
project_expenses
id
project_id
expense_category
expense_subcategory
amount
userid
date
payment_status
project_status
project_income
id
project_id
project_income
date
userid
project_status
projects.id, project_expenses.project_id and project_income.project_id are related.
I need to create a query displaying Project_ID,Project_Name,SUM of Project_Income,SUM of Project_expenses
I tried the following query but not getting correct result.
SELECT p.id AS id, p.project_name AS project_name, SUM(i.project_income) AS income, SUM(e.amount) AS expenses
FROM project_income i, project_expenses e, projects p
WHERE i.project_id = p.id AND e.project_id = p.id AND p.status = 'Active'
GROUP BY id
I have currently 2 rows in project_income and 4 rows in project_expenses. The result is project_income displays double the values. Something is wrong with my JOIN.
Being a newbie I am unable to understand what i am doing wrong?? Requesting help...
Use sub-selects in the query result columns. No need for GROUP BY.
SELECT p.id
, p.project_name
, ( SELECT SUM(i.project_income)
FROM project_income i
WHERE i.project_id = p.id
) AS income
, ( SELECT SUM(e.amount)
FROM project_expenses e
WHERE e.project_id = p.id
) AS expenses
FROM projects p
WHERE p.status = 'Active'
The problem with the query in the question is best explained with an example.
You say there are 2 rows in project_income and 4 rows in project_expenses. Let say the 2 incomes are 1000 and 1500, and the 4 expenses are 615, 750, 840, and 900.
Since there are no restrictions between them, that means you'll get the cross join, i.e. 8 records:
income expense
1000 615
1000 750
1000 840
1000 900
1500 615
1500 750
1500 840
1500 900
Now, when you sum income you get 4 times the value you want, and when you sum expense you get 2 times the value you want.
Related
My goal is the get a list of current prices and prices at the time of whatever date is given. The price as of today is always product.price. Each time a new price is set, an entry is added to product_audit and revinfo.
If we are looking for what the prices were on 2020-11-31, it would return:
num CurrentPrice OldPrice
--------------------------------------
1001 100 175
1030 110 100
2010 150 130
EDIT FOR CLARIFICATION: My intention is to get what the price was on a specific day. So OldPrice is actually the newest entry in Product_aud/revinfo that is before or on the set date (in this case, 2020-11-31). Looking specifically at code 1001, the price was changed on 2020-08-02, 2020-09-26, and 2020-01-08. If we are looking at 2020-11-31, that means it should grab 2020-09-26 because it is the soonest date before then. This means the price of 1001 on 2020-11-31 was 175.
There are three tables: Product, product_audit, revinfo
Everytime the price is changed, an entry is added to product_audit with the new price and a reference to a new entry in revinfo that has the date/time. Revinfo contains entries for other audit tables mixed in.
product.id = product_audit.id
product_audit.rev = revinfo.id
product
id num price
------------------------
1 1001 100
2 1030 110
3 2010 150
product_audit
id rev price
------------------------
1 1 200
1 3 175
1 6 100
2 2 100
2 7 110
3 4 130
3 5 120
3 8 150
revinfo
id timestamp
-------------------
1 2020-08-02
2 2020-09-25
3 2020-09-26
4 2020-11-12
5 2020-12-20
6 2021-01-08
7 2021-01-09
8 2021-01-23
Of course this just returns the oldest price from product_audit:
SELECT product.num, product.price AS CurrentPrice, product_audit.price AS OldPrice
FROM product
LEFT JOIN product_audit ON product_audit.id = product.id
LEFT JOIN revinfo ON revinfo.id = product_audit.rev
WHERE rev.timestamp <= "2020-11-31"
GROUP BY product.id
I tried nesting joins like this based on some stuff I was reading, but quickly realized it still wasn't going to get the right price:
SELECT product.id, product.num, product.price AS CurrentPrice, revisions.price AS OldPrice
FROM product
LEFT JOIN (SELECT product_audit.id AS id, product_audit.price AS price, MAX(revinfo.timestamp) AS timestamp
FROM product_audit
LEFT JOIN revinfo ON product_audit.rev = revinfo.id
WHERE revinfo.timestamp <= $DATE{Date}
GROUP BY product_aud.id) AS revisions ON revisions.id = product.id
I can't seem to think of how to get to that last step. Some sort of WHERE timestamp = (SELECT...) maybe? But I haven't been able to figure that out.
Also, just a heads up, I'm limited to statements that start with SELECT because of permissions. I can't add functions or anything like that.
I had to assume how we were getting the "old" price, and my assumption was that you wanted the "earliest" revision record, so I used Row_number and a derived table to get that record and then use it in the join constraint for the revision table... not exactly sure what your logic is, but here is a fiddle with the resultset that matches your "desired results"
SELECT product.num, product.price AS CurrentPrice, product_audit.price AS OldPrice
FROM product
LEFT JOIN (select p.price, p.id, p.rev,
ROW_NUMBER() over (partition by p.id order by p.rev asc) as rn
From product_audit p
) AS product_audit ON product_audit.id = product.id
and product_audit.rn = 1
LEFT JOIN revinfo ON revinfo.id = product_audit.rev
WHERE revinfo.timestamp <= '2020-11-31';
https://www.db-fiddle.com/f/fbvrgo2gRLoBPhgwQnuvY9/3
WITH cte AS ( SELECT product.num,
product.price CurrentPrice,
product_audit.price OldPrice,
ROW_NUMBER() OVER (PARTITION BY product.num
ORDER BY revinfo.`timestamp` DESC) rn
FROM product
JOIN product_audit ON product_audit.id = product.id
JOIN revinfo ON revinfo.id = product_audit.rev
WHERE revinfo.`timestamp` <= #date
)
SELECT num, CurrentPrice, OldPrice
FROM cte
WHERE rn = 1;
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=a276ec8ad89e3c2f3aaeee411072fa3e
We ran a promotion where users can receive their first subscription order free. Price = $0.00 when a user uses the promo. I am interested in the data from Example A.
Example A - User 50 started with the promo and continued for two months
order_id user_id price created_at
1 50 0.00 2018-01-15
5 50 20.00 2018-02-15
9 50 20.00 2018-03-15
Example B - User 100 was already an active subscriber who cancelled his account and reactivated with the promo, I do not wish to count him
order_id user_id price created_at
2 100 20.00 2018-01-16
3 100 0.00 2018-01-17
7 100 20.00 2018-02-17
--Here is my query--
This returns all users who have multiple orders
WHERE at least one of their orders has a price = 0.00
-This dataset returns example A and example B
--My question--
Most of this data is correct (Example A) but a handful of them I want to omit because they are skewing my data (Example B). I want to remove Example B users.
I want to remove people who's first order was not the promo.
How can I request that their FIRST order had a price = 0.00? I was thinking something with min(created_at)?
You can get the time of the first order using:
select user_id, min(created_at) as min_ca
from t
group by user_id;
Next, you can get the price of the first order using:
select oi.*
from order_items oi join
(select user_id, min(created_at) as min_ca
from order_items oi
group by user_id
) ooi
on oi.user_id = ooi.user_id and oi.created_at = ooi.min_ca
where oi.price = 0.00;
Then you can get all records, using join, in, or exists;
select oi.*
from order_items oi join
order_items oi1
on oi.user_id = oi1.user_id join
(select user_id, min(created_at) as min_ca
from order_items oi
group by user_id
) u1
on oi1.user_id = u1.user_id and oi1.created_at = u1.min_ca
where oi1.price = 0.00;
You can use EXISTS to check that for the record with zero price there is no earlier created_at:
SELECT COUNT(*), user_id
FROM Promo
WHERE user_id IN (
-- Query below yields [user_id]s of users who got the promo
-- that wasn't a result of a cancellation and re-activation
SELECT user_id
FROM Promo p
WHERE p.price = 0 AND NOT EXISTS (
-- Look for a record with the same user ID and an earlier date
-- than p.created_at, which is the date of the promo with 0.00 price
SELECT *
FROM Promo pp
WHERE pp.user_id=p.user_id AND pp.created_at < p.created_at
)
)
GROUP BY user_id
I have 2 tables, cart and product. User can choose product from product table and then its details are added to cart table. i wanted a listing that combines the values of these two tables and gives a result
prodsize
id catid catname productid prodsize cost prodname
1 2 CN1 13 small 130 P1
2 2 CN1 13 large 200 P1
3 2 CN1 14 small 50 P2
4 2 CN1 14 medium 90 P2
5 2 CN1 14 large 110 P2
6 2 CN1 12 small 70 P3
7 2 CN1 12 medium 110 P3
8 2 CN1 13 medium 200 P1
cart
id catid catname userid productid prodname prodsize prodcost quantity status
1 2 CN1 1 13 P1 small 130 2 add_to_cart
1 2 CN1 1 13 P1 large 200 2 order_placed
when i run the following query combined value from both the table but here i want that only those products quantity should get displayed whose status is add_to_cart, if the status is order_placed then the quantity should be 0, can anyone plz tell how can i modify the below code to get desired result
$sql= "SELECT p.catid, p.catname, p.productid, p.prodimg, GROUP_CONCAT(p.prodsize ORDER BY p.id ASC) as size, GROUP_CONCAT(p.cost ORDER BY p.id ASC) as cost, p.prodname,
GROUP_CONCAT(c.prodsize,'-',c.quantity) as cart_details, GROUP_CONCAT(DISTINCT(c.userid)) as user_id
FROM productsize p
LEFT JOIN cart c ON(c.productid = p.productid AND c.userid = '$userid' AND p.prodsize = c.prodsize)
WHERE p.catid ='$catid'
GROUP BY p.productid
ORDER BY user_id DESC, p.productid ASC";
I'm a little confused about your two conditions ("ordered" items shouldn't get displayed, and should have quantity 0). If they have a quantity 0, they need to be displayed, right? Anyhow, here's my fix considering you may want to do the latter (get "ordered" items in your query result with a quantity of 0)
You need to group by productid AND status
You need a quantity as an aggregate SUM that takes the status into account
Cleaned up some other things too (e.g. quoted indexes, which will harm performance):
$sql = "SELECT p.catid, p.catname, p.productid, p.prodimg,
GROUP_CONCAT(p.prodsize ORDER BY p.id ASC) as size,
GROUP_CONCAT(p.cost ORDER BY p.id ASC) as cost, p.prodname,
GROUP_CONCAT(c.prodsize,'-',c.quantity) as cart_details,
GROUP_CONCAT(DISTINCT(c.userid)) as user_id,
SUM(CASE WHEN c.status = 'add_to_cart' THEN quantity ELSE 0 END) AS quantity
FROM productsize p
LEFT JOIN cart c ON(c.productid = p.productid
AND p.prodsize = c.prodsize)
AND c.userid = {$userid}
WHERE p.catid = {$catid}
GROUP BY p.productid, c.status
ORDER BY p.productid ASC";
You've already got the where clause. Try adding
AND status = 'add_to_cart'
just after the existing where clause.
Change c.quantity to:
case c.status
when 'add_to_cart' then c.quantity
else 0
end
Since you used a left join, the zero result will also apply when that product didn't appear in the user's cart.
And note that the sort on userid may be effectively ordering the cart and non-cart products but that is the wrong way to express that intention. It's also best to aggregate the non-group columns everywhere.
I'm trying to figure out how to grab the average rating for each salesperson over their last 100 ratings if they are currently employed, and if they have an average rating less than 3 (out of 5).
I have the following tables (leaving out information that isn't needed in the query):
users
id name employed
-----------------------
1 John 1
2 Sue 1
3 Bob 0
...
sales
id users_id
------------------
100 3
101 2
102 3
103 1
...
ratings
sales_id rating
-----------------
100 4
101 5
102 5
103 2
...
The current query I have searches everything and returns the average for all orders ever but I want it to only grab the most recent 100 ratings (or less if the salesperson hasn't sold that many items), still excluding anyone that is no longer employed or has a rating for their last 100 orders greater than 3. This is the current query:
SELECT u.name, avg(r.rating) as avg_rating, count(r.rating)
FROM users AS u
JOIN sales AS s ON s.users_id = u.id
JOIN ratings AS r ON r.sales_id = s.id
WHERE u.employed = 1
GROUP BY u.id
HAVING avg_rating <= 3;
Any help would be great! Thanks! :D
You can use my sql variables to keep track of the number of ratings so that you can get only recent 100 ratings , ordering by sales_id so you get recent ratings.
SQL FIDDLE DEMO
SELECT T.name, avg(T.rating) as avg_rating, count(T.rating)
FROM
(
SELECT u.name, r.rating, #num := if (#name = name, #num+1, 1) as rn,
#name:= name as var_name
FROM users AS u
JOIN sales AS s ON s.users_id = u.id
JOIN ratings AS r ON r.sales_id = s.id
AND u.employed = 1
JOIN ( select #name :='' , #num :=1) var
order by sales_id desc
)T
where T.rn <=100
GROUP BY T.name
HAVING avg_rating <= 3
My project is about a jewelery store and i try to find the profit of each product-category.
Let me be more specific
I have 3 tables which gives me the info:
SALES(salesid,productid,quantity,price)
salesid productid Quantity Price
11001 13001 4 5
11002 13002 6 10
11003 13003 5 16
.
.
11012 13012 7 15
RETURN(salesid,productid,date,quantity,price)
salesid productid Quantity Price
11003 13003 1 16
11007 13007 3 12
11008 13008 3 8
PROCUREMENT(procurementid,productid,quantity,price)
procurementid productid Quantity Price
100001 13001 10 2
100002 13002 10 2
.
.
100012 13012 10 2
product_category(categoryid,category)
categoryid category
1 Gold
2 Silver
.
5 Platin
product(Productid,categoryid)
Productid categoryid
13001 1
13002 3
.
.
13010 5
The profit is given from this type:
Profit=Quantity*Price(Sell)-Quantity*Price(Return)-Quantity*Price(Procurement)
And now here is the problem.. I came up to this so far
SELECT categoryid,
category,
(coalesce(a.rev,0)- coalesce(b.ret,0),
coalesce(c.cost,0)) AS profit
FROM product category AS g
JOIN product AS h ON g.categoryid = h.categoryid
JOIN
(SELECT categoryid,
sum(quantity*price) AS rev
FROM sales AS a,
product AS b
WHERE a.productid = b.productid
GROUP BY categoryid) a
LEFT OUTER JOIN
(SELECT cartegoryid,
sum(quantity*price) AS ret
FROM RETURN AS a ,
product AS b
WHERE a.productid = b.productid
GROUP BY categoryid) b ON a.categoryid = b.categoryid
LEFT OUTER JOIN
(SELECT categoryid,
sum(quantity*price) AS cost
FROM procurement AS a,
product AS b
WHERE a.productid = b.productid
GROUP BY categoryid) c ON a.categoryid = c.categoryid ,
product AS d,
procurement AS e
WHERE MONTH(f.date) = MONTH(e.date)
AND YEAR(date) = 2013
[sorry for the align i am new to the site dont know how to copy paste code well(:D)]
wahtever when i do this it comes to a state like
categoryid category profit
1 Gold -100
2 Silver -100
.
5 Platin -100
dont know where is the problem...i made a lot of changes and switches but nothing came up...any suggestion would be so helpfull.Thank u in advane
Initially looks like your profit formula has an extra comma in it.
this
(coalesce(a.rev,0) - coalesce(b.ret,0),coalesce(c.cost,0)) as profit
should be this
coalesce(a.rev,0) - coalesce(b.ret,0) - coalesce(c.cost,0) AS profit
Few more issues with this query
Right before the where clause, after you join the cost subquery, you add product and procurement tables but don't join them. This will cause a cartesian join which will throw off your results.
In the where clause you don't specify which tables date field you want to use. AND YEAR(date) = 2013 should be e.date or f.date. That should have given you an error if you tried to run it.
WHERE MONTH(f.date) = MONTH(e.date) which table is f.date referring to? You didn't give an alias of f to any table.
You join in procurement and use its date field to filter results by month, but none of your revenue, returns, and cost subquery totals take dates into account. This will throw off your results.