Computation with different unit of measurement - sql

I have a table like this:-
Selling Sales
Item SIZE QTY Price U/M
---- ---- ---- ----- -----
AAA 5 10 15.00 PCE
BBB 60 5 5.50 CM
CCC 8 7 12.50 PCE
DDD 75 3 6.80 CM
I need to compute the above into this using SQL:-
Item Sales Value
AAA 150.00
BBB 1,650.00
CCC 87.50
DDD 1,530.00
The problem is the Sales U/M, some in PCE (per piece) & some in CM (per centimeter).
When it's in PCE, it shd be "Selling Price * Qty" & when it's in CM, it shd be "Selling Price * Size * Qty".
How can I do this ??
Thanks.

select item, amount = case
when salesum = 'PCE'
then price * qty
else price * size * qty
end
from ...

Use a CASE:
select item,
case um
when 'PCE' then qty * price
when 'CM' then size * qty * price
end
from your_table
where um in ('PCE', 'CM');
The where clause might be over doing it a bit but you didn't specify that um could only have those two values and I'm not sure what you want to do for an "else" clause in the case.

I would do something like this.
Select Item, Case when UM='CM' then Price * Qty else Price * Qty * Size end as Value
from sellingSales

SELECT item, qty * price AS Sales_Value
FROM your_table
WHERE um = 'PCE'
UNION ALL
SELECT item, SIZE * qty * price
FROM your_table
WHERE um = 'CM';

Related

SQL Insert two rows in other table based on If confition

I have 2 table which is DATA and MAIN. DATA table is the raw data extract from excel and MAIN table is the data after validation(few rules) have been made.
Rule:
If Amoun1<>'' and Amount2<>''.
Insert 2 row in MAIN table. The first row will have the value for Amount and Percentage from Amount2 & Percentage2 with TaxRateType = Taxable.
And the second row will have Amount and Percentage get from Amount1 & Percentage1 with TaxRateType = EXEMPT. The invoiceNo also will be add with '_1'
If Amount2<>'' and Amount1=''
Insert 1 row with Amount and Percentage from Amount2 & Percentage2 with TaxRateType = Taxable.
Else
Insert 1 row with Amount and Percentage get from Amount1 & Percentage1 with TaxRateType = EXEMPT
The example is as below table:
**DATA**
InvoiceNo | TotalAmount | Percentage1 | Amount1 | Percentage2 | Amount2
abc123 100 5 45 20 55
abc124 60 5 60 20
abc125 50 5 22 50
**MAIN**
InvoiceNo | Percentage | Amount | TaxRateType | ReferenceValue
abc123 20 55 TAXABLE 2
abc123_1 5 45 EXEMPT 1
abc124 5 60 EXEMPT 1
abc125 22 50 TAXABLE 2
I'm stuck in here for 4 hours searching for which method to use. Currently I have an idea to use if exists but still it's not correct and somehow I feel its not a good method.
IF EXISTS (SELECT ID from [alcatel].[Main_Temp] where Amount0<>'' and Amount21<>'')
BEGIN
INSERT INTO [alcatel].[Main]
( [Country],[InvoiceNo],[Amount],[Percentage],[TaxRateType],[Reference Value])
SELECT
[Country],[InvoiceNo],[Amount2],[Percentage2],'TAXABLE' as [TaxRateType],2 as [Reference Value]
FROM [alcatel].[Data];
INSERT INTO [alcatel].[Main]
( [Country],[InvoiceNo],[Amount],[Percentage],[TaxRateType],[Reference Value])
SELECT
[Country],[InvoiceNo]+'_1' as InvoiceNo,[Amount1],[Percentage1],'EXEMPT' as [TaxRateType],'1' as [Reference Value]
FROM [alcatel].[Data];
END
Followed with other condition.
I think you just want to unpivot the data with some logic:
select invoiceno + v.suffix, v.percentage, v.amount,
v.taxratetype, v.referencevalue
from data d cross apply
(values (1, d.Percentage1, d.Amount1, 'EXEMPT', ''),
(2, d.Percentage2, d.Amount2, 'TAXABLE', (case when d.amount1 is not null then '_1' else '' end))
) v(ReferenceValue, Percentage, Amount, TaxRateType, Suffix)
where amount is not null;
Here is a db<>fiddle.

How to sum and subtract values using update with group by Sql query?

MyTable:
item_name Qty item_area
Belts 2 India
Shoes 20 US
T-Shirt 10 India
T-Shirt 12 US
T-Shirt 25 US
I get this by select group by query
SELECT item_name, Sum(item_qty) as Qty, item_area
FROM dbo.item_stocks
group by item_area, item_name
and my output is below:
item_name Qty item_area
Belts 2 India
Shoes 20 US
T-Shirt 10 India
T-Shirt 37 US
Now i need to subtract and update .How can i do this ?
For eg. i want to subtract 5 T-Shirt ,it will update in MyTable?
T-shirt=37-5=32
how can i update in MyTable?
You can try to use ROW_NUMBER window function to choose any one row to subtract.
;WITH CTE AS (
SELECT *,ROW_NUMBER() over(PARTITION BY item_name,item_area order by Qty desc)rn
FROM item_stocks
)
update CTE
set Qty = Qty - 5
where rn = 1 and item_name = 'T-Shirt' and item_area = 'US'
SELECT item_name, Sum(Qty) as Qty, item_area
FROM item_stocks
group by item_area, item_name
just add a column which denotes in/Out, say INOUT
don't update row, just add new row for the stock sale.
Here,
INOUT = 1 denotes IN and 2 denotes OUT
table structure like
item_name Qty item_area INOUT
Belts 2 India 1
Shoes 20 US 1
T-Shirt 10 India 1
T-Shirt 12 US 1
T-Shirt 25 US 1
T-Shirt 5 US 2
now your query looks like that
SELECT item_name,
Sum(case when INOUT = 1 then item_qty else (0-item_qty) end) as Qty,
item_area
FROM dbo.item_stocks
group by item_area, item_name

Divide in SQL with a where clause

I need to divide where Salesman is Yes and Product is Pen over the total amount of sales (in my case is 8) and get the total percentage.
select * from mytable
Sale_Number Date Salesman_Used Product
1 01/01/2014 No Pen
2 01/02/2014 No Pen
3 01/03/2014 Yes Pen
4 01/04/2014 Yes Pencil
5 01/05/2014 Yes Pen
6 01/06/2014 Yes Pencil
7 01/07/2014 No Pencil
8 01/08/2014 No Pencil
I am stuck on the missing piece:
select concat(100 * count(missing this piece) / count(Sale_Number), '%')
as "Salesman Sales for Pens"
from mytable
where Salesman_Used = 'Yes'
and Product = 'Pen'
The problem with my attempt is that the where clause affects the Sale_Number as well.. How can I do this? Thanks in advance.
There is filter clause for an aggregates:
If FILTER is specified, then only the input rows for which the filter_clause evaluates to true are fed to the aggregate function; other rows are discarded.
So:
select concat(100 * count(*) filter (where Salesman_Used = 'Yes' and Product = 'Pen') / count(Sale_Number), '%')
as "Salesman Sales for Pens"
from mytable
You could use a subquery to get the overall count of sales entries:
select concat(100 * count(*) / (select count(*) from mytable), '%')
as "Salesman Sales for Pens"
from mytable
where Salesman_Used = 'Yes'
and Product = 'Pen'
Try this:
select concat(100 * count(missing this piece) / count(select Sale_Number from mytable), '%')
as "Salesman Sales for Pens"
from mytable
where Salesman_Used = 'Yes'
and Product = 'Pen'
I have never used Postgresql but maybe this works:
select concat(100 * (select count(*) from mytable) / count(Sale_Number), '%')
as "Salesman Sales for Pens"
from mytable
where Salesman_Used = 'Yes'
and Product = 'Pen'

SQL to deduct total sales from total purchase based on date

I am trying to calculate total purchase units left after sales unit in time based. Below is my sample table and required output underneath.
fno pno vdate pdate units ttype
1234 1 1-Jan-2011 1-Jan-2011 100 p
12 1 1-Jan-2012 1-Jan-2011 100 p
1234 1 1-Jan-2012 1-Jan-2011 100 p
1234 1 1-Jan-2012 1-Jan-2011 100 p
12 1 1-Jan-2013 1-Jan-2011 100 s
1234 1 1-Jan-2013 1-Jan-2011 150 s
Output should be
fno pno vdate pdate units ttype
1234 1 1-Jan-2012 1-Jan-2011 50 p
1234 1 1-Jan-2012 1-Jan-2011 100 p
Here ttype is transaction type 's' means sales and 'p' means purchase. I would like to have SQL query which can calculate units based on order on which purchase was made.
Like if look at 1234 fno in 1-jan-2011 100 units were purchased and second purchase was on 1-jan-2012 of 100 units each. Same kind of transaction happend with fno = 12 and units are 100 (purchase) and 100 units have been sold on 1-jan-2013.
Here is SQL
select (t3.pUnits - t2.sUnits) as sb, t1.fno, t1.ttype, t1.vdate from
dbtrans as t1
left join (
select sum(units) as sUnits, fno, pno
from dbtrans
group by fno, pno, ttype having ttype = 's'
) as t2 on t1.fno = t2.fno
left join (
select sum(units) as pUnits, fno, pno
from dbtrans
group by fno, pno, ttype having ttype = 'p'
) as t3 on t1.fno = t3.fno
group by t1.vdate
So calculation should be made on vdate basis for transaction type.
If still I am missing anything please let me know.
Thanks in advance.

How to calculate

I have a table like this:
Item Qty Price A Price B
abc 5 36.00 0
qwe 8 0 48.00
zxc 6 12.00 0
poi 4 10.00 0
lkj 9 12.00 0
mnb 3 0 14.00
vfr 7 0 6.00
How can I sum the value using SQL ie. if Price A is zero, it will pick Price B. The expected results will be as follows :-
Item Value
abc 180.00
qwe 384.00
zxc 72.00
poi 40.00
lkj 36.00
mnb 42.00
vfr 42.00
SELECT
ITEM
, (Qty * [Price A]) + (Qty * [Price B]) AS Value
FROM
TableName
Use the following if A and B could be non-zero, and you only want to use price A when this occurs:
SELECT Item, CASE WHEN `Price A` != 0 THEN `Price A` * Qty
ELSE `Price B` * Qty
END AS Value
FROM table;
Why not do something like
SELECT SUM(Price A) FROM table; SELECT SUM(Price B) FROM table WHERE Price A = 0;
And then add the results together...
from the example above you could really just select priceA + priceB, since one or the other is always zero, otherwise... I am not quite sure this can be done in pure SQL...
Assuming both can be not null:
SELECT item, QTY * IF(PriceA!=0, PriceA, PriceB) AS VALUE from tableName;