I have 3 tables.
SALES
transactionNumber
salesDate
customerNumber
employerNumbet
SALESDETAIL
transactionNumbet
Productcode
Quantity
PRICEHISTORY
effectivitydate
Productcode
Unitprice
I want to compare the salesdate and effectivity date of the product to get its latest unitprice.
Select UnitPrice
From Sales s
Inner join SalesDetail sd on s.transactionNumber = sd.transactionNumber
Inner join PriceHistory ph on ph.ProductCode = sd.ProductCode
Where salesDate =
(
Select Max(salesDate)
From Sales s
Inner join SalesDetail sd on s.transactionNumber = sd.transactionNumber
Inner join PriceHistory ph on ph.ProductCode = sd.ProductCode
)
Related
I want to calculate this table. Everytime that there is a new participant per month it will add the previous value to current value.
month
no_participant
2021-01
10
2021-02
20
2021-03
5
2021-04
17
Something like this, output
month
no_participant
count
2021-01
10
10
2021-02
20
30
2021-03
5
35
2021-04
17
52
Here's my query: I am using Postgres. Thanks to your help
SELECT (TO_CHAR(CSD.SCHEDULED_START_DATETIME, 'YYYY-MM'))AS MONTH,
COUNT(DISTINCT PARTICIPANT_ID) AS PARTICIPANT
FROM TSUP.COURSE_SCHEDULE_DETAIL AS CSD
INNER JOIN TSUP.COURSE_PARTICIPANT AS CP
ON CSD.COURSE_SCHEDULE_ID = CP.COURSE_SCHEDULE_ID
INNER JOIN(
SELECT
MIN(COALESCE(CSD.RESCHEDULED_START_DATETIME, CSD.SCHEDULED_START_DATETIME)) AS SCHEDULED_START_DATETIME,
MAX(COALESCE(CSD.RESCHEDULED_END_DATETIME, CSD.SCHEDULED_END_DATETIME)) AS SCHEDULED_END_DATETIME,
COUNT(CSD.SCHEDULED_START_DATETIME) AS "COUNT"
FROM TSUP.COURSE_SCHEDULE_DETAIL AS CSD
INNER JOIN (
SELECT CP.PARTICIPANT_ID AS "PARTICIPANT",
MIN(COALESCE(CSD.RESCHEDULED_START_DATETIME, CSD.SCHEDULED_START_DATETIME)) AS SCHEDULED_START_DATETIME,
MAX(COALESCE(CSD.RESCHEDULED_END_DATETIME, CSD.SCHEDULED_END_DATETIME)) AS SCHEDULED_END_DATETIME
FROM TSUP.COURSE_PARTICIPANT AS CP
INNER JOIN TSUP.COURSE_SCHEDULE_DETAIL AS CSD
ON CP.COURSE_SCHEDULE_ID = CSD.COURSE_SCHEDULE_ID
INNER JOIN TSUP.COURSE_SCHEDULE AS CS
ON CSD.ID = CS.ID
INNER JOIN TSUP.COURSE AS C
ON CS.COURSE_ID = C.ID
INNER JOIN TSUP.COURSE_CATEGORY AS CC
ON C.COURSE_CATEGORY_ID = CC.ID
INNER JOIN TSUP.EMPLOYEE AS E
ON CP.PARTICIPANT_ID = E.ID
INNER JOIN TSUP.MEMBER_ROLE AS MR
ON E.MEMBER_ROLE_ID = MR.ID
WHERE C.MANDATORY = 'Yes'
AND MR.ROLE_TYPE = 'Dev'
AND CC.CATEGORY = 'JJ'
GROUP BY CP.PARTICIPANT_ID)
TEMP ON CSD.SCHEDULED_START_DATETIME = TEMP.SCHEDULED_START_DATETIME
GROUP BY CSD.RESCHEDULED_START_DATETIME, CSD.SCHEDULED_START_DATETIME,
CSD.RESCHEDULED_END_DATETIME, CSD.SCHEDULED_END_DATETIME
)
TEMP ON CSD.SCHEDULED_START_DATETIME = TEMP.SCHEDULED_START_DATETIME
GROUP BY MONTH
The query you provided is verbose, and also does not seem to exactly line up with the sample data. I will give the following query based on the sample data shown:
SELECT month, no_participant, SUM(no_participant) OVER (ORDER BY month) AS count
FROM yourTable
ORDER BY month;
The above logic uses SUM() as an analytic function.
I have a query like below to find the stock details of certain products.The query is working fine but i think it is not efficient and fast enough(DB: postgresql version 11).
There is a CTE "result_set"in this code where i need to find the "quantity of a product ordered"(qty_last_7d_from_oos_date) during the period between out of stock and last 7 days before out of stock date.Same like this i have to find the revenue also.
So what i did is wrote a same subquery two times one outputting the revenue and other the quantity which is not an efficient step.So someone have any suggestions on how to rewrite this and make it an efficient code.
WITH final as
(
SELECT product_id,product_name,item_sku,out_of_stock_at
,out_of_stock_at - INTERVAL '7 days' as previous_7_days
,back_in_stock_at
FROM oos_base
)
SELECT product_id,product_name,item_sku,out_of_stock_at,previous_7_days
,back_in_stock_at
,(SELECT coalesce(sum(i.qty_ordered), 0) AS qty_last_7d_from_oos_date
FROM ol.orders o
LEFT JOIN ol.items i ON i.order_id = o.order_id
LEFT JOIN ol.products p ON p.product_id = i.product_id AND i.store_id = p.store_id
WHERE o.order_state_2 IN('complete','processing')
AND f.product_id=p.product_id
AND o.created_at_order :: DATE BETWEEN f.previous_7_days::DATE AND COALESCE(f.out_of_stock_at::DATE,current_date)
)
,( SELECT coalesce(sum(i.row_amount_minus_discount_order), 0) AS rev_last_7d_from_oos_date
FROM ol.orders o
LEFT JOIN ol.items i ON i.order_id = o.order_id
LEFT JOIN ol.products p ON p.product_id = i.product_id AND i.store_id = p.store_id
WHERE o.order_state_2 IN('complete','processing')
AND f.product_id=p.product_id
AND o.created_at_order :: DATE BETWEEN f.previous_7_days::DATE AND COALESCE(f.out_of_stock_at::DATE,current_date)
)
FROM final f
In the above code the CTE "final" gives you two dates "out_of_stock_at" &
"previous_7_days". I want to find the quantity and revenue of a product based on this 2 dates means between "previous_7_days" & "out_of_stock_at".
Below query will give the quantity and revenue of the products but the period between "previous_7_days" & "out_of_stock_at"from the above CTE.
As of now i have used the below code two times to obtain the information of revenue and quantity.
SELECT coalesce(sum(i.qty_ordered), 0) AS qty ,
coalesce(sum(i.row_amount_minus_discount_order), 0)
FROM ol.orders o
LEFT JOIN ol.items i ON i.order_id = o.order_id
LEFT JOIN ol.products p ON p.product_id = i.product_id AND i.store_id = p.store_id
WHERE o.order_state_2 IN('complete','processing')
AND f.product_id=p.product_id
AND o.created_at_order :: DATE BETWEEN f.previous_7_days::DATE AND COALESCE(f.out_of_stock_at::DATE,current_date)
Select p.prodCode,
p.description,
p.unit,
SUM(sd.quantity) "Total quantity"
FROM salesDetail sd
RIGHT JOIN product p
ON p.prodCode = sd.prodCode
GROUP BY p.prodCode
ORDER BY 4 DESC
Help! My Script is not running. I need to get the total quantity of every product but my group by is not working.
Compute the sum of the quantity per product in separate subquery, and then join this back to the original product table:
SELECT t1.prodCode,
t1.description,
t1.unit,
t2.total_quantity
FROM product t1
INNER JOIN
(
SELECT p.prodCode, SUM(sd.quantity) total_quantity
FROM product p
LEFT JOIN salesDetail sd
ON p.prodCode = sd.prodCode
GROUP BY p.prodCode
) t2
ON t1.prodCode = t2.prodCode
Note that I replaced the RIGHT JOIN with a LEFT JOIN by switching the order of the joined tables in the subquery.
Update:
If you absolutely need to use a RIGHT JOIN, then just replace the subquery with this:
SELECT p.prodCode, SUM(sd.quantity) total_quantity
FROM salesDetail sd
RIGHT JOIN product p
ON p.prodCode = sd.prodCode
GROUP BY p.prodCode
Using the schema below, write a query which lists the total value of all sales for customers who bought an item from a sales person with the same middle initial as them. Your query should return a single result.
The quantity and price columns are both ints.
The Sales - Employee link is on EmployeeID - SalesPersonID.
Something like this should work:
select sum(s.Quantity * p.Price) as TotalSales
from Sales s
join Products p on s.ProductID = p.ProductID
join Customers c on s.CustomerID = c.CustomerID
join Empleyees e on s.SalesPersonID = e.EmployeeID
where c.MiddleInitial = e.MiddleInitial
I want to ask how to combine these two queries become one subquery?
select c.CustomerName, A.Qty
from Customer c join (select s.CustomerID, pd.Date, s.Qty
from Period pd join Sales s on pd.TimeID = s.TimeID) A on c.CustomerID = A.CustomerID
where #Date = A.Date
and
select sum(case when (pd.Date between '2010-03-15' and #Date) then s.Qty else 0 end) as TotalQty
from Period pd full join Sales s on pd.TimeID = s.TimeID full join Customer c on s.CustomerID = c.CustomerID
group by c.CustomerName, c.CustomerID
They should result one table contains these following columns: CustomerName, Qty, and TotalQty. I've tried many ways but they didn't work at all. Really hope your help, thanks.
Answering your question assuming you are looking at the result as one row and not "column" :
select c.CustomerName, A.Qty,(select sum(case when (pd.Date between '2010-03-15' and #Date) then s.Qty else 0 end)
from Period pd full join Sales s on pd.TimeID = s.TimeID full join Customer c on s.CustomerID = c.CustomerID
group by c.CustomerName, c.CustomerID
) as TotalQty
from Customer c join (select s.CustomerID, pd.Date, s.Qty
from Period pd join Sales s on pd.TimeID = s.TimeID) A on c.CustomerID = A.CustomerID
where #Date = A.Date
If you still want the result as a single column , google "row to column transpose"