Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

SQL queries for finding values in 4 different tables

$
0
0

I need to have certain queries to find specific data in this table, this is just an example table, but I will use the same ideas for my actual website and database.

customers (customerID: integer, fName: string, lName: string)
items (itemID: integer, description: string, price: float)
orders (orderID: integer, itemID: integer, aID: integer, customerID: integer, date: date)
addresses (aID: integer, housenum: integer, streetName: string, town:string, state: string, zip:integer)

Values I need to find are

 List the town, first name, and last name of any customer who has shipped an item to the same town as another customer.

Return the average amount of money each customer spent in March of 2013. (Note that the answer will be a single number

List the first and last names of all customers who have had the same item shipped to at least two different addresses.

List the top two states that have generated the most total revenue and the revenue they generated

I did try a few different queries, for #3 I tried 

SELECT customers.fName,

customers.lName,

COUNT(orders.itemID) AS `total items with diff address >= 2`

FROM customers

JOIN (SELECT customerID,itemID,

COUNT(DISTINCT aID) AS diff_address

FROM orders

GROUP BY orders.itemID

          HAVING diff_address >= 2

         ) AS orders

      ON orders.customerID = customers.customerID 

but I only got 1 result, and I do not think thats correct.

Thanks for the help and I appreciate you taking the time to help me


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>