Rows values as distinct comma separated values based on identifier - oracle11g

I have a table as shown below
ID|Name|Address|Pincode
I1|Ramesh|Hyderabad|1234
I2|Bhaskar|india|1234
I2|Bhaskar|srilnaka|124
I3|Prasad|india|1234
I3|Prasad|india|1235
I4|Chandu|malaysia|1236
I4|Veeru|india|1236
I am required only one row for each ID wise and name,address and pincode should be comma separated values of all rows group by ID.
I want to get out put as shown below
ID|Name|Address|Pincode
I1|Ramesh|Hyderabad|1234
I2|Bhaskar|india,srilnaka|1234,124
I3|Prasad|india|1234,1235
I4|Chandu,veeru|malaysia,india|1236
Help me oracle query for getting the desired result

Take a look at LISTAGG function. You just have to group by id and apply this function to name, address and pincode

Related

I have a table to find comma or pipeline present in the columns in teradata...any help would be appreciated.. please suggest generic query

Id Name address
123 mona #13,Jainroad|Mh
Here i have to find commas and pipeline in address column
Similarly i have to apply on different columns
Select * from table
Where col1 like '%,%';

SQLite count number of occurence of word in a string

I want to count number of occurrences of a word in a string for example ,
[{"lastUpdatedDateTime":{"timestamp":1.54867752522E12},"messageStatus":"DELIVERED","phoneNumber":"+916000060000"},{"lastUpdatedDateTime":{"timestamp":1548677525220},"messageStatus":"DELIVERED","phoneNumber":"+916000060000"}]
in above string i want to count no of occurrences of a word 'DELIVERED' here it is 2.
i want to get result 2. pls help me on this. i should have to use only sql query to achieve this.
thanks in advance.
If your table's name is tablea and the column's name is col:
SELECT
(LENGTH(col) - LENGTH(REPLACE(col, '"DELIVERED"', '')))
/
LENGTH('"DELIVERED"') as counter
from tablea
remove every occurrence of "DELIVERED" and subtract the length of the string from the original string and finally divide the result with the length of "DELIVERED"
Assuming your data is in a table something like:
CREATE TABLE example(json TEXT);
INSERT INTO example VALUES('[{"lastUpdatedDateTime":{"timestamp":1.54867752522E12},"messageStatus":"DELIVERED","phoneNumber":"+916000060000"},{"lastUpdatedDateTime":{"timestamp":1548677525220},"messageStatus":"DELIVERED","phoneNumber":"+916000060000"}]');
and your instance of sqlite has the JSON1 extension enabled:
SELECT count(*) AS "Number Delivered"
FROM example AS e
JOIN json_each(e.json) AS j
WHERE json_extract(j.value, '$.messageStatus') = 'DELIVERED';
gives you:
Number Delivered
----------------
2
This will return the total number of matching entries from all rows in the table as a single value. If you want one result per row instead, it's an easy change but the exact details depend on your table definition. Adding GROUP BY e.rowid to the end of the query will work in most cases, though.
In the long run it's probably a better idea to store each object in the array as a single row in a table, broken up into the appropriate columns.

SQLite- Storing multiple values in same column

In SQLite , How would you store the information like this:
id name groups
1 xyz one,two
2 abc one
3 lmn two,three
The groups column may multiple entries. How can we store like that?
The main thing is the multiple values are should be appended.
I'm not sure I understand it correctly but why not store it as delimited string? Something like string1;string2;string3..or use comma instead of semi-colon like you already posted.
Just fetch the row, append the data followed by your delimiter and update the record. When you need the individual entries, just split the string using your delimiter.

Merging two tables and returning value through r script

I am attempting to add a dynamic column to a table in spotfire that is updated using r-script/data functions in order to handle different variable types. When you just insert columns, it does not allow you to change the column from a text value to a string value.
The basic code structure is create a new table by merging the base table with the information table, select a column header to populate the new column from, and return the calculated column values to the base table. Parameters are as follows:
Input Parameters:
Name Type
columnMatch Value
baseTable Table
infoTable Table
Output Parameters (to be added to baseTable)
Name Type
outputColumn Column
Script
newTable <- merge(baseTable,infoTable, by = "uniqueIdentifier")
cnames <- colnames(newTable)
outputColumn <- newTable[,match(colorSelection, cnames, nomatch=1)]
outputColumn
The issue thatI am having is as follows:
The code is not returning the correct value for the correct uniqueIdentifier. Is there a way that I can make the values line up, or sort the table in order to return the correct value for the correct uniqueIdentifier?
Thanks!
Jordan
EDIT: found out how to dynamically refer to column number using match function.

String Aggregation in sqlite

Anyone knows if String Aggregation in sqlite is possible?
If i have an animal column with 5 rows/datas, how can i combine them so that the output would be in one field
'dog','cat','rat','mice','mouse' as animals
Thanks
You're looking for something like the following:
select group_concat(animal) from animals;
This will return something like the following:
dog,cat,rat,mice,mouse
If you don't want to use a comma as the separator, you can add your own separator as a second parameter:
select group_concat(animal, '_') from animals;
which will return:
dog_cat_rat_mice_mouse
I think this will be useful:
group_concat(X)
group_concat(X,Y)
The group_concat() function returns a string which is the concatenation of all non-NULL values of X. If parameter Y is present then it is used as the separator between instances of X. A comma (",") is used as the separator if Y is omitted. The order of the concatenated elements is arbitrary.

Resources