Solved

SQL Query CASE statement in SELECT

Posted on 2014-10-13
1
195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 34

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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Duplicated data in GROUP_CONCAT 2 52
Connect to SQL 2008 r2 server over the Internet 4 85
SQL Server remove line breaks and tabbed 2 26
t-sql left join 2 34
In this article I will describe the Backup & Restore 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.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 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