I'm trying to get the most expensive and cheapest items from two different tables.
The output should be one row with the values for MostExpensiveItem, MostExpensivePrice, CheapestItem, CheapestPrice
I was able to get the price of the most expensive and cheapest items in the two tables with following query:
SELECT
MAX(ExtrasPrice) as MostExpensivePrice, MIN(ExtrasPrice) as CheapestPrice
FROM
(
SELECT ExtrasPrice FROM Extras
UNION ALL
SELECT ItemPrice FROM Items
) foo
How can I add the names of the items (ItemName, ExtrasName) to my output? Again, there should only be one row as the output.
Try this:
SELECT TOP 1 FIRST_VALUE(Price) OVER (ORDER BY Price) AS MinPrice,
FIRST_VALUE(Name) OVER (ORDER BY Price) AS MinName,
LAST_VALUE(Price) OVER (ORDER BY Price DESC) AS MaxPrice,
LAST_VALUE(Name) OVER (ORDER BY Price DESC) AS MaxName
FROM (
SELECT ExtrasName AS Name, ExtrasPrice AS Price FROM Extras
UNION ALL
SELECT ItemName As Name, ItemPrice AS Price FROM Items) u
SQL Fiddle Demo
TOP 1 with order by clause should work for you. Try this
SELECT *
FROM (SELECT TOP 1 ExtrasPrice,ExtrasName
FROM Extras ORDER BY ExtrasPrice Asc),
(SELECT TOP 1 ItemPrice,ItemName
FROM Items ORDER BY ItemPrice Desc)
Note: Comma can be replaced with CROSS JOIN
You can use row_number() for this. If you are satisfied with two rows:
SELECT item, price
FROM (SELECT foo.*, row_number() over (order by price) as seqnum_asc,
row_number() over (order by price) as seqnum_desc
FROM (SELECT item, ExtrasPrice as price FROM Extras
UNION ALL
SELECT item, ItemPrice FROM Items
) foo
) t
WHERE seqnum_asc = 1 or seqnum_desc = 1;
EDIT:
If you have an index on "price" in both tables, then the cheapest method is probably:
with exp as (
(select top 1 item, ExtrasPrice as price
from Extras e
order by price desc
) union all
(select top 1 i.item, ItemPrice
from Items i
order by price desc
)
),
cheap as (
(select top 1 item, ExtrasPrice as price
from Extras e
order by price asc
) union all
(select top 1 i.item, ItemPrice
from Items i
order by price asc
)
)
select top 1 *
from exp
order by price desc
union all
select top 1 *
from cheap
order by price asc;
If you want this in one row, you can replace the final query with:
select e.*, c.*
from (select top 1 *
from exp
order by price desc
) e cross join
(select top 1 *
from cheap
order by price asc
) c
Related
I can get the top 1000 selling items from a sales table in a day (and display item info from another table) with the following query:
select
cs.item_no, sum(quantity) as quantity, i.item_type, i.item_name, i.group_name
FROM `sales` cs
left join
(
SELECT
item_no,
MIN(item_name) as item_name,
MIN(item_type) as item_type,
MIN(group_name) as group_name,
FROM
`items`
WHERE group_name!='UNKNOWN'
GROUP BY
item_no
) i
on cs.item_no = i.item_no
where date BETWEEN '2022-08-21' AND '2022-08-22'
group by item_no, i.item_type, i.item_name, i.group_name
order by sum(quantity) desc
limit 1000
The query displays top 1000 items by quantity sold and also shows the item's type, name and group_name. How can I modify this query so that I can see the top 10 items for each group_name? So instead of top 1000 items overall, I want to see the top 10 items in each group_name.
Thank you
Does below SQL works ?
Basically i created a subquery to calculate rank over partition on item group and then ordered by quantity.
A filter on rank column will display top 10 records for each group.
select item_no,item_type, item_name, group_name
FROM (
select subq.*, rank() over (partition by group_name order by group_name,quantity desc) as rn
FROM ( select
cs.item_no, sum(quantity) as quantity, i.item_type, i.item_name, i.group_name
FROM `sales` cs
left join
(
SELECT
item_no,
MIN(item_name) as item_name,
MIN(item_type) as item_type,
MIN(group_name) as group_name,
FROM
`items`
WHERE group_name!='UNKNOWN'
GROUP BY
item_no
) i
on cs.item_no = i.item_no
where date BETWEEN '2022-08-21' AND '2022-08-22'
group by item_no, i.item_type, i.item_name, i.group_name) subq) outerqry
WHERE rn<=10
I have a products table that contains date-of-create, price, rate,....... and multiple columns I need one select statement that returns the 10 newest records, 10 lowest prices and the top rated products as separated result of each other something like
(select top 10 from products Order By Date ASC ) as newest_list
(select top 10 from products Order By price DESC ) as price_list
(select top 10 from products Order By Rate ASC ) as rateList_list
where ( newest_list price_list rateList_list) are different table result
Which better way to approach that result. Thanks.
You basically want union all . . . and a subquery:
select *
from ((select top 10 'newest' as which, p.*
from products p
Order By Date ASC
) union all
(select top 10 'priciest', p.*
from products p
Order By price DESC
) union all
(select top 10 'ratiest', p.*
from products p
Order By Rate ASC
)
) p
If you don't want duplicates, you can use union or window functions:
select p.*
from (select p.*,
row_number() over (order by date desc) as seqnum_d,
row_number() over (order by price desc) as seqnum_p,
row_number() over (order by rate asc) as seqnum_r
from p
) p
where seqnum_d <= 10 or seqnum_p <= 10 or seqnum_r <= 10;
Customer have ordered from different cities. Thus we have multiple cities against same customer_id. I want to display that city against customer id which has occurred maximum number of times , in case where customer has ordered same number of orders from multiple cities that city should be selected from where he has placed last order. I have tried something like
SELECT customer_id,delivery_city,COUNT(DISTINCT delivery_city)
FROM analytics.f_order
GROUP BY customer_id,delivery_city
HAVING COUNT(DISTINCT delivery_city) > 1
WITH cte as (
SELECT customer_id,
delivery_city,
COUNT(delivery_city) as city_count,
MAX(order_date) as last_order
FROM analytics.f_order
GROUP BY customer_id, delivery_city
), ranking as (
SELECT *, row_number() over (partition by customer_id
order by city_count DESC, last_order DESC) as rn
FROM cte
)
SELECT *
FROM ranking
WHERE rn = 1
select customer_id,
delivery_city,
amount
from
(
select t.*,
rank() over (partition by customer_id order by amount asc) as rank
from(
SELECT customer_id,
delivery_city,
COUNT(DISTINCT delivery_city) as amount
FROM analytics.f_order
GROUP BY customer_id,delivery_city
) t
)
where rank = 1
I'm trying to make a query that outputs a list with the company data and the number of Products and Discounts of each Company and order by product_count.
Also i need to limit the output to groups of 30 rows
SELECT * FROM (
SELECT *, (
SELECT COUNT(*) FROM Products WHERE Product_Comp_id = Comp_id
) as product_count, (
SELECT COUNT(*) FROM Discount WHERE Disc_Comp_id = Comp_id
) as discount_count , ROW_NUMBER() OVER (
ORDER BY product_count ASC
) AS RowNum FROM Company
) AS finalTable WHERE finalTable.RowNum BETWEEN 0 AND 30
But i get this error
Invalid column name 'product_count'.
Table Structure
Products
|-Product_id
|-Product_Name
|-Product_Description
|-Product_Comp_id
Discount
|-Disc_id
|-Disc_Name
|-Disc_Comp_id
|-Disc_Ammount
Company
|-Comp_id
|-Comp_Name
|-Comp_Address
You need an additional level of subquery to give you product_count.
SELECT * FROM (
SELECT * , ROW_NUMBER() OVER (ORDER BY product_count ASC) AS RowNum
FROM
(
SELECT *, (SELECT COUNT(*) FROM Products WHERE Product_Comp_id = Comp_id) as product_count,
(SELECT COUNT(*) FROM Discount WHERE Disc_Comp_id = Comp_id) as discount_count
FROM Company
) C
) AS finalTable WHERE finalTable.RowNum BETWEEN 0 AND 30
consider the below:
ProductID Supplier
--------- --------
111 Microsoft
112 Microsoft
222 Apple Mac
222 Apple
223 Apple
In this example product 222 is repeated because the supplier is known as two names in the data supplied.
I have data like this for thousands of products. How can I delete the duplicate products or select individual results - something like a self join with SELECT TOP 1 or something like that?
Thanks!
I think you want to do the following:
select t.*
from (select t.*,
row_number() over (partition by product_id order by (select NULL)) as seqnum
from t
) t
where seqnum = 1
This selects an arbitrary row for each product.
To delete all rows but one, you can use the same idea:
with todelete (
(select t.*,
row_number() over (partition by product_id order by (select NULL)) as seqnum
from t
)
delete from to_delete where seqnum > 1
DELETE a
FROM tableName a
LEFT JOIN
(
SELECT Supplier, MIN(ProductID) min_ID
FROM tableName
GROUP BY Supplier
) b ON a.supplier = b.supplier AND
a.ProductID = b.min_ID
WHERE b.Supplier IS NULL
SQLFiddle Demo
or if you want to delete productID which has more than onbe product
WITH cte
AS
(
SELECT ProductID, Supplier,
ROW_NUMBER() OVER (PARTITION BY ProductID ORDER BY Supplier) rn
FROM tableName
)
DELETE FROM cte WHERE rn > 1
SQLFiddle Demo
;WITH Products_CTE AS
(
SELECT ProductID, Supplier,
ROW_NUMBER() OVER (PARTITION BY ProductID ORDER BY <some value>) as rn
FROM PRODUCTS
)
SELECT *
FROM Products_CTE
WHERE rn = 1
The some value is going to be the key that determines which version of Supplier you keep. If you want the first instance of the supplier, you could use the DateAdded column, if it exists.