radecky radecky 07.08.2019, 18:16:25 0 Mireczki od #programowanie #sql #bazydanych, potrzebuje pomocy bo mam zaćmienie i nie wiem jak zapytać o to internet ;)Mam tabeleA; 1,2,3,4B; 1,2,3,4....potrzebuje podpowiedzi jak zapytaniem wyciągnąć to w postaci:A;1A;2A;3A;4B;1B;2...Pomożecie? (╥﹏╥)
DarkAlchemy DarkAlchemy 07.08.2019, 18:27:50 0 @radecky: jaka baza? MS SQL 2016 i wyżej?https://docs.microsoft.com/en-us/sql/t-sql/functions/string-split-transact-sql?view=sql-server-2017(przykład z CROSS APPLY w dokumentacji)
JacobTheLiar JacobTheLiar 07.08.2019, 19:04:21 0 dla znanej ilości kolumn można bez pivota i bez corss apply, powinno działać dla każdej bazy. przykład podam dla MSSQLdeclare @tableIn table (id char(1), col1 int, col2 int, col3 int)insert into @tableIn values ('A', 1, 2, 3), ('B', 4, 5, 6), ('C', 7, 8, 9)declare @tableOut table (id char(1), col int)insert into @tableOut select id , col1 from @tableIn union all select id , col2 from @tableIn unionPokaż całość
JacobTheLiar JacobTheLiar 07.08.2019, 19:15:59 0 a z unpivotem na MSSQLselect tout.id, tout.marks colfrom @tableIn tinunpivot( marks for col in (col1, col2, col3)) tout;
radecky radecky 07.08.2019, 19:27:05 0 Dzięki Panowie ale udało mi sie znaleźć dość eleganckie rozwiazanie do excela ;)Option ExplicitSub Main()Columns("B:B").NumberFormat = "@"Dim i As Long, c As Long, r As Range, v As VariantFor i = 1 To Range("B" & Rows.Count).End(xlUp).Row v = Split(Range("B" & i), ", ") c = c + UBound(v) + 1Next iFor i = 2 To c Set r = Range("B" & i) Dim arr AsPokaż całość
Mam tabele
A; 1,2,3,4
B; 1,2,3,4
....
potrzebuje podpowiedzi jak zapytaniem wyciągnąć to w postaci:
A;1
A;2
A;3
A;4
B;1
B;2
...
Pomożecie? (╥﹏╥)
https://docs.microsoft.com/en-us/sql/t-sql/functions/string-split-transact-sql?view=sql-server-2017
(przykład z CROSS APPLY w dokumentacji)
declare
@tableIn table (id char(1), col1 int, col2 int, col3 int)
insert into @tableIn values
('A', 1, 2, 3),
('B', 4, 5, 6),
('C', 7, 8, 9)
declare
@tableOut table (id char(1), col int)
insert into @tableOut
select id , col1 from @tableIn union all
select id , col2 from @tableIn union
select tout.id, tout.marks col
from @tableIn tin
unpivot
(
marks
for col in (col1, col2, col3)
) tout;
Option Explicit
Sub Main()
Columns("B:B").NumberFormat = "@"
Dim i As Long, c As Long, r As Range, v As Variant
For i = 1 To Range("B" & Rows.Count).End(xlUp).Row
v = Split(Range("B" & i), ", ")
c = c + UBound(v) + 1
Next i
For i = 2 To c
Set r = Range("B" & i)
Dim arr As