Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Query CASE statement in SELECT

Posted on 2014-10-13
1
Medium Priority
?
199 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 35

Accepted Solution

by:
ste5an earned 2000 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

715 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