I have a mysql table with the fields id, name, address.
Some entries have identical names but different addresses; some have identical addresses but different names.
I want to get a list including one entry at most for each unique address and one entry at most for each unique name.
If I could do a group by name then put the results in a temp table and then select again and do a group by address, then I would get exactly what i want - but that seems too complicated.
And doing a "group by name,address" will not give me what I am looking for - that would include entries for all different combinations of name,address whereas I want one entry for each name and one for each address. Thanks for your help!
There's probably a nicer way to do this, and I'm making some assumptions about what you want, but you could try something like this:
SELECT Name, '' AS Address, Count(Address) AS RowCount
FROM TableName
GROUP BY Name
UNION
SELECT '' AS Name, Address, Count(Name) AS RowCount
FROM TableName
GROUP BY Address
Related
I have a table of invoice line items. Each line item can originate from one of several possible sources, e.g., client_id, entity_id, vendor_id, etc. Each one of these id's would match the primary key of another table, in which we would find the name and other info. I am trying to run a report of invoice line items. I want that report to include a value called "Source Name" for each line item that would display the corresponding name. So, if the source was the client it would show the client's name, if the source was the vendor it would show the vendor's name, etc. I would also like to be able to concatenate text to indicate whether it was the client, entity, vendor, etc.
I believe you are looking for UNION. If you have 2 tables you can use union to create one table
SELECT *
FROM (
SELECT client_id AS 'ID_FIELD', client_name AS 'NAME_FIELD', 'client' as 'TABLE_TYPE' FROM client_table
UNION
SELECT vendor_id AS 'ID_FIELD', vendor_name AS 'NAME_FIELD', 'vendor' as 'TABLE_TYPE' FROM vendor_table
) AS t1
WHERE t1.ID_FIELD = 'YOUR_VALUE_HERE'
So I have a table with persons.
This table connects with vehicles with manyToMany.
I need to find how many times does id appear on the table person_vehicle.
How can I do this?
Thanks everybody for asking
Here are two suggestion.
For all the names:
Gives number of times the names are repeated.
SELECT count(name) FROM persons GROUP BY name;
Specifically for the jake:
SELECT count(name) FROM persons WHERE name = "Jake";
What have you tried? Depending on the table, you can do something like:
SELECT * FROM persons WHERE first_name = 'Jake'
Which will return the number of rows with the first name of "Jake"
Alternatively, if you only want one row with the total count of occurrences:
SELECT COUNT(first_name) FROM persons WHERE first_name = 'Jake'
You're looking for GROUP BY.
A table contains multiple columns. You have to decide first which column(s) of the data that you need are in common.
Suppose I have a table address(person_name, plot_no, pin);
If I try to select, for a particulate pin, how many people are living there:
SELECT pin, COUNT(*)
FROM address
GROUP BY pin;
If I try to select, for a particulate plot_no and pin, how many people are living there:
SELECT plot_no, pin, COUNT(*)
FROM address
GROUP BY plot_no, pin;
Use group by statement on the column, on which you are looking for duplicates if the count comes more than 1, it means they are repeating.
Select count(id), name from table_name group by id HAVING count(id) > 1;
Only the duplicates records will come.
Try this:
SELECT count(name) FROM persons WHERE name = "Jake";
I have a Table containing columns Email, Ip, State, City, TimeStamp, Id
I need to count where Email and IP are distinct, group by State
So when I run a MYSQL query,
select State, City ,count(distinct( Email )), count(DISTINCT( IP))
from table
group by Stat, City
It gives me distinct of each, but not AND
I need a count of distinct Email && Distinct IP ; grouped by State, City.
And distincts cant be within the Group, it has to be the 1st instance of EMAIL, and first instance of IP in entire database. So if i expand it, and add a date parameter, even though im selecting a specific date, I still can check whole database for the uniques.
So if i need
select state, city, count ( distinct ( IP ) , count ( distinct ( EMAIL ))
from table
where timestamp > date(2014-12-01)
group by state, city
What type of query is this? And how can I accomplish this?
My gut tells me i need to do CONCAT as suggested, but also another select inside. So select whole database distinct ip, then select that specific criteria from the other select.
This can help a bit to have a "distinct(A && B)"
SELECT DISTINCT(CONCAT(A,'_',B)),C,D
FROM table
GROUP BY C,D
We struggled to do this on a production server and found the query required was too resource intensive. So we created a table with an update on first instance the item occurs, then we check for counts with a join like so:
select count(a.State) from tablename A
inner join table_update U
on a.id = u.id
WHERE a.parameters..
and c.first_email = 1
and c.first_ip = 1
We couldnt find a single table that wouldnt bring our server down with 400,000 records. Its not a classy answer, but its what we had to use.
In my table I've got a list of names, email addresses and a column called referrer. A users name and email is entered multiple times into the table for a contest. The referrer column tells me how the contestant heard about the contest.
How do I get a recordset of contestants that shows if the referrer is web and/or email but only choose one record for each? For example, a contestant could be entered 10 times under email and 100 times under web. What I'm trying to do is get just a single instance of each record.
This is my current query:
select * from table_contestants
where referrer IN ('email', 'web') GROUP BY email order by referrer;
Of course, when I use GROUP BY it only gives me one instance of the record regardless of whether it's found to have email or web.
Removing the GROUP BY will give me all the instances, which is a lot...
How do I write the query to give me one record of each contestant that has both email and web?
There is various solution depending on your need:
If you just need distinct (name, email) pairs regardless of referer, use the DISTINCT keyword:
SELECT DISTINCT name, email FROM tbl
WHERE referer IN ("email", "web");
If you need distinct (name, email, referrer) tuple, the same solution applies:
SELECT DISTINCT name, email, referer FROM tbl
WHERE referer IN ("email", "web");
As an extension to SQL, MySQL allow in the SELECT clause reference to non-aggregate column not referenced in the GROUP BY clause. In that case, the value of the non-aggregate column is undetermined (understand: "is one of the values of the group" and "the result is un-reproductible").
http://dev.mysql.com/doc/refman/5.0/en/group-by-extensions.html
So if you need distinct (name, email) pairs and one origin without preference, the following query will work with MySQL:
SELECT DISTINCT name, email, referer FROM tbl
WHERE referer IN ("email", "web")
GROUP BY name, email;
See http://sqlfiddle.com/#!2/85292/5 for a comparison;
Just use DISTINCT...
select distinct(referrer) as user from table_contestants
where referrer IN ('email', 'web') GROUP BY email order by referrer;
Can someone "break down" the syntax here. Please. I need to learn this ASAP.
From my limited experience -
firstname and lastname are columns and list is a table.
count(id)>1 is used to check if there is more than one row with the same...
That's it. I don't know what this does but I need to understand it.
SELECT firstname, lastname, list.address FROM list
INNER JOIN (SELECT address FROM list
GROUP BY address
HAVING count(id) > 1) dup
ON list.address = dup.address
This query will return a list of all names (first and last name), which contain a duplicate address. This part
SELECT address FROM list
GROUP BY address HAVING count(id) > 1
Gets a list of all the addresses that occur more than once in the table, This is then joined back to the table itself, to return all names which have the same address. This should return a list of all the unique address which have more than 1 name associated with them, along with the names that go along with the addresses.