I have a report that I am trying to write for members that counts the total number of unique values for each type on one line. Here is what I have now:
Member Name Letter Type
John Doe Member 7 Day Letter
Jane Doe Provider 7 Day Letter
Jane Doe Member 7 Day Letter
ID Letter Type
1001 Member 7 Day Letter
1002 Provider 7 Day Letter
How do I get the following output:
Member Name Letter Sent
John Doe 1
Jane Doe 2
This will give you the output you want.
SELECT
"Member Name",
COUNT(DISTINCT "Letter Type") as "Letter Sent"
FROM <your-table>
GROUP BY "Member Name"
Also, I would advise you to go through the Oracle docs or follow some of the tutorials online, if you plan on using Oracle SQL more. These are really basic operations that should be covered by any decent tutorial.
probably you need only count and group by:
SELECT "Member Name"
, "Letter Type"
, COUNT(1) AS n
FROM your_table
GROUP BY "Member Name"
, "Letter Type";
Related
I have this query which works without problems in Mysql 5.* but I recently upgraded to MySQL 8 and now the query throws a syntax error as follows:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'row_number, #breed'
The query is (lots of useless detail removed for simplicity):
SELECT `name`, `age`, breed
FROM (
SELECT
`dogs`.`name`,
`dogs`.`age`,
#row_number:=CASE WHEN #breed=breed
THEN #row_number+1
ELSE 1
END AS row_number
, #breed:=breed AS breed
FROM `dogs` /* other details with joins, subqueries and limits left out for simplicity*/;
breed is supposed to maintain a row count so I can get n rows for rows grouped by breed. That is, if n=2, for example, my result would be:
name | age | breed
------------------
fifi | 2 | labrador
bingo | 5 | labrador
rocket | 1 | german shepherd
sky | 1 german shepherd
My main question is why I get the syntax error. Google, is my friend, but not in this case... I tried. I also tried removing "as", adding brackets around the case/when/then/end but no joy!
I'm not certain that MySQL 8 supports user variables in the same way as MySQL 5.x does. In any case, your current syntax is at least deprecated, and you should just be using the ROW_NUMBER analytic function. For example, assuming you wanted the two youngest animals per breed, you could try:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY breed ORDER BY age) rn
FROM dogs
-- maybe joins here
)
SELECT name, age, breed
FROM cte
WHERE rn <= 2;
I have 2 sqlite3 tables :
FND is a Table of names and their likely gender i.e.:
nm,gndr <-column names
Aliyah,F
Moses,M
Peter,M
Members is second table i.e.
Fname,Lname <-column names
DAVID X, BAKER
MARY MIA,MCGEE
TINA HEATHER,JOHNSON
JIM PETER TOM, SANTINO
The members table has first and middle names in the fname column.
I am trying to write a query to list the Members table fnames column, with a generated column indicating gender based on the first word in the fname column.
I tried this but it didn't work:
select m.fname,(select gndr from FND where upper(nm) like m.fname||'%')as gender
from Members m
can anyone correct my sql statement?
... upper(nm) like m.fname||'%'
Let's look at some example values:
nm: 'David'
fname: 'DAVID X'
SQL: 'DAVID' LIKE 'DAVID X%'
This obviously does not match.
You have to reverse the LIKE operands:
m.fname LIKE nm||'%'
I have been asked this question;
You list county names and the surnames of the representatives if the representatives in the counties have the same surname.
and I have the following tables;
***REPRESENTATIVE***
REPI SURNAME FIRSTNAME COUNTY CONS
---- ---------- ---------- ---------- ----
R100 Gorege Larry kent CON1
R101 shneebly john kent CON2
R102 shneebly steve kent CON3
I cant seem to figure out the correct way to ask Orical to display a surname that exists more then twice and the surnames are in the same country.
I know how to ask WHERE something = something, but that's doesn't ask what I want to know.
It sounds like you want to use the HAVING clause after doing a GROUP BY
SELECT surname, county, count(*)
FROM you_table
GROUP BY surname, county
HAVING count(*) > 1;
If you really mean "more than twice" as you wrote, none of the data you'd want HAVING count(*) > 2 but then none of your sample data would be returned.
In words, this SQL statement says
Group the data into buckets by surname and county. Each distinct combination of surname and county is a separate bucket.
Count the number of rows in each bucket
Return those buckets where there are at least two rows
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.