How to get the last sold price in SQL - sql

I am working on a query that will show me basic item information as well as the total qty shipped (for all orders),last date that the item was sold and the price it was sold at. I cant figure out how to get the last sold price. This is what I have so far,
SELECT
item_id
,item_desc
,sum(qty_shipped) AS 'Total Qty Shipped'
,count(item_id) AS 'No of times Shipped'
,max(invoice_date) AS 'Last Invoice_date'
,unit_price AS 'Last Price'
FROM sales_history_report
WHERE
item_id = '1234'
AND year_for_period >= '2017'
AND sales_location_id like '10'
GROUP BY
item_id
,item_desc
,unit_price
with this query I am getting all of the lines that this item is on. It looks like this right now,
Item_id,Item_desc,Total_QTY_shipped,no_of_times_shipped,Last_Invoice_date,Last_price
1234,Item 1234,4,1,2014-10-15,2.47
1234,Item 1234,6,1,2014-09-20,2.519
But I am looking for
Item_id,Item_desc,Total_QTY_shipped,no_of_times_shipped,Last_Invoice_date,Last_price
1234,Item 1234,10,2,2014-10-15,2.47
Any help would be appreciated.

If I understand correctly, you can use conditional aggregation:
select item_id, item_desc,
sum(qty_shipped) as [Total Qty Shipped],
count(item_id) as [No of times Shipped],
max(invoice_date) as Max_Date,
max(case when seqnum = 1 then unit_price end) as [Last Price],
from (select shr.*,
row_number() over (partition by item_id order by invoice_date desc) as seqnum
from sales_history_report shr
) shr
where item_id = 1234 and
year_for_period >= 2017 and
sales_location_id like '10'
group by item_id, item_desc;
Comments:
Do not use single quotes for column aliases. Only use single quotes for string and date constants.
The columns in the GROUP BY define the rows in the result set. I don't think you want unit_price in it.
Do not use single quotes for numeric constants. I assume item_id an the year are numeric.

Related

PARTITION BY first use of a particular product

I'm trying to produce a table that lists the month, account and product name from our billing database. However, I also want to understand (for subsequent cohort analysis) what the earliest use is of "Product A" for each line item too. I was hoping I could do the following:
SELECT
Month,
AccountID,
ProductName,
SUM(NetRevenue) AS NetRevenue,
MIN(Month) OVER(PARTITION BY AccountID, 'Product A') AS EarliestUse
FROM
<<my-billing-table>>
WHERE
NetRevenue > 0
AND AccountID IN (
SELECT DISTINCT AccountID
FROM <<my-billing-table>>
WHERE ProductName = 'Product A' AND NetRevenue > 0
)
GROUP BY 1,2,3
...but it seems that just using "Product A" within the OVER clause does not have the desired effect (it seems to just return the first month for AccountID).
While the syntax is fine and the query runs, I'm obviously missing something regarding PARTITIONing the OVER clause. Any help much appreciated!
I think you want conditional aggregation along with a window function:
SELECT Month, AccountID, ProductName,
SUM(NetRevenue) AS NetRevenue,
MIN(MIN(CASE WHEN ProductName = 'Product A' THEN month END)) OVER (PARTITION BY AccountID) AS EarliestUse
FROM <<my-billing-table>>
WHERE NetRevenue > 0 AND
AccountID IN (SELECT AccountID
FROM <<my-billing-table>>
WHERE ProductName = 'Product A' AND NetRevenue > 0
)
GROUP BY 1,2,3;
The key expression here is an aggregation function nestled inside a window function. The aggregation function is MIN(CASE WHEN ProductName = 'Product A' THEN month END). This calculates the earliest month for the specified product on each row. This could be a column in the result set, and you would see the minimum value on the product row.
The window function then "spreads" this value over all rows for a given AccountID.
you are using a constant in partition it will not impact in your result, should use the column ProductName in partition to get the earliest use of the product
SELECT
Month,
AccountID,
ProductName,
SUM(NetRevenue) AS NetRevenue,
MIN(Month) OVER(PARTITION BY AccountID, ProductName) AS EarliestUse
FROM
<<my-billing-table>>
WHERE
NetRevenue > 0
AND AccountID IN (
SELECT DISTINCT AccountID
FROM <<my-billing-table>>
WHERE ProductName = 'Product A' AND NetRevenue > 0
)
GROUP BY 1,2,3

How to get the asked columns for each customers

I have this table called table a
I need to get the CustomerID, sum(Income) of 2015, sum(Income) of 2016, did he ever bought productId A (boolean), is the total sum(income)> 1000 (boolean), number of total InvoiceID
all that in one query and the results should be with 1 row per customer.
please help I don't even know how to start!
This is basically conditional aggregation:
select customerid,
sum(case when extract(year from date) = 2015 then sales end) as sales_2015,
sum(case when extract(year from date) = 2016 then sales end) as sales_2016,
max( product = 'A' ) as ever_bought_a,
sum(income) > 1000 as sum_exceeds_1000,
count(*) as num_invoices
from t
group by customerid;
You haven't specified a database, so this is really psuedocode. You'll need to adapt it for your particular database.

Conditioned based Running total

I am rewriting this Post!
This is my Query
SELECT ITEMNUM,
STORELOC,
TRANSDATE,
ACTUALDATE,
CONDITIONCODE,
ISSUETYPE,
QUANTITY,
CURBAL,
'' AS Balnce_In_Condition_02,
ENTERBY,
MEMO,
DOCNUM
FROM MATUSETRANS
WHERE ITEMNUM = 'Z95-04-BHH811550'
AND STORELOC ='04'
AND TRANSDATE >'01-JAN-19'
ORDER BY TRANSDATE DESC;
and it is my result
I need running Balance_In_Condition_02 which I write separately in image. Basically my need is to create a Inventory report of Items which is issue from CONDITIONCODE 01 while the same item was present in CONDITIONCODE 02. Items are frequently ISSUE and RETURN. Hope you can understand my question
At first sum quantity conditionally, only for rows with conditioncode = '02', like in my inner query. Then use analytic lead() to fill values for the rest of rows.
select itemnum, storeloc, transdate, quantity, conditioncode,
nvl(bal02, lead(bal02) ignore nulls
over (partition by itemnum order by transdate desc)) balance02
from (
select itemnum, storeloc, transdate, quantity, conditioncode,
case conditioncode when '02' then sum(quantity)
over (partition by itemnum order by transdate desc) end bal02
from matusetrans mtu
where itemnum = 'Z95-04-B806073A' and storeloc = '04')
order by transdate desc;
dbfiddle
I am not sure if you are summing all quantities or only 02 rows, because in your example these sums are equal. In this second case instead of sum(quantity) use:
sum(case conditioncode when '02' then quantity end) over (...

Working out total from sub total and amount

I have a table with purchased orders data.
Each row contails the amount of certain item purchased, cost per item and the order number group. Each different item purchased is a new row with same order number.
I basically want to return the total cost for that order. I have tried the following but am getting nowhere:
SELECT order_number, SUM( sub_total ) AS `total`
FROM
SELECT order_number, SUM( SUM( amount ) * SUM( cost_per_item ) ) AS `sub_total`
FROM `ecom_orders`
WHERE member_id = '4'
GROUP BY order_number
ORDER BY purchase_date DESC
Pretty much any SQL-92 compliant RDBMS will take this:
SELECT
order_number
,SUM(amount * cost_per_item) AS total
,purchase_date
FROM
ecom_orders
WHERE member_id = '4'
GROUP BY order_number,purchase_date
ORDER BY purchase_date DESC

SQL Selecting multiple sums?

Let's say I have a table:
SELECT SUM(quantity) AS items_sold_since_date,
product_ID
FROM Sales
WHERE order_date >= '01/01/09'
GROUP BY product_ID
This returns a list of products with the quantity sold since a particular date. Is there a way to select not only this sum, but ALSO the sum WITHOUT the where condition? I'd like to see sales since a particular date for each product alongside all (not date limited) sales.
SELECT SUM(CASE WHEN order_date >= '01/01/09' THEN quantity ELSE 0 END) AS items_sold_since_date,
SUM(quantity) AS items_sold_total,
product_ID
FROM Sales
GROUP BY product_ID
something like this?:
SELECT SUM(quantity) AS items_sold_since_date,
total_items_sold = (SELECT SUM(quantity) from Sales GROUP BY product_ID),
product_ID
FROM Sales
WHERE order_date >= '01/01/09'
GROUP BY product_ID
If you like to see total sales alongside, then you would use sum(sale_amt), and in the group by add the sale_amt. I hope it helps.
You could use GROUP BY to split up the Sales based on date.
In Oracle you could say:
select count(*)
,case when order_date >= '01/01/09' then 'after' else 'before' end
from log
group by case when order_date >= '01/01/09' then 'after' else 'before' end;
you can write
SELECT SUM(quantity) AS items_sold_since_date,(SELECT SUM(quantity) AS items_sold_since_date FROM Sales
GROUP BY product_ID) as items_sold,
product_ID
FROM Sales
WHERE order_date >= '01/01/09'
GROUP BY product_ID