I'm having a problem finding the right solution for this problem. I want to find duplicate invoices in a list of thousands of invoices and want to make sure were not paying the same invoice twice where the same invoice has been scanned in to our system twice but where the invoice number is not the same but has the same supplier id. I have tried to use COUNT to find all the invoices that have the same supplier id and the same amount but cannot get it to work.
(In the example I want to find the two Johns Bakery invoices)
You would seem to want:
select ip.*
from invoice_payment ip
where exists (select 1
from invoice_payment ip2
where ip2.supplier_id = ip.supplier_id and
ip2.name = ip.name and
ip2.amount = ip.amount and
ip2.invoice_number <> ip.invoice_number
)
order by supplier_id, name, amount;
Related
Background
I am currently working on a simple access database for a small bookstore. The business sells books as well as other items.
See following ER-diagram here
Problem
My problem starts in the table Transaction. I would like to create a simple form where a subtotal is calculated as quantity purchased multiplied by the price (both of a certain item).
However the Price field is either in the BookInfo, FoodAndBev, or ApparelAndSupplies table.
Question
How can I traverse the Transaction table to Items and to one of the tables I listed depending on the StockNum in Transaction?
For example, a customer buys an item with StockNum BK001. I would like to retrieve the Price corresponding to BK001 in the BookInfo table and return to Transaction to be used for calculating the subtotal.
The field StockNum exists in tables BookInfo, FoodAndBev, and ApparelAndSupplies.
Sample Data: BookInfo, Items, Transaction
Ideal Output: Transaction 1: A customer buys 2 copies of a book with StockNum 'BK001' (disregard what I already have in the screenshot). The Price of 'BK001' is $119.99 which I would need to retrieve from BookInfo (my problem is I want to make sure it goes to BookInfo not the other two tables which have StockNum also), so the subtotal would then be calculated and would show $239.98. For now, I've been doing this manually. I want to be able to do this with different StockNum such as those that start with AS for Apparel and Supplies and FB for food and beverage.
Thanks in advance!
Left-Joining all tables and using Case-When-Expression (which is function switch in Access) for different price multiplication:
SELECT t.StockNum, t.NumOrdered,
switch(
Left(t.StockNum,2) = 'BK' , (t.NumOrdered * b.Price),
Left(t.StockNum,2) = 'AS' , (t.NumOrdered * a.Price),
Left(t.StockNum,2) = 'FB' , (t.NumOrdered * f.Price),
) AS subtotal
FROM Transaction t
LEFT JOIN BookInfo b ON b.StockNum = t.SockNum
LEFT JOIN ApparelAndSupplies a ON a.StockNum = t.SockNum
LEFT JOIN FoodAndBev f ON f.StockNum = t.SockNum
I have 3 tables, Clients, Products, Transactions.
When we enter a product, it is given a PID (product id) and a CID (client ID), which relate to the Clients and Transaction tables. The transaction table has a CID and Quantity.
I am trying to list all unique products and quantities, some clients have 2 listings of the same product, so if 1 is 10 units and the other is 20, then that client has 30 of product a.
The transactions table lists all sales, which are subtracted from the total.
I need the query to show the product name, client name, quantity available.
Here is the code I have so far, apologies for the mess and thanks much for any help.
This is an Access database.
SELECT Min(Products.PID) exPID,
Min(Products.[Product Name]) AS exProdName,
Min(Products.[Seller Asking]) AS exAsking,
Min(Products.CID) AS exClientID,
Min(Transactions.[CID Seller]) AS exSellerID,
Sum(Products.Quantity - ((SELECT Transactions.[No Units], Clients.Name,
Transactions.[CID Seller], Products.CID
FROM Transactions, Clients, Products
WHERE Transactions[CID Seller]=Products.CID)
) AS exSumofTrans),
Min(Clients.Name) AS exClientName,
Min(Transactions.[CID Seller]) AS exSeller
FROM Transactions, Clients, Products
WHERE (((Transactions.[CID Seller])=[Products].[CID]
AND (Products.[PID])=([Transactions].[PID])));
First issue here is an error on the inner select.
The error says:
'Syntax error in query expression (Sum(Products.Quantity-((Select Transactions.[No Units], Clients.Name, Transactions.[CID Seller], Products.CID
FROM Transactions, Clients, Products
Where Transactions[CID Seller]=Products.CID)) as exSumofTrans)'.
I had to do some guessing based on what it looks like you are trying to do and I have no way of testing it so it may need further tweaking but this will get you on the right path:
SELECT Products.PID AS exPID,
FIRST(Products.[Product Name]) AS exProdName,
FIRST(Products.[Seller Asking]) AS exAsking,
FIRST(Products.CID) AS exClientID,
SUM(Products.Quantity)-FIRST(T.exNoUnits) AS exSumofTrans,
T.exSellerID,
FIRST(Clients.Name) AS exClientName
FROM (Products
INNER JOIN (SELECT [CID Seller] AS exSellerID, PID, SUM([No Units]) AS exNoUnits
FROM Transactions GROUP BY [CID Seller], PID) AS T
ON T.PID=Products.PID)
INNER JOIN Clients ON T.exSellerID=Clients.CID
GROUP BY Products.PID, T.exSellerID
I have a data like this in Table Receipts, for the plus value is the amount of receipt of goods while the negative value in case of returns.
In the picture above, the Receiver_No 01267A is the receipt of the first item with Qty_Received 1. Then there is the return of goods on the receipt number of goods 01268A. I want this transaction not to show up.
Here's statement where i have tried.
select I2.Receiver_No,
I2.Purchase_order,
I2.PO_Line,
I2.Qty_received
from (Select Purchase_order,
PO_Line
from Receipts
group by Purchase_order,
PO_Line
having sum(qty_received) <>'0') I1 inner join
Receipts I2 On
I1.Purchase_Order = I2.Purchase_Order and
I1.PO_Line = I2.PO_Line
But the result did not meet with i want.
Please Help.
This will work for the very limited data that you have provided. If you care to provide more information about what the data looks like, then it will be easier to come up with a better solution.
If you have data with a lot of lines that have identical quantities, then this solution will not work.
SELECT
r.Receiver_No, r.Purchase_order, r.PO_Line, r.Qty_received
FROM
receipts r
WHERE
NOT EXISTS (SELECT
purchase_order
FROM
receipts s
WHERE
r.qty_received = -s.qty_received
)
If your requirement is to fetch rows with quantities less than one and not the negative ones :
SELECT * FROM Receipts WHERE ABS(QtyRecvd) >0 AND ABS(QtyRecvd) < 1
Try This
SELECT * FROM Receipts where Qty_received>0 AND Qty_received < 1
Considering I have three tables:
product
shop
supplier
and their junction tables:
product_shop
product_supplier
I am trying to get all products that are supplied by a specific supplier and available in multiple shops, i.e. all products from supplier A that are in shop 1 and 2.
If we assume, supplier has 10 products, of which 5 are available in shop 1 and 7 are available in shop 2, but only 3 are available in shop 1 and 2, then those are the 3 I am seeking.
Here is what I have so far (in postgresql 9.6), which simulates an inclusive IN. Which works, but it doesn't seem right (proper) to me:
edit:
I use this query dynamically and have therefor to pass the list of shops (which varies), the supplier of course, and the count (which is equal to the length of the list of shops). It's not that this is a bother, but I was nonetheless wondering if there is a way to create a query using only joins.
SELECT
product_shop.product
FROM
(SELECT product FROM product_shop where shop in (1, 2)) product_shop JOIN
product_supplier ON product_shop.product = product_supplier.product
WHERE
product_supplier.supplier = A
GROUP BY
product_shop.product
HAVING
count(product_shop.product) = 2
Is there a better query to achieve the result I am seeking?
*Not a native speaker
I believe this will give you the desired result
SELECT product.* FROM product
JOIN product_supplier ON product.id = product_supplier.productId
JOIN product_shop ON product.id = product_shop.productId
WHERE product_shop.shopId IN (1 , 2)
AND product_supplier.supplierId = 'A'
GROUP BY product.id
HAVING COUNT(product.id) > 1
Its basically equivalent, but you do not need the subquery. There is, however, an error.
With your query you will only get the products that are available in exactly 2 shops.
since you wrote:
I am trying to get all products that are supplied by a specific supplier and available in multiple shops
Using HAVING COUNT(product.id) > 1 would be more generic and correct.
However, it would not make any difference in this case, since you restricted the search to 2 shops.
I Also changed references like product_supplier.supplier to product_supplier.supplierId. You shroud use Id fields for table relationships.
Here is the query with out Having the count
with cte_product -- fetching all the products which are in shop 1&2
(
SELECT product,count(product) as cnt FROM product_shop
where shop in (1, 2)
group by product
)
SELECT *
FROM
cte_product psh
JOIN
product_supplier psu
ON psh.product = psu.product
WHERE
psu.supplier = A
and psh.cnt > 1;
Trying to return only the 1st supplier code and have all other fields unaffected.
`Select
Container.Part_Key,
Part.Part_No,
Part.Name,
Part.Revision,
Container.Quantity,
Container.Container_Status,
OP.Operation_No,
Opp.Operation_Code,
Part.Part_Status,
Supplier.Supplier_Code
From Part_v_Container as Container
Join Part_v_Part as Part
ON Part.Part_Key = Container.Part_Key
Join Part_v_Part_Operation as Op
On Op.Part_Operation_Key = Container.Part_Operation_Key
Join Part_v_Operation as OPP
On OPP.Operation_Key = OP.Operation_Key
Join part_v_approved_supplier as Approved
On Approved.part_key = container.part_key
Join common_v_Supplier as Supplier
On Supplier.supplier_no = Approved.Supplier_No
Where Container.Active = '1'
group by container.part_key`
There will be duplicate part numbers, revisions, etc. Not worried about that. I just want the part no to list only one approved supplier to the far right, even though for any given part, there are multiple approved suppliers listed in the database.
Furthermore, the order the database lists the approved suppliers does not matter.
Thanks!
Add a sub query in your select list
( select top 1 supplier.supplier_code
From supplier
Where Supplier.supplier_no = Approved.Supplier_No order by Supplier.supplier_no) as supplier code
This can be the last field in the select list
You could add An appropriate order by.
This would work in SQL Server