Joining two tables by multiple fields in MS Access - ms-access

I have two tables. One lists my store's Customer Number, The Item They Bought, The Date They Bought It, and the Price They Paid.
I have a second table that lists my store's Customer Number, The Item They Returned, The Date They Returned It, and the Credit They Received.
Obviously, not all Sales have Credits.
I want to return results that look like:
Customer1, Item1, Date, Price, Blank
Customer1, Item2, Date, Price, Credit
Customer1, Item3, Date, Price, Blank
Customer1, Item4, Date, Price, Blank
Customer2, Item1, Date, Price, Blank
Customer2, Item2, Date, Price, Blank
Customer2, Item3, Date, Price, Credit
etc.
If I join the tables on the Customer Number, it returns me:
Customer1, Item1, Date, Price, Credit
Customer1, Item2, Date, Price, Credit
Customer1, Item3, Date, Price, Credit
Customer1, Item4, Date, Price, Credit
Customer2, Item1, Date, Price, Credit
Customer2, Item2, Date, Price, Credit
Customer2, Item3, Date, Price, Credit
If I join the tables on the Item and sort by Customer number, it returns gobbledygook.
My idea was to join on the Customer number and use an IIf(Sales.Item1=Credits.Item1, Credit, 0) in the criteria, but I am getting an error "You tried to execute a query ... that is not part of an aggregate expression."
What am I doing wrong?

Assuming you will have no returns without Sales, try this Query:
SELECT Sales.Customer, Sales.Item, Sales.Sale_Date, Sales.Sale_Price,
Credits.Credit_Date, Credits.Credit_Price,
IIf(IsDate([Credits].[Credit_Date]),"Credit","Blank") AS Result
FROM Sales LEFT JOIN Credits ON (Sales.Customer = Credits.Customer)
AND (Sales.Item = Credits.Item);
Define your values in the IIF statement (Credit or Blank) as needed.
You also can hide your fields Credits.Credit_Date, Credits.Credit_Price, so the output will be exactly what desired.

Related

How to get pin codes without zeros in it?

Write a query to display the customer_id,customer full name ,city,pincode,and order details (order id, product class desc, product desc, subtotal(product_quantity * product_price)) for orders shipped to cities whose pin codes do not have any 0s in them. Sort the output on customer name and subtotal. (52 ROWS) [NOTE: TABLE TO BE USED - online_customer, address, order_header, order_items, product, product_class].
These are the attributes of each table:
Address- ADDRESS_ID, ADDRESS_LINE1,ADDRESS_LINE2,CITY,STATE,PINCODE,COUNTRY
ONLINE_CUSTOMER- CUSTOMER_ID, CUSTOMER_FNAME, CUSTMER_LNAME, CUSTOMER_EMAIL,CUSTOMER_PHONE, ADDRESS_ID, CUSTOMER_CREATION_DATE,CUSTOMER_USERNAME, CUSTOMER_GENDER
ORDER_HEADER- ORDER_ID, CUSTOMER_ID, ORDER_DATE, ORDER_STATUS, PAYMENT_MODE, PAYMENT_DATE, ORDER_SHIPMENT_DATE,SHIPPER_ID
ORDER_ITEMS- ORDER_ID, PRODUCT_ID, PRODUCT_QUANTITY
PRODUCT- PRODUCT_ID, PRODUCT_DESC, PRODUCT_CLASS_CODE, PRODUCT_PRICE, PRODUCT_QUANTITY_AVAIL, LEN, WIDTH, HEIGHT, WEIGHT
PRODUCT_CLASS- PRODUCT_CLASS_CODE, PRODUCT_CLASS_DESC
Can anyone help me on how to filter pincodes without any zeros in it and order status as shipped.
Test if no zeros exist
where length(replace(pincode,'0','')) = length(pincode)
or
where instr(pincode,'0') = 0
or you could use regex or test valid pincodes by downloading and validating

Formula expanded field and rounding cannot figure out

I have the following formula to put into mySQL
SELECT Order_Number,
Order_Date,
Product,
Quantity,
Price,
Commission,
Employee_Sold,
(Quantity*Price)*(Commission*.01) AS Commision_Paid
FROM Orders
WHERE Order_Date BETWEEN '2017-12-01' AND '2017-12-31'
The commission paid amount comes out with 2.992500 as one example.....
The commission field is setup as decimal(5,2).
In the expanded field (Commission_Paid) it would be nice for it to show up as 2.99 in this case.
I have tried the ROUND function in many places but I am not sure as I keep getting error messages.
Thanks in advance. Student here and deadline on my paper. Appreciated.
MySQL 5.5X running.
Use Round(x, 2)
SELECT Order_Number,
Order_Date,
Product,
Quantity,
Price,
Commission,
Employee_Sold,
round((Quantity*Price)*(Commission*.01), 2) AS Commision_Paid
FROM Orders
WHERE Order_Date BETWEEN '2017-12-01' AND '2017-12-31'

Hard to explain, new at mysql

Calculate each supplier's total sales quantity and get the sales person's name if the sales person supplies parts more than 1000 units in total.
Table info:
Supplier {s_num, s_name, status, city}
Spj {s_num, p_num, j_num, qty}
This is what I have:
SELECT s_name, SUM(qty) AS sum
FROM Supplier, Spj
WHERE Supplier.s_num = Spj.s_num
AND qty > 1000
GROUP BY s_name;
I think my error is in this line:
AND qty > 1000
maybe I am using the "GROUP BY" incorrectly...
No suppliers quantity is above 800, so I get a blank result.
I want to test this:
"sum of quantity for each supplier" > 1000
Use HAVING to access aggregate results after a group by clause:
SELECT s_name, SUM(qty) AS sum
FROM Supplier, Spj
WHERE Supplier.s_num = Spj.s_num
GROUP BY s_name
HAVING qty > 1000;
See this post for more info on having vs where.

SQL select query, is this a correct solution?

I have been practicing SQL online and I am having doubt in one exercise. In the following database:
product (prod id, product name, product price, product manufacturer)
customer (cust id, customer name, customer address)
rating (prod id, cust id, rating date, rating stars)
sale (sale id, prod id, cust id, sale quantity, sale cost)
"For all cases in which the same customer rated the same product
more than once, and in some point in time gave it a lower rating
than before, return the customer name, the name of the product,
and the lowest star rating that was given."
I am not entirely sure about the answer, this is what I got:
SELECT customer_name,
product_name,
min(rating_stars)
FROM customer,
product,
rating
WHERE rating_stars < SOME
(SELECT rating_stars
FROM customer,
product,
rating)
GROUP BY customer,
product,
rating
Could any of you have a check in the code and see if it is correct?

Is this query correct?

The question is:- For the Star Model write an OLAP query that retrieves the quantity, the total income and discount
with respect to each city, type of furniture and month.
This is what I have done and want to know is this correct
SELECT Quantity, Income, Discount, City, Type, Month
FROM Sales, Customer, Furniture, Time
WHERE Count(quantity,income,discount)
GROUP BY City, Type, Month;
You are misisng to do the JOIN of the tables.
You probably want to JOIN your tables.
Sales, Customer, Furniture and Time
Also try to remove the AND from your queries like
SELECT Quantity, Income, Discount, City, Type , Month