Select subset from subset - sql

Data has 1 table with 2 relevant fields:
OrderNumber
ProductID
How do I structure sql to find :-
Select All OrderNumber where ProductID in (A,B)
Now, on this subset, Select all where ProductID in (A,B,C,D,E)
Show CustomerName, OrderNumber, ProductID, ProductPrice
Goal is to find all Orders that contain 2 specific products, then to measure sales of only 3 specific products related to A,B.

I'm not sure what you want, but I will take a stab.
This will show you the details of orders with one of the 5 product id's
SELECT CustomerName, OrderNumber, ProductID, ProductPrice
FROM yourTable
WHERE ProductId IN ('A','B','C','D','E')
This will count the orders for you
SELECT ProductID, COUNT(*) AS Count
FROM yourTable
WHERE ProductId IN ('A','B','C','D','E')
GROUP BY ProductId

Related

Get the date for each duplicated row in SQL Server

I've made a query to get how many products are sold more than one time and it worked.
Now I want to show the transaction date for each of these duplicated sales, but when I insert the date on the select it brings me a lot less rows: something is going wrong. The query without the date returns 9855 rows and with the date just 36 rows.
Here is the query I'm doing:
SELECT TransactionDate,
ProductName,
QtyOfSales = COUNT(*)
FROM product_sales
WHERE ProductID = 1 -- Product Sold ID
AND ProductName IS NOT NULL
GROUP BY ProductName,
TransactionDate
HAVING COUNT(*) > 1
Perhaps a subquery? Can you help in that regard?
You can use the corresponding COUNT window function, that will find the amount of transactions by partitioning on the "ProductName" as required:
WITH cte AS(
SELECT TransactionDate,
ProductName,
COUNT(*) OVER(PARTITION BY ProductName) AS QtyOfSales
FROM product_sales
WHERE ProductID = 1 -- Product Sold ID
AND ProductName IS NOT NULL
)
SELECT DISTINCT TransactionDate,
ProductName
FROM cte
WHERE QtyOfSales > 1

How to compare the dates within the column and based on the latest dates, print other corresponding columns

I have ProductId, PriceType, Price, FromDate columns.
in this, Product ID is list of ids given to different products, pricetype is the category of different prices like deal price, wholesale price etc., Price is money values of each products.
Currently, for each product ids there are multiple pricetypes & fromdates.
Click here for the image.
I want a final output where for every product Id there should be unique price for every price type category based on the latest FromDate.
Example, for Product id 221, there are 2 list price with 2 different values in price and fromdate. now my final output should be product id as 221, fromdate as 01/07/2014, price as 19.8 and pricetype as list price.
Try this
;WITH temp AS
(
SELECT ProductId, PriceType, Price, t.FromDate , ROW_NUMBER() over(PARTITION BY ProductId, PriceType ORDER BY Fromdate DESC) AS RowIndex
FROM #ActualData
)
SELECT ProductId, PriceType, Price, FromDate
FROM temp t WHERE t.RowIndex = 1
#TriV's answer should work. Here is another option. You'll have to try them both to see which performs better on your data.
select t.ProductId, t.PriceType, t.Price, t.FromDate
from #temp1 t
where exists(select 1
from #temp1 t2
where t2.ProductId = t.ProductId
and t2.PriceType = t.PriceType
group by t2.ProductId, t2.PriceType
having t.FromDate = max(t2.fromDate))

SQL Server : TOP along with Distinct

I have two tables Products and PurchaseDetails.
The schema for Products table is
ProductId (primary key)
ProductName
CategoryId
Price
QuantityAvailable
The schema for PurchaseDetails table is
PurchaseId
EmailId
ProductId
QuantityPurchased
DateOfPurchase
The question asks me to find out the TOP 3 products that are purchased in large quantity.
I wrote this SQL query:
Select TOP 3
Distinct(ProductName), Price, QuantityPurchased
from
Product, PurchaseDetails
where
Product.ProductId = PurchaseDetails.ProductId
order by
QuantityPurchased DESC
But the above query throws an error. I fail to see why the error is being generated by the above query ?
Below query will give you the top 3 products that are purchased in large quantity
Select TOP 3 ProductName,sum(Price) as [price],sum(QuantityPurchased) as QuantityPurchased
from Product , PurchaseDetails
where Product.ProductId=PurchaseDetails.ProductId
group by ProductName
order by QuantityPurchased DESC
Select TOP 3 ProductName,sum(Price) as [price],sum(QuantityPurchased) as QuantityPurchased
from Product , PurchaseDetails
where Product.ProductId=PurchaseDetails.ProductId
group by ProductName
order by QuantityPurchased DESC

SQL GROUP BY on a sub query

I have a query that will return results from 2 tables into 1 using a UNION ALL, which all works as I need it to. However I need to run a GROUP BY and an ORDER BY on the returned dataset however I am getting many errors and I'm not sure how to solve it.
Here is my Query:
SELECT ProductID, Quantity
FROM BasketItems
UNION ALL
SELECT ProductID, Quantity
FROM OrderItems
This will return a results set such as this:
ProductID Quantity
15 2
20 2
15 1
8 5
5 1
I then want to run a GROUP BY on the ProductID field and then finally an ORDER BY DESC on the Quantity field. So in the final output, this particular results set will finally result in this:
ProductID
8
15
20
5
I can then run queries on this result set as I usually do
EDIT:
As stated above, but maybe not implied enough is that I will need to run queries on the returned results, which isn't working as you cannot run a query on a set of results that have an ORDER BY clause (so far as I gathered from the error list)
If you want more information on the problem, here it is:
From this results set, I want to get the products from the product table that they relate to
SELECT * FROM Products WHERE ID IN (
SELECT ProductID
FROM
(
SELECT ProductID, Quantity
FROM BasketItems
UNION ALL
SELECT ProductID, Quantity
FROM OrderItems
) v
GROUP BY ProductID
ORDER BY SUM(Quantity) DESC
)
However, I get this error: The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.
The output of products need to be in the order that they are returned in the sub query (By quantity)
SELECT Products.*
FROM Products
INNER JOIN
(
SELECT ProductID, Sum(Quantity) as QuantitySum
from
(
SELECT ProductID, Quantity
FROM BasketItems
UNION ALL
SELECT ProductID, Quantity
FROM OrderItems
) v
GROUP BY ProductID
) ProductTotals
ON Products.ID = ProductTotals.ProductID
ORDER BY QuantitySum DESC
will this work?
SELECT ProductID
from
(
SELECT ProductID, Quantity
FROM BasketItems
UNION ALL
SELECT ProductID, Quantity
FROM OrderItems
) temp
GROUP BY temp.ProductID
ORDER BY SUM(temp.Quantity) desc
Here's a cte version (no live test so please excuse blunders)
EDIT
;WITH myInitialdata_cte(ProductID,Quantity)
AS
(
SELECT ProductID, Quantity FROM BasketItems
UNION ALL
SELECT ProductID, Quantity FROM OrderItems
)
SELECT b.ID
FROM
myInitialdata_cte a
INNER JOIN Products b ON
a.ProductID = b.ID
GROUP BY ProductID
ORDER BY SUM(a.Quantity) DESC

Dedupe records without DELETE

I need to bring back only one of the records from a duplicated row in SQL Server
I have data like this
-------------------------------------------
CustomerID, OrderID, ProductID, Title
-------------------------------------------
1,1001,131,orange
1,1002,131,orange
-------------------------------------------
These rows are shown as 2 items that have been ordered by the same person, really they are just two as the quantity chosen in the basket and 2 records.
My question is how can i retrieve only one of these rows?
Thanks
Maybe something like this:
First some test data:
DECLARE #tbl TABLE(CustomerID INT,OrderID INT,ProductID INT,Title VARCHAR(100))
INSERT INTO #tbl
VALUES
(1,1001,131,'orange'),
(1,1002,131,'orange')
Then the query
;WITH CTE AS
(
SELECT
ROW_NUMBER() OVER(PARTITION BY tbl.CustomerID,tbl.ProductID,tbl.Title
ORDER BY tbl.OrderID) AS RowNbr,
tbl.CustomerID,
tbl.OrderID,
tbl.ProductID,
tbl.Title
FROM
#tbl AS tbl
)
SELECT
*
FROM
CTE
WHERE
CTE.RowNbr=1
This way you can get, not only one of both rows, but also the quantity ordered
SELECT
CustomerID, ProductID, Title, max(OrderID) as orderID, COUNT(*) as quantity
FROM
TableName
GROUP BY
CustomerID,
ProductID,
Title
Using Max will get you the most recent order
SELECT CustomerID, MAX(OrderId), ProductID, Title
FROM table
GROUP BY CustomerID, ProductID, Title
OR
Using Min will get you the first order
SELECT CustomerID, MIN(OrderId), ProductID, Title
FROM table
GROUP BY CustomerID, ProductID, Title
Provided that it's really what you want you can get the first order of each order with the same customer, product and title using a grouping and the MIN function (MAX would give you the last order):
SELECT CustomerID, MIN(OrderID) AS OrderID, ProductID, Title
FROM MyTable
GROUP BY CustomerID, ProductID, Title
If you want the number of duplicate orders (that would be the ordered quantity judging by your question) you can add a count:
SELECT CustomerID, MIN(OrderID) AS OrderID, ProductID, Title,
COUNT(*) AS Quantity
FROM MyTable
GROUP BY CustomerID, ProductID, Title