How to combine unique values into count function

时间:2016-04-04 16:56:12

标签: oracle

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

2 个答案:

答案 0 :(得分:0)

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";

答案 1 :(得分:0)

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.