I have a N:M relation that connects customers and purchase Customer_id,Purchase_id . I would like to know how many purchases each specific user made, from one query.
I tried this:
SELECT DISTINCT(Customer_id) AS ID,COUNT(Purchase_id) AS P FROM CustomerPruchases;
but it only gives me one row for the first customer in the DB. I want it for all.
If you group then aggregate functions like count() apply to each group and not to the complete table
SELECT Customer_id as ID,
COUNT(Purchase_id) AS P_COUNT
FROM CustomerPruchases
group by ID
Related
I am trying to calculate in a SQL statement. I am trying to calculate the total amount in the invoice.total column per customer. I created the following statement:
SELECT customers.firstname, customers.lastname, customers.status, SUM(invoice.total) AS total
FROM customers
INNER JOIN invoice
ON customers.id=invoice.id;
When I run this, I get the total amount in the table. I have 15 different customers in this table but I get only the name of the first customer and the total amount of all customers. What am I doing wrong?
First, you need to Group the data when you want to have aggregate results:
SELECT customers.firstname, customers.lastname, customers.status, SUM(invoice.total) AS total
FROM customers
INNER JOIN invoice
ON customers.id=invoice.id
GROUP BY customers.firstname, customers.lastname, customers.status;
Second, are you sure you are joining the table by correct fields? Is invoice.id correct column? I would expect invoice.id to be primary key for the table and instead I would expect another column for foreign key, invoice.customerid for example. Please double check it is correct.
UPDATE: As it was mentioned in comments, if you have two customers with same first name, last name and status, the data will be grouped incorrectly. In that case you need to add unique field (e.g. customers.id) to SELECT and GROUP BY statements.
You have to add a group by customer (id property for example). If you want to have first and last name in select then you will have to group by them as well.
Use group by clause in your query or you can use common table expression to get the sum of records by customer and then make a inner join with customer table.
You need to add a Group By with column names in your query.
So I have two tables that I need information from. I have a
ballot_table(vote CHAR(30), username CHAR(30)) that has the name of the candidate each username voted for. I also have another table with a list of the candidates. I need someway to return the list of candidates with the corresponding amount of times there name appears in ballot_table in the same query. Thanks!
This is a horrible design but here is how you do it:
select count(*) as votes, vote as [candidate]
from ballot_table
where ucase(vote) in (select ucase(item) from table_with_list_of_candidates)
group by ucase(vote)
a better design would have the list of candidates table include a key and then just have the key in the ballot_table with a varchar for a write in (if needed).
You can do this with an Outer Join and a Group By. I'm assuming the field name in the candidate table is Name.
Select c.Name, Count(Distinct b.UserName) Votes
From ballot_table b
Right Join candidate c On c.Name = b.Vote
Group By c.Name
This would only return the total votes for the candidates you have in your Candidate table. Any other "write-in" vote wouldn't be included.
Pretty new to sql statements here so this one is a bit tricky for me.
I have two tables: sold and product. Sold(SID, UPC, Date) Product(UPC, Name, Brand)
I need to find in how many stores(sid) does one brand outsell another brand.
I was thinking it was something like:
select count(*) from sold natural join product
where count(brand = 'sony') > count(brand = 'samsung');
Clearly that isn't valid however...
SELECT COUNT(*)
FROM (
SELECT SID
FROM Sold
JOIN product
ON product.UPC = Sold.UPC -- otherwise we have a cartesian product
GROUP BY SID -- if we need totals _by store_, we need to group on that.
HAVING SUM(brand='sony') > SUM(brand='samsung')
) Totals.
I have a problem with SQL Select query. I need to count order's which belong to account which has one or more orders which cost equals 1.
Here is the structure:
Could anyone help with select query. The result should be 2. Many thanks for help.
You have to make two nested queries against the table. An outer one that counts the number of orders for an account and an inner one that finds the accounts that have at least one order with cost equals 1.
SELECT Account_ID, COUNT(*)
FROM Orders
WHERE Account_ID IN (SELECT Account_ID FROM Orders WHERE PRODUCT_Cost = 1)
GROUP BY Account_ID
I have the following query:
SELECT
DISTINCT sites.site_id,
sites.site_name,
sites.site_url,
earnings.cust_id
FROM
sites,
earnings
WHERE sites.site_id = earnings.site_id AND sites.site_id IN('8', '1666')
That query gives me very well the information asked. It returns two rows, one for site 8 and another for site 1666, with the information on them from those tables.
Now, I want that the cust_id number be used to select from another table (let's say table customers) where they are stored by id and where other info is such as name, last name, etc.
Basically what I need is to expand that query to extract customer name and last name from the table customers, using the ids obtained.
Same way you got the info from two tables. Add a comma, add the third table name, and add the relationship to your WHERE clause like you did with the first two tables.
SELECT
DISTINCT sites.site_id,
sites.site_name,
sites.site_url,
earnings.cust_id,
customers.name,
customers.last_name
FROM
sites,
earnings,
customers
WHERE sites.site_id = earnings.site_id AND sites.site_id IN('8', '1666') AND customers.id = earnings.cust_id
I think it's clearer to write out the JOINs though:
SELECT
sites.site_id,
sites.site_name,
sites.site_url,
earnings.cust_id,
customers.name,
customers.last_name
FROM
sites
INNER JOIN
earnings
ON
earnings.site_id = sites.site_id
INNER JOIN
customers
ON
customers.id = earnings.cust_id
WHERE
sites.site_id IN (8, 1666)
GROUP BY
sites.site_id