inserting data into table from string with splitting [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to insert the XML string like
1,2,3,4,5,6,7,8,9,,1,1,1,1,2,23,4
into a table
I wanted to insert the data into following table in a such manner that after every 3 values it should go into next row is it possible do this?
and the xml string is generated in the program.

Your question is quite unclear, but you might be looking for something like this:
DECLARE #Numbers VARCHAR(MAX)='1,2,3,4,5,6,7,8,9,,1,1,1,1,2,23,4';
WITH Splitted AS
(
SELECT A.B.value('.','int') AS Number
,ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS RowInx
FROM (SELECT CAST('<x>' + REPLACE(#Numbers,',','</x><x>') + '</x>' AS XML)) AS Casted(AsXml)
CROSS APPLY Casted.AsXml.nodes('/x') AS A(B)
)
,Extended AS
(
SELECT (RowInx -1 ) / 3 AS Rank3
,Number
,REPLACE('Field_' + CAST((RowInx % 3) AS VARCHAR(1)),'_0','_3') AS ColumnName
FROM Splitted
)
SELECT p.*
FROM Extended
PIVOT
(
MAX(Number) FOR ColumnName IN(Field_1,Field_2,Field_3)
) AS p
The result (btw: the empty value in your example "...8,9,,1,1" is taken as "0"):
Rank3 Field_1 Field_2 Field_3
0 1 2 3
1 4 5 6
2 7 8 9
3 0 1 1
4 1 1 2
5 23 4 NULL

Related

Convert column to row Using UNION ALL in Firebird [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
I need a help to convert my table.
I have original table named "SizeTable"
name
size1
size2
size3
shoes
8
9
10
and I want to convert the table as below
name
sizetype
sizeCode
shoe
size1
8
shoe
size2
9
shoe
size3
10
How can I convert column into row? since PIVOT and UNPIVOT CROSS APPLY keyword is not support in Firebird:
SELECT
name, [sizetype ], [sizeCode ]
FROM
(SELECT
sizetype, size1 value, 'size1' name
FROM
SizeTable
UNION ALL
SELECT
sizetype, size2 value, 'size2 ' name
FROM
SizeTable
UNION ALL
SELECT
sizetype, size3 value, 'size3' name
FROM
SizeTable) SRC
GROUP BY
name
Perhaps you can give this a go:
Select A.name
,B.*
From SizeTable A
Cross Apply ( values ('size1',size1)
,('size2',size2)
,('size3',size3)
) B(sizetype,sizeCode)
If you must use UNION ALL
Select Name,sizetype='size1',sizecode=size1 from SizeTable
Union All
Select Name,sizetype='size2',sizecode=size2 from SizeTable
Union All
Select Name,sizetype='size3',sizecode=size3 from SizeTable

How do I get the max records of a max subset in SQL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have the following tables:
Training Occurrence
Id Training Id Due Date
5 1 09/01/2018
6 1 09/15/2018
7 2 09/01/2018
Training Occurrence User
Id Training Material Occurrence Id User Id
1 5 'Chad'
2 5 'Chad'
3 6 'Chad'
4 6 'Chad'
5 7 'Chad'
My query needs to get the newest Training Material Occurrence User record of the newest Training Material Occurrence table. So if I pass in the user 'Chad'
I would want to see:
Id Occurrence Id User Training Id Due Date
4 6 'Chad' 1 09/15/2018
5 7 'Chad' 2 09/01/2018
Here is my query:
SELECT tmou.*, tmo.TrainingMaterialId, tmo.DueDate
FROM dbo.TrainingMaterialOccurrenceUser as tmou
INNER JOIN dbo.TrainingMaterialOccurrence as tmo on
tmou.TrainingMaterialOccurrenceId = tmo.Id
AND tmou.Id IN (SELECT MAX(tmou.Id)
FROM dbo.TrainingMaterialOccurrenceUser as tmou
WHERE tmou.UserId = #UserId
AND tmou.TrainingMaterialOccurrenceId IN (SELECT MAX(tmo.Id) as occurrenceId
FROM dbo.TrainingMaterialOccurrence as tmo
WHERE tmo.Id IN (Select TrainingMaterialOccurrenceId FROM dbo.TrainingMaterialOccurrenceUser as tmou1 WHERE tmou1.UserId = #UserId)
GROUP BY tmo.TrainingMaterialId)
GROUP BY tmou.TrainingMaterialOccurrenceId)
As you can see this is a mess. Any ideas on how I can clean this up.
Rank your rows with ROW_NUMBER:
SELECT Id, TrainingMaterialOccurrenceId, UserId, TrainingMaterialId, DueDate
FROM
(
SELECT
tmou.Id,
tmou.TrainingMaterialOccurrenceId,
tmou.UserId,
tmo.TrainingMaterialId,
tmo.DueDate,
ROW_NUMBER() OVER(PARTITION BY tmo.TrainingMaterialId
ORDER BY tmo.DueDate DESC,
tmo.Id DESC,
tmou.TrainingMaterialOccurrenceId DESC,
tmou.Id DESC) AS rn
FROM dbo.TrainingMaterialOccurrenceUser as tmou
INNER JOIN dbo.TrainingMaterialOccurrence as tmo
ON tmo.Id = tmou.TrainingMaterialOccurrenceId
)
WHERE rn = 1;

Output of SQL using one column only [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a table with column name "Val" which contains following values:
Val
1
2
3
4
My desired output is as follows:
1 1
2 1
2 2
3 1
3 2
3 3
4 1
4 2
4 3
4 4
Thanks!
Ajaysharma2061
You can do this with a self join:
select t.val, t2.val
from t join
t t2
on t2.val <= t.val
order by t.val;
You can join the table on itself as if its a two different tables using the SQL join keyword. Following your attached image your SQL query is expected to be like this
SELECT t.Val, c.Val
FROM Table t, Table c
WHERE t.id <= c.id
Here is a link on
tutorial point
that can help you as well

Select rows that if I sum their value = 0 from table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Let's say I have a table like this:
ID. Location. Value.
1. AGF. 10.00
2. VHJ. -20.00
3. AGF. -20.00
4. AGF. 5.00
5. KLZ. 50.00
6. AGF. 10.00
I want to select the rows that have same Location AND whose Values sum to zero.
In this case the result should be:
1
3
6
because those rows are all in Location AGF and they sum to 0 (10 + -20 + 10).
Try:
Select ID from YourTable where Location IN(
Select location from YouTable
Group By Location
Having sum(Value) = 0
)
You need to find all locations with zero sum using grouping and group filters (group by and having clauses respectively). This can be done in a subquery. Then select all IDs with the just selected locations.
select ID
from YOUR_TABLE
where Location in (
select Location
from YOUR_TABLE
group by Location
having sum(Value) = 0
)
You could use GROUP BY and HAVING, like this:
Select ID from tablelocation where Location IN(
Select location from tablelocation
Group By Location
Having sum(Value) = 0
)

How to select uncommon entry from 2 tables [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
TableA TableB
Id Name Id Role
1 abc 1 Test
2 xyz
3 zxc
I want output as all Ids from table A which are not present in table B
O/p
Id Name
2 xyz
3 zxc
You could use the not in operator:
SELECT *
FROM TableA
WHERE id NOT IN (SELECT id FROM TableB)
Try this:
SELECT * FROM TABLE_1 WHERE (Id,Name ) NOT IN (SELECT Id,Name FROM TABLE_2)
Edit (after question was formatted properly):
SELECT * FROM TABLE_1 WHERE Id NOT IN (SELECT Id FROM TABLE_2)