SQL should I use a subquery? - sql

I need help on a course question for my school. So I am supposed to get two tables Seller & Item, and I need to return the most active seller based on the most items offered. I have the tables as links below.
How could I just return one record with the sellers ID# and Name? Do I need to do a subquery? Thank you so much in advance.

Actually this is a way to accomplish it via subquery. I don't know that any teacher would anticipate students using >= all though:
select s.sellerid, min(s.name) as name
from seller s inner join item i on i.sellerid = s.sellerid
group by s.sellerid
having count(*) >= all (
select count(*)
from item
group by sellerid
)
You can also do it doubly=nested without even needing aliases!
select * from seller where sellerid in
(
select sellerid from item group by sellerid
having count(*) >= all (select count(*) from item group by sellerid)
)

Related

MIN and DISTINCT for ORACLE DB

I have this query
SELECT table_article.articleID, table_article_to_date.sellDate, MIN(table_article.price) AS minPrice
FROM table_article table_article
LEFT JOIN table_article_to_date table_article_to_date ON (table_article.ord_no=table_article_to_date.ord_no)
WHERE table_article.price > 0 AND table_article_to_date.sellDate BETWEEN_TWO_DATES
GROUP BY table_article.articleID, table_article_to_date.sellDate, table_article.price
For the sell_date I use a time range. My problem is, that i get more than one entrie each articleID.
I wish to have the lowest price of each articleID in a specified time range. DISTINCT is not woking with MIN
Givs a change to make this with a query?
The problem here is that you are adding the sell date to the GROUP BY clause. TO solve the issue, you need to take it out and make use of subqueries to get it back. To achieve this, you can do an inner join of the query and a query with the id, sell date and prize on the id and prize.
Also, no need for the prize in the group by, since it's already in the MIN.
SELECT articleData.articleId, articleData.sellDate, articleData.price FROM
(
SELECT articleId, MIN(price)
FROM table
[...]
GROUP BY articleId
) AS minPrice
INNER JOIN
(
SELECT articleId, sellDate, price
FROM table
) AS articleData
ON minPrice.price = articleData.price AND minPrice.articleId = articleData.articleId

A simple subquery in SQL

I have a table ORDERS with columns NAME and AMOUNT.
I need to get a NAME and total AMOUNT of each product.
I have such a solution
select PRODUCT_NAME, SUM(AMOUNT) from ORDERS GROUP BY PRODUCT_NAME;
But I dont use any subqueries for achieving that. But the lesson that Im going through is about subqueries. May be Im wrong about this solution?
Its a simple aggregation query without a need of sub query like:
SELECT name, SUM(AMOUNT)
FROM Orders
GROUP BY name
This solution is perfectly fine:
selectname, sum(amount)
from Orders
group by name
But, if you must use subquery, use this:
select o2.name,
(select sum(amount)
from Orders o1
where o1.name = o2.name) as total
from
(select distinct name
from Orders) o2

MS-Access: HAVING clause not returning any records

I have a Select query to extract Customer Names and Purchase Dates from a table. My goal is to select only those names and dates for customers who have ordered on more than one distinct date. My code is as follows:
SELECT Customer, PurchDate
FROM (SELECT DISTINCT PurchDate, Customer
FROM (SELECT CDate(FORMAT(DateAdd("h",-7,Mid([purchase-date],1,10)+""+Mid([purchase-date],12,8)), "Short Date")) AS PurchDate,
[buyer-name] AS Customer
FROM RawImport
WHERE sku ALIKE "%RE%"))
GROUP BY Customer, PurchDate
HAVING COUNT(PurchDate)>1
ORDER BY PurchDate
This returns no results, even though there are many customers with more than one Purchase Date. The inner two Selects work perfectly and return a set of distinct dates for each customer, so I believe there is some problem in my GROUP/HAVING/ORDER clauses.
Thanks in advance for any help!
You are doing in the inner select
SELECT DISTINCT PurchDate, Customer
and in the outter select
GROUP BY Customer, PurchDate
That mean all are
having count(*) = 1
I cant give you the exact sintaxis in access but you need something like this
I will use YourTable as a replacement of your inner derivated table to make it easy to read
SELECT DISTINCT Customer, PurchDate
FROM YourTable
WHERE Customer IN (
SELECT Customer
FROM (SELECT DISTINCT Customer, PurchDate
FROM YourTable)
GROUP BY Customer
HAVING COUNT(*) > 1
)
inner select will give you which customer order on more than one day.
outside select will bring you those customer on all those days.
.
Maybe you can try something simple to get the list of customer who brought in more than one day like this
SELECT [buyer-name]
FROM RawImport
WHERE sku ALIKE "%RE%"
GROUP BY [buyer-name]
HAVING Format(MAX(purchase-date,"DD/MM/YYYY")) <>
Format(MIN(purchase-date,"DD/MM/YYYY"))

How to count rows by group in a SQL query

I have a SQL query that returns data which includes quote types along with customer information. There are 4 types of quotes (Open, Dead, Requote, Project). I want to be able to count each type for each customer. And also count the total. I have not found a way to accomplish this.
Ultimately I want this to go into a gauge on an SSRS report so that we can tell how many of the quotes (percentage) eventually turn into a project.
I haven't found anything in my searches that works. Thanks in advance for any advice.
Using CTE's
WITH quoteTotal as (
Select customer, count(*) as customer_total
from customer
group by customer
),
typeQuote as (
Select customer, quote_type, count(*) as quote_total
from customer
group by customer, quote_type
)
SELECT T.customer, T.quote_type, T.quote_total, Q.customer_total
FROM typeQuote T
INNER JOIN quoteTotal Q
ON T.customer = Q.customer
I think using window functions can be easy.
SELECT DISTINCT
customer,
quote_type,
COUNT(*) OVER (partition by customer, quote_type order by customer) as type_total,
COUNT(*) OVER (partition by customer order by customer) as customer_total
FROM customers
For the data as below:
I have executed the query below
select CustomerEntityId,QuoteType,
(select count(QuoteType) from Customers c
where c.QuoteType=Customers.QuoteType
and c.CustomerEntityId=customers.CustomerEntityId
group by CustomerEntityId,QuoteType) as QuoteTypeTotal,
(select count(*) from Customers c1
where c1.CustomerEntityId=Customers.CustomerEntityId
group by CustomerEntityId) as CustomerTotal
from Customers
Group By CustomerEntityId,QuoteType
Order By CustomerEntityId
The result is as below :
I hope it helps.

How to Count Items that are return by a Group By Statements

Im currently trying to create a query in SQL that groups a list of products by vendor ID and then by categoryID an returns the groups that only have more than one product listed in them.
This is what I have so far:
SELECT Product.VendorID, CategoryID, Count (*) as NumProducts, avg(ProductPrice) as AveragePrice
From Product, Vendor
Where ProductPrice>50
Group By Product.VendorID, CategoryID
Having Count (Product.ProductID)>1;
My problem is that it returns categories that also have only one item in them.
You would seem to need a join. My guess is:
SELECT Product.VendorID, CategoryID, Count(*) as NumProducts, avg(ProductPrice) as AveragePrice
From Product inner join
Vendor
on Product.VendorId = Vendor.VendorId
Where ProductPrice>50
Group By Product.VendorID, CategoryID
Having Count(*) > 1;
It is not clear where the columns are coming from in your query, but you may not need the Vendor table at all.