Combine Rows but concatenate on a certain field in Excel Power Query or Microsoft SQL - sql

I have brought a table from an Authority database into Excel via power query OBDC type, that includes fields like:
Date - various
Comments - mem_txt
Sequence - seq_num
The Comments field has a length restriction, and if a longer string is entered, it returns multiple rows with the Comments field being chopped into suitable lengths and the order returned in the Sequence field as per extract below. All other parts of the records are the same.
I want to collapse the rows based and concatenate the various Comments into a single entry. There is a date/time column just outside of the screen shot above that can be used to group the rows by (it is the same for the set of rows, but unique across the data set).
For example:
I did try bring the data in by a query connection, using the GROUP_CONCAT(Comments SEPARATOR ', ') and GROUP BY date, but that command isn't available in Microsoft Query.

Assuming the date/time column you refer to is named date_time, the M code would be:
let
Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content],
#"Grouped Rows" = Table.Group(
Source,
{"date_time"},
{{"NewCol", each Text.Combine([mem_text])}}
)
in
#"Grouped Rows"
Amend the Source line as required.

Related

SQL Server - collapsing multiple row values into one field

I'm trying to produce the result shown in the "Desired Results" section (in SQL Server 2016) in my screenshot below, where the objections associated with each borrower number are in one column delimited by commas instead of separate rows.
I don't want to restructure my database - I just want to display the results as requested for a customer report. Thanks
I was able to use the STRING_AGG function which I didn't know existed to produce the results I need.
SELECT ht.Hearings_CaseID, Borrower_Number, STRING_AGG(Objection,',') AS Objection
FROM Hearings_Test ht INNER JOIN
Hearings_Objections_Test hot on ht.Hearings_CaseID = hot.Hearings_CaseID
GROUP BY
ht.Hearings_CaseID, Borrower_Number

SSRS - How to get column value by column name

I have one table with rows and each row has a column that contains a field name (say raw1 - 'Number001', raw2-'ShortChar003', etc). In order for me to get that value of these fields I have to use a second table; this table has 1 raw with many columns (number001, Number002, ShortChar003, etc).
How can I extract the value?
Good Question..You can use lookup function
=Lookup(Fields!CityColumn.Value, Fields!CityColumn.Value, Fields!CountColumn.Value, "Dataset1")
Or you might have to use string functions..ex LEFT, Substring, Right same like SQL.If possible pls post some data of both tables, I will explain in detail

Pentaho Adding summary rows

Any idea how to summarize data in a Pentaho transformation and then insert the summary row directly under the group being summarized.
I can use a Group By step and get a summarised result stream having one row per key field, but what I want is each sorted group written to the output and the summary row inserted underneath, thus preserving the input.
In the Group By, you can do 'Include all Rows', but this just appends the summary fields to the end of each existing row. It does not create new summary rows.
Thanks in advance
To get the summary rows to appear under the group by blocks you have to use some tricks, such as introducing a numeric "order" field, setting the value of the original data to 1 and the sub totals rows to 2.
Also in the group-by/ sub-totals stream, I am generating a sum field, say "subtotal". You have to make sure to also include this as a blank in your regular stream or else the metadata will be divergent and the final merge will not work.
Here is the best explanation I have found for this pattern:
https://www.packtpub.com/books/content/pentaho-data-integration-4-working-complex-data-flows
You will need to copy the rows too a different stream, and then merge or join them again, to make it a separate row.

Counting the no. of commas across multiple fields using SQL

I've 3 fields which contain only text. However, i want to add a calculated field which counts the number of commas in each of these 3 fields and displays it separately in the adjacent column. The snippet of SQL i use is shown below. How can i build the calculated field?
SELECT week, client_I, client_II, client_III
FROM quality_control_test;
Please advise!
well, you can "count" the number of a given character in a string, by using this:
length(c) - length(replace(c,',',''))
I'm assume you can figure out how to leverage that for your own query ;)

How to Split a Column into 2 Columns in SQL

I have an SQL Query which extracts columns from a table that must be in this order
Item Id,
Parms,
Move list,
MsgId. I also have a couple of requirements in the query that must remain there such as UserCode = automation and the Date is set to one day prior to the current day.
An example of the Parms output for one row is such
CURRENT OUTPUT
Parms:
V51370,Move List,M-000001
--
REQUIRED OUTPUT
Media: v51370
Comments: Was Removed From
Move List: M-000001
What I want to do is split this Parms column into two separate columns that are in between the Item Id and Move list Column. The first Column should be called Media which extracts the first six characters from the Parms Column:v51370. The second column is comments and if the MsgId is 1 the comment should be: Was Removed from.
Can Anyone make any suggestions to a possible query I could test? I'm not sure where to fit the Parms column split in there. That would be greatly appreciated.
Please Note: I am using SQL Server Management Studio 2008, and the table name is JnlList.
1 SAMPLE ROW (SELECT * FROM JNLLIST):
ItemId: 2
Date: 20122102.124652563
Object: S-000001
MsgID: 1
Parms: V86143,Scratch List,S-000001
Delimiter: -
UserCode: Automation
ActionId: 5
List Type: S
to seperate the first 6 chars from the parms use
SELECT SUBSTRING(Parms,1,6) AS Media
FROM YOUR TABLE;
Then you can use a subquery with the same syntax to select the rest of the chars from the Parms column and put them into a seperate column