Solved

SQL Query CASE statement in SELECT

Posted on 2014-10-13
1
189 Views
Last Modified: 2014-10-19
I have two problems and I believe it has to do with my query in the CASE statement. First one is how to get the parname value to show up in my parname without having to add it in SELECT, because if I add it in SELECT as a column, my xml output is messed up. Second is how I can tweak my CASE such that the group GID element is present only when parname = setgroup.

My table:
MODNM | CARNM | PARTID   | PARNAME  | SEQ | TYPE | VALUE                     | FLAG
-----------------------------------------------------------------------------------
M-880 | HONA  | 29000153 | SETGROUP | 1   | A    | 3                         | P
M-880 | HONA  | 29000153 | *CDMZI   | 2   | A    | F%+A8E=,,,0;+MS=V123,456; | P
M-880 | HONA  | 29000153 | DMTZ     | 3   | A    |                           | P
M-880 | HONA  | 29000153 | CDRFCP   | 4   | A    | 0                         | P
M-880 | HONA  | 29000153 | SETGROUP | 5   | A    | 15                        | P
M-880 | HONA  | 29000153 | #PT      | 5   | 6    | NONE                      | P
M-880 | HONA  | 29000153 | CDH6     | 7   | A    | 100.111.100.44            | P
M-880 | HONA  | 29000153 | CDEPORT  | 8   | A    | 9003                      | P

Open in new window


Desired XML Output Format:
<Device PartID="29000153" MODNM="M-880">
  <Car Name="HONA">
    <Group GID="3">
      <Pmeter ParName="*CDMZI" Value="F%+A8E=,,,0;+MS=V123,456;" Type="A" Flag="P" />
      <Pmeter ParName="DMTZ" Value="" Type="A" Flag="P" />
      <Pmeter ParName="CDRFCP" Value="0" Type="A" Flag="P" />
    </Group>
    <Group GID="15">
      <Pmeter ParName="#PT" Value="NONE" Type="A" Flag="P" />
      <Pmeter ParName="CDH6" Value="100.111.100.44" Type="A" Flag="P" />
      <Pmeter ParName="CDEPORT" Value="9003" Type="A" Flag="P" />
    </Group>
  </Car>
  <DevFiles />
</Device>

Open in new window


Actual Output Format:
<Device PartID="29000153" MODNM="M-880">
  <Car Name="HONA">
    <Group GID="3">
	  <Pmeter ParName="0" Value="3" Type="A" Flag="P" />
	</Group>
    <Group GID="F%+A8E=,,,0;+MS=V123,456;">	
      <Pmeter ParName="1" Value="F%+A8E=,,,0;+MS=V123,456;" Type="A" Flag="P" />
      <Pmeter ParName="1" Value="" Type="A" Flag="P" />
      <Pmeter ParName="1" Value="0" Type="A" Flag="P" />
    </Group>
    <Group GID="15">
	  <Pmeter ParName="0" Value="15" Type="A" Flag="P" />	
    </Group>	
    <Group GID="NONE">	
      <Pmeter ParName="1" Value="NONE" Type="A" Flag="P" />
      <Pmeter ParName="1" Value="100.111.100.44" Type="A" Flag="P" />
      <Pmeter ParName="1" Value="9003" Type="A" Flag="P" />
    </Group>
  </Car>
  <DevFiles />
</Device>

Open in new window


Query:
<cfquery name="getParts" ....>
    SELECT PARTID
			, MODNUM
            , CARNM
            , CASE WHEN PARNAME = 'SETGROUP' THEN 0 ELSE 1 END AS PARTGroup
			, VALUE
			, TYPE
			, FLAG
    FROM   PMETER 
    WHERE  PARTID IN ('29000153')
    ORDER BY PARTID
            , CARNM
            , SEQ
            , PARTGroup
</cfquery>

Open in new window


Generate XML:
<cfprocessingdirective suppresswhitespace="Yes">
<cfcontent type="text/xml; charset=utf-8">
<cfxml variable="xmlobject">
<cfoutput query="getParts" group="PARTID">
<Device PartID="#PARTID#" MODNM="#MODNUM#">
   <cfoutput group="CARNM">
      <Car Name="#CARNM#">
      <cfoutput group="PARTGroup">  
         <Group GID="#VALUE#">
         <cfoutput>
         <Pmeter ParName="#PARNAME#" Value="#VALUE#" Type="#TYPE#" Flag="#FLAG#" />
         </cfoutput>
         </Group>
     </cfoutput>
     </Car>
   </cfoutput>
</Device>
</cfoutput>
</cfxml>

<cfoutput>#xmlobject#</cfoutput>
</cfprocessingdirective>

Open in new window

0
Comment
Question by:bman2011
1 Comment
 
LVL 33

Accepted Solution

by:
ste5an earned 500 total points
ID: 40379439
Why are you not doing it in T-SQL?

DECLARE @Sample TABLE
    (
      MODNM NVARCHAR(255) ,
      CARNM NVARCHAR(255) ,
      PARTID INT ,
      PARNAME NVARCHAR(255) ,
      SEQ INT ,
      [TYPE] NVARCHAR(255) ,
      VALUE NVARCHAR(255) ,
      FLAGN VARCHAR(255)
    );


INSERT  INTO @Sample
VALUES  ( 'M-880', 'HONA', 29000153, 'SETGROUP', 1, 'A', '3', 'P' ),
        ( 'M-880', 'HONA', 29000153, '*CDMZI', 2, 'A', 'F%+A8E=,,,0;+MS=V123,456;', 'P' ),
        ( 'M-880', 'HONA', 29000153, 'DMTZ', 3, 'A', NULL, 'P' ),
        ( 'M-880', 'HONA', 29000153, 'CDRFCP', 4, 'A', '0', 'P' ),
        ( 'M-880', 'HONA', 29000153, 'SETGROUP', 5, 'A', '15', 'P' ),
        ( 'M-880', 'HONA', 29000153, '#PT', 5, '6', 'NONE', 'P' ),
        ( 'M-880', 'HONA', 29000153, 'CDH6', 7, 'A', '100.111.100.44', 'P' ),
        ( 'M-880', 'HONA', 29000153, 'CDEPORT', 8, 'A', '9003', 'P' );

WITH    Data
          AS ( SELECT   O.MODNM ,
                        O.CARNM ,
                        O.PARTID ,
                        O.PARNAME ,
                        O.SEQ ,
                        O.TYPE ,
                        O.VALUE ,
                        O.FLAGN ,
                        ( SELECT TOP 1
                                    CAST([VALUE] AS INT) 
                          FROM      @Sample I
                          WHERE     I.PARTID = O.PARTID
                                    AND I.MODNM = O.MODNM
                                    AND I.CARNM = O.CARNM
                                    AND I.PARNAME = 'SETGROUP'
                                    AND I.SEQ <= O.SEQ
                          ORDER BY  I.SEQ DESC
                        ) AS GID
               FROM     @Sample O
               WHERE    O.PARNAME <> 'SETGROUP'
             )
    SELECT  S1.PARTID AS [@PartID] ,
            S1.MODNM AS [@MODNM] ,
            S1.CARNM AS [Car/@Name] ,
            ( SELECT    S2.GID AS [@GID] ,
                        ( SELECT    S3.PARNAME AS [@ParName] ,
                                    S3.VALUE AS [@Value] ,
                                    S3.[TYPE] AS [@Type] ,
                                    S3.FLAGN AS [@Flag]
                          FROM      Data S3
                          WHERE     S3.PARTID = S2.PARTID
                                    AND S3.MODNM = S2.MODNM
                                    AND S3.CARNM = S2.CARNM
                                    AND S3.GID = S2.GID
                          ORDER BY  S3.SEQ
						  FOR		XML PATH('Pmeter') , TYPE
                        )
              FROM      Data S2
              WHERE     S2.PARTID = S1.PARTID
                        AND S2.MODNM = S1.MODNM
                        AND S2.CARNM = S1.CARNM
              GROUP BY  S2.PARTID ,
                        S2.MODNM ,
                        S2.CARNM ,
                        S2.GID              
              FOR		XML PATH('Group') , TYPE
            ) AS [Car]
    FROM    Data S1
    GROUP BY S1.PARTID ,
            S1.MODNM ,
            S1.CARNM
	FOR     XML PATH('Device');

Open in new window


Result is

<Device PartID="29000153" MODNM="M-880">
  <Car Name="HONA">
    <Group GID="3">
      <Pmeter ParName="*CDMZI" Value="F%+A8E=,,,0;+MS=V123,456;" Type="A" Flag="P" />
      <Pmeter ParName="DMTZ" Type="A" Flag="P" />
      <Pmeter ParName="CDRFCP" Value="0" Type="A" Flag="P" />
    </Group>
    <Group GID="15">
      <Pmeter ParName="#PT" Value="NONE" Type="6" Flag="P" />
      <Pmeter ParName="CDH6" Value="100.111.100.44" Type="A" Flag="P" />
      <Pmeter ParName="CDEPORT" Value="9003" Type="A" Flag="P" />
    </Group>
  </Car>
</Device>

Open in new window

0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question