How to negate records - sql

I have an assignment where I'm supposed to figure out how many parts a person bought in a certain month. Each record comes from an invoice with part orders.
The important parts of the table are:
Orders (PartNo, Quantity, Price, discount, EXP_Price, date)
the data would looks like this:
P1 1 30.00 15.00 15.00 11-NOV-06
P2 1 30.00 6.00 24.00 19-NOV-06
P2 -2 30.00 0.00 -30.00 11-NOV-06
P2 2 30.00 0.00 30.00 20-NOV-06
The issue I'm having is with the last two lines. The last two orders cancel each other out, as if someone rang the last part in error and had to cancel it out of the final bill.
How does someone construct a query that account for the fact that the last two rows negate each other?
Thanks in advance
OK I've been trying this and iteraction around it
select PartNo
from Practice
Where PARTNO = (
select PARTNO , SUM(Quantity)
from Practice
group by PARTNO);
The questions just states how many parts did Joe Smith (the name that corresponds to this invoice) buy in the month of November(sorry not mouth) of November. Know this guy he will want us to account for the fact that Joe never actually bought those parts since they were never charged to the buyer.

It looks very simple, you just need to sum all columns multiplied to the sign of quantity.
select PartNo, sum(Quantity) as Quantity, sum(Price*sign(Quantity)) as Price, sum(discount*sign(Quantity)) as discount, sum(EXP_Price*sign(Quantity)) as EXP_Price
from Orders
group by PartNo

Related

How to count and categorize values by the same criteria

I have a table in SQL Server with just 2 columns, the ProductID and the Discount.
There are many products registered in rows.
So, I want to know how many products there are in this table with the same discount, categorized by the discount column value.
How can I query this by the easiest and simplest way?
The view that I expect for:
Discount - QTD
-------------------
0.25 - 150
0.40 - 320
0.75 - 532
It seems like a simple aggregation should do the trick.
Select Discount
,QTD = count(*)
From YourTable
Group By Discount

SQL where function used with Over Partition

I am calculation a range of figures, one of the calculated columns are a weighted average calculation based on "Accountlink" by "DTstamp".
The problem is that I need the final result (per accountlink) to show in a new column - each Accountlink's final result per accountlink - to be used in a new calculation - as per example below:
Accountlink WA_Calc WA_Calc_Final
1 20.00 30.00
1 40.00 30.00
1 30.00 30.00
2 15.00 20.00
2 35.00 20.00
2 28.00 20.00
on the image is an extract of the script I am compiling.
However, I am just getting errors.
Any assistance and/or direction will be much appreciated.
You need a column that specifies the ordering. SQL tables represent unordered sets, so there is no "last row", unless a column specifies that.
Let me assume you have one. I'll just call it ?.
Then you can use first_value():
select t.*,
first_value(wa_calc) over (partition by id order by ? desc) as wa_calc_final
from t;

sql query and sum the two fields

I want to display unit price, quantity but my problem is I want to multiply the unit price * quantity. How to combine this 3 fields? I want the result like this:
UNITPRICE QUANTITY AMOUNT
2 3 6
2X3 equals 6.. please help me how to query this..
thank you newbie in multiplying table
I made this sqlfiddle for you. Perhaps it will help you.
select
UNITPRICE,
QUANTITY,
UNITPRICE * QUANTITY as [Amount]
from data

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

How can I selectively blank out SSAS aggregative levels?

I'm trying to build a cube which will contain a history of product prices by on-line sellers. So, it has one simple "fact" table and three dimension tables. The fact looks like this:
product_id
seller_id,
price_date,
product_price
and the dimensions are product, seller, and date. The product dimensions rolls up into manufacturers (so products can be grouped by manufacturers). The seller dimensions just has the seller name, and the date dimension has the normal complement of date levels.
I'd like to have the cube respond to users by not displaying any data unless the user has drilled down into the sku level, and the individual seller level, although I wouldn't mind having the aggregations be averages on the manufacturer level.
But for the date dimension I would like the cube to display lastnonempty.
When I choose lastnonempty as the aggregation property, the prices get summed along the manufacturer and seller dimensions, which is wrong.
Here is a sample of what I'd like to see:
fact table:
date product manufacturer seller price
1/1/2000 sku1 manu1 seller1 $10.00
1/2/2000 sku1 manu1 seller1 $12.00
cube result
manu1 -
sku1 -
Jan 2000 $12.00
1/1/2000 $10.00
1/2/2000 $12.00
Is this possible?
Thanks, --sw
Be careful actually nulling out subtotals since this makes it very difficult for users to even start a PivotTable. I blogged about this dilemma and a solution here:
http://www.artisconsulting.com/blogs/greggalloway/2012/6/8/na-for-subtotals
So it is possible. Try something like:
scope( [Product].[Product].[All], [Measures].[Price] );
this = IIf(IsEmpty([Measures].[Price]),null,0);
Format_String(this) = ";;"; //format zeros as blank
end scope;
Then repeat that code to blank out the manufacturer and seller subtotals.
You can switch the AggregateFunction on your Price measure to LastNonEmpty. But I tend to prefer LastChild for the reasons mentioned here and here. It does add a little more MDX to use LastChild as I explained in that second article. And you may be ok with LastNonEmpty if every product is snapshotted every day.