SQL: Need to find duplicate rows across multiple columns - sql

I've tried using other solutions I've found, but none have worked. I have a table with four columns, Supplier, ProductCode, Description, and Price. The Supplier field is linked to another table with a list of suppliers. I need to find any records that have the exact same Supplier and ProductCode. Thanks in advance!!
I copied this code from another thread and tried to modify it for my table, but I get errors:
SELECT s.id, t.*
FROM ListPrices AS s
JOIN (SELECT Supplier, ProductCode, count(*) AS qty
FROM ListPrices GROUP BY Supplier, [ProductCode] HAVING count(*) > 1)
AS t ON (s.ProductCode = t.ProductCode) AND (s.Supplier = t.Supplier);

May not be exactly what you are looking for but one way to do this is to group by supplier and productcode and check if the count > 1
select * from (
select supplier,productcode,count(*) as count_rows
from listprices group by supplier,productcode) inner_table
where inner_table.count_rows > 1;

Related

Select all the columns from a table but group it with only few

May be this is a basic question but I’m unable solve this by myself, it will be greatly appreciated if someone can help me in achieving the following:
I want to get all the columns from a table but group it with only few. My query is as follows:
SELECT NATIONAL_ID,
EMPLID,
PLAN,
CHECK_DT,
PERIODS,
SUM (SALARY),
SUM (EE_CONT),
SUM (ER_CONT),
SUM (AL_HOURS)
FROM DTL_TMP
WHERE EMPLID = 'xxxxx'
GROUP BY NATIONAL_ID,
EMPLID,
PLAN,
CHECK_DT,
PERIODS;
If I add more columns to the above query it returns all the rows but I want only rows that are returned by the above query.
I did try few other options/examples like below:
SELECT OD.ProductID, OD.ProductName, CalQ.OrderQuantity
FROM (SELECT DISTINCT ProductID, ProductName
FROM OrderDetails) OD
INNER JOIN (SELECT ProductID, OrderQuantity SUM(OrderQuantity)
FROM OrderDetails
GROUP BY ProductID) CalQ
ON CalQ.ProductID = OD.ProductID
But it retrieves multiple rows because of the non-existence of keys in the table. Now, if I make any field as a key it gives unique constraint violation.
Please help.
Thanks in advance!

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 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.

SQL Server query to further summarize grouped data

Assume a table named transactions with two columns: invoiceNumber and itemNumber. Multiple quantities of an item on a single invoice are reflected by multiple records in the table. (I know this isn't an appropriate design, but I'm simplifying a more complex structure to get at the root question.)
I can determine the average number of unique items for each invoice with a query like:
SELECT invoiceNumber, COUNT(DISTINCT itemNumber)
FROM transactions
GROUP BY invoiceNumber
This query effectively ignores the quantity of an item, counting each one only once per invoice and shows the result for each invoice.
Instead of all this detailed information, however, all I really want is to determine the average number of unique items across all invoices. That is, I just want to summarize the per-invoice information. How do I do that?
You can aggregate the result you've already figured out how to obtain.
WITH DistinctCounts AS (
SELECT invoiceNumber, COUNT(DISTINCT itemNumber) AS distinctItems
FROM transactions
GROUP BY invoiceNumber
)
SELECT AVG(distinctItems)
FROM DistinctCounts
Select avg(numberininvoice)
From
(
Select invoicenumber, count(itemnumber) as numberininvoie
From
(Select distinct invoicenumber, itemnumber
From transactions) a
Group by invoicenumber
) b

How to get the products that all Warehouse exist?

I have a question as the title,I wanna get the products which appeared in every Warehouse,
I have no idea when i thought long time,i found i am so beetleheaded,
Thare are three tables in my sql server database:
Product
(
productID,
name,model,
size,
color
)
Warehouse
(
warehouseID,
name,
address
)
Inventory
(
warehouseID,
productID,
quantity
)
i hope someone help me to have a look and can write the sql that get the result
Thank you.
Use a GROUP BY to count the number of warehouses each product is in. Accept those rows for which this count is equal to the total number of warehouses.
SELECT productID
FROM Inventory
WHERE quantity > 0
GROUP BY productID
HAVING COUNT(*) = (SELECT COUNT(*) FROM Warehouse)
This assumes that (productID, warehouseID) is unique in the inventory table. If it is not then use COUNT(DISTINCT warehouseID) instead of COUNT(*).