Will multiple columns concatenate in the same order if using STUFF and For Xml Path - sql

Please see http://www.sqlfiddle.com/#!3/fb107/3 for an example schema and query I want to run.
I want to use the STUFF and FOR XML PATH('') solution to concatenate columns having grouped by another column.
If I use this method to concatenate multiple columnns into a csv list, am I guaranteed that the order will be the same in each concatenated string? So if the table was:
ID Col1 Col2 Col3
1 1 1 1
1 2 2 2
1 3 3 3
2 4 4 4
2 5 5 5
2 5 5 5
Am I certain that if Col1 is concatenated such that the result is:
ID Col1Concatenated
1 1,2,3
2 4,5,6
That Col2Concatenated will also be in the same order ("1,2,3", "4,5,6") as opposed to ("2,3,1", "5,6,4") for example?
This solution will only work for me if the index of each row's value is the same in each of the concatenated values. i.e. first row is first in each csv list, second row is second in each csv list etc.

You can add an ORDER BY clause in the query within your STUFF function

Related

Distribute Records By Type Id Into Repetitive Columns

I am looking for a way to append repetitive columns and fill in the corresponding values from records of each type.
Source table:
Id
TypeId
ColA
ColB
ColC
0
0
valA0
valB0
valC0
1
0
valA1
valB1
valC1
2
0
valA2
valB2
valC2
3
1
valA3
valB3
valC3
4
1
valA4
valB4
valC4
5
1
valA5
valB5
valC5
6
2
valA6
valB6
valC6
7
2
valA7
valB7
valC7
8
2
valA8
valB8
valC8
Desired target output:
ColA.0
ColB.0
ColC.0
ColA.1
ColB.1
ColC.1
ColA.2
ColB.2
ColC.2
valA0
valB0
valC0
valA3
valB3
valC3
valA6
valB6
valC6
valA1
valB1
valC1
valA4
valB4
valC4
valA7
valB7
valC7
valA2
valB2
valC2
valA5
valB5
valC5
valA8
valB8
valC8
From all that I found, this is the most matching example: SQL - Group By Into Separate Columns.
But this solution works on two hardcoded ids, whereas in my case, the TypeIds are arbitrary and may be of 1 to n counts.
Is there a way other than working with cursors to achieve this result?
If cursors would be needed, I would prefer doing this in the application logic instead.

BigQuery: UNNESTING string representation of list of JSONs

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

BigQuery INSERT SELECT results in random order of records?

I used standard SQL to insert data form one table to another in BigQuery using Jupyter Notebook.
For example I have two tables:
table1
ID Product
0 1 book1
1 2 book2
2 3 book3
table2
ID Product Price
0 5 book5 8.0
1 6 book6 9.0
2 4 book4 3.0
I used the following codes
INSERT test_data.table1
SELECT *
FROM test_data.table2
ORDER BY Price;
SELECT *
FROM test_data.table1
I got
ID Product
0 1 book1
1 3 book3
2 2 book2
3 5 book5
4 6 book6
5 4 book4
I expected it appears in the order of ID 1 2 3 4 5 6 which 4,5,6 are ordered by Price
It also seems that the data INSERT and/or SELECT FROM display records in a random order in different run.
How do I control the SELECT FROM output without including the 'Price' column in the output table in order to sort them?
And this happened when I import a csv file to create a new table, the record order is random when using SELECT FROM to display them.
The ORDER BY clause specifies a column or expression as the sort criterion for the result set.
If an ORDER BY clause is not present, the order of the results of a query is not defined.
Column aliases from a FROM clause or SELECT list are allowed. If a query contains aliases in the SELECT clause, those aliases override names in the corresponding FROM clause.
So, you most likely wanted something like below
SELECT *
FROM test_data.table1
ORDER BY Price DESC
LIMIT 100
Note the use of LIMIT - it is important part - If you are sorting a very large number of values, use a LIMIT clause to avoid resource exceeded type of error

Access SQL query select with a specific pattern

I want to select each 5 rows to be unique and the select pattern applies for the rest of the result (i.e if the result contains 10 records I am expecting to have 2 set of 5 unique rows)
Example:
What I have:
1
1
5
3
4
5
2
4
2
3
Result I want to achieve:
1
2
3
4
5
1
2
3
4
5
I have tried and searched a lot but couldn't find anything close to what I want to achieve.
Assuming that you can somehow order the rows within the sets of 5:
SELECT t.Row % 5, t.Row FROM #T t
ORDER BY t.Row , t.Row % 5
We could probably get closer to the truth with more details about what your data looks like and what it is you're trying to actually do.
This will work with the sample of data you provided
SELECT DISTINCT(thevalue) FROM theresults
UNION ALL
SELECT DISTINCT(thevalue) FROM theresults
But it's unclear to me if it's really what you need.
For instance :
if your table/results returns 12 rows, do you still want 2x5 rows or do you want 2x6 rows ?
do you have always in your table/results the same rows in double ?
There's a lot more questions to rise and no hint about them in what you asked.

How to write query using self join and group by?

I have sql server 2008 db table FILE_DETAILS in following format.
ID FileName Filesize_in_MB
--------------------------------
1 a.txt 5
2 b.txt 2
3 c.txt 2
3 d.txt 4
4 e.txt 6
4 f.txt 1
4 g.txt 2
5 h.txt 8
6 i.txt 7
now what i want to fetch is as bellow
ID FileName Filesize_in_MB
--------------------------------
1 a.txt 5
2 b.txt 2
3 c.txt;d.txt 6
4 e.txt;f.txt;g.txt 9
5 h.txt 8
6 i.txt 7
In above results what happens ID became unique key and FILENAME has get attached and separated by ; and also FILESIZE_IN_MB field in sum of group by ID
I tried with various combination like groupby + self join, also sub queries and all that
but i think i missing something.
is it possible to handle this in SQL query?
Thanks in advance
Try this:
SELECT ID,
STUFF(( SELECT ';' + [FileName]
FROM FILE_DETAILS
WHERE ID = f.ID FOR XML PATH('')), 1, 1, ''),
SUM(Filesize_in_MB)
FROM FILE_DETAILS f
GROUP BY ID
Here's some more information:
Concatenate many rows into a single text string?
You should be able to do this using a group by. Aggregating Filesize_IN_MB can be done using sum as aggregator. However, to aggregate FileName you may need to create an AGGREGATE in SQL SERVER 2008R2. This will allow you to use Concatenate as an aggregation function.
select Concatenate(FileName), sum(Filesize_IN_MB) FROM FILE_DETAILS group by ID
There is another way of aggregate concatenation which seems fairly simple but I haven't tried.