Hello I am trying create comma separated list for a group of id's but not bale to do since Netezza doesn't have any Sting_Agg,LISTAGG,or group_CONCAT which can convert, is there any way through SQL i can get the result.
Data
ID Attribute
1 test
1 abc
1 test2
1 test3
2 abc
2 test
3 test2
Result i want
ID List_of_Attribute
1 test,abc,test2,test3
2 abc,test,test2
i tried this but it gives only one from the list no all values, i need without any string function since netezza doesn't have string aggregation function.
SELECT a.AC9,
MAX(CASE a.RNO WHEN 1 THEN a.value ELSE '' END) ||
MAX(CASE a.RNO WHEN 2 THEN ', '||a.value ELSE '' END)
FROM (SELECT AC9, value, ROW_NUMBER() OVER (PARTITION BY AC9 ORDER BY value) RNO FROM table) a
GROUP BY a.AC9
Any help would be appreciated
Related
I have a STRING column with a LIST [,,] of JSONS that I would like to UNNEST into separate lines.
For example:
ROW TICKET_ID Subject UPDATES(STRING)
1 1 Need help... [{"Actor":"Tom","Type":"Request"}, {"Actor":"John","Type":"Update"}]
2 2 Something... [{"Actor":"Kate","Type":"Request"}, {"Actor":"Tim","Type":"Update"}]
I would like it to look like:
ROW TICKET_ID SUBJECT UPDATE
1 1 Need help... {"Actor":"Tom","Type":"Request"}
2 1 Need help... {"Actor":"Tom","Type":"Request"}
3 2 Something... {"Actor":"Kate","Type":"Request"}
4 2 Something... {"Actor":"Kate","Type":"Request"}
I have tried using JSON_EXTRACT_ARRAY() and CROSS JOIN UNNEST() so far but unable to split the updates into separate lines as the updates appear as separate rows within the same row (array)
Use below
select * except(updates)
from your_table,
unnest(json_extract_array(updates)) update
if applied to sample data in your_question - output is
I have been given a task to develop a script/ function/ query to aggregate groups of rows in a table and then search for specific keywords in it. The column to be aggregated is a varchar2 column with size 3200 and some of the aggregated rows have lengths way beyond 5000.
(I understand that the size of varchar2 is 4000)
When I try to aggregate the data into a single column, it gives a "result of string concatenation is too long" error (ORA-01489)
I have tried inbuilt aggregators like LISTAGG, XMLAGG, and also some custom functions but I have been asked to prefer a SQL query over a function or procedure.
Once I can get the data to be aggregated, I have to then search through the rows for matching keywords.
(can't just search the rows without aggregating as some of the words are split across the rows, eg row1 ends with "KEYW" and row2 starts with "ORD" if I need to look for "KEYWORD" in the table
my table kind of looks like this (can't post the real table data, sorry),
id_1 | id_2 | name | row_num | description
1 5 A 0 this has so
1 5 A 1 me keyword
1 5 B 0 this is
1 3 E 0 new some
2 12 A 0 diff str
here the unique rows are identified using the first 3 columns and the 4th column lists the order in which these "description" strings need to be concatenated.
I would like to get the output as:
id_1 | id_2 | name | description (concated)
1 5 A this is **some** keyword
1 3 E new **some**
when looking for the keyword "some"
Please help as I am fairly new to DBs and any help will be highly appreciated.
Thanks & Regards
Kunal
I have a column that stores 2 values. Example below:
| Column 1 |
|some title1 =ExtractThis ; Source Title12 = ExtractThis2|
I want to remove 'ExtractThis' into one column and 'ExtractThis2' into another column. I've tried using a substring but it doesn't work as the data in column 1 is variable and therefore it doesn't always carve out my intended values. SQL below:
SELECT substring(d.Column1,13,24) FROM dbo.Table d
This returns 'Extract This' but for other columns it either takes too much or too little. Is there a function or combination of functions that will allow me to split consistently on the character? This is consistent in my column unlike my length count.
select substring(col1,CHARINDEX('=',col1)+1,CHARINDEX (';',col1)-CHARINDEX ('=',col1)-1) Val1,
substring(col1,CHARINDEX('=',col1,CHARINDEX (';',col1))+1,LEN(col1)) Val2
from #data
there is duplicate calculation that can be reduced from 5 to 3 to each line.
but I want to believe this simple optimization done by SQL SERVER.
Table Structure is like this
Table Name:Employee
**Employee_Id APPRS_TY_CD**
540589 2
540589 UNK
1952938 2
1952938 UNK
2488178 1
2488178 UNK
3818934 1
3818934 UNK
5402944 1
If a Employee ID has APPRS_TY_CD as (UNK AND a value) then APPRS_TY_CD should be a value and not UNK. If APPRS_TY_CD is not UNK for an Employee ID then that value should be populated as it is.
My final output should look like this.
**Employee_Id APPRS_TY_CD**
540589 2
1952938 2
2488178 1
3818934 1
5402944 1
I'm using MS Access.
This should be fairly simple as numbers are considered "lower" than strings you can use an aggregate function, I've created an SQL Fiddle here (note this is Sql Server but the code should be the same as it's not using proprietary features). Given your data you could use the MIN function from SQL to get the APPRS_TY_CD for each user. Here is my suggested code:
SELECT
Employee_Id
, MIN(APPRS_TY_CD) APPRS_TY_CD
FROM
Employee
GROUP BY
Employee_Id
The results returned are (you should be able toe execute the fiddle yourself to prove this):
EMPLOYEE_ID APPRS_TY_CD
540589 2
1952938 2
2488178 1
3818934 1
5402944 1
I have a column that has multiple numbers separated by a comma. Example for a row:
`numbers`:
1,2,6,66,4,9
I want to make a query that will select the row only if the number 6 (for example) is in the column numbers.
I cant use LIKE because if there is 66 it'll work too.
You can use like. Concatenate the field separators at the beginning and end of the list and then use like. Here is the SQL Server sytnax:
where ','+numbers+',' like '%,'+'6'+',%'
SQL Server uses + for string concatenation. Other databases use || or the concat() function.
You should change your database to rather have a new table that joins numbers with the row of your current table. So if your row looks like this:
id numbers
1 1,2,6,66,4,9
You would have a new table that joins those values like so
row_id number
1 1
1 2
1 6
1 66
1 4
1 9
Then you can search for the number 6 in the number column and get the row_id