Say I've got a list of names in column A.
Chandler
Ross
Monika
Joey
Phoebe
Rachel
Ross
Monika
Joey
Ross
Monika
Joey
Entering the below formula, produces the below result
=query(A:A, "select A, count(A) group by A")
count
Chandler
1
Joey
3
Monika
3
Phoebe
1
Rachel
1
Ross
3
Can I somehow make the below formula work and produce the below table?
=query(A:A, "select A, count(A) where count(A)>2 group by A")
count
Joey
3
Monika
3
Ross
3
You can't add aggregate function to WHERE clause, so using one query it is not possible. But you can add another QUERY:
=QUERY(query(A:A, "select A, count(A) group by A"),"SELECT * WHERE Col2>2")
Related
We have sheet with column names and values in the cells below.
We like to have a list of the names and the value next to it ordered.
example.
A
B
C
D
E
1
John
Mary
Tom
Grace
2
3
4
5
2
and we would like the same data below which looks like...
A
B
1
Tom
5
2
Mary
4
3
John
3
4
Grace
2
Any ideas? Thanks
use:
=SORT(TRANSPOSE(A1:D2), 2, )
or:
=SORT(TRANSPOSE({A1:D1; A4:D4}), 2, )
SUGGESTION
Perhaps you can try this way:
=QUERY(TRANSPOSE(A1:D2),"SELECT * order by Col2 DESC")
Sample Sheet
Reference
TRANSPOSE
QUERY
I have a table 'relationships' like below
id | user_1_nick | user_2_nick
1 peter kevin
2 jake peter
3 peter abby
4 aaron abby
5 abby kevin
So if Peter is logged in his 'friends' will be shown as below
1 abby
2 jake
3 kevin
And Abbys 'friends' as below
1 aaron
2 kevin
3 peter
I hope you get me.
Thanks in advance.
UPDATE
My current query looks like this:
SELECT * FROM relationships AS r
LEFT JOIN members AS m
ON (r.user_2 = m.hash AND r.user_2 != '$userhash')
OR (r.user_1 = m.hash AND r.user_1 != '$userhash')
WHERE (user_2 = '$userhash' OR user_1 = '$userhash')
AND accepted = '1'
In MySQL
You have to filter the friends in both directions and merge the results as illustrated below.
So the friends of peter could be fetched with the query:
SELECT A.FRIENDS FROM
(SELECT user_2_nick friends FROM relationships
WHERE user_1_nick='peter'
UNION
SELECT user_1_nick friends FROM relationships
WHERE user_2_nick='peter') A
ORDER BY A.FRIENDS;
And for abby with:
SELECT A.FRIENDS FROM
(SELECT user_2_nick friends FROM relationships
WHERE user_1_nick='abby'
UNION
SELECT user_1_nick friends FROM relationships
WHERE user_2_nick='abby') A
ORDER BY A.FRIENDS;
Here is the SQL FIDDLE DEMO
In the school assignment I'm working on I need to display the 3 criminals with the most crimes. But I'm having a few problems
Here's the code I have so far, and its output:
`Select Last, First, Count(Crime_ID)
From Criminals Natural Join crimes
Group by Last, First, Criminal_ID
order by Count(Crime_Id) Desc`
`LAST FIRST COUNT(CRIME_ID)
--------------- ---------- ---------------
Panner Lee 2
Sums Tammy 1
Statin Penny 1
Dabber Pat 1
Mansville Nancy 1
Cat Tommy 1
Phelps Sam 1
Caulk Dave 1
Simon Tim 1
Pints Reed 1
Perry Cart 1
11 rows selected `
I've been toying around with ROWNUM, but when I include it in the SELECT it won't run because of my GROUP BY. But If you put ROWNUM in the GROUP BY it just separates everything back out.
I just want to display the top 3 with the most crimes, which is weird because only 1 guy has more than 1 crime. Theoretically, more criminals would be added to the Database, but these are the tables given in the assignment.
select *
from
( Select Last, First, Count(Crime_ID)
From Criminals Natural Join crimes
Group by Last, First, Criminal_ID
order by Count(Crime_Id) Desc )
where ROWNUM <= 3;
Let's say I have table data similar to the following:
123456 John Doe 1 Green 2001
234567 Jane Doe 1 Yellow 2001
234567 Jane Doe 2 Red 2001
345678 Jim Doe 1 Red 2001
What I am attempting to do is only isolate the records for Jane Doe based upon the fact that she has more than one row in this table. (More that one sequence number)
I cannot isolate based upon ID, names, colors, years, etc...
The number 1 in the sequence tells me that is the first record and I need to be able to display that record, as well as the number 2 record -- The change record.
If the table is called users, and the fields called ID, fname, lname, seq_no, color, date. How would I write the code to select only records that have more than one row in this table? For Example:
I want the query to display this only based upon the existence of the multiple rows:
234567 Jane Doe 1 Yellow 2001
234567 Jane Doe 2 Red 2001
In PL/SQL
First, to find the IDs for records with multiple rows you would use:
SELECT ID FROM table GROUP BY ID HAVING COUNT(*) > 1
So you could get all the records for all those people with
SELECT * FROM table WHERE ID IN (SELECT ID FROM table GROUP BY ID HAVING COUNT(*) > 1)
If you know that the second sequence ID will always be "2" and that the "2" record will never be deleted, you might find something like:
SELECT * FROM table WHERE ID IN (SELECT ID FROM table WHERE SequenceID = 2)
to be faster, but you better be sure the requirements are guaranteed to be met in your database (and you would want a compound index on (SequenceID, ID)).
Try something like the following. It's a single tablescan, as opposed to 2 like the others.
SELECT * FROM (
SELECT t1.*, COUNT(name) OVER (PARTITION BY name) mycount FROM TABLE t1
)
WHERE mycount >1;
INNER JOIN
JOIN:
SELECT u1.ID, u1.fname, u1.lname, u1.seq_no, u1.color, u1.date
FROM users u1 JOIN users u2 ON (u1.ID = u2.ID and u2.seq_no = 2)
WHERE:
SELECT u1.ID, u1.fname, u1.lname, u1.seq_no, u1.color, u1.date
FROM users u1, thetable u2
WHERE
u1.ID = u2.ID AND
u2.seq_no = 2
Check out the HAVING clause for a summary query. You can specify stuff like
HAVING COUNT(*) >= 2
and so forth.
I have two tables, let's call them PERSON and NAME.
PERSON
person_id
dob
NAME
name_id
person_id
name
And let's say that the NAME table has data like:
name_id person_id name
1 1 Joe
2 1 Fred
3 1 Sam
4 2 Jane
5 2 Kim
I need a query (Oracle 10g) that will return
name_id names
1 Joe, Fred, Sam
2 Jane, Kim
Is there a simple way to do this?
Update:
According to the article that figs was kind enough to provide, starting in 9i you can do:
SELECT wmsys.wm_concat(dname) departments FROM dept;
For this example, the answer becomes:
SELECT name_id, wmsys.wm_concat(name) from names group by name_id
The short answer is to use a PL/SQL function. For more details, have a look in this post.