Sum of 2 level discount - sql

I am using SQL Server 2008 R2. I need to calculate sum 2 of levels discount. 1st one is item level, like 10% discount on any item. After offering this discount there is amount to receive is for example Rs.510. But shop keeper will get 500 and give him another discount Rs.10.
Now the second level discount is in main table and item wise discount is in child table. How I can calculate the sum of discount daily or monthly etc. and how I will get the following output. As when I am using inner join the discount in main table will be repeat for N number of items. Sum of discount on item wise is not an issue, but the invoice level is. Please help.
--------------------------------------------------------------
Invoice No. Date Total Discount Invoice Amount
--------------------------------------------------------------
INV-00001 01/01/2020 12 500
INV-00002 01/01/2020 10000
INV-00003 01/01/2020 30 900
--------------------------------------------------------------

Related

Populate SQL Server table column with percentage

I have created a table that has a column for prices and I have another column called percentage. I want to be able to auto populate the percentage column by dividing a row's price by the total price*100. Is there any way that this is possible?
Example:
Price Percentage
-------------------
8.00 80%
2.00 20%
to get these percentage values:
(price in row 1 / sum of prices) * 100
(price in row 2 / sum of prices) * 100

SQL - how to show SKUs with the lowest price from a query

I am making a query for a price comparison site and I have to provide them a csv file with certain data. I have done the query and the first problem was that the prices that showed up in it were the full prices from sql database. So when we have for example a discount applied to the whole category of products the price shown is still the full price not the discounted one. I solved that by checking if an SKU is in a discounted category and the query calculates the price accordingly.
The new problem is now that a query checks through categories and if one sku is in multiple categories it creates a row with same SKU for each category and I end up with multiple rows with same SKU and only one has correct price (discounted one).
Example query result. NOTE that the Price column is a calculated result not the data from the table.
SKU
Product
Price
A
Ball
19
A
Ball
19
A
Ball
15
B
Cube
10
B
Cube
8
How do I filter it to just this
SKU
Product
Price
A
Ball
15
B
Cube
8
You need GROUP BY with aggregate function MIN()
SELECT SKU
, Product
, MIN(Price) as Price
FROM <table>
GROUP
BY SKU
, Product

Setting up GST invoice application report differs for item wise sales against total sales

We have set up GST application where I am getting difference between reports like monthly item wise sales report against monthly total sales report.
Please suggest me any changes I need to make for following rules:
We have 3 tables
A. Order master - single line for every order is stored with summarized values
B. Order product details - Each line item stored with respective qty, price etc.
C. Order tax details - line item wise tax details having separate row for CGST, SGST etc.
Here is how I am storing the data:
Tax is applicable on every line item [To generate final invoice all items in order group, we create some of all line items and storing value in Order Master.]
We are rounding 2 decimal value for 3 precision [eg. 4.657 = 4.66, 4.643 = 4.64] as per GST council
Rounding value is stored in Order Master table
For discount there are 2 cases
Percent based discount - Let's say if discount is 10% then from every individual line item 10% discount gets deducted than tax will be applicable for each line item
Flat discount - Let's say for 1000/- Rs order somebody wants to give 80/- Rs. discount than from each line item 8% is deducted and than tax will be applied, off course customer may not pay exact 920/- Rs. amount due to reversal of tax amount.
Now when I am generating following reports:
Monthly item wise sales report [Product wise sales report]
Monthly total sales report [Total sales]
Monthly payment type wise report [Bank, Cheque, Cash]
I am getting difference due to rounding of values. Can anyone suggest me which is the best way to set up rounding formula and up to which decimal point should I go to round the values.
I am using SQL server as backend and .net as front end technology.

Checking quantities for future dates and returning shorts

I have two tables Inventory and Orders in SQL Server.
Say Inventory reflects the current and future inventory for all orders placed.
Inventory
Item Quantity
Shoes 100
Coats 200
Socks -300
In this case, the company is short 300 socks based on the table.
Orders reflects both items we buy from manufacturers and items we sell.
Item Quantity Ship_ArrivalDate Buy_Sell
Shoes 300 10/27/2016 Sell
Socks 500 10/27/2016 Buy
Socks 500 10/29/2016 Sell
Shoes 800 10/30/2016 Buy
So if today is 10/24/2016, I actually have 0 shoes today. I have 200 socks on 10/27/2016 and then be short 300 socks on 10/29/2016 because Inventory reflects all future transactions. And after the 300 sell and 800 buy of shoes, I have 100 in inventory. But that means I will be short on 10/27/2016 for shoes.
I want to build a table that reflects all future dates and checks for days that I am going to be short.
So the table would end up looking like:
Output
Date Item Quantity_Short
10/27/2016 Shoes -400
10/29/2016 Socks -300
I am confused on how to check for each date and really don't know where to start. I understand how to check for 1 date in isolation, but how do you essentially "loop" through all future dates and save the dates and quantities into an output table?
Maybe writing a function that takes in a date and does the math then use apply?
Any starting advice is appreciated. Thanks!
You can try something like this
select date, item,
sum(case when date > getdate() and buy_sell = 'sell' then -Quantity
else Quantity
End) as SumQuantity
from YourTable
group by date, item

replenishment formula oracle sql query

I have a table with the following structure:
Item Code
item Description
Minimum Quantity
Maximum Quantity
Reorder Quantity
Current Stock in Location
Current Stock in Main Location
Replenishment Quantity
I would like to calculate the replenishment quantity, what would be the correct formula in oracle SQL?
Example:
Item Code - ABCD
item Description - ABCD whole item
Minimum Quantity - 20
Maximum Quantity - 100
Reorder Quantity - 20
Current Stock in Location - 15
Current Stock in Main Location - 5930
from the above, I have to calculate replenishment quantity based on the data that the replenishment quantity will be in increments of reorder quantity but should not exceed maximum quantity and only to be replenished if current stock in location is below minimum quantity and if stock is available in main location.
From the above example, I have to get replenishment quantity as 80.
Thanks in advance.
That's not really an Oracle-Question, but I guess you mean something like that, (insert your table and column-names):
select trunc((ma-cu)/re)*re
from(
select 20 mi, 100 ma, 20 re, 15 cu
from dual)
Edit:
Maybe that is also important(I am really wild guessing here):
select least(trunc((ma-cu)/re)*re, trunc(cu_m/re)*re)
from(
select 20 mi, 100 ma, 20 re, 15 cu, 5930 cu_m
from dual)