Extracting xml data from CLOB in Oracle and Pivoting - sql

I have a table that has a CLOB column with the following data:
<entry xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<content type="application/xml">
<m:properties>
<d:Parameters>
<data>
<parameter>
<id>{1234}</id>
<value>1000</value>
</parameter>
<parameter>
<id>{3456}</id>
<value>John Doe</value>
</parameter>
<parameter>
<id>{6789}</id>
<value>NY</value>
</parameter>
</data>
</d:Parameters>
</m:properties>
</content>
</entry>
The ID values are stored in a separate table where {3456} would be «Name » etc.
I need to retrieve data from multiple rows and pivot it so the result is
{1234} {3456}. {6789}
1000. John Doe. NY
1001. Jane Doe. DE
1002. Ivan Riveras. NJ
And potentially replace {1234} strings with actual column names from another table such as ID, Name and State
How can I do that with a query?
So far I have
SELECT xmltype(t1.column1).extract(“//data/text()”)
FROM Table1 t1
But how do I break it down further and pivot?

Extracting the values is fairly straightforward; as you want multiple node values from each XML document you can use XMLTable:
select x.code, x.value
from table1 t1
cross join xmltable(
'/data/parameter'
passing xmltype(t1.column1)
columns
code path 'id',
value path 'value'
) x;
which with two sample documents gives:
CODE
VALUE
{1234}
1000
{3456}
John Doe
{6789}
NY
{1234}
1001
{3456}
Jane Doe
{6789}
DE
You can then pivot those; but really you need a way to link the values together. If your table has a unique key then use that; otherwise you can use the first table's rowid as a stand-in:
select rowidtochar(t1.rowid), x.code, x.value
from table1 t1
cross join xmltable(
'/data/parameter'
passing xmltype(t1.column1)
columns
code path 'id',
value path 'value'
) x;
Either way, use that value internally to pivot, but don't include it in the final select list:
select id, name, state
from (
select rowidtochar(t1.rowid), x.code, x.value
from table1 t1
cross join xmltable(
'/data/parameter'
passing xmltype(t1.column1)
columns
code path 'id',
value path 'value'
) x
)
pivot (
max(value)
for code in ('{1234}' as id, '{3456}' as name, '{6789}' as state)
);
ID
NAME
STATE
1000
John Doe
NY
1001
Jane Doe
DE
fiddle
I've hard-coded the mapping between ID values and column headings in the IN() clause; you said
And potentially replace {1234} strings with actual column names from another table such as ID, Name and State
but that would requite a dynamic pivot, as #astentx said in a comment.
As a middle-ground, if you know you will have ID/name/state but don't know what the codes are and want to look up the mapping then you could do that before pivoting, with something like:
select id, name, state
from (
select rowidtochar(t1.rowid), t2.label, x.value
from table1 t1
cross join xmltable(
'/data/parameter'
passing xmltype(t1.column1)
columns
code path 'id',
value path 'value'
) x
join table2 t2 on t2.code = to_number(translate(x.code, 'x{}', 'x'))
)
pivot (
max(value)
for label in ('ID' as id, 'Name' as name, 'State' as state)
);
fiddle
The inner query now translates the original id from the XML to a label from your look-up table, and that is then used to pivot.
But if you don't know the label values, or how many rows you will pivot into how many columns, then you're still stuck with a dynamic pivot; or having a reporting too do that for you, perhaps.
Sorry, I did not post the full xml. Just added the rest
Your updated XML has namespaces, so you need to declare those:
select id, name, state
from (
select rowidtochar(t1.rowid), x.code, x.value
from table1 t1
cross join xmltable(
xmlnamespaces(
default 'http://www.w3.org/2005/Atom',
'http://schemas.microsoft.com/ado/2007/08/dataservices' as "d",
'http://schemas.microsoft.com/ado/2007/08/dataservices/metadata' as "m"
),
'/entry/content/m:properties/d:Parameters/data/parameter'
passing xmltype(t1.column1)
columns
code path 'id',
value path 'value'
) x
)
pivot (
max(value)
for code in ('{1234}' as id, '{3456}' as name, '{6789}' as state)
);
does the first parameter of the xmltable need to be the full path or can I use //?
You can still use
'//data/parameter'
with the namespaces declared, but I generally prefer to use the full path for clarity.
fiddle

Related

SQL merging rows with dynamic column headings

I am trying to populate a Gridview to have checkboxes enabled per student, but depending to certain values from this query:
#SelectedDate is provided via a TextBox as a date only
SELECT v1.StudentID,
v1.StudentPreferredName + ' ' + v1.StudentFamilyName AS StudentName,
bcs.CheckStatusName,
rce.DateSubmitted,
rcp.RollCallPeriod
FROM tblBoardingRollCallEntries AS rce
INNER JOIN vwBoardingTenants AS v1
ON v1.StudentID = rce.StudentID
AND v1.[Year] = YEAR(#SelectedDate)
INNER JOIN tblBoardingCheckStatus AS bcs
ON bcs.CheckStatusID = rce.CheckStatusID
AND bcs.StatusActive = 1
INNER JOIN tblBoardingRollCallPeriods AS rcp
ON rcp.RollCallPeriodID = rce.RollCallPeriodID
AND rcp.PeriodYear = YEAR(#SelectedDate)
AND #SelectedDate BETWEEN rcp.PeriodStart AND rcp.PeriodEnd
AND rcp.RowStatus = 1
WHERE dbo.fnDateOnly(rce.DateSubmitted) = dbo.fnDateOnly(#SelectedDate)
My gridview:
Shows the following:
The data:
I want to be able to basically condense the rows in the GridView to be one student per row and the checkboxes ticked according to RollCallPeriod text.
I am playing with SQL pivots, to get the data to be as close as possible to what I am after so as to avoid code-behind, etc. However, I cannot get this to work.
select StudentID, [1],[10],[2],[3],[4],[5],[6],[7],[8],[9]
from
(
select StudentID, RollCallID, CheckStatusID
from tblBoardingRollCallEntries
unpivot
(
value for name in ([RollCallID],[StudentID],[CheckStatusID],[DateSubmitted],[StaffID])
) unpiv
) src
pivot
(
sum(RollCallPeriodID)
for RollCallPeriodID in ([1],[10],[2],[3],[4],[5],[6],[7],[8],[9])
) piv
I receive the following error:
Lookup Error - SQL Server Database Error: The type of column
"StudentID" conflicts with the type of other columns specified in the
UNPIVOT list.
Any other ideas?
Thanks
A couple of ways you can do this depending on your actual data.
This will give you the CheckStatusName as the value for the RollCallPeriod
SELECT *
FROM (
SELECT StudentName,
CheckStatusName,
RollCallPeriod
FROM [YourQueryGoesHere]
) t
PIVOT (
MAX(CheckStatusName)
FOR RollCallPeriod IN ([6:15 AM],[8:00 AM],[3:00 PM],[6:00 PM],[9:00 PM])
) p
Or you get the status and a COUNT() to show if that Student has a value for that CheckStatusName, RollCallPeriod
SELECT *
FROM (
SELECT StudentName,
CheckStatusName,
RollCallPeriod
FROM [YourQueryGoesHere]
) t
PIVOT (
COUNT(RollCallPeriod)
FOR RollCallPeriod IN ([6:15 AM],[8:00 AM],[3:00 PM],[6:00 PM],[9:00 PM])
) p
Two options:
Instead of unpivotting directly on tblBoardingRollCallEntries: first select columns cast to a VARCHAR(...) type in a derived table, then UNPIVOT the derived table. Shortened example:
select StudentID, RollCallID, CheckStatusID
from
(
SELECT ..., CAST(StudentId AS VARCHAR(128)) AS StudentId, ... FROM tblBoardingRollCallEntries)
) AS ups
unpivot
(
value for name in ([RollCallID],[StudentID],[CheckStatusID],[DateSubmitted],[StaffID])
) unpiv
Use CROSS APPLY (SELECT CAST(StudentId AS VARCHAR(128)) UNION ALL ... ) to unpivot, that way you can UNPIVOT casting the column directly to the appropriate type.

Comma separated column ids should show values or text(with function or query)

I have a table like this
Foreign table:
select * from table1
ID......NameIds
-------------------
1 ......1, 2 (its comma separated values)
Primary table(table2)
ID Name
-------------------
1 Cleo
2 Smith
I want to show table 1 as like (I require SQL function or query for it)
ID......NameIds
-------------------
1........Cleo, smith (show text/Name instead of values)
As per stated in comments - you should really rethink your table design, but it was interesting enough to try and write a query for that:
SELECT T1.ID, NameID, Name
INTO #Temporary
FROM #Table1 AS T1
CROSS APPLY (
SELECT CAST(('<X>' + REPLACE(T1.NameIDs, ',', '</X><X>') + '</X>') AS XML)
) AS X(XmlData)
CROSS APPLY (
SELECT NameID.value('.', 'INT')
FROM XmlData.nodes('X') AS T(NameID)
) AS T(NameID)
INNER JOIN #Table2 AS T2
ON T2.ID = T.NameID
SELECT ID, STUFF(T.Names, 1, 1, '') AS Names
FROM #Table1 AS T1
CROSS APPLY (
SELECT ',' + Name
FROM #Temporary AS T
WHERE T.ID = T1.ID
ORDER BY T.NameID
FOR XML PATH('')
) AS T(Names)
Result:
ID Names
--------------
1 Cleo,Smith
What it does, it splits your comma seperated list into rows, joins them on NameIDs and then concatenates them again. Guess how efficient is that?
It's probably not the most best way to do that, but it works.

Unpivoting multiple columns

I have a table in SQL Server 2014 called anotes with the following data
and I want to add this data into another table named final as
ID Notes NoteDate
With text1, text2, text3, text4 going into the Notes column in the final table and Notedate1,notedate2,notedate3,notedate4 going into Notedate column.
I tried unpivoting the data with notes first as:
select createdid, temp
from (select createdid,text1,text2,text3,text4 from anotes) p
unpivot
(temp for note in(text1,text2,text3,text4)) as unpvt
order by createdid
Which gave me proper results:
and then for the dates part I used another unpivot query:
select createdid,temp2
from (select createdid,notedate1,notedate2,notedate3,notedate4 from anotes) p
unpivot (temp2 for notedate in(notedate1,notedate2,notedate3,notedate4)) as unpvt2
which also gives me proper results:
Now I want to add this data into my final table.
and I tried the following query and it results into a cross join :(
select a.createdid, a.temp, b.temp2
from (select createdid, temp
from (select createdid,text1,text2,text3,text4 from anotes) p
unpivot
(temp for note in(text1,text2,text3,text4)) as unpvt) a inner join (select createdid,temp2
from (select createdid,notedate1,notedate2,notedate3,notedate4 from anotes) p
unpivot (temp2 for notedate in(notedate1,notedate2,notedate3,notedate4)) as unpvt) b on a.createdid=b.createdid
The output is as follows:
Is there any way where I can unpivot both the columns at the same time?
Or use two select queries to add that data into my final table?
Thanks in advance!
I would say the most concise, and probably most efficient way to unpivot multiple columns is to use CROSS APPLY along with a table valued constructor:
SELECT t.CreatedID, upvt.Text, upvt.NoteDate
FROM anotes t
CROSS APPLY
(VALUES
(Text1, NoteDate1),
(Text2, NoteDate2),
(Text3, NoteDate3),
(Text4, NoteDate4),
(Text5, NoteDate5),
(Text6, NoteDate6),
(Text7, NoteDate7)
) upvt (Text, NoteDate);
Simplified Example on SQL Fiddle
ADDENDUM
I find the concept quite a hard one to explain, but I'll try. A table valued constuctor is simply a way of defining a table on the fly, so
SELECT *
FROM (VALUES (1, 1), (2, 2)) t (a, b);
Will Create a table with Alias t with data:
a b
------
1 1
2 2
So when you use it inside the APPLY you have access to all the outer columns, so it is just a matter of defining your constructed tables with the correct pairs of values (i.e. text1 with date1).
Used the link above mentioned by #AHiggins
Following is my final query!
select createdid,temp,temp2
from (select createdid,text1,text2,text3,text4,text5,text6,text7,notedate1,notedate2,notedate3,notedate4,notedate5,notedate6,notedate7 from anotes) main
unpivot
(temp for notes in(text1,text2,text3,text4,text5,text6,text7)) notes
unpivot (temp2 for notedate in(notedate1,notedate2,notedate3,notedate4,notedate5,notedate6,notedate7)) Dates
where RIGHT(notes,1)=RIGHT(notedate,1)
Treat each query as a table and join them together based on the createdid and the fieldid (the numeric part of the field name).
select x.createdid, x.textValue, y.dateValue
from
(
select createdid, substring(note, 5, len(note)) fieldId, textValue
from (select createdid,text1,text2,text3,text4 from anotes) p
unpivot
(textValue for note in(text1,text2,text3,text4)) as unpvt
)x
join
(
select createdid, substring(notedate, 9, len(notedate)) fieldId, dateValue
from (select createdid,notedate1,notedate2,notedate3,notedate4 from anotes) p
unpivot (dateValue for notedate in(notedate1,notedate2,notedate3,notedate4)) as unpvt2
) y on x.fieldId = y.fieldId and x.createdid = y.createdid
order by x.createdid, x.fieldId
The other answer given won't work if you have too many columns and the rightmost number of the field name is duplicated (e.g. text1 and text11).

How to determine what fields were update in an update trigger

UPDATE: Using Update_Columns() is not an answer to this question, as the fields may change in the order which will break the trigger (Update_Columns depends on the column order).
UPATE 2: I already know that the Deleted and Inserted tables hold the data. The question is how to determine what has changed without having to hard code the field names as the field names may change, or fields may be added.
Lets say I have a table with three fields.
The row already exists, and now the user updates fields 1 and 2.
How do I determine, in the Update Trigger, what the field were updated, and what the before and after values where?
I want to then log these to a log table. If there were two fields update, it should result in two rows in the history table.
Table
Id intField1 charField2 dateField3
7 3 Fred 1995-03-05
Updated To
7 3 Freddy 1995-05-06
History Table
_____________
Id IdOfRowThatWasUpdated BeforeValue AfterValue (as string)
1 7 Fred Freddy
2 7 1995-03-05 1995-05-06
I know I can use the Deleted table to Get the old values, and the inserted table to get the new values. The question however, is how to do this dynamically. In other words, the actual table has 50 columns, and I don't want to hard code 50 fields into a SQL statement, and also if the fields change, and don't want to have to worry about keeping the SQL in sync with table changes.
Greg
you can use one of my favorite XML-tricks to do this:
create trigger utr_Table1_update on Table1
after update, insert, delete
as
begin
with cte_inserted as (
select id, (select t.* for xml raw('row'), type) as data
from inserted as t
), cte_deleted as (
select id, (select t.* for xml raw('row'), type) as data
from deleted as t
), cte_i as (
select
c.ID,
t.c.value('local-name(.)', 'nvarchar(128)') as Name,
t.c.value('.', 'nvarchar(max)') as Value
from cte_inserted as c
outer apply c.Data.nodes('row/#*') as t(c)
), cte_d as (
select
c.ID,
t.c.value('local-name(.)', 'nvarchar(128)') as Name,
t.c.value('.', 'nvarchar(max)') as Value
from cte_deleted as c
outer apply c.Data.nodes('row/#*') as t(c)
)
insert into Table1_History (ID, Name, OldValue, NewValue)
select
isnull(i.ID, d.ID) as ID,
isnull(i.Name, d.Name) as Name,
d.Value,
i.Value
from cte_i as i
full outer join cte_d as d on d.ID = i.ID and d.Name = i.Name
where
not exists (select i.value intersect select d.value)
end;
sql fiddle demo
In this post:
How to refer to "New", "Old" row for Triggers in SQL server?
It is mentioned that/how you can access the original and the new values, and if you can access, you can compare them.
"INSERTED is the new row on INSERT/UPDATE. DELETED is the deleted row on DELETE and the updated row on UPDATE (i.e. the old values before the row was updated)"

Converting a pivot table to a flat table in SQL

I would like to transform a pivot table into a flat table, but in the following fashion: consider the simple example of this table:
As you can see, for each item - Address or Income -, we have a column for old values, and a column for new (updated values). I would like to convert the table to a "flat" table, looking like:
Is there an easy way of doing that?
Thank you for your help!
In order to get the result, you will need to UNPIVOT the data. When you unpivot you convert the multiple columns into multiple rows, in doing so the datatypes of the data must be the same.
I would use CROSS APPLY to unpivot the columns in pairs:
select t.employee_id,
t.employee_name,
c.data,
c.old,
c.new
from yourtable t
cross apply
(
values
('Address', Address_Old, Address_new),
('Income', cast(income_old as varchar(15)), cast(income_new as varchar(15)))
) c (data, old, new);
See SQL Fiddle with demo. As you can see this uses a cast on the income columns because I am guessing it is a different datatype from the address. Since the final result will have these values in the same column the data must be of the same type.
This can also be written using CROSS APPLY with UNION ALL:
select t.employee_id,
t.employee_name,
c.data,
c.old,
c.new
from yourtable t
cross apply
(
select 'Address', Address_Old, Address_new union all
select 'Income', cast(income_old as varchar(15)), cast(income_new as varchar(15))
) c (data, old, new)
See Demo
select employee_id,employee_name,data,old,new
from (
select employee_id,employee_name,adress_old as old,adress_new as new,'ADRESS' as data
from employe
union
select employee_id,employee_name,income_old,income_new,'INCOME'
from employe
) data
order by employee_id,data
see this fiddle demo : http://sqlfiddle.com/#!2/64344/7/0