3 Tables into dropdown list - sql

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

Related

Find duplicate values in SQL search

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;

Query AND and not OR using WHERE IN (list)

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;

SQL Query Daily Sales Per Month of a specific product

I've got a query which gets the lists of customers who've made a purchase of the product for the current day
select Customer.customerName, sum(InvoiceDetail.itemPrice * InvoiceDetail.itemQuantity) as dailyPurchase from Invoice
inner join InvoiceDetail on Invoice.invoiceID = InvoiceDetail.invoiceID
inner join Customer on Invoice.customerID = Customer.customerID
inner join Item on InvoiceDetail.itemID = Item.itemID
inner join Branch on Branch.branchID = Invoice.branchID
inner join Region on Region.regionID = Branch.regionID
where
Invoice.inactive = 0 and InvoiceDetail.inactive = 0
and Item.itemTypeID = 3
and Region.regionCode = 'CR'
and cast(Invoice.invoiceDate as date) = convert(date, '01/08/2016', 103)
group by Customer.customerName
What I need is a table with a list of all the dates in the current month, listing ALL customers who have at least purchased the product ONCE. It should resemble something similar to this image here.
Any help on how to get started or a general idea of how to get the desired result, is much appreciated. Thanks!
Sample Data from Results:
customerName dailyPurchase
AGH COMMUNICATIONS 450.00
ARIEL AMARCORD SHOP 285.00
AKN COMMUNICATION 300.00
AWSDAC TELECOMMUNICATION 2850.00
BARLEY MOBILE & SERVICES 285.00
Table Structure - I'm sorry, I don't know an easier way to copy this.
First get the customers who have purchased the product atleast once this month alongwith date.
Then use pivot to get the result in the form that you want (as seen in image). Search stackoverflow for pivot in sql server, you will get good info.
Give some information about the table structure and sample data and I might be able to help you with the query to get the results.

SQL JOIN TWO TABLES WITH GROUP BY

I am new to the world of databases etc. A developer who made my shop's POS took a large sum of money and would no longer answer me. I need to report my receivables as end end of the quarter. Please help with this query in access. I tried left join and it works fine when I join only the purchases, but not purchases and payments.
TABLES:
CUSTOMERSTABLE:
CUSTOMERID**********NAME
PURCHASESTABLE:
SALEID**********CUSTOMERID**********AMOUN******TDATE
PAYMENTSTABLE:
PAYMENTSID**********customerid*********amount******TDATE
The result should show all the customerids (with available transations or not), the total amount of purchases for each, the total amount of payments for each, the outstanding amount (net of the two), it should only consider transactions before a given TDATE
SELECT cust.customerid, Sum(purch.Amount_charged) AS totalPurchases, Sum(pay.Payment_Made) AS SumOfPayment_Made
FROM (cust INNER JOIN pay ON cust.customerid = pay.Account_Id) INNER JOIN purch ON cust.customerid = purch.Account_Id
WHERE ((([purch].[TDATE]) Between '4/6/2015' And '5/18/2015'))
GROUP BY cust.customerid;

Combining tables in SQL (only for for certain rows)

I wasn't even sure how to word this in the subject line. However, I have a SELECT statement that pulls information from an SQL view called CONTACTS (F_ame, L_Name, Address, Email, etc...). I have another view called INVOICES with purchase information for the members in the contacts view. Keep in mind that the INVOICES view has mulitple purchases of varying products for each member. These two views can be linked with a ContactID key
I only need to display a certain product (EADP Package), if purchased, from the INVOICES view on the same line as the member who purchased it. I also need to retain the entire member list in the pull. So, if I use a WHERE clause to only pull that product, It only gives me those who purchased that product. I need to keep the entire member list and still have a column that displays that particular product for those who purchased it. Hope that made sense.
Sorry, but there are 3 views, not 2. Here is what I have so far:
SELECT Contact.FirstName, Contact.LastName, Contact.CFSDesignation, Contact.EMailAddress1, Contact.Telephone1, Contact.DefaultPriceLevelIdName
FROM Contact INNER JOIN
FilteredInvoice ON Contact.ContactId = FilteredInvoice.contactid INNER JOIN
FilteredInvoiceDetail ON FilteredInvoice.invoiceid = FilteredInvoiceDetail.invoiceid
WHERE (Contact.DefaultPriceLevelIdName = 'member') AND FilteredInvoiceDetail.productidname = 'eAudiology 2014 Unlimited On-Demand Package'
This works fine,but only pulls those who purchased the package. I need the entire member list from CONTACTS (about 10,000 records) plus the product column showing the product above for those who purchased it. I belive is has something to do with joins, but can't get my head around it.
Getting Close, but it doesn't like the keyword "ON":
Also. in your previous answer, what are "C" and "I" used for?
SELECT Contact.FirstName, Contact.LastName, Contact.CFSDesignation, Contact.EMailAddress1, Contact.Telephone1, Contact.DefaultPriceLevelIdName
FROM Contact LEFT JOIN
FilteredInvoice ON Contact.ContactId = FilteredInvoice.contactid LEFT JOIN (SELECT FilteredInvoiceDetail.productidname
FROM FilteredInvoiceDetail
WHERE productidname = 'eAudiology 2014 Unlimited On-Demand Package') ON FilteredInvoice.invoiceid = FilteredInvoiceDetail.invoiceid
WHERE (Contact.DefaultPriceLevelIdName = 'member')
SELECT *
FROM Contacts C
LEFT JOIN ( SELECT *
FROM Invoices
WHERE Product = 'EADP Package') I
ON C.ContactID = I.ContactID
This finally worked for me:
SELECT DISTINCT Contact.ContactId,Contact.FirstName, Contact.LastName, Contact.CFSDesignation, Contact.EMailAddress1, Contact.DefaultPriceLevelIdName, productidname
FROM Contact LEFT JOIN
FilteredInvoice ON Contact.ContactId = FilteredInvoice.contactid LEFT JOIN (SELECT FilteredInvoiceDetail.productidname, FilteredInvoiceDetail.invoiceid
FROM FilteredInvoiceDetail
WHERE productidname = 'eAudiology 2014 Unlimited On-Demand Package' OR productidname = 'eAudiology 2014 Unlimited On-Demand Pkg Renewal') I ON FilteredInvoice.invoiceid = I.invoiceid
WHERE (Contact.DefaultPriceLevelIdName = 'member' OR DefaultPriceLevelIdName = 'student')
ORDER BY LastName