SQL group by and cross apply - sql

id | systemName | Systemid
-------------------------------
100 | A100 | 1
100 | B100 | 2
100 | C100 | 3
100 | D100 | 4
200 | A100 | 1
200 | B200 | 2
What is the best way to achieve the below result? Column System name should have the comma separated values of counted systems
id Systemidcount SystemName
---------------------------------------------
100 | 4 | A100,B100,C100,D100
200 | 2 | A100,B200
I am not able to format it correctly for some reason, apologies

You may notice the sub-query alias A. This is to prevent redundant calls in the CROSS APPLY
Declare #YourTable table (id int,systemname varchar(25),systemid int)
Insert Into #YourTable values
(100,'A100',1),
(100,'B100',2),
(100,'C100',3),
(100,'D100',4),
(200,'A100',1),
(200,'B200',2)
Select A.*
,SystemName = B.Value
From (Select ID,Systemidcount = count(*) From #YourTable Group By ID) A
Cross Apply (
Select Value=Stuff((Select Distinct ',' + systemname
From #YourTable
Where ID=A.ID
For XML Path ('')),1,1,'')
) B
Order By ID
Returns
ID Systemidcount SystemName
100 4 A100,B100,C100,D100
200 2 A100,B200

This is string aggregation:
select id, count(*) as systemidcount,
stuff(v.systemnames, 1, 1, '') as systemnames
from t cross apply
(select ',' + systemName
from t t2
where t2.id = t.id
for xml path ('')
) v(systemnames);

SELECTid,COUNT(Systemid) as SystemidCount,
SystemName=Stuff((SELECT ',' + SystemName FROM t t1 WHERE t1.id=t.id
FOR XML PATH (''))
, 1, 1, '' )
FROM t
GROUP BY id

Related

SQL Server recursive query to show path of parents

I am working with SQL Server statements and have one table like:
| item | value | parentItem |
+------+-------+------------+
| 1 | 2test | 2 |
| 2 | 3test | 3 |
| 3 | 4test | 4 |
| 5 | 1test | 1 |
| 6 | 3test | 3 |
| 7 | 2test | 2 |
And I would like to get the below result using a SQL Server statement:
| item1 | value1 |
+-------+--------------------------+
| 1 | /4test/3test/2test |
| 2 | /4test/3test |
| 3 | /4test |
| 5 | /4test/3test/2test/1test |
| 6 | /4test/3test |
| 7 | /4test/3test/2test |
I didn't figure out the correct SQL to get all the values for all the ids according to parentItem.
I have tried this SQL :
with all_path as
(
select item, value, parentItem
from table
union all
select a.item, a.value, a.parentItem
from table a, all_path b
where a.item = b.parentItem
)
select
item as item1,
stuff(select '/' + value
from all_path
order by item asc
for xml path ('')), 1, 0, '') as value1
from
all_path
But got the "value1" column in result like
/4test/4test/4test/3test/3test/3test/3test/2test/2test/2test/2test
Could you please help me with that? Thanks a lot.
based on the expected output you gave, use the recursive part to concatenate the value
;with yourTable as (
select item, value, parentItem
from (values
(1,'2test',2)
,(2,'3test',3)
,(3,'4test',4)
,(5,'1test',1)
,(6,'3test',3)
,(7,'2test',2)
)x (item,value,parentItem)
)
, DoRecursivePart as (
select 1 as Pos, item, convert(varchar(max),value) value, parentItem
from yourTable
union all
select drp.pos +1, drp.item, convert(varchar(max), yt.value + '/' + drp.value), yt.parentItem
from yourTable yt
inner join DoRecursivePart drp on drp.parentItem = yt.item
)
select drp.item, '/' + drp.value
from DoRecursivePart drp
inner join (select item, max(pos) mpos
from DoRecursivePart
group by item) [filter] on [filter].item = drp.item and [filter].mpos = drp.Pos
order by item
gives
item value
----------- ------------------
1 /4test/3test/2test
2 /4test/3test
3 /4test
5 /4test/3test/2test/1test
6 /4test/3test
7 /4test/3test/2test
Here's the sample data
drop table if exists dbo.test_table;
go
create table dbo.test_table(
item int not null,
[value] varchar(100) not null,
parentItem int not null);
insert dbo.test_table values
(1,'test1',2),
(2,'test2',3),
(3,'test3',4),
(5,'test4',1),
(6,'test5',3),
(7,'test6',2);
Here's the query
;with recur_cte(item, [value], parentItem, h_level) as (
select item, [value], parentItem, 1
from dbo.test_table tt
union all
select rc.item, tt.[value], tt.parentItem, rc.h_level+1
from dbo.test_table tt join recur_cte rc on tt.item=rc.parentItem)
select rc.item,
stuff((select '/' + cast(parentItem as varchar)
from recur_cte c2
where rc.item = c2.item
order by h_level desc FOR XML PATH('')), 1, 1, '') [value1]
from recur_cte rc
group by item;
Here's the results
item value1
1 4/3/2
2 4/3
3 4
5 4/3/2/1
6 4/3
7 4/3/2

How to find duplicate row when any one word of desc column matching within group

I have a result like this:
I need to update "flag" column as duplicate when any one word from the row matches with second row within group of "mfgid" column.
--test dataset
declare #table as table
(id int,
mfgid int,
[desc] varchar(100))
insert into #table
values (1,111,'abc xyz pqr'),
(2,111,'abc tyu fgh'),
(3,222,'abc pqr'),
(4,222,'lmn stu'),
(5,333,'pqr spd hki abc'),
(6,333,'lmn jsk pqr klo')
How can I do this?
Here is a possible solution
WITH K AS
(
SELECT mfgid,
value,
count(*) over ( partition by mfgid, value order by mfgid) Dups
FROM #Table cross apply STRING_SPLIT([desc], ' ')
)
SELECT T.*,
IIF(
EXISTS(SELECT 1 FROM K WHERE K.mfgid = T.mfgid AND K.Dups > 1),
'Duplicte',
''
) Flag
FROM #Table T;
Results:
+----+-------+-----------------+----------+
| id | mfgid | desc | Flag |
+----+-------+-----------------+----------+
| 1 | 111 | abc xyz pqr | Duplicte |
| 2 | 111 | abc tyu fgh | Duplicte |
| 3 | 222 | abc pqr | |
| 4 | 222 | lmn stu | |
| 5 | 333 | pqr spd hki abc | Duplicte |
| 6 | 333 | lmn jsk pqr klo | Duplicte |
+----+-------+-----------------+----------+
Demo
two possible solutions below:
--test dataset
declare #table as table
(id int,
mfgid int,
[desc] varchar(100))
insert into #table
values (1,111,'abc xyz pqr'),
(2,111,'abc tyu fgh'),
(3,222,'abc pqr'),
(4,222,'lmn stu'),
(5,333,'pqr spd hki abc'),
(6,333,'lmn jsk pqr klo')
Solution 1:
If you have only 4 words in string (based on your screenshot)
;with cte2 as
(select *
from (select id,
mfgid,
parsename(replace(s.[desc],' ','.'),1) as [a1],
parsename(replace(s.[desc],' ','.'),2) as [a2],
parsename(replace(s.[desc],' ','.'),3) as [a3],
parsename(replace(s.[desc],' ','.'),4) as [a4]
from #table as s) as a
unpivot (testval FOR val IN (a1, a2, a3, a4)) unpvt
)
select m.id, m.mfgid, m.[desc], t.flag
from #table as m
outer apply
(select top (1) 'duplicate' as flag
from cte2 as a
join cte2 as b
on a.mfgid = b.mfgid
and a.id != b.id
and a.testval = b.testval
and m.mfgid = a.mfgid) as t
test is here
Solution 2:
If you have more that 4 words in string
;with cte as
( select t.*, s.[value]
from #table as t
cross apply
(select ltrim(rtrim(split.a.value('.','varchar(100)'))) as [value]
from (select cast('<M>'+replace([desc],' ','</M><M>')+'</M>' as xml) as data) as a
cross apply data.nodes ('/M') as split(a)
) as s
)
select m.id, m.mfgid, m.[desc], t.flag
from #table as m
outer apply
(select top (1) 'duplicate' as flag
from cte as a
join cte as b
on a.mfgid = b.mfgid
and a.id != b.id
and a.Value = b.Value
and m.mfgid = a.mfgid) as t
test is here
This assumes the OP is using SQL Server 2016+, as they haven't let us know the version:
WITH Split AS(
SELECT T.id,
T.mfgid,
T.[desc],
SS.[value]
FROM #table T
CROSS APPLY STRING_SPLIT([desc],' ') SS)
SELECT S.id,
S.mfgid,
S.[desc],
CASE MAX(Dups) WHEN 0 THEN NULL ELSE 'Duplicate' END AS Flag
FROM Split S
CROSS APPLY (SELECT COUNT(*) AS [Dups]
FROM Split ca
WHERE ca.mfgid = S.mfgid
AND ca.[value] = S.[value]
AND ca.id != S.id) C
GROUP BY S.id,
S.mfgid,
S.[desc];

SQL: Pick highest and lowest value (int) from one row

I am looking for a way to pick the highest and lowest value (integer) from a single row in table. There are 4 columns that i need to compare together and get highest and lowest number there is.
The table looks something like this...
id | name | col_to_compare1 | col_to_compare2 | col_to_compare3 | col_to_compare4
1 | John | 5 | 5 | 2 | 1
2 | Peter | 3 | 2 | 4 | 1
3 | Josh | 3 | 5 | 1 | 3
Can you help me, please? Thanks!
You can do this using CROSS APPLY and the VALUES clause. Use VALUES to group all your compared columns and then select the max.
SELECT
MAX(d.data1) as MaxOfColumns
,MIN(d.data1) as MinOfColumns
,a.id
,a.name
FROM YOURTABLE as a
CROSS APPLY (
VALUES(a.col_to_compare1)
,(a.col_to_compare2)
,(a. col_to_compare3)
,(a.col_to_compare4)
,(a. col_to_compare5)
) as d(data1) --Name the Column
GROUP BY a.id
,a.name
Assuming you are looking for min/max per row
Declare #YourTable table (id int,name varchar(50),col_to_compare1 int,col_to_compare2 int,col_to_compare3 int,col_to_compare4 int)
Insert Into #YourTable values
(1,'John',5,5,2,1),
(2,'Peter',3,2,4,1),
(3,'Josh',3,5,1,3)
Select A.ID
,A.Name
,MinVal = min(B.N)
,MaxVal = max(B.N)
From #YourTable A
Cross Apply (Select N From (values(a.col_to_compare1),(a.col_to_compare2),(a.col_to_compare3),(a.col_to_compare4)) N(N) ) B
Group By A.ID,A.Name
Returns
ID Name MinVal MaxVal
1 John 1 5
3 Josh 1 5
2 Peter 1 4
These solutions keep the current rows and add additional columns of min/max.
select *
from t cross apply
(select min(col) as min_col
,max(col) as max_col
from (
values
(t.col_to_compare1)
,(t.col_to_compare2)
,(t.col_to_compare3)
,(t.col_to_compare4)
) c(col)
) c
OR
select *
,cast ('' as xml).value ('min ((sql:column("t.col_to_compare1"),sql:column("t.col_to_compare2"),sql:column("t.col_to_compare3"),sql:column("t.col_to_compare4")))','int') as min_col
,cast ('' as xml).value ('max ((sql:column("t.col_to_compare1"),sql:column("t.col_to_compare2"),sql:column("t.col_to_compare3"),sql:column("t.col_to_compare4")))','int') as max_col
from t
+----+-------+-----------------+-----------------+-----------------+-----------------+---------+---------+
| id | name | col_to_compare1 | col_to_compare2 | col_to_compare3 | col_to_compare4 | min_col | max_col |
+----+-------+-----------------+-----------------+-----------------+-----------------+---------+---------+
| 1 | John | 5 | 5 | 2 | 1 | 1 | 5 |
+----+-------+-----------------+-----------------+-----------------+-----------------+---------+---------+
| 2 | Peter | 3 | 2 | 4 | 1 | 1 | 4 |
+----+-------+-----------------+-----------------+-----------------+-----------------+---------+---------+
| 3 | Josh | 3 | 5 | 1 | 3 | 1 | 5 |
+----+-------+-----------------+-----------------+-----------------+-----------------+---------+---------+
A way to do this is to "break" apart the data
declare #table table (id int, name varchar(10), col1 int, col2 int, col3 int, col4 int)
insert into #table values (1 , 'John' , 5 , 5 , 2 , 1)
insert into #table values (2 , 'Peter' , 3 , 2 , 4 , 1)
insert into #table values (3 , 'Josh' , 3 , 5 , 1 , 3)
;with stretch as
(
select id, col1 as col from #table
union all
select id, col2 as col from #table
union all
select id, col3 as col from #table
union all
select id, col4 as col from #table
)
select
t.id,
t.name,
agg.MinCol,
agg.MaxCol
from #table t
inner join
(
select
id, min(col) as MinCol, max(col) as MaxCol
from stretch
group by id
) agg
on t.id = agg.id
Seems simple enough
SELECT min(col1), max(col1), min(col2), max(col2), min(col3), max(col3), min(col4), max(col4) FROM table
Gives you the Min and Max for each column.
Following OP's comment, I believe he may be looking for a min/max grouped by the person being queried against.
So that would be:
SELECT name, min(col1), max(col1), min(col2), max(col2), min(col3), max(col3), min(col4), max(col4) FROM table GROUP BY name

Sql Server: Comma separated column values exist in other comma separated column

Let's say I have a table
OrgData
OrgDataid columvalues
1 1,2,3,4,5
2 6,7,8,9
3 16,17,18,19
Another table holds selected values
selectedData
rowid sid orgid values
1 1 1 1,2
2 1 2 6,7,8,9
3 2 1 1,2,3,4,5
Where sid is the id of OrgData
I want output like
for sid 1;
outputData
OrgData columvalues
1 partial selected
2 Full selected
3 Nothing selected
I want some straightway query, while I am trying to do it with splitting every comma separated to rows and looping for each one.
Thanks
You might try this:
--mock-up-tables with your data
DECLARE #OrgData TABLE(OrgDataid INT,columvalues VARCHAR(100));
INSERT INTO #OrgData VALUES
(1,'1,2,3,4,5')
,(2,'6,7,8,9')
,(3,'16,17,18,19');
DECLARE #selectedData TABLE(rowid INT,[sid] INT,orgid INT,[values] VARCHAR(100));
INSERT INTO #selectedData VALUES
(1,1,1,'1,2')
,(2,1,2,'6,7,8,9')
,(3,2,1,'1,2,3,4,5');
--First CTE to split OrgData
WITH OrgDataSplitted AS
(
SELECT od.*
,LEN(od.columvalues)-LEN(REPLACE(od.columvalues,',',''))+1 AS CountParts
,B.Part.value('.','int') AS PartInt
FROM #OrgData AS od
CROSS APPLY(SELECT CAST('<x>' + REPLACE(columvalues,',','</x><x>') + '</x>' AS XML)) AS A(Casted)
CROSS APPLY A.Casted.nodes('/x') AS B(Part)
)
--Second CTE to split SelectedData
,SelectedSplitted AS
(
SELECT sd.*
,B.Part.value('.','int') AS PartInt
FROM #selectedData AS sd
CROSS APPLY(SELECT CAST('<x>' + REPLACE([values],',','</x><x>') + '</x>' AS XML)) AS A(Casted)
CROSS APPLY A.Casted.nodes('/x') AS B(Part)
)
--The query to join them
SELECT o.OrgDataid,o.CountParts,s.rowid,COUNT(rowid) AS CountIdent
FROM Orgdatasplitted AS o
FULL OUTER JOIN SelectedSplitted AS s ON o.OrgDataID=s.orgid and o.PartInt=s.PartInt
GROUP BY o.OrgDataid,o.CountParts,s.rowid
ORDER BY o.OrgDataid
The result. If CountParts and CountIdent is the same it's full, >0 is partial and 0 is none
+-----------+------------+-------+------------+
| OrgDataid | CountParts | rowid | CountIdent |
+-----------+------------+-------+------------+
| 1 | 5 | 1 | 2 |
+-----------+------------+-------+------------+
| 1 | 5 | 3 | 5 |
+-----------+------------+-------+------------+
| 2 | 4 | 2 | 4 |
+-----------+------------+-------+------------+
| 3 | 4 | NULL | 0 |
+-----------+------------+-------+------------+
Try the below script, may be help you. But I agree with #Gordon Linoff,
Fix you data structure. Storing values in comma-delimited strings is the wrong way.
SELECT OrgDataid
,(CASE WHEN OD.columvalues = SD.[values] THEN 'Full selected'
WHEN SD.rowid IS NULL THEN 'Nothing selected'
ELSE 'partial selected' END) AS columvalues
FROM OrgData AS OD
LEFT OUTER JOIN selectedData AS SD ON OD.columvalues LIKE '%'+SD.[values]+'%'

Can anyone write query for this?

My table is as below
recordId fwildcardId refNumber wildcardName wildcardValue comments
404450 154834 2 aaa p p
404450 154833 1 aa oi p
406115 154867 1 98 ff ff
406199 154869 1 aa aaaa ssss
406212 154880 1 bbbbb card comm
and I need the output as
RecordId fwildcardid1 refNo1 Name1 Value1 comments1 fwildcardid2 refNo2 Name2 Value2 comments2 fwildcardid3 refNo3 Name3 Value3 comments3
404450 154834 2 aaa p p 154833 1 aa oi p
406115 Null Null Null Null Null Null Null Null Null Null 154867 1 98 ff ff
406199 Null Null Null Null Null 154869 1 aa aaaa ssss Null Null Null Null
I tried pivoting like below , but didnt succeed .
select t1.recordId,t1.wildcardid as fwildcardId,t1.refNo as refNumber,t2.wildcardName,t1.attributeValue as wildcardValue,t1.comments
into #tempp
from fwildcards t1
inner join fwildcardattributes t2 on t2.WildcardID=t1.attributenameid and t2.MarketID=5
inner join fitems t3 on t3.recordid=t1.recordid and t3.marketid=5
order by recordid,attributenameid
select * from #tempp
pivot (min (wildcardValue) for wildcardName in ([aaa],[aa],[aaaa],[98],[kki],[bbbbb],[SUN])) as wildcardValuePivot
In order to get this result, you will have to UNPIVOT and hen PIVOT the data. The UNPIVOT will take the values in the columns fwildcardId, refNumber, wildcardName, wildcardValue and comments and turns them into rows. Once the data is in rows, then you can apply the PIVOT function to get the final result.
To unpivot the data, you can use either the UNPIVOT function or you can use the CROSS APPLY and VALUES clause.
UNPIVOT:
select recordid,
col+cast(rn as varchar(10)) col,
unpiv_value
from
(
select recordid,
cast(fwildcardid as varchar(10)) fwildcardid,
cast(refnumber as varchar(10)) refnumber,
cast(wildcardname as varchar(10)) name,
cast(wildcardvalue as varchar(10)) value,
cast(comments as varchar(10)) comments,
row_number() over(partition by recordid
order by fwildcardid) rn
from tempp
) d
unpivot
(
unpiv_value
for col in (fwildcardid, refnumber, name, value, comments)
) c
See SQL Fiddle with Demo.
CROSS APPLY and VALUES:
select recordid,
col+cast(rn as varchar(10)) col,
value
from
(
select recordid,
cast(fwildcardid as varchar(10)) fwildcardid,
cast(refnumber as varchar(10)) refnumber,
wildcardname,
wildcardvalue,
comments,
row_number() over(partition by recordid
order by fwildcardid) rn
from tempp
) d
cross apply
(
values
('fwildcardid', fwildcardid),
('refnumber', refnumber),
('name', wildcardname),
('value', wildcardvalue),
('comments', comments)
) c (col, value)
See SQL Fiddle with Demo.
These convert the results in a format:
| RECORDID | COL | VALUE |
------------------------------------
| 404450 | fwildcardid1 | 154833 |
| 404450 | refnumber1 | 1 |
| 404450 | name1 | aa |
| 404450 | value1 | oi |
| 404450 | comments1 | p |
| 404450 | fwildcardid2 | 154834 |
When you unpivot data into the same column, it has to be the same datatype. You will notice that I applied a cast to the columns so the datatype is the same.
Once the data is in the row format, you can convert it back into columns with PIVOT:
select *
from
(
select recordid,
col+cast(rn as varchar(10)) col,
unpiv_value
from
(
select recordid,
cast(fwildcardid as varchar(10)) fwildcardid,
cast(refnumber as varchar(10)) refnumber,
cast(wildcardname as varchar(10)) name,
cast(wildcardvalue as varchar(10)) value,
cast(comments as varchar(10)) comments,
row_number() over(partition by recordid
order by fwildcardid) rn
from tempp
) d
unpivot
(
unpiv_value
for col in (fwildcardid, refnumber, name, value, comments)
) c
) src
pivot
(
max(unpiv_value)
for col in (fwildcardid1, refnumber1, name1, value1, comments1,
fwildcardid2, refnumber2, name2, value2, comments2)
) piv;
See SQL Fiddle with Demo.
The above version works great if you have a known number of columns, but if you will have an unknown number of values that will be converted into columns, then you will need to use dynamic sql to get the result:
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
select #cols = STUFF((SELECT ',' + QUOTENAME(c.col+cast(rn as varchar(10)))
from
(
select row_number() over(partition by recordid
order by fwildcardid) rn
from tempp
) t
cross apply
(
select 'fwildcardid' col, 1 sortorder union all
select 'refNumber', 2 union all
select 'name', 3 union all
select 'value', 4 union all
select 'comments', 5
) c
group by col, rn, sortorder
order by rn, sortorder
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT recordid,' + #cols + ' from
(
select recordid,
col+cast(rn as varchar(10)) col,
unpiv_value
from
(
select recordid,
cast(fwildcardid as varchar(10)) fwildcardid,
cast(refnumber as varchar(10)) refnumber,
cast(wildcardname as varchar(10)) name,
cast(wildcardvalue as varchar(10)) value,
cast(comments as varchar(10)) comments,
row_number() over(partition by recordid
order by fwildcardid) rn
from tempp
) d
unpivot
(
unpiv_value
for col in (fwildcardid, refnumber, name, value, comments)
) c
) src
pivot
(
max(unpiv_value)
for col in (' + #cols + ')
) p '
execute(#query);
See SQL Fiddle with Demo. Both of these give the result:
| RECORDID | FWILDCARDID1 | REFNUMBER1 | NAME1 | VALUE1 | COMMENTS1 | FWILDCARDID2 | REFNUMBER2 | NAME2 | VALUE2 | COMMENTS2 |
-------------------------------------------------------------------------------------------------------------------------------
| 404450 | 154833 | 1 | aa | oi | p | 154834 | 2 | aaa | p | p |
| 406115 | 154867 | 1 | 98 | ff | ff | (null) | (null) | (null) | (null) | (null) |
| 406199 | 154869 | 1 | kki | aaaa | ssss | (null) | (null) | (null) | (null) | (null) |
| 406212 | 154880 | 1 | bbbbb | card | comm | (null) | (null) | (null) | (null) | (null) |
No Pivot No Cross Apply
According to edited Question.
select
DISTINCT
A.recordId AS recordId,
A1.fwildcardId AS fwildcardId1,
A1.refNumber AS refNumber1,
A1.wildcardName AS wildcardName1,
A1.wildcardValue AS wildcardValue1,
A1.comments AS comments1,
A2.fwildcardId AS fwildcardId2,
A2.refNumber AS refNumber2,
A2.wildcardName AS wildcardName2,
A2.wildcardValue AS wildcardValue2,
A2.comments AS comments2,
A3.fwildcardId AS fwildcardId3,
A3.refNumber AS refNumber3,
A3.wildcardName AS wildcardName3,
A3.wildcardValue AS wildcardValue3,
A3.comments AS comments3,
A4.fwildcardId AS fwildcardId4,
A4.refNumber AS refNumber4,
A4.wildcardName AS wildcardName4,
A4.wildcardValue AS wildcardValue4,
A4.comments AS comments4,
A5.fwildcardId AS fwildcardId5,
A5.refNumber AS refNumber5,
A5.wildcardName AS wildcardName5,
A5.wildcardValue AS wildcardValue5,
A5.comments AS comments5,
A6.fwildcardId AS fwildcardId6,
A6.refNumber AS refNumber6,
A6.wildcardName AS wildcardName6,
A6.wildcardValue AS wildcardValue6,
A6.comments AS comments6,
A7.fwildcardId AS fwildcardId7,
A7.refNumber AS refNumber7,
A7.wildcardName AS wildcardName7,
A7.wildcardValue AS wildcardValue7,
A7.comments AS comments7,
A8.fwildcardId AS fwildcardId8,
A8.refNumber AS refNumber8,
A8.wildcardName AS wildcardName8,
A8.wildcardValue AS wildcardValue8,
A8.comments AS comments8,
A9.fwildcardId AS fwildcardId9,
A9.refNumber AS refNumber9,
A9.wildcardName AS wildcardName9,
A9.wildcardValue AS wildcardValue9,
A9.comments AS comments9,
A10.fwildcardId AS fwildcardId10,
A10.refNumber AS refNumber10,
A10.wildcardName AS wildcardName10,
A10.wildcardValue AS wildcardValue10,
A10.comments AS comments10
from Table_name A
LEFt JOIN Table_name A1 ON A.recordId=A1.recordId AND A1.wildcardName='aaa'
LEFT JOIN Table_name A2 ON A.recordId=A2.recordId AND A2.wildcardName='aa'
LEFT JOIN Table_name A3 ON A.recordId=A3.recordId AND A3.wildcardName='98'
LEFT JOIN Table_name A4 ON A.recordId=A4.recordId AND A4.wildcardName=''
LEFT JOIN Table_name A5 ON A.recordId=A5.recordId AND A5.wildcardName=''
LEFT JOIN Table_name A6 ON A.recordId=A6.recordId AND A6.wildcardName=''
LEFT JOIN Table_name A7 ON A.recordId=A7.recordId AND A7.wildcardName=''
LEFT JOIN Table_name A8 ON A.recordId=A8.recordId AND A8.wildcardName=''
LEFT JOIN Table_name A9 ON A.recordId=A9.recordId AND A9.wildcardName=''
LEFT JOIN Table_name A10 ON A.recordId=A10.recordId AND A10.wildcardName=''
SQL Fiddle