HANA SQL select count and and array for ids - sql

Is there any way to generate a query where I can have 2 fields. First field is get the count and second is to get the names as an array?
Sample Table:
|---------------------|------------------|
| id | name |
|---------------------|------------------|
| 1 | John |
|---------------------|------------------|
| 2 | Doe |
|---------------------|------------------|
And then, I can get results as:
|---------------------|------------------|
| count | array_name |
|---------------------|------------------|
| 2 | ["John", "Doe"] |
|---------------------|------------------|

You can try the below - using STRING_AGG()
SELECT count(id), STRING_AGG(name,',' ORDER BY id)AS array_name
FROM tablename

Related

postgresql & json - counting distinct values

In PostgreSQL, I have a table that looks like,
| id | json |
| -- | ------------------------------- |
| 1 | {"id":1,"customer":"BANK"} |
| 1 | {"id":1,"customer":"BANK"} |
| 2 | {"id":2,"customer":"GOVT"} |
| 3 | {"id":3,"customer":"BANK"} |
| 4 | {"id":4,"customer":"ASSET MGR"} |
| 4 | {"id":4,"customer":"ASSET MGR"} |
I need the output of counting the occurrences of customers with unique ids, such as
| customer | count |
| ----------- | ----- |
| "BANK" | 2 |
| "GOVT" | 1 |
| "ASSET MGR" | 1 |
Is there a good way to achieve using PostgreSQL & json? I currently am able to extract the customer and IDs, but am having difficulty counting the unique json objects.
select count(distinct id), jsondata ->> 'customer' customer
from data
group by customer
count | customer
----: | :--------
1 | ASSET MGR
2 | BANK
1 | GOVT
db<>fiddle here

SQL Server select all max and min data in a column

+----+------+
| id | data |
+-----------+
| 1 | a |
| 2 | b |
| 3 | a |
| 3 | c |
+----+------+
I need to select data [a,b,c].
Yes, got it.
the query is 'SELECT distinct(data) from dataTable'
Thanks.
Do you want the distinct values in the second column?
select distinct data
from t;

SQL Concatenate all values in column B that have same value in column A for all values in column A

I am running PostgreSQL 12.4. I have a relatively large table like the following where column 1 and 2 are both of character varying type:
|---------------------|------------------|
| Column 1 | Column 2 |
|---------------------|------------------|
| foo | X |
|---------------------|------------------|
| foo | Y |
|---------------------|------------------|
| foo | Z |
|---------------------|------------------|
| bar | A |
|---------------------|------------------|
| bar | B |
|---------------------|------------------|
| bar | C |
|---------------------|------------------|
I would like to create something like the following:
|---------------------|------------------|
| Column 1 | Column 2 |
|---------------------|------------------|
| foo | X, Y, Z |
|---------------------|------------------|
| bar | A, B, C |
|---------------------|------------------|
Is there an easy way to do this?
You can use string_agg:
select column1, string_agg(column2, ', ')
from table_name
group by column1
You can find more info here.

Sybase - Split delimited column into multiple rows

I'm still new to SQL and I was wondering if there is a way to split a delimited column into multiple rows.
Here's my table:
|---------------------|------------------|
| ID | Name |
|---------------------|------------------|
| 12 | a##b##c##d |
|---------------------|------------------|
Expected output:
|---------------------|------------------|
| ID | Name |
|---------------------|------------------|
| 12 | a |
|---------------------|------------------|
| 12 | b |
|---------------------|------------------|
| 12 | c |
|---------------------|------------------|
| 12 | d |
|---------------------|------------------|

SQL Concat Id column with another column

Here is what I want to do:
I have this table
+----+-------------+
| id | data |
+----+-------------+
| 1 | max |
| 2 | linda |
| 3 | sam |
| 4 | henry |
+----+-------------+
and I want to Update the data with concatenating Id column with data, which will look like this:
+----+-------------+
| id | data |
+----+-------------+
| 1 | max1 |
| 2 | linda2 |
| 3 | sam3 |
| 4 | henry4 |
+----+-------------+
Sounds like this is basically what you want (T-SQL, Other platforms may have different methods for type conversion and concatenation):
update myTable
set data=data+convert(varchar(50),id)