I'm Trying to Get the date , Discount , Total , Net Total ... ordered by date , the discount is showing the real amount but when I select multiple dates the total will be summed in those dates I've selected and it will be ordered by date
DECLARE #pR FLOAT = (SELECT SUM(CAST(Price AS FLOAT)) AS Price
FROM Orders WHERE isPaid = 1
AND PaidDate BETWEEN '8/17/2015' AND '8/18/2015' ) ;
SELECT Orders.PaidDate
, #pR AS Total
,sum(theorderids.Discount) As Discount
,(#pR - sum(theorderids.Discount)) AS [Net Total]
From
(SELECT OrderId, PaidDate
FROM Orders
WHERE Orders.PaidDate BETWEEN '8/17/2015' AND'8/18/2015'
GROUP BY Orders.OrderId, Orders.PaidDate) AS Orders
INNER JOIN theorderids ON Orders.OrderId = theorderids.ID
GROUP BY Orders.PaidDate ;
Example Data :
Row 1
"PaidDate": "17-08-2015",
"Total": 7388.0,
"Discount": 38.0,
"NetTotal": 7363.0
Row 2
"PaidDate": "18-08-2015",
"Total": 7388.0,
"Discount": 2.0,
"NetTotal": 7363.0
This will work.
SELECT TheOrderids.PaidDate, MAX(Price) AS Total
,sum(theorderids.Discount) As Discount
,(MAX(Price) - sum(theorderids.Discount)) AS [Net Total]
From
(SELECT PaidDate ,SUM(Price ) AS Price
FROM Orders
WHERE Orders.PaidDate BETWEEN '8/17/2015' AND'8/19/2015'
GROUP BY Orders.PaidDate
) AS Orders
INNER JOIN theorderids ON Orders.PaidDate = theorderids.PaidDate
GROUP BY theorderids.PaidDate
ORDER BY theorderids.PaidDate ;
Try this way
SELECT Orders.PaidDate
, #pR AS Total
,sum(theorderids.Discount) As Discount
,(#pR - sum(theorderids.Discount)) AS [Net Total]
From
(SELECT ROW_NUMBER() OVER(ORDER BY Orders.PaidDate ) AS Row, OrderId, PaidDate
FROM Orders
WHERE Orders.PaidDate BETWEEN '8/17/2015' AND'8/18/2015'
GROUP BY Orders.OrderId, Orders.PaidDate) AS Orders
INNER JOIN theorderids ON Orders.OrderId = theorderids.ID
GROUP BY Orders.PaidDate ;
Related
I'm new in SQL. Need some help to improve my query to ovoid duplicate code.
SELECT customers.name, orders.price
FROM customers
JOIN orders ON orders.id = customers.order_id
WHERE customers.order_id IN (
SELECT orders.id
FROM orders
WHERE orders.price = (
SELECT orders.price
FROM orders
WHERE orders.order_date BETWEEN
(SELECT MIN(orders.order_date) FROM orders)
AND
(SELECT DATE_ADD(MIN(orders.order_date), INTERVAL 10 year)FROM orders)
ORDER BY orders.price DESC LIMIT 1
)
AND orders.order_date BETWEEN
(SELECT MIN(orders.order_date) FROM orders)
AND
(SELECT DATE_ADD(MIN(orders.order_date), INTERVAL 10 year)FROM orders)
)
I would like ovoid duplicate code here
SELECT MIN(orders.order_date) FROM orders
and
SELECT DATE_ADD(MIN(orders.order_date), INTERVAL 10 year)FROM orders
You can use WITH to get first 10 years orders. By defitinion there exists no orders with the date < min(date), so you needn't between, just <= .
firstOrders as (
SELECT *
FROM orders
WHERE order_date <=
(SELECT DATE_ADD(MIN(o.order_date), INTERVAL 10 year)
FROM orders o)
)
SELECT customers.name, orders.price
FROM customers
JOIN FirsrOrders orders ON orders.id = customers.order_id
AND orders.price = (
select price
from firstOrders
order py price desc
limit 1
)
You want orders from the first ten years where the price was equal to the maximum price among those orders. So rank by price and grab those holding the #1 spot.
with data as (
select *,
date_add(min(order_date) over (), interval 10 year) as max_date,
rank() over (order by price desc) as price_rank
from orders
)
select *
from data
where order_date <= max_date and price_rank = 1;
I am trying to get the following result in SQL server:
From the purchase order rows, last purchase quantity + date from all item codes in the order rows table and from the warehouse table amount in stock for the item codes I get from the rows table.
Order rows:
ORDER_DATE ITEM_CODE QTY
2019-03-01 A 5
2019-03-02 A 3
2019-03-05 A 4
2019-03-03 B 3
2019-03-04 B 10
Warehouse:
ITEM_CODE INSTOCK STOCKPLACE
A 10 VV
A 3 LP
A 8 XV
B 5 VV
B 15 LP
Wanted result (Latest order date, latest order qty and total in stock):
ORDER_DATE ITEM_CODE QTY INSTOCK
2019-03-05 A 4 21
2019-03-04 B 10 20
I have tried some queries but only failed. I have a steep learning curve ahead of me :) Thanks in advance for all the help!
Here is one method:
select o.*, wh.*
from (select wh.item_code, sum(wh.instock) as instock
from warehouse wh
group by wh.item_code
) wh outer apply
(select top (1) o.*
from orders o
where o.item_code = wh.item_code
order by o.order_date desc
) o;
You can use row_number() with apply :
select t.*, wh.instock
from (select o.*, row_number () over (partition by item_code order by o.order_date desc) as seq
from Order o
) t cross apply
( select sum(wh.instock) as instock
from warehouse wh
where wh.item_code = t.item_code
) wh
where t.seq = 1;
Your Orders aren't identified with a unique ID, and therefore if multiple Orders were to coincide on the same date, you have no way of telling which is the most recent order on that day.
Anyway, assuming that the database you posted is correct and an Order date + Item Code combines to form a unique key, you could use grouping and some CTE to get the desired output as follows.
;WITH MostRecentOrders (ITEM_CODE, ORDER_DATE)
AS (
SELECT
O.ITEM_CODE
, MAX(O.ORDER_DATE) AS ORDER_DATE
FROM
#Order O
GROUP BY ITEM_CODE
)
SELECT
O.ORDER_DATE
, O.ITEM_CODE
, O.QTY
, SUM(WH.INSTOCK) AS INSTOCK
FROM
#Warehouse WH
INNER JOIN #Order O ON O.ITEM_CODE = WH.ITEM_CODE
INNER JOIN MostRecentOrders MRO ON MRO.ITEM_CODE = O.ITEM_CODE
AND MRO.ORDER_DATE = O.ORDER_DATE
GROUP BY
O.ORDER_DATE
, O.ITEM_CODE
, O.QTY
ORDER BY O.ITEM_CODE
This is my table:
Using this query, I am getting most sold items:
SELECT [Purchased Item], SUM([Overall Quantity purchased] )
FROM ReportDraft
GROUP BY [Purchased Item]
ORDER BY SUM([Overall Quantity purchased] )
This returns items and total quantity purchased by customer.
how I can create a table like this
ItemName | Total quantity purchased | Customer who purchased most | Customer quantity bought
Pie--------|---------6------------|---------------Tonya----------|--------4------------|
Big Burger-|---------3------------|---------------Tonya----------|--------3------------| and etc
Thank you
Why do you post a new question instead of nodifying/extending the previous one?
WITH cte AS
(
SELECT [Purchased Item],
-- quantity per item
SUM(SUM([Overall Quantity purchased]))
OVER (PARTITION BY [Purchased Item]) AS "Total quantity purchased",
[Customer name],
-- quantity per item/customer
SUM([Overall Quantity purchased]) AS customer_qantity,
-- rank the customer quantity per item
ROW_NUMBER()
OVER (PARTITION BY [Purchased Item]
ORDER BY SUM([Overall Quantity purchased]) DESC) AS rn
FROM ReportDraft
GROUP BY [Purchased Item], [Customer name]
)
SELECT *
FROM cte
WHERE rn = 1
ORDER BY "Total quantity purchased" DESC
i have sql query.
this query is providing me a lots of record of customer and that voucher and order subtotal but i want sum of debit and credit row and credit and debit column sum at bottom position of table.
so guide me how can i achieve this table using by query.
this is my sql query.
declare #Credit decimal(18,2)
declare #Debit decimal(18,2)
select #debit= SUM(sd.SubTotal)
from SalesOrderDetails sd
left join SalesOrder so
on sd.SalesOrderId = so.SalesOrderId
select #credit = SUM(Amount) from Vouchers
SELECT CustomerId,
OrderDate,
VoucherType,
VoucherNo,
SUM(Debit) as Debit,
SUM(Credit) as Credit
FROM (SELECT so.CustomerId,
so.OrderDate ,
'Sales' As VoucherType,
'' AS VoucherNo,
a.Total As Debit
, NULL As Credit
from SalesOrder so
inner join (
SELECT SalesOrderId,
sum(SubTotal) AS Total
FROM SalesOrderDetails
group by SalesOrderId
)as a
on so.SalesOrderId = a.SalesOrderId
UNION ALL
SELECT V.CustomerId
,v.VoucherDate As OrderDate
, 'Receipt' AS VoucherType
,v.VoucherNumber AS VoucherNo
,NULL AS Debit
,v.Amount AS Credit
from Vouchers v
) ledger
GROUP BY GROUPING SETS ((CustomerId),(OrderDate, VoucherType, VoucherNo), ())
i have attached my output image.
in my image i got this result but that is very messy process so give me a proper solution for this.
Add a HAVING Clause which happens after the grouping and aggregating?
....
GROUP BY GROUPING SETS ((CustomerId),(OrderDate, VoucherType, VoucherNo), ())
HAVING VoucherType Is Null
I have an orders table that I need to generate a report from that groups by month/year, showing the total orders for the month, and the number actually received.
This gives me total # of orders by month/year:
SELECT YEAR(o.OrderDate) As 'Year'
, MONTH(o.OrderDate) As 'Month'
, COUNT(ID) As 'OrderCount'
FROM Orders o
GROUP BY YEAR(o.OrderDate), MONTH (o.OrderDate)
ORDER BY YEAR(o.OrderDate) DESC, MONTH (o.OrderDate) DESC
This gives me total # of orders RECEIVED by month/year:
SELECT YEAR(o.OrderDate) As 'Year'
, MONTH(o.OrderDate) As 'Month'
, COUNT(o.ID) As 'OrderReceivedCount'
FROM Orders o
INNER JOIN OrderStatus s ON s.ID = o.CurrentStatus
WHERE s.StatusPriority > 4 AND s.IsCancelled = 0
GROUP BY YEAR(o.OrderDate), MONTH (o.OrderDate)
ORDER BY YEAR(o.OrderDate) DESC, MONTH (o.OrderDate) DESC
But how can I combine that into one query? I can't work out how the 'group by' would work?
I think you want something like:
SELECT YEAR(o.OrderDate) As [Year]
, MONTH(o.OrderDate) As [Month]
, COUNT(*) As OrderCount
, SUM(CASE
WHEN s.StatusPriority > 4 AND
s.IsCancelled = 0 THEN 1
ELSE 0 END) as OrderReceivedCount
FROM Orders o
INNER JOIN OrderStatus s ON s.ID = o.CurrentStatus
GROUP BY YEAR(o.OrderDate), MONTH (o.OrderDate)
ORDER BY YEAR(o.OrderDate) DESC, MONTH (o.OrderDate) DESC
That is join all of the orders to their statuses, whatever they may be, and then do a conditional count of those (via SUM/CASE) to work out which ones have been received.