Book a Demo

Author Topic: SQL pivot  (Read 5337 times)

RusDavies

  • EA Novice
  • *
  • Posts: 16
  • Karma: +1/-0
    • View Profile
SQL pivot
« on: February 18, 2016, 03:53:55 am »
Hello.
In a EAP repository, I have a bunch of TOGAF stakeholders defined.  Each has various tagged values.   I want to make a nice little ModelView for a dashboard out of that information.

So, I've started with a query, as follows:
Code: [Select]
SELECT o.Object_ID, o.Name, tag.Property, tag.Value
FROM (t_object o INNER JOIN t_objectproperties tag ON o.Object_ID = tag.Object_ID)
WHERE o.Stereotype = "Stakeholder"
AND tag.Property IN ("Class", "CurrentCommitment", "LevelOfInterest", "Power", "RequiredSupport")

That gets me a list of properties vs stakeholders, with one property per row, something like this:
Object_IDNamePropertyValue
123CEOClassHigh
123CEOPowerKeep Satisfied
123CEOLevelOfInterestHigh
124CIOClassHigh
124CIOPowerKeep Satisfied
124CIOLevelOfInterestHigh


But what I want is one row per stakeholder, like this:
Object_IDNameClassPowerLevelOfInterest
123CEOHighKeep SatisfiedHigh
124CIOHighKeep SatisfiedHigh

So, clearly, I need to pivot the data.  Doing so *should* be trivial.

Various errors received when goofing around suggest EA to be using the Microsoft Jet Database Engine under the hood.  Therefore, this is how I expect to pivot the data:

Code: [Select]
TRANSFORM First(tag.Value)
SELECT o.Object_ID, o.Name, tag.Property, tag.Value
FROM (t_object o INNER JOIN t_objectproperties tag ON o.Object_ID = tag.Object_ID)
WHERE o.Stereotype = "Stakeholder"
AND tag.Property IN ("Class", "CurrentCommitment", "LevelOfInterest", "Power", "RequiredSupport")
GROUP BY o.Object_ID, o.Name
PIVOT tag.Property

But, when I run this query, the result is nothing.  Nada.  I don't receive any errors, yet it doesn't do anything.  In fact, it doesn't even clear the output from any previous successful query. 

So, what am I doing wrong?  Is my SQL way off?  Or is EA limited in some way? And in either case, why no error?

UPDATE: I see from the "Inside Enterprise Architect" book sample from LeanPub, that "[The SQL Editor window] accepts only SELECT statements. Any other SQL (like e.g. UPDATE) is silently ignored!".   Is the issue then that my query begins with TRANSFORM, rather than SELECT? 

UPDATE 2:   I was proofing my SQL via the SQL Scratch Pad of the Project Search Tool, where it could be saved without error but failed to run.  However, if the same query is used in the source for an actual ModelView, then saving the query generates the error "Unable to save Model View Data, invalid Custom SQL. Only Select statements are supported."   It seems then that the issue is indeed the use of the Transform construct, (together with EA being inconsistent about error reporting).   Therefore, how does one perform a pivot in EA?  Does this mean I now have to create (maintain/distribute/etc) a whole Search-Addin to perform what should be a trivial pivot?
« Last Edit: February 18, 2016, 04:35:32 am by RusDavies »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: SQL pivot
« Reply #1 on: February 18, 2016, 04:44:52 am »
Hi Rus,

You can do so by joining the same table multiple times.
Something like this:

Code: [Select]
SELECT o.ea_guid AS CLASSGUID, o.Object_Type AS CLASSTYPE, o.Object_ID, o.Name,
clTag.Value AS Class, ccTag.Value AS CurrentCommitment, liTag.Value as LevelOfInterest, pTag.Value as Power, rsTag.Value as RequiredSupport
FROM (((((t_object o
INNER JOIN t_objectproperties clTag ON ( o.Object_ID = clTag.Object_ID
and clTag.Property = 'Class'))
inner join t_objectproperties ccTag on ( o.Object_ID = ccTag.Object_ID
and ccTag.Property = 'CurrentCommitment'))
inner join t_objectproperties liTag on ( o.Object_ID = liTag.Object_ID
and liTag.Property = 'LevelOfInterest'))
inner join t_objectproperties pTag on ( o.Object_ID = pTag.Object_ID
and pTag.Property = 'Power'))
inner join t_objectproperties rsTag on ( o.Object_ID = rsTag.Object_ID
and rsTag.Property = 'RequiredSupport'))
WHERE o.Stereotype = 'Stakeholder'

Geert

RusDavies

  • EA Novice
  • *
  • Posts: 16
  • Karma: +1/-0
    • View Profile
Re: SQL pivot
« Reply #2 on: February 18, 2016, 04:53:38 am »
Ho ho!  Yeap, that's what I needed, perfect.  Thanks Geert.