.select S.Sid, S.Sname, SS.Amount as "TAMOUNT" from Salesman S Inner Join Sale SS on S.Sid=SS.Sid; [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed yesterday.
Improve this question
select S.Sid, S.Sname, SS.Amount as "TAMOUNT"
from Salesman S Inner Join Sale SS
on S.Sid=SS.Sid;
I tried this code but didn't get required output. I am not sure how to get total sale amount and total discount. Please help me on this.
Display the salesmen id, salesmen name, total sale amount and total discount by salesman who have made sales. The total sale amount and total discount should appear as 0(Zero) for those Salesmen who were not able to make any sale. The sales amount for one sale can be calculated from price and quantity and discount.
Salesman (Sid, Sname, Location)
Product (Prodid, Pdesc, Price, Category, Discount)
Sale (Saleid, Sid, Sldate, Amount)
Saledetail (Saleid, Prodid, Quantity)
I am expecting below result but unable to get it, please help me on this. Thank you.
SID SNAME TAMOUNT TDISCOUNT
5 Kevin 110 10.5
6 Alex 0 0
3 John 0 0
2 Michael 70 9.5
4 Harry 30 4.5
1 Peter 360 57

Related

How to show all departments where in March 2014 there were sales of goods with a price of less than 400 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Please find below Tables.
How to show all departments where in March 2014 there were sales of goods with a price of less than 400?
CREATE TABLE dep
(
id NUMBER (10) NOT NULL,
city VARCHAR2 (100) NOT NULL,
name VARCHAR2 (100) NOT NULL,
CONSTRAINT dep_pk PRIMARY KEY (id)
);
I guess you need below query -
SELECT D.name, SUM(cost)
FROM sales S
JOIN dep D ON S.dep_id = D.id
WHERE EXTRACT(YEAR FROM time) = 2014
AND EXTRACT(MONTH FROM time) = 3
GROUP BY D.name
HAVING SUM(cost) < 400

Remove duplicates based on common where condition in sql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Select * from Table_name i am getting below result.
S.no Emp_id Date Description Amount Splitup amount
2667737 12345 8/12/2019 Icecream 50 30
2667738 12345 8/12/2019 Icecream 50 20
2667739 12346 8/12/2019 Chocolate 50 20
But i need the result
S.no Emp_id Date Description Amount Splitup amount
2667737 12345 8/12/2019 Icecream 50 30
2667738 12345 Icecream 20
2667739 12346 8/12/2019 Chocolate 50 20
i need only the first as amount .For same s.no and same emp_id was to have only one date and amount, remaining rows i want to make it empty.
You can consider using lag() windows analytic function :
select [S.no], [Emp_id],
case when lag([Date],1,null) over
(partition by [Date] order by [S.no]) = [Date] then
null
else
[Date]
end as [Date], [Description],
case when lag([Amount],1,null) over
(partition by [Amount] order by [S.no]) = [Amount] then
null
else
[Amount]
end as [Amount], [Splitup amount]
from tab;
Demo
You can use conditional logic with row_number():
select s.s_no, s.emp_id,
(case when row_number() over (partition by emp_id, description order by s_no) = 1
then date
end) as date,
description,
(case when row_number() over (partition by emp_id, description order by s_no) = 1
then amount
end) as amount,
splitup
from t
order by emp_id, s_no;
Note the outer order by and how it matches the conditions in the window clause. SQL result sets are unordered unless there is an explicit order by. If you want the "first" row to have the values, then you need to be sure that the result set is in the right order.

SQL Group by prevents summing across multiple records

I am trying to sum the amount paid across multiple entries in the Payment table and show who the buyers were for that Order
so if two people made payments on one order it would show their combined payments and show who the people who made the payment were. So if Bob and Sue made 10 dollar payments on a 30 dollar bill it would show
Order Buyer Paid Owes
1 Bob 20 10
1 Sue 20 10
but i'm currently at
Order Paid Owes
1 20 10
How do I add in the buyers? possibly with a join?
Currently I have.
Select Order.Orderid, sum(Payment.amount) as "Paid", Order.Price-sum(Payment.amount) as "Owes"
from order, payment
where payment.orderId = order.orderid
group by (order.orderid, order.price)
payment has a buyerId and orderid as foreign keys since it is an m to n relation.
As you mention in your comment when you join the buyer it does not give you the desired output which is the sum of all payments. To get the buyers and the sum of all payments joining another instance of the payments table and using that to calculate the sum will do the trick.
SELECT
Order.Orderid,
Buyer.Name,
SUM(PaymentSum.amount) AS 'Paid',
Order.Price-SUM(PaymentSum.amount) AS 'Owes',
FROM
Order
JOIN Payment
ON Order.Orderid = Payment.Orderid
JOIN Buyer
ON Payment.BuyerId Buyer.BuyerId
JOIN Payment AS PaymentSum
ON Order.Orderid = PaymentSum.Orderid
GROUP BY
Order.Orderid,
Order.Price,
Buyer.Name

Totalprice column [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is it possible to create a totalprice column in a table which gets its answer from multiplying 2 values from 2 different tables?
e.g
orderquantity
order_id | product_code | quantity
ordertotalprice
order_id | customer_id | totalprice
products
product_Code | product_name | product_desc | productcost
I want the totalprice column to be = productcost*quantity
To query the totals per order:
SELECT orderquantity.order_id,
SUM(products.productcost * orderquantity.quantity) AS total
FROM orderquantity
INNER JOIN products ON products.product_code = orderquantity.product_code
GROUP BY orderquantity.order_id
Wrap this query in an UPDATE statement to update column totalprice in table ordertotalprice:
UPDATE ordertotalprice
SET totalprice = grouped.total
FROM (
SELECT orderquantity.order_id,
SUM(products.productcost * orderquantity.quantity) AS total
FROM orderquantity
INNER JOIN products ON products.product_code = orderquantity.product_code
GROUP BY orderquantity.order_id
) AS grouped
WHERE ordertotalprice.order_id = grouped.order_id
I am assuming here table ordertotalprice already contains the necessary records. If that is not the case, then you will need an INSERT statement instead of an UPDATE. But then I wonder where to get the customer_id from.

MySQL - Finding out Balancing Figure From 2 Tables

I have 2 tables like this:
Stock Table
product_id bigint(20)
qty float
Sales Table
product_id bigint(20)
qty float
Sample Data
Stock Table
product_id---qty
1---10
2---11
3---20
4---50
1---10
3---10
Sales Table
product_id---qty
1---2
2---5
3---20
4---40
1---7
I want the following Output after running the Query
product_id---qty
1---11
2---6
3---10
4---10
Well, as spender ask I am trying to more clear the situation.
First of All, let's think that I store
10 quantity of product 1
11 quantity of product 2
20 quantity of product 3
50 quantity of product 4
10 quantity of product 1 (now I have total 20 of product 1)
10 quantity of product 3 (now I have total 30 of product 3)
Secondly, let's think that I sell
2 quantity of product 1
5 quantity of product 2
20 quantity of product 3
40 quantity of product 4
7 quantity of product 1 (now I have sold total 9 of product 1)
Thirdly, I want to know how much stock is now in my hand
11 quantity of product 1 (20-9 = 11)
6 quantity of product 2 (11-5 = 6)
10 quantity of product 3 (30-20 = 10)
10 quantity of product 4 (50-4 = 10)
My Question is: To find out this stock what is the Query?
Thanks in Advance for answering my question.
This answer works in Oracle - don't have MySql so can't test there
select product_id, sum(qty) from
(
select product_id, qty from stock
union all
select product_id, (-1 * qty) from sales
) as a
group by prod
You question is lacking detail and looks like it might even contain typos in the presented data. I'm going to make the assumption you are trying to calculate the diff between stock quantities and sales quantities, despite your data not actually supporting this (!!!). It looks like you require the following:
select
st.product_id,
sto.qty-st.qty
from
salesTable as st
join stockTable as sto on sto.product_id=st.product_id
Chris's answer is absolutely correct. But for the information I want to add this one which I found on NET.
SELECT tunion.product_id, (
(IFNULL((SELECT SUM(s.qty) FROM stock s WHERE s.product_id=tunion.product_id),0))-
(IFNULL((SELECT SUM(p.qty) FROM sales p WHERE p.product_id=tunion.product_id),0)))
AS quantity
FROM (SELECT DISTINCT s.product_id FROM stock s
UNION ALL SELECT DISTINCT p.product_id FROM sales p)
AS tunion GROUP BY tunion.product_id