How to get pin codes without zeros in it? - mysql

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

Related

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 Query related to average and length

i have a question. im currently doing an assignment, and having trouble in one particular request.
the request is:
Find Customers who have purchased more than 2 times. Get the
1. customer information
2. purchase frequency
3. total spending
4. avg basket size.
i already got 1 - 3, but i cant do much about number 4.
the values on product can have several values, separated by commas.
~> (product id: 12,4,5) -> if an order buys more than 1 product
i got to the point where i can separate it with length replace, but im a bit confused on how to show it to the average basket size.
thanks for any help!
SQL Table Image
sql table
**Customer**
Customer ID
Name
AddressCity
**Order**
OrderID
CustomerID
ProductID
Total
**Delivery**
DeliveryID
OrderID
AddressCity
this is my current sql statement
SELECT Customer.*, Order.CustomerID,
COUNT(Order.CustomerID) AS PurchaseFrequency,
SUM(Order.Total) AS TotalSpending
FROM Customer JOIN Order
ON Customer.CustomerID=Order.CustomerID
Group By Customer.CustomerID
Having Count(*) > 1
LEN(Order.ProductID) - LEN(REPLACE(Order.ProductID, ',', '')) + 1
Should give you the total amount of products per order.
Just add that to your existing statement.
SELECT Customer.*, Order.CustomerID,
COUNT(Order.CustomerID) AS PurchaseFrequency,
SUM(Order.Total) AS TotalSpending,
AVG(LEN(Order.ProductID) - LEN(REPLACE(Order.ProductID, ',', '')) + 1) as AvgProdQuantityPerOrder
FROM Customer JOIN Order
ON Customer.CustomerID=Order.CustomerID
Group By Customer.CustomerID
Having Count(*) > 1
EDIT:
The fields used in the SELECT part should also be specified in the GROUP BY part. Your query becomes then something like this:
SELECT Customer.CustomerID, Customer.Name, Customer.AdressCity,
COUNT(Order.CustomerID) AS PurchaseFrequency,
SUM(Order.Total) AS TotalSpending,
AVG(LEN(Order.ProductID) - LEN(REPLACE(Order.ProductID, ',', '')) + 1) as AvgProdQuantityPerOrder
FROM Customer JOIN
Order ON Customer.CustomerID=Order.CustomerID
GROUP BY Customer.CustomerID, Customer.Name, Customer.AdressCity
Having Count(*) > 1

SELECT the customer who has ordered the greatest quantity of Products in the case of two customers

I have the following ORDERS table
I know query to select the customer that has ordered the greatest quantity. However, how would it work, if say, two customers have the same quantity. What query should I write to show both the customers?
You can use a subquery which checks that the quantity for a given record matches the largest quantity observed in the table:
SELECT *
FROM yourTable
WHERE qty = (SELECT MAX(qty) FROM yourTable)
This will return multiple records if there are more than one customer sharing the maximum quantity.
If you only wanted to get back a single record, even in the presence of ties, you could use this approach:
SELECT *
FROM yourTable
ORDER BY qty DESC
LIMIT 1
I think you want sum of qty per custNum.
If so you can try like:
select custNum,
sum(qty) as qty
from Orders
group by custNum
order by sum(qty) desc;
Fiddle here:
http://sqlfiddle.com/#!9/47931b/10
SELECT custnum,sum(qty) as total
FROM orders
group by custnum
having sum(qty) = (SELECT MAX(qty) FROM orders);
This will return both values.

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?

SQL statement workflow

Every month we need to create a statement based off of a CSV file. Here is an example:
SELECT Name, OrderID, Product, Price
From Orders
Where OrderID = (here is where I am stuck);
So basically every month we get a new set of roughly 50 to 60 OrderID values in CSV format. How can I create a statement that outputs Name, OrderID, Product, Price for these OrderID's?
I think you are looking for something like this.
SELECT Name, OrderID, Product, Price
From Orders
Where OrderID In ('row1', 'row2', 'row3', ...);
Assuming you can put the IDs in a comma delimited list you would use an IN clause:
SELECT Name, OrderID, Product, Price
From Orders
Where OrderID IN (23,567,78);