I have tables as below -
Employee -
Employee_Id Name Limit_Amt
1 Sachin 3000
2 Mahi 2500
Employee_Wage -
Employee_Id Amount Pay_Date
1 200 2017-01-01
1 250 2017-02-01
1 300 2017-03-01
2 350 2017-01-01
2 400 2017-02-01
2 300 2017-03-01
Now to find out Remaining limit for individual employee below query works fine -
SELECT e.Limit_Amt - SUM(Amount) AS 'Remaining Limit'
FROM Employee e, Employee_Wage ew
WHERE e.Employee_Id = ew.Employee_Id
GROUP BY e.Employee_Id, e.Limit_Amt
It gives output as -
Remaining Limit
2250
1450
But, further I wish to calculate Total of remaining limit (i.e. deriving 3700), then if I apply SUM(e.Limit_Amt - SUM(Amount)) ... it's not working.
Conceptually I am blocked. Could someone please guide me here? Thanks in advance.
You could use a subquery:
SELECT SUM(remaining)
FROM (
SELECT e.Limit_Amt - SUM(Amount) AS remaining
FROM Employee e
JOIN Employee_Wage ew
ON e.Employee_Id = ew.Employee_Id
GROUP BY
e.Employee_Id
, e.Limit_Amt
) sub
The from a join b on a.id = b.id syntax is clearer than SQL92's from a, b where a.id = b.id.
select e.Name,e.Limit_Amt,sum(cast(w.Amount as int)) 'sum',
e.Limit_Amt-sum(cast(w.Amount as int)) 'Remaining'
from Employee e join Employee_Wage w
on e.Employee_Id=w.Employee_Id
group by e.Name,e.Limit_Amt
Related
I need to join 2 dataframes using month and name. However, in one of these dataframes I don't have all the monthly result, so I want to repeat the most recent one.
For example,
Dataframe A
name score month
Alex 20 2020/01
Alex 30 2020/03
Dataframe B
name month tenure
Alex 2020/01 1
Alex 2020/02 2
Alex 2020/03 3
Join A+B using name and month - expected result
name month score tenure
Alex 2020/01 20 1
Alex 2020/02 20 2 --> repeat the score from the most recent date
Alex 2020/03 30 3
Does someone know how can I do that?
You can use a correlated subquery:
select b.*,
(select a.score
from a
where a.name = b.name and a.month <= b.month
order by a.month desc
limit 1
) as score
from b;
Or, you can use window functions and a join:
select b.*, a.score
from b left join
(select a.*,
lead(month) over (partition by name order by month) as next_month
from a
) a
on b.name = a.name and
b.month >= a.month and
(b.month < a.next_month or a.next_month is null);
This method is convenient if you want to fetch multiple columns from a.
I was working on a problem from Leetcode #185
I could understand the solution but I want to know how to write the query that add a column which indicate the count the people have a better salary than the tuple one. I think it is possible in SQL, but i don't know how to make it right, i always get syntax error. :-/
from Employee e1 (Select count(distinct e2.Salary)
from Employee e2
Where e2.Salary > e1.Salary) as c
For exemple I have such a table Employee:
Id - Name - Salary
1 toto 60000
2 tata 50000
3 kiki 90000
4 lily 70000
5 momo 60000
I want to have such a result:
Id - Name - Salary - Head_count_of_higher_salary
1 toto 60000 2
2 tata 50000 4
3 kiki 90000 0
4 lily 70000 1
5 momo 60000 2
Thanks guys
Your subquery is almost correct.
Just remove DISTINCT from COUNT() (although just COUNT(*) would also work) and use it as the new column:
select *,
(
select count(e2.Salary)
from Employee e2
where e2.Salary > e1.Salary
) as Head_count_of_higher_salary
from Employee e1
See the demo.
You can also implement this type of query with a LEFT JOIN on the joined table having a higher salary than the first, and then counting the number of rows in the joined table:
SELECT e1.Id, e1.Name, e1.Salary, COUNT(e2.Id) AS Head_count_of_higher_salary
FROM Employee e1
LEFT JOIN Employee e2 ON e2.Salary > e1.Salary
GROUP BY e1.Id, e1.Name, e1.Salary
Output:
Id Name Salary Head_count_of_higher_salary
1 toto 60000 2
2 tata 50000 4
3 kiki 90000 0
4 lily 70000 1
5 momo 60000 2
Demo on SQLfiddle
If you are running MySQL 8.0: what you ask for is exactly what rank() does.
This would be as simple as:
select e.*, rank() over(order by salary desc) - 1 head_count_of_higher_salary
from employees e
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.
I have 3 tables: product, customer and transaction.
Product:
id_product price
1 1000
2 2000
Customer:
id_customer name
1 Tom
2 Jack
Transaction:
id_transaction id_product id_customer qty date
1 1 1 10 2013-02-21
2 2 1 50 2013-02-21
3 1 2 15 2013-02-21
I want to achieve this result:
id_customer name total_transaction purchase_qty subtotal
1 Tom 2 60 110000
2 Jack 1 15 15000
How I can get that result using a query in MySQL?
SELECT t.id_customer, c.name,
COUNT(t.id_customer) AS total_transaction,
SUM(t.qty) as purchase_qty
FROM transaction t
INNER JOIN customer c
ON t.id_customer = c.id_customer
GROUP BY t.id_customer,c.name
SELECT cs.id_customer, cs.name, COUNT(tr.transaction) AS total_transaction, SUM(tr.qty) as purchase_qty, SUM(tr.qty*pr.prize)
FROM customer AS cs
LEFT JOIN transaction AS tr ON tr.id_customer = cs.id_customer
LEFT JOIN product AS pr ON pr.id_product = tr.id_product
GROUP BY cs.id_customer
I suppose you are a total beginner, so I did this for you. Next time, if you have ANY own ideas, give to us, so we don't have to write the whole query for you. Show some effort.
SELECT c.id_customer, c.name, count(t.id_transaction) AS total_transaction
FORM Customer c INNER JOIN Transaction T
ON C.id_customer = T.id_customer
GROUP BY c.id_customer
You need a group by statement since you want to aggregate results for a given customer :
SELECT id_customer, name, count(id_transaction) AS total_transaction
FORM Customer, Transaction
WHERE Transaction.id_customer = Customer.id_customer
GROUP BY id_customer
This does not solve your whole problem, but you've got the idea.
i have a scenario where there are 2 tables
tblMember with 2 columns MemberID & MemberName
100 Aakash
101 Seema
tblLoanHistory with columns LoanID, MemberID, FineCharged
1 100 30
2 100 60
3 101 30
I want to find the member whose total fine charged is highest.
output should be
100 Aakash 90 (60+30=90)
can anybody please help
thanks in advance.
SELECT TOP 1
M.MemberID, M.MemberName,
SUM(LH.FineCharged)
FROM
tblMember M
JOIN
tblLoanHistory LH ON LH.MemberID=M.MemberID
GROUP BY
M.MemberID, M.MemberName
ORDER BY
SUM(LH.FineCharged) DESC
Update... Oops! Added JOIN condition
SELECT M.MemberID ,M.MemberName,SUM(LH.FineCharged) FineCharged
FROM tblMember M
INNER JOIN tblLoanHistory LH ON LH.MemberID=M.MemberID
GROUP BY M.MemberID, M.MemberName