Alternate to LISTAGG function in Amazon Redshift - amazon-redshift

I using LISTAGG function to convert row values into one single column but facing below error. When searched, when the result rows exceeds more than 65535 [https://docs.aws.amazon.com/redshift/latest/dg/r_LISTAGG.html]. My result set is more 100000 so I get the exception below. What is the alternate to this function? How can I achieve my use case in redshift. Please help.
-----------------------------------------------
error: Result size exceeds LISTAGG limit
code: 8001
context: LISTAGG limit: 65535
process: query18_863_20899937 [pid=66066]```
**My Use Case**
From:
ID dates
00 date00
01 date00
00 date01
00 date02
00 date03
01 date01
**To: [Using LISTAGG function]**
ID dates
00 date00,date01,date02,date03
01 date00,date01
Thanks.

It would be helpful to show the query (or the key parts of it) that you are running but I think I can suggest a direction. LISTAGG() has a "WITHIN GROUP" option and is documented at the link you mention in your post.
Listagg() within group will aggregate the text column only for the values in the group defined in the "GROUP BY" clause. So to meet your deesired output you should be able to do something like:
select ID, listagg(dates) within group as dates
from <table>
group by ID;
The same listagg() limit will exist - it is just that now it is applied to each "group by" group. If this still produces a text string that is too long you will need to break things into more groups. When this happens I will calculate a "subpart" column which increments for every 1,000 rows in the group. This way I can ensure that no group will produce more than 1,000 rows for listagg() to consume.

Related

SQL Server 2012 : query for finding Minimum and Maximum time based on the group and time window

I have a table with date and time data like below.
I want to collect the data of employee/section/date/time window wise min and max date/time which are bold in the above table. Below table is the result that I want to derive from the above table.
Is there any SQL query options are available to get this?
Thanks in Advance
You can use min,maxandgroup by` functions for your desired result:
select EmpId,Section,min(EntryTime)MinEntryTime,Max(EntryTime)MaxEntryTime
from yourtable
group by EmpId,Section

Azure Data Flow filter distinct rows

What I want to achieve is that I have sources which sending me some data, but before saving that data in sink I want to filter that distinct with respect to columns I am not able to find Distinct function in expression functions. Can anyone tell me how to achieve this
Not sure if you still have this problem, I suggest to use the 'Aggregate' component in dataflow, I did a test like below:
in 'Aggregate Settings' we define all the 'Group by' columns and 'Aggregates' columns, the source table have 9 columns in total, and 900 rows in total containing 450 distinct rows plus 450 duplicated rows.
I use max to aggregate the 'ModifiedDate' column, and in sink table there's only 450 distinct rows.
This can be done by manually editing the script (and then linking it together on the UI).
The following snippet does a distinct filtering using all columns:
aggregate(groupBy(mycols = sha2(256,columns())),
each(match(true()), $$ = first($$))) ~> DistinctRows
https://learn.microsoft.com/en-us/azure/data-factory/data-flow-script#distinct-row-using-all-columns

Sum Left Outer Join In Subreport

I am working in a sub report. Visual Studio 2010 if relevant.
My first table has a unique record for every row in the details section. I am then attempting to use a Left Outer Join to join a second table to the first but then sum those values where there may be more than one row in table two per table one.
Right now I am having a problem with row duplication and the values from table two will not sum and instead print out individually. I have tried using groups and tried using Sum formula fields without success.
My first table is unique on a Record Number, Component and Sequence.
My second table also has those three items but may contain no record per the first table, one or multiple.
The Record Number and Component are passed to the report as parameters and are used in select expert. So really Sequence is only a unique record in my first table.
I have tried Group By Record Number, Component and Sequence for Table one. I have also tried Grouping by Sequence for Both Tables. I have tried adding a sum formula and even tried a running total without success.
Is this something that is possible? Is there something I may have missed trying?
Edit:
What I am current getting
Seq T1 Data T2 Data
10 A1 35
20 C2 25
20 C2 15
30 D5 30
40 D6 10
40 D6 50
What I am looking for
Seq T1 Data T2 Data
10 A1 35
20 C2 40
30 D5 30
40 D6 60
I finally figured it out. I grouped by Table 1 Sequence, then Table 2 Sequence (not sure if grouping by both is necessary). Then I used a Running Total Field in Group Footer right after the details section (Group for Table 2) and set the Reset value to On change of group 1 (Table 1).

SQL Server 2008: Pivot column with no aggregate function workaround

Yes I know, this question has been asked MANY times but after reading all the posts I found that there wasn't an answer that fits my need. So, Heres my question. I would like to take a column of values and pivot them into rows of 6 columns.
I want to take this...... And turn it into this.......................
G Letter Date Code Ammount Name Account
081278 G 081278 12 00123535 John Doe 123456
12
00123535
John Doe
123456
I have 110000 values in this one column in one table called TempTable. I need all the values displayed because each row is an entity to itself. For instance, There is one unique entry for all of the Letter, Date, Code, Ammount, Name, and Account columns. I understand that the aggregate function is required but is there a workaround that will allow me to get this desired result?
Just use a MAX aggregate
If one row = one column (per group of 6 rows) then MAX of a single value = that row value.
However, the data you've posted in insufficient. I don't see anything to:
associate the 6 rows per group
distinguish whether a row is "Letter" or "Name"
There is no implicit row order or number to rely upon to generate the groups
Unfortunately, the max columns in a SQL 2008 select statement is 4,096 as per MSDN Max Capacity.
Instead of using a pivot, you might consider dynamic SQL to get what you want to do.
Declare #SQLColumns nvarchar(max),#SQL nvarchar(max)
select #SQLColumns=(select '''+ColName+'''',' from TableName for XML Path(''))
set #SQLColumns=left(#SQLColumns,len(#SQLColumns)-1)
set #SQL='Select '+#SQLColumns
exec sp_ExecuteSQL #SQL,N''

Minimum & Maximum Values in Crystal Reports 2008 Column

Say I have this column returned in a command for Crystal:
deposit_no
123
130
125
124
126
127
128
129
and I need to have this in the report title:
Includes deposits between 123 - 130
I've tried a running formula for minimum and maximum and they aren't returning the correct values no matter how I manipulate them. I've tried evaluate for every record, on change of the deposit_no field, etc. I have no grouping on this report.
Edited to add:
While I preferred to handle this on the CR side of things, I changed my command to include what mson wrote below. So technically, mson had the correct answer.
create a stored procedure or view that has the information you want. access the stored procedure or view through crystal reports.
crystal reports is a hindrance to properly coding anything.
the unexpected result you are getting may be because the column is not numeric. often, number values are stored as varchar/nvarchar. this is done especially for fields like zipcode/phone number/etc. where the value may be numeric, but you would never do math on them.
in any event, here are the snippets you can use to build in sql server (and then call from crystal)
select min(coalesce(cast(deposit_no as int),0)) as min_deposit from tableA
select max(coalesce(cast(deposit_no as int),0)) as max_deposit from tableA
Came across this while searching for the same thing, and would like to add to SqlACID's answer which does work.
You can do this in your formula editor.
'XX'+totext(Minimum ({YY.Num}), 0, '') + '-XX'+totext(Maximum ({YY.Num}), 0, '')
Create a formula field using summary functions for minimum and maximum of the deposit_no field, then drag the formula field to the page header