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

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.

Related

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

Oracle SQL Developer - Concat Data

Good Day,
I have a table which has a column which contains numbers only. However due its containing rule data for special IDs it giving back the result in more lines, for example:
you have a bookstore DB where all the costumers have an ID and the books also have their own ID. When you wish to know that how many books have been ordered by each costumer where the result is bugger than 1 it will appear in multiple lines, where the ID of the costumer is the same but in every line the books' IDs are different.
My question is:
What is the proper syntax, or code portion to use to get one line only for each and every costumer, where the book IDs are separated by commas in the same column?
I've tried like this:
BOOK_CONT AS (
SELECT DISTINCT BOOK_ID, LISTAGG(' ('||BOOK_ID||')', '; ')WITHIN GROUP (ORDER BY BOOK_ID) AS BOOKS
FROM BOOK_LIST)
You would use group_by and listagg(). Assuming that your table has columns customer_id and book_id, you would go:
select
customer_id,
listagg(book_id, ', ') within group(order by book_id) all_book_ids
from book_list
group by customer_id

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

How to insert a count column into a sql query

I need the second column of the table retrieved from a query to have a count of the number of rows, so row one would have a 1, row 2 would have a 2 and so on. I am not very proficient with sql so I am sorry if this is a simple task.
A basic example of what I am doing would be is:
SELECT [Name], [I_NEED_ROW_COUNT_HERE],[Age],[Gender]
FROM [customer]
The row count must be the second column and will act as an ID for each row. It must be the second row as the text file it is generating will be sent to the state and they require a specific format.
Thanks for any help.
With your edit, I see that you want a row ID (normally called row number rather than "count") which is best gathered from a unique ID in the database (person_id or some other unique field). If that isn't possible, you can make one for this report with ROW_NUMBER() OVER (ORDER BY EMPLOYEE_ID DESC) AS ID, in your select statement.
select Name, ROW_NUMBER() OVER (ORDER BY Name DESC) AS ID,
Age, Gender
from customer
This function adds a field to the output called ID (see my tips at the bottom to describe aliases). Since this isn't in the database, it needs a method to determine how it will increment. After the over keyword it orders by Name in descending order.
Information on Counting follows (won't be unique by row):
If each customer has multiple entries but the selected fields are the same for that user and you are counting that user's records (summed in one result record for the user) then you would write:
select Name, count(*), Age, Gender
from customer
group by name, age, gender
This will count (see MSDN) all the user's records as grouped by the name, age and gender (if they match, it's a single record).
However, if you are counting all records so that your whole report has the grand total on every line, then you want:
select Name, (select count(*) from customer) as "count", Age, Gender
from customer
TIP: If you're using something like SSMS to write a query, dragging in columns will put brackets around the columns. This is only necessary if you have spaces in column names, but a DBA will tend to avoid that like the plague. Also, if you need a column header to be something specific, you can use the as keyword like in my first example.
W3Schools has a good tutorial on count()
The COUNT(column_name) function returns
the number of values (NULL values will not be counted) of the
specified column:
SELECT COUNT(column_name) FROM table_name;
The COUNT(*) function returns the number of records in a table:
SELECT COUNT(*) FROM table_name;
The COUNT(DISTINCT column_name) function returns the number of
distinct values of the specified column:
SELECT COUNT(DISTINCT column_name) FROM table_name;
COUNT(DISTINCT) works with ORACLE and Microsoft SQL Server, but
not with Microsoft Access.
It's odd to repeat the same number in every row but it sounds like this is what you're asking for. And note that this might not work in your flavor of SQL. MS Access?
SELECT [Name], (select count(*) from [customer]), [Age], [Gender]
FROM [customer]

Select 2 distinct columns in 4GL

Needed for my 4gl program:
Let's say I have a table that holds a phone number and a name. There can be 2 people with the same phone number, or 2 names with 1 phone number.
I need to select just 1 of each phone number in the table.
I did:
SELECT DISTINCT phone_number, last_name FROM table
The results will show 2 records. Even phone number is the same, since the names are different it is no longer unique. How can I get a unique phone number regardless of its last_name? (But I want to get the last name as well. I don't care which one)
DISTINCT, as you've noticed, will return rows that are distinct in their entirety.
It sounds like you're looking for something like group by. Essentially, GROUP BY phone_number will return one row for each phone number. Because you also want to get last_name, you'll need to instruct the database how you want it to be returned. You said you don't care which so you could simply write:
SELECT phone_number, MAX(last_name) as last_name
FROM table
GROUP BY phone_number
Informix also supports a FIRST_VALUE aggregate function although I've only used that in OLAP situations so I don't recall if it will work in this context.
If you don't care which last name, then try this out:
SELECT phone_number,
MAX(last_name) AS last_name
FROM table
GROUP BY phone_number