Count numbers of records for member id - mysql

I am working on a little project for one of my customer. Basically I want to count the number of invoice per member ID. Each row of my tblinvoice has a column called member_id.
I have a gridview setup and stuff but I really have no clue how to count them.
like :
SELECT * FROM tblinvoices WHERE member_id=#membered
what would be the correct MySQL CMD?
Cheers,
Pierre

Try this
SELECT count(*) as total FROM tblinvoices WHERE member_id=#membered

SELECT count(member_id) as total FROM tblinvoices WHERE member_id=#membered

#Krish R comment should work.
But if what you want is to get a list of all members (you said sth about a gridview...), and for each of them, get the number of invoices:
SELECT member_id, count(*) as totalno
FROM tblinvoices
GROUP BY member_id

You can use Count in your sql query to get the ouptut....
SELECT count(*) as tt FROM tblinvoices WHERE member_id=#membered

Related

mysql group by multiple

I'm not sure if this is specifically a group by question, as I've tried grouping this by multiple columns. Basic problem is a table like this:
I would like to get the sum of the order total_price for different countries, but grouped by the order_id. So for France the sum of total_price should be 8000 as two of the rows are for the same order. My sql is clearly wrong as I am not getting this.
SELECT sum(total_price) as total_price_per_country
FROM cars
WHERE (country IN ('France'))
group by order_id, country;
Nope, GROUP BY won't quite get you that. If you GROUP BY country you get one row per country.
There's no way to do this without a second query, so it'd have to be something like this:
SELECT *, (SELECT SUM(total_price) FROM cars AS c2 WHERE c2.country = cars.country) AS total_price_per_country
FROM cars
(An INNER JOIN to a second copy of the table would work too.)
select sum(total_price) from cars where country = 'France' group by order_id
Try above query

Calculate in SQL with Inner Join

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.

Count specific occurence using SQL

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

MySQL Group two column with where clause on both two group

What I have:
I have two table , first is user_faktorha save invoices data and second is u_payment save payment data .
What I want:
I want to group all data from this two table and have a result as one table with sum both table.
My two table with sample query's is on sqlfiddle : http://sqlfiddle.com/#!2/b9f9e/4
What's problem:
I try to solve this problem , but give wrong result each time , for example (can be see on sqlfiddle) , user/tell named as habib on give wrong sum(price) result.
habib's faktorhaprice = -508261 and habib's paymentprice = 648000 but sum result in main query have wrong data -7115654 and 13000000
what's the solution ?
(Updated) One way:
SELECT tell,SUM(FAKTORHAPRICE) FAKTORHAPRICE, SUM(PaymentPrice) PaymentPrice
FROM (SELECT tell, price as FAKTORHAPRICE, null PaymentPrice
from user_faktorha
union all
SELECT Username as tell, null as FAKTORHAPRICE, Price as PaymentPrice
FROM `u_payment` WHERE Active='1') sq
GROUP BY tell ORDER BY FAKTORHAPRICE ASC;
SQLFiddle here.
The essence of your problem here is that you are trying to relate to unrelated tables. Sure they have common data in the user name, but there is not a clean relation between them like an invoice id that can be used to relate the items together such that the OUTER JOIN wouldn't duplicate records in your result set. My suggestion would be to do the aggregation on each table individually and then join the results like this:
SELECT f.tell, f.faktorhaprice, p.paymentprice
FROM
(SELECT tell, SUM(price) AS faktorhaprice FROM user_faktorha GROUP BY tell) AS f
INNER JOIN
(SELECT username, SUM(price) AS paymentprice FROM u_payment GROUP BY username) AS p
ON f.tell = p.username

Prevent duplicates in mysql group by with join statement

I've got a problem that can't be new, but I can't figure out how to get the answer I want. It is probably something simple that I'm missing
Using mysql 5.5, I have 2 tables, 'referrals' and 'status'. I want to count referrals that have been cancelled, grouped by appt_date:
SELECT SUM(1) AS count, appt_date FROM referrals
GROUP BY appt_date
JOIN status ON referrals.id=status.referral_id
WHERE status.status_name="cancelled"
This works fine until I have a referral that gets cancelled twice. In other words, a referral with a given id that has 2 rows in the status table with matching referral_id will get counted twice.
How to count each referral record only once when doing the join operation here?
EDIT:
My real question should have been this:
SELECT SUM(quantity) AS count, appt_date FROM referrals
GROUP BY appt_date
JOIN status ON referrals.id=status.referral_id
WHERE status.status_name="cancelled"
since each referral can have its own quantity.
Try changing count to count(DISTINCT id)
So count was only a label, I should have seen that :)
The temptation is to do SUM(DISTINCT quantity) but obviously will just remove dupe "quantities", which isn't what you want. It looks like you will need to join on a derived select table. Have a look at this...
http://www.sqlteam.com/article/how-to-use-group-by-with-distinct-aggregates-and-derived-tables