Add all Quantity Value with the same ID - sql

I want to get the sum of Qty Column with the same ProductID.
Qty
ProductID
1
1001
1
1002
1
1002
1
1001
1
1001
1
1001
So far this is my query:
SELECT
Sales.Qty,
Sales.ProductID,
Products.ProductDesc,
Sales.Status FROM
dbo.Sales
INNER JOIN dbo.Products ON Sales.ProductID = Products.ProductID

Assuming that ProductID uniquely identifies a product, you could group by the ProductID and ProductName:
SELECT SUM(Sales.Qty),
Sales.ProductID,
Products.ProductDesc,
FROM dbo.Sales
INNER JOIN dbo.Products ON Sales.ProductID = Products.ProductID
GROUP BY Sales.ProductID, Products.ProductDesc

Related

sql join on join

transactions table (1 row --> 1 transaction)
customer_code amount
A0BEFG 100
DEC21A 80
payment table (1 row --> 1 transaction)
customer_id payment_type
1 cash
2 credit_card
customer table (1 row --> 1 customer_code)
customer_code customer_id
A0BEFG 2
DEC21A 1
Expected output:
combined table
customer_code customer_id amount payment_type
AOBEFG 2 100 credit_card
DEC21A 1 80 cash
In words, my idea is to get the payment_type into the transactions table, but because there is no matching variable, I need to first merge payment table and customer table, before connecting them to the transactions table.
Code I've tried:
with
connection as (
select c.customer_code, c.customer_id, p. payment_type
from data.payment p
left join data.customer c on p.customer_id = c.customer_id
),
transactions as (
select t.merchant_code, t.amount
from data.transactions t
)
select
t.merchant_code, c.customer_id, c.amount, p.payment_type
from transactions as t
Code is for PostgreSQL.
SELECT c.customer_code, c.customer_id, t.amount, p.payment_type
FROM customer AS c
INNER JOIN payment AS p ON p.customer_id = c.customer_id
INNER JOIN transactions AS t ON t.customer_code = c.customer_code
Join customer to the other tables like this:
SELECT c.customer_code, c.customer_id, t.amount, p.payment_type
FROM transactions t
INNER JOIN customer c ON t.customer_code = c.customer_code
INNER JOIN payment p ON p.customer_id = c.customer_id
See the demo.
Results:
customer_code
customer_id
amount
payment_type
A0BEFG
2
100
credit_card
DEC21A
1
80
cash

How to show the count of all items in cross joined table in SQL Server

I have a table that has all Items in the inventory, table called CI
CI has 2 columns (ProdID and Price), and it looks like this
ProdID Price
-------------
A8373 700
G8745 900
J7363 300
K7222 800
Y6311 350
I have another table for documents called Docs with columns DocID, CustID and InvoiceID.
DocID, CustID, InvoiceID
------------------------
1 1001 751
2 1001 752
3 1001 753
4 1002 831
5 1002 832
6 1003 901
7 1003 902
Another table for purchases called Purchase with DocID, ProdID, ProdSize.
In the same invoice, ProdID can be repeated as it can be in different sizes
DocID, ProdID, ProdSize
------------------------
1 A8373 41
1 A8373 42
1 A8373 43
1 G8745 35
1 G8745 36
2 A8373 44
2 A8373 45
Now I want to get the quantity of of products for all customer and invoice, but for highest priced products
So it should be like this
CustID, InvoiceID, ProdID, Quantity
-----------------------------------
1001 751 A8373 3
1001 751 G8745 2
1001 751 K7222 0
1001 752 A8373 2
1001 752 G8745 0
1001 752 K7222 0
and to show 0 for the products that do not exist in that invoice
I wrote this query, but it is extremely slow. I wonder if there is an easier fast way to get this results
DECLARE #Features AS TABLE
(
CustID varchar(100),
InvoiceID varchar(100)
INDEX IX3 CLUSTERED(CustID, InvoiceID),
ProdID varchar(100),
Quantity bigint
)
INSERT INTO #Features (CustID, InvoiceID, ProdID, Quantity)
SELECT
R.CustID, R.InvoiceID, T.ProdID, COUNT(*) AS Quantity
FROM
Docs R
CROSS JOIN
(SELECT TOP 1000 * FROM CIs ORDER BY Price DESC) C
INNER JOIN
Purchase T ON T.DocID = R.DocID
GROUP BY
R.CustID, R.InvoiceID, T.ProdID
SELECT TOP 100 *
FROM #Features
ORDER BY CustID, InvoiceID, ProdID
SELECT COUNT(*) FROM #Features
UPDATE F
SET Quantity = Cnt
FROM #Features F
INNER JOIN
(SELECT R.CustID, R.InvoiceID, COUNT(*) Cnt
FROM Purchase T
INNER JOIN Docs R ON T.DocID = R.DocID
GROUP BY R.CustID, R.InvoiceID ) X ON F.CustID = X.CustID
AND F.InvoiceID = X.InvoiceID
SELECT * FROM #Features
here is a way to do this. I filter out the 1000 products first and then perform the join as follows..
Also there isn't a need for update query, all could be obtained in the SQL itself.
Filter early join late
with top_product
as (select prodid,price, rownumber() over(order by price desc) as rnk
from ci
)
,invoice_product
as(select d.docid,d.custid,d.invoiceid,p.prodid
from top_product
join docs d
on 1=1
and rnk<=1000
)
select a.CustID, a.InvoiceID, a.ProdID,count(b.prodid) as qty
from invoice_product a
left join purchase b
on a.DocID=b.docid
and a.ProdID=b.prodid
group by a.CustID, a.InvoiceID, a.ProdID
You can use the DENSE_RANK as follows:
select CustID, InvoiceID, ProdID, sum(qty) as qty
from (select d.CustID, d.InvoiceID, ci.ProdID, p.prodid as qty,
dense_rank() over (order by ci.price desc) as rn
from ci cross join docs d
left join purchase p on d.docid = p.docid and ci.prodid = p.prodid) t
where rn <= 1000
group by CustID, InvoiceID, ProdID
Can you please try following SQL Select statement where I used Common Table Expression SQL CTEs
with topproducts as (
select top 3 ProdID from CI order by Price desc
), sales as (
select
CustID,
InvoiceID,
ProdId,
count(ProdId) as cnt
from (
select
d.CustID,
d.InvoiceID,
p.ProdId
from Docs d
inner join Purchase p
on p.DocID = d.DocID
where p.ProdId in (select ProdId from topproducts)
) t1
group by
CustID,
InvoiceID,
ProdId
)
select
t.*, isnull(ss.cnt,0) as Qty
from (
select
distinct s.CustID, s.InvoiceID, p.ProdId
from sales s, topproducts p
) t
left join sales ss on ss.InvoiceID = t.InvoiceID and ss.ProdId = t.ProdId

T-Sql select and group by MIN()

I Have 3 tables like:
ProductCategory [1 - m] Product [1-m] ProductPrice
a simple script like this :
select pc.CategoryId ,pp.LanguageId , pp.ProductId ,pp.Price
from ProductCategory as pc
inner join Product as p on pc.ProductId = p.Id
inner join ProductPrice as pp on p.Id = pp.ProductId
order by CategoryId , LanguageId , ProductId
shows these tabular data :
CategoryId LanguageId ProductId Price
----------- ----------- ----------- ---------------------------------------
1 1 1 55.00
1 1 2 55.00
1 2 1 66.00
1 2 2 42.00
2 1 3 76.00
2 1 4 32.00
2 2 3 89.00
2 2 4 65.00
4 1 4 32.00
4 1 5 77.00
4 2 4 65.00
4 2 5 85.00
now what I need is:
for each category, get full row as is but only with the product that has the minimum price.
I just wrote a simple query that does this like :
with dbData as
(
select pc.CategoryId ,pp.LanguageId , pp.ProductId ,pp.Price
from ProductCategory as pc
inner join Product as p on pc.ProductId = p.Id
inner join ProductPrice as pp on p.Id = pp.ProductId
)
select distinct db1.*
from dbData as db1
inner join dbData as db2 on db1.CategoryId = db2.CategoryId
where db1.LanguageId = db2.LanguageId
and db1.Price = (select Min(Price)
from dbData
where CategoryId = db2.CategoryId
and LanguageId = db2.LanguageId)
and its result is correct:
CategoryId LanguageId ProductId Price
----------- ----------- ----------- ---------------------------------------
1 1 1 55.00
1 1 2 55.00
1 2 2 42.00
2 1 4 32.00
2 2 4 65.00
4 1 4 32.00
4 2 4 65.00
Is there a cooler way for doing this ?
Note: The query must be compliant with Sql-Server 2008 R2+
You could use windowed function like RANK():
WITH cte AS
(
select pc.CategoryId, pp.LanguageId, pp.ProductId, pp.Price,
rnk = RANK() OVER(PARTITION BY pc.CategoryId ,pp.LanguageId ORDER BY pp.Price)
from ProductCategory as pc
join Product as p on pc.ProductId = p.Id
join ProductPrice as pp on p.Id = pp.ProductId
)
SELECT CategoryId, LanguageId, ProductId, Price
FROM cte
WHERE rnk = 1;
LiveDemo
you can add languageid to partition if you need product prices per categoryid and languageid
select top 1 with ties pc.CategoryId ,pp.LanguageId , pp.ProductId ,pp.Price
from ProductCategory as pc
inner join Product as p on pc.ProductId = p.Id
inner join ProductPrice as pp on p.Id = pp.ProductId
order by row_number() over (partition by pc.categoryid order by price)
You are not using the Product table in your query, so it doesn't seem necessary. I would right this as:
select ppc.*
from (select pc.CategoryId, pp.LanguageId , pp.ProductId, pp.Price,
row_number() over (partition by pc.CategoryId order by pp.Price) as seqnum
from ProductCategory pc inner join
ProductPrice pp
on pc.ProductId = pp.ProductId
) ppc
where seqnum = 1
order by CategoryId, LanguageId, ProductId;

Get top n occurences based on related table value

I have a table Orders (Id, OrderDate, CreatorId) and a table OrderLines (Id, OrderId, OwnerIdentity, ProductId, Amount)
Scenario is as follows: Someone opens up an Order and other users can then place their product orders on that order. Those users are the OwnerId of OrderLines.
I need to retrieve the top 3 latest orders that a user has placed an order on and display all of his orders placed, to give him an insight in his personal recent orders.
So my end result would be something like
OrderId | ProductId | Amount
----------------------------
1 | 1 | 2
1 | 7 | 1
1 | 2 | 5
4 | 4 | 3
4 | 1 | 2
8 | 4 | 1
8 | 9 | 2
Select o.Id as OrderId, ol.ProductId, ol.Amount from Orders o
inner join OrderLines ol
on o.Id = ol.OrderId where o.Id in
(Select top 3 OrderId from Orders where OwnerId = #OwnerId)
Order By o.OrderDate desc
You can add date time column to OrderLines table to query latest personal orders and then update the code by moving "order by OrderDate desc" section to sub select query.
select * from
(
select OrderId, ProductId, Amount
row_number() over (partition by OrderID order by Orders.OrderDate) as rn
from OrderLines
join Orders
on OrderLines.OrderId = Orders.Id
where OwnerIdentity = x
) lskdfj
where rn <= 3
Try the below query:
SELECT OL.OrderId, OL.ProductID, OL.Amount
FROM OrderLines OL WHERE OL.OrderId IN
(
SELECT TOP 3 O.OrderID FROM orders O LEFT JOIN OrderLines OL2
ON OL2.orderId=O.OrderID
WHERE OL2.OwnerIdentity =...
ORDER BY O.OrderDate DESC
) AND WHERE OL.OwnerIdentity =...
;WITH cte AS (
SELECT ol.OrderId, ol.ProductId, ol.Amount,
ROW_NUMBER()OVER (PARTITION BY ol.OrderId ORDER BY o.OrderDate DESC) rn
FROM OrderLines ol
JOIN Orders o ON ol.OrderId = o.Id
WHERE OwnerIdentity = #OwnerId
)
SELECT OrderId, ProductId, Amount
FROM cte
WHERE rn <= 3

Group By / Having correlated subquery

I am trying to solve this query, I'm really lost
least one order in which the quantity they ordered was greater than
the average quantity of all other orders.
My tables are:
Customer
Cust_ID | CustName | Region | Phone
Orders
Ordernum | Cust_ID | Item_ID | Quantity
Vendor
Vendor_ID | Item_ID | Costs | Region
stock
Item_ID | Description | Price | On_hand
Database schema is:
Customer (Cust_ID, CustName, Region, Phone)
Orders (Ordernum, Cust_ID, Item_ID, Quantity) Foreign key Cust_ID
references Customer Not Null, On Delete Restrict
Foreign key Item_ID references Stock Not Null, On Delete Restrict
Stock (Item_ID, Description, Price, On_hand)
Vendor (Vendor_ID, Item_ID, Cost, Region)
Foreign key Item_ID references Stock Not null, On Delete Restrict
Here is what I have tried so far. What am I doing wrong?
select custname, phone, count(distinct(item_id)), sum(quantity)
from customer c, orders o
Where c.cust_id= o.cust_id and count (o.ordernum) >= 2
group by cust_id
having sum (quantity) >
(select avg(quantity)
from orders o2
where o.item_id != o2.item_id)
order by custname;
I re-wrote my code and this is what I came up with. I'm really lost:
select c1.custname, phone, count(distinct(o.item_id)), sum(quantity) as quantity
from customer c1, orders o
Where c1.cust_id= o.cust_id
group by c1.cust_id, custname, phone, quantity
Having 2>=
(select count(o1.item_id)
From orders o1
where c1.cust_ID = o1.cust_ID)
AND sum(quantity) >
(select AVG(quantity)
from orders o2
Where c1.cust_ID != o2.cust_ID AND o.Item_ID = o2.Item_ID)
order by custname;
Try this query
select a.cust_id, count(distinct Item_ID) as itemOrderd,
sum(b.quantity) as sum
from
customer a
inner join
orders b
on a.cust_id=b.cust_id
group by a.cust_id
having count(distinct ordernum) > 1
and max(b.quantity) > avg(b.quantity)
Fiddle
| CUST_ID | ITEMORDERD | SUM |
|---------|------------|-----|
| 1 | 2 | 61 |
I think it will be:
SELECT MAX(c.CustName) as CustName,
MAX(c.Phone) as Phone,
COUNT(DISTINCT o.Item_ID) as CountOfDistinctItems,
SUM(o.Quantity) as SumOfQuantity
FROM Customer as c
JOIN Orders as o on c.Cust_ID=o.Cust_ID
JOIN (SELECT Ordernum, SUM(Quantity) as Order_q_sum
FROM Orders
GROUP BY Ordernum) as oq
ON o.Ordernum=oq.Ordernum
GROUP BY c.Cust_ID
HAVING COUNT(DISTINCT o.Ordernum)>=2
AND
MAX(oq.Order_q_sum)>
(
SELECT AVG(SumQuantity) FROM
(SELECT SUM(Quantity) SumQuantity FROM Orders GROUP BY OrderNum) as t1
)
HOPE THIS CLOSER TO YOUR NEEDS
SELECT x.custname,
x.phone,
Sum(x.itemcnt),
Sum(x.quantity)
FROM (SELECT c.custname,
c.phone,
1 AS itemCnt,
Sum(o.quantity),
Isnull((SELECT Avg(y.quantity)
FROM orders y
WHERE y.cust_id = o.cust_id), 0.00) AvgQty
FROM customer c
LEFT OUTER JOIN orders o
ON c.cust_id = o.cust_id
GROUP BY c.custname,
c.phone,
o.item_id) x
GROUP BY x.custname,
x.phone
HAVING Sum(x.itemcnt) > 1
AND Sum(x.quantity) > x.avgqty