Having trouble with Distinct(Coloumn_name) - sql

I'm having problems with getting the correct data from my tables.
I've got a table with information on customers sending letters.
My table have following coloumbs( Name, Postalcode, Country, Phone_number, Letters(The number of letters))
I want to get the numbers of letters send from every customer, depending on the Postal_code.
Select Distinct(Postal_Code), Count(*)
from Table_name
Group by Postal_Code, Letters
The result of this, is not exactly what I wanted, because I get duplicates of the postalcode, and the numbers of letters are wrong.
I'm pretty new to this stuff, so I hope someone can help me.
EDIT:
At the moment, i'm trying to get a date on the record, and my code i slike this now.
Select Postal_Code, Sum(Letters), To_Char(trunc(Start_time),'DD-MM-YYYY') AS StartTime
from Table_name
Group by Postal_Code
But I get an error when running it. The error says: ORA-00937: not a single-group group function.
I have googlet the problem, and tried with my start_time in the group by, but this gives me the wrong result.

If you use GROUP BY you don't need DISTINCT. Since letters is a number you need to SUM() them. So total letters for each postal code would be something along the lines of:
Select Postal_Code, SUM(letters)
from Table_name
Group by Postal_Code

GROUP BY gives you distinct values for all the columns in the clause. So grouping by POSTAL_CODE, LETTERS is why you get duplicate POSTAL_CODE values.
Not sure I completely understand what you're trying to do, but here is my interpretation:
Select Postal_Code
, sum(letters) no_of_letters
, Count(*) no_of_customers
from Table_name
Group by Postal_Code

Related

CONCAT values in specific cells based off values in a different column

I need help and apologies in advance for not including a code because I do not know where to even start.
I work as a reporting analyst and have a table full of customer data + customer remarks. I need to contact the customer remarks and cannot think of a way to do so.
My table has 7 columns and I need to concatenate the REMARK_TEXTS rows before sending the report off to our internal clients.
The remarks are based on the REMARKS_SEQ_NBR column and the sequence numbers aligned with the REMARK TEXTS. I am including a screenshot of an excel mockup I created.
This is my query using LISTAGG():
SELECT DISTINCT
AA.ID_NUMBER,
AA.LAST_NAME,
AA.FIRST_NM,
AA.DATE_OF_BIRTH,
AA.CUSTOMER_SINCE,
AA.REMARKS_SEQ_NBR
LISTAGG(‘REMARK_TEXTS’
) WITHIN GROUP (
AA.ID_NUMBER
)
AS CUSTOMERS
FROM CUSTOMER_CURRENT
GROUP BY ID_NUMBER
ORDER BY ID_NUMBER
Thank you in advance!
enter image description here
You're trying to combine multiple rows into one, so you need an aggregate function. For concatenating text, that's LISTAGG. You have it pretty much right:
select id_number, last_name, first_name, date_of_birth, customer_since
listagg( trim( remark_texts ), ' ' ) within group ( order by remarks_seq_nbr )
as customers
from customer_current
group by id_number, last_name, first_name, date_of_birth, customer_since
order by id_number
I'm trimming the text and using space as a separator, so you don't wind up with CUSTOMER PREFERS DONOT CALL.

Count how many employees with the same name exist in the database in PostgreSQL

I have a data table of employees, I want to get how many same name employees in the database. Name information is saved as first_name and last_name. I have tried.
Select count(concat(first_name,'',last_name) as empname, (concat(first_name,'',last_name) as empname from xyz.
getting error.
Your SQL is missing some parentheses (brackets), and to use an aggregation function as well as a non-aggregated column, you must include the non-aggregated column in a GROUP BY
So your SQL should look like this:
Select count(concat(first_name,' ',last_name)) as countempname,
(concat(first_name,' ',last_name)) as empname
from xyz
GROUP BY (concat(first_name,' ',last_name));
Notice also that I added a space. It is a bit odd to include an empty string in the concat. Also as a short form, if you do not want to repeat the concat in the GROUP BY, you can replace it with the column ordinal number (in this case 2) so it becomes:
Select count(concat(first_name,' ',last_name)) as countempname,
(concat(first_name,' ',last_name)) as empname
from xyz
GROUP BY 2;
If you want to count how many times each first/last name tuple appears in the table, you can use aggregation:
select first_name, last_name, count(*) cnt
from xyz
group by first_name, last_name
This gives you one row per first/last name tuple, with the total count. Note that there is not point concatenating the variables; group by can operate of column tuples.
On the other hand, maybe you want the entire employee row, with an additional column that holds the total count of other rows having the same names. If so, you can use a window count instead of aggregation:
select x.*, count(*) over(partiton by first_name, last_name) cnt
from xyz

In one query, get length of last name, length of first name and group by lengths - three columns

I'm a noob...and working on a homework problem.
Have a members table with last_name, first_name. I want to write one query that aggregates the length of last_name, first_name by string length.
The output would have three columns - name_length, count_of_last_names, count_of_first_names.
Two individual queries:
`SELECT LENGTH(last_name) AS "LnameLen", COUNT(LENGTH(last_name)) AS "CntLnameLen"
FROM members
GROUP BY LENGTH(last_name)
ORDER BY "LnameLen" DESC;
SELECT LENGTH(first_name) AS "FnameLen", COUNT(LENGTH(first_name)) AS "CntFnameLen"
FROM members
GROUP BY LENGTH(first_name)
ORDER BY "FnameLen";`
But I want to write one query that outputs what the above two queries do, so the output would be three columns: Length_of_Name, CntLnameLen, CntFnameLen.
Suggestions? Thanks!
The way I understood the question, something like this might be what you're looking for.
select length(first_name),
length(last_name),
count(*)
from members
group by length(first_name),
length(last_name);

using count and group by correctly

I have a relation CandyC(id, email, age, name, candy_id)
I want to count the CandyC.ids associated with a CandyC.candy_id once.
Attempt:
SELECT email, age, name
FROM CandyC
GROUP BY id
HAVING COUNT(DISTINCT candy_id) = 1;
It gives me an error:
not a group by expression
The group by clause need to have all the non aggregated columns selected directly. Also, it's usually a good idea to use having after the group by as it's the standard way of writing this (even though Oracle supports it the other way too).
Does this do what you want:
select email, age, name
from candyc
group by id, email, age, name
having count(distinct candy_id) = 1
If not, you should provide sample data and expected results in your question to clarify.
I think you want something more like this:
SELECT candy_id, COUNT(*)
FROM CandyC
GROUP BY candy_id;
I don't know what the email/age/name columns have to do with the question:
I want to count the CandyC.ids associated with a CandyC.candy_id once.

sql query for the following statement?

My table 'Customer' contains customerid , firstname, lastname, company,city,state,country, email, invoicetotal
Question: For countries that have at least two customers using yahoo as email provider, display the name alongside the revenue
My solution:
select county,sum(invoiceTotal)from customer where email like '%yahoo%'
group by Country,Email having Count(Country)>2
I am unable to get proper result the no of rows displayed in my output are different from the number of rows in expected output,Can any1 tell me where have i gone wrong???
You are grouping by email as well - just group by Country and you should be fine
select
county,
sum(invoiceTotal)
from customer
where email like '%yahoo%'
group by Country
having Count(Country)>2
You can't group by email, since that's unique. Fortunately, you don't have to.
select
county,
sum(invoiceTotal)
from customer
where email like '%yahoo%'
group by Country
having Count(Country)>=2
Because the statement say at least then you need put >=.