Split comma separated string columns into several rows in SSIS? - ssis-2019

I want to achieve the output in 2nd table based on the input from the first table. I want to do this via a SSIS package.

Assuming your source table have only a two element CSV, we could try a union approach here:
SELECT
ID,
SUBSTRING([Group], 1, CHARINDEX(',', [Group]) - 1) AS [Group],
SUBSTRING([Category], 1, CHARINDEX(',', [Category]) - 1) AS Category
FROM yourTable
WHERE
[Group] IS NOT NULL
UNION ALL
SELECT
ID,
SUBSTRING([Group], CHARINDEX(',', [Group]) + 1, LEN([Group])),
SUBSTRING([Category], CHARINDEX(',', [Category]) + 1, LEN([Category]))
FROM yourTable
WHERE
[Group] IS NOT NULL
UNION ALL
SELECT ID, [Group], Category
FROM yourTable
WHERE [Group] IS NULL
ORDER BY
ID,
[Group];
Demo

Related

typeorm group by using column order number

I'm trying to get typeorm to generate a select query with group by clause, in which the parameters of the group by clause references the column in the select clause using its numeric order number:
SELECT SUBSTRING(A, 1, LENGTH(A)), some_expression(B), DISTINCT C
FROM table
GROUP BY 1, 2
Instead of
SELECT SUBSTRING(A, 1, LENGTH(A)), some_expression(B), DISTINCT C
FROM table
GROUP BY SUBSTRING(A, 1, LENGTH(A)), some_expression(B)
Is there an elegant way to do this? I'm using the QueryBuilder API to generate a sql query which is then used for some other purpose.

SQL Join based on three keys

Database is Teradata
I have two table which I am trying to join. Following are the table structures. When I join these table I expect to get two rows as output but getting 4 rows.what is reason for this behavior. Join based on three keys should uniquely identify a row but still getting 4 rows as output. Any help is appreciated.
TableA
Weekkey|segment|type|users
201501|1|A|100
201501|1|B|100
TableB
Weekkey|segment|type|revenue
201501|1|A|200
201501|1|B|200
when I join these two table using the following query i get the following result
select a.* ,b.user
from tablea a left join tableb b on a.weekkey=b.weekkey
and a.segment=b.segment
and a.type=b.type
Weekkey|segment|type|revenue|users
201501|1|A|200|100
201501|1|B|200|100
201501|1|A|200|100
201501|1|B|200|100
Using sql server, here is ddl and sample data along with the query you posted. The output you state you are getting doesn't happen here.
create table #tablea
(
Weekkey int
, segment int
, type char(1)
, users int
)
insert #tablea
select 201501, 1, 'A', 100 union all
select 201501, 1, 'B', 100
create table #TableB
(
Weekkey int
, segment int
, type char(1)
, revenue int
)
insert #TableB
select 201501, 1, 'A', 200 union all
select 201501, 1, 'B', 200
select a.*
, b.revenue
from #tablea a
left join #tableb b on a.weekkey = b.weekkey
and a.segment = b.segment
and a.type = b.type
drop table #tablea
drop table #TableB

Sqlite where clause with multiple values in select statement

I am trying a sqlite select query statement as below:
SELECT IndicatorText
FROM Table
where IndicatorID in('13','25','64','52','13','25','328')
AND RubricID in('1','1','1','1','1','1','6')
This gives an output but the duplicate values are not displayed. I want to display all the values of IndicatorText even though it is duplicate.
Please help me with this query.
The two IN conditions are evaluated individually.
To check both values at once, you could concatenate them so that you have a single string to compare:
SELECT IndicatorText
FROM MyTable
WHERE IndicatorID || ',' || RubricID IN (
'13,1', '25,1', '64,1', '52,1', '13,1', '25,1', '328,6')
However, doing this operation on the column values prevents the query optimizer from using indexes, so this query will be slow if the table is big.
To allow optimizations, create a temporary table with the desired values, and join that with the original table:
SELECT IndicatorText
FROM MyTable
NATURAL JOIN (SELECT 13 AS IndicatorID, 1 AS RubricID UNION ALL
SELECT 25, 1 UNION ALL
SELECT 64, 1 UNION ALL
SELECT 52, 1 UNION ALL
SELECT 13, 1 UNION ALL
SELECT 25, 1 UNION ALL
SELECT 328, 6)

Extracting data from two tables

I have two tables "CAR" -it's fields are- (car_id,name), the second table "CARSFORSALE" contains cars for sale (id,car_id,....)
How i can get 10 rows from "CARSFORSALE" table for each car_id in one query
that mean if i have 7 car_id my result will be <=70
select 1,car_id
union all
select 2,car_id
union all
select 3,car_id
union all
select 4,car_id
union all
select 5,car_id
union all
select 6,car_id
union all
select 7,car_id
union all
select 8,car_id
union all
select 9,car_id
union all
select 10,car_id

SQLite select query. Get row with array

I coder for iOS and I use SQLite for my data storage.
For now i have two tables in my database. This is table_1 and table_2 (it's easier for understanding)
About struct:
table_1 contain id field (int type) and some_value field (text type)
table_2 contain id field (int type) and table_1_id field (int type)
My question is next: how to select all row from table_1 via array table_1_id field table_2, and this is must SELECT with ORDER BY. I mean order by array.
For example:
if table_2 -> table_1_id contain value 5, 1, 10, 3, 15, 2
the result of output must be in this order.
For now my query is simple:
SELECT * FROM table_1 WHERE id IN (5, 1, 10, 3, 15, 2)
but the select not return order for this query it return data order by 1, 2, 3, 5, 10, 15.
I think I make relation between my table and make SELECT from table_2 uses ORDER BY, but I don't know how to do this correct.
Thanks in advance!
(Very important for me return this order - 5, 1, 10, 3, 15, 2)
I find some result in stackoverflow
link
This is work for me:
SELECT * FROM "comments" WHERE ("comments"."id" IN (1,3,2,4)) ORDER
BY id=1 DESC, ID=3 DESC, id=2 DESC, ID=4 DESC
This is hard code but I create this query string programmatically in cycle and this work great.

Resources