Naming the evaluated case statement field with concatenating input variable value

Hi,
I am trying to add the input variable value to case statement evaluated field like below query:
declare @Year varchar(4)
select ProductCode,datepart(mm,Date) MonthValue,
  case when datepart(mm,Date) = 1 then sum(Price) else 0 END AS Jan+@Year,
  case when datepart(mm,Date) = 2 then sum(Price) else 0 END AS Feb+@Year,
  case when datepart(mm,Date) = 3 then sum(Price) else 0 END AS Mar+@Year,
  case when datepart(mm,Date) = 4 then sum(Price) else 0 END AS Apr+@Year,
  case when datepart(mm,Date) = 5 then sum(Price) else 0 END AS May+@Year,
  case when datepart(mm,Date) = 6 then sum(Price) else 0 END AS Jun+@Year,
  case when datepart(mm,Date) = 7 then sum(Price) else 0 END AS Jul+@Year,
  case when datepart(mm,Date) = 8 then sum(Price) else 0 END AS Aug+@Year,
  case when datepart(mm,Date) = 9 then sum(Price) else 0 END AS Sep+@Year,
  case when datepart(mm,Date) = 10 then sum(Price) else 0 END AS Oct+@Year,
  case when datepart(mm,Date) = 11 then sum(Price) else 0 END AS Nov+@Year,
  case when datepart(mm,Date) = 12 then sum(Price) else 0 END AS Dec+@Year
from Sales_Imports where datepart(yyyy,Date) = @Year
group by  ProductCode, datepart(mm,Date)
Please help me any other way as this is not working...
Thanks,
Srini

create table Sales_Imports (ProductCode int, Price int, [date] date)
insert into Sales_Imports values
(1,10,'1/1/2014'),(1,109,'2/1/2014'),(1,10,'1/1/2014'),(1,109,'12/1/2014'),
(2,10,'1/1/2014'),(2,109,'2/1/2014'),(2,10,'10/1/2014'),(2,109,'12/1/2014')
declare @Year varchar(4)='2014'
DECLARE @MaxCount INT, @SQL NVARCHAR(max) ,@i INT;
Set @MaxCount = 12 
SET @i = 0;
SET @SQL = '';
WHILE @i < @MaxCount
BEGIN
    SET @i = @i + 1;
    SELECT @SQL = @Sql + ', SUM(CASE WHEN datepart(mm,[Date]) = ' + cast(@i AS NVARCHAR(10)) + ' THEN  Price Else 0 END) AS ' +Left(DateName(month,Dateadd(month,@i-1,0)) ,3)+@Year
END
SET @SQL = N' SELECT  ProductCode, datepart(mm,Date) MonthValue ' + @SQL 
+ N' FROM  Sales_Imports
 where datepart(yyyy,Date) ='+ @Year + N'group by  ProductCode, datepart(mm,Date)
 Order by ProductCode, datepart(mm,Date)';
--PRINT @SQL;
execute (@SQL);
--Or your original one
declare @Year varchar(4)
Set @Year ='2014'
declare @SQL  Nvarchar(4000)
SET @SQL=
N' select ProductCode,datepart(mm,Date) MonthValue,
  case when datepart(mm,Date) = 1 then sum(Price) else 0 END AS [jan' +@year+'],
  case when datepart(mm,Date) = 2 then sum(Price) else 0 END AS [feb' +@year+'],
  case when datepart(mm,Date) = 3 then sum(Price) else 0 END AS [Mar'+@year+'],
  case when datepart(mm,Date) = 4 then sum(Price) else 0 END AS [Apr'+@year+'],
  case when datepart(mm,Date) = 5 then sum(Price) else 0 END AS [May'+@year+'],
  case when datepart(mm,Date) = 6 then sum(Price) else 0 END AS [june'+@year+'],
  case when datepart(mm,Date) = 7 then sum(Price) else 0 END AS [july'+@year+'],
  case when datepart(mm,Date) = 8 then sum(Price) else 0 END AS [Aug'+@year+'],
  case when datepart(mm,Date) = 9 then sum(Price) else 0 END AS [Sep'+@year+'],
  case when datepart(mm,Date) = 10 then sum(Price) else 0 END AS [Oct'+@year+'],
  case when datepart(mm,Date) = 11 then sum(Price) else 0 END AS [Nov'+@year+'],
  case when datepart(mm,Date) = 12 then sum(Price) else 0 END AS [Dec'+@year+']
from Sales_Imports where datepart(yyyy,Date) = ' + @Year +N' group by  ProductCode, datepart(mm,Date)'
--print @Query
Execute (@SQL)
drop table Sales_Imports

Similar Messages

  • Automatic tab when reaching the end of a field with limited character number

    Hello!
    Is there a way of activating an automatic tab behaviour when the user reaches the end of a field with limited character number?
    I'd like the cursor to jump to the next field in tab order automatically.
    Possible?
    Thank you for any hints!
    Marcos

    Hi,
    What if you tried something like the following example? This works using formCalc on the change event for the field the user is currently typing into:
        var fullCount = 3
        var currentString = xfa.event.newText
        if(Len(currentString) >= fullCount)then
            xfa.host.setFocus("TextField2")
        endif
    Or, if you prefer javaScript:
        var fullCount = 3;
        var currentString = xfa.event.newText;
        if(currentString.length >= fullCount){
            xfa.host.setFocus("TextField2");
    These work very smoothly.
    Hope this helps!
    Stephen

  • Change the widht of a field with lingo

    Is it possible to adjust the widht of a field with lingo? Or adjust the length of the field to the text content?
    Thanks.

    Thenks Sean,
    I have two field set to "fixed" for a pop up menu.
    The script for the first field is this:
    property variabileLinea
    on mouseDown
      sprite(me.spriteNum).member = member("MyField1")
    end
    on mouseUp me
      sprite(me.spriteNum).member = member("MyField3")
      member("MyField2").line[1] = member("MyField1").Line[variabileLinea]
    end
    on EnterFrame me
      variabileLinea = _mouse.mouseLine
      member("MyField1").Line[variabileLinea].hilite()
    end
    "MyField1" adjust the lenght of the field to the text content, "MyField2"  after the mouse up no. Any suggestion?
    Thanks.

  • Call Transaction and fill a select-options field with more than one value?

    Hello everybody,
    how can I fill a select-options field with more than one value.
    Here is the code example:
      CLEAR: GT_BDCDATA, GS_BDCDATA.
      GS_BDCDATA-program = 'RHALEINI'.
      GS_BDCDATA-DYNPRO = '1000'.
      GS_BDCDATA-DYNBEGIN = 'X'.
      APPEND GS_BDCDATA TO GT_BDCDATA.
      CLEAR: GS_BDCDATA.
      GS_BDCDATA-FNAM = 'PCHOTYPE'.
      GS_BDCDATA-FVAL = 'P'.
      APPEND GS_BDCDATA TO GT_BDCDATA.
      CLEAR: GS_BDCDATA.
      LOOP AT gt_hrobjinfty INTO gs_hrobjinfty.
        GS_BDCDATA-FNAM = 'PCHOBJID-LOW'.
        GS_BDCDATA-FVAL = gs_hrobjinfty-objid.
        APPEND GS_BDCDATA TO GT_BDCDATA.
        CLEAR: GS_BDCDATA.
      ENDLOOP.
      CALL TRANSACTION 'PFAL' USING GT_BDCDATA MODE 'A'
                       MESSAGES INTO GT_MESSAGES.
    THX.

    Hi,
    Please refer the code below:
    *Code used to populate 'select-options' & execute report
    DATA: seltab type table of rsparams,
          seltab_wa like line of seltab.
      seltab_wa-selname = 'PNPPERNR'.
      seltab_wa-sign    = 'I'.
      seltab_wa-option  = 'EQ'.
    * load each personnel number accessed from the structure into
    * parameters to be used in the report
      loop at pnppernr.
        seltab_wa-low = pnppernr-low.
        append seltab_wa to seltab.
      endloop.
      SUBMIT zreport with selection-table seltab
                                    via selection-screen.
    Thanks,
    Sriram Ponna.

  • Show or Hide fields based upon a variable value

    Hi,
    I am a rookie in the field of Acrobat. And my only question is :
    How to Circle a Form Field based upon a variable value .
    For example, If x=1, I want to circle Japan and if x=0, I want to leave it without any change.
    Please help me out. Thanks for your time and help.
    Thanks,
    Vijayvijay77.

    Hi George,
    I am pre-populating a Pdf file based upon the data submitted by the user. And I have to circle options selected in the form.
    I am writing coldfusion code to flatten, populate the form into a PDF form. The end result is the PDF generation with all the data in it.
    The data comes from the database, where I set variable values if any is selected or not.
    Thanks for your reply, I really appreciate your help.
    Vijayvijay77.

  • Conditional Expressions and the note about state-field's serialized form

    There's a note about state-field's serialized form in 4.6 Conditional expression chapter. Could anyone help me understanding it? It's on the page 88.
    State-fields that are mapped in serialized form or as lobs may not be portably used in conditional expressions[26].
    What's the serialized form? I think I understand what lobs might mean, but some additional explanation or a code snippet would be greatly appreciated.
    Jacek
    Jacek Laskowski
    http://www.JacekLaskowski.pl

    But that's my point. In the original expression the ++k and --k are evaluated first so the whole thing goes down as you described.  I agree completely.  But in the second case the term k is a urnary operator which would have higher precedence than the + or --- operators and therefore would be evaluated first while k is still equal to 1. Then again maybe I'm confused !
    Maybe I'll just compile the program and see what I get...

  • How to change the caption of a field with a button?

    I have serious trouble, don't know why, in order to change the caption of a field.
    I can't do it with a button as in any other way...dropdown list, ecc...
    Assumes that we have a button and "on click" i want to change the caption of a numeric or text field.
    form1.ppp.Button1::click - (FormCalc, client)
    NumericField1.caption.value.text.value = "whatever you want to set it to";
    TextField1.caption.value.text.value = "whatever you want to set it to";
    xfa.form.form1.ppp.NumericField1.rawValue = xfa.form.form1.ppp.NumericField1.rawValue + 1;
    The numerical value inside the numericalfield1 is increased each click...but the caption still the same.
    Why this happen?
    I check many threads on this forum and on google and i still find these samples...
    please help.
    Thank you

    Yes, it is. Also in preview i checked "dynamic XML". is it right?
    i try to download this sample while searching in the forum to solve my problem
    https://acrobat.com/#d=pxtis4z3OxCqWxajZwDS2Q
    i try to add to that form my code and it's work!! i can't undersand why...
    Below you can find my PDF...i can't understand why here the same code doesn't work
    http://www.hsc350.it/download/Untitled2.pdf
    Thank you again!

  • Is the RMS server should have the "health service state" folder with some folders and files?

    we are using scom 2007 sp1 and we experienced issues with our RMS last yesterday. i dont see any content within the "Health Service State" directory even after restarting the health service.
    i dont see any valuble information from even logs as well. From where can i start the troubleshootings?
    C:\Program Files\System Center Operations Manager 2007\Health Service State

    Hi,
    Please try to find the "health service state" by checking the key “State Directory” under the following location of Registry:
    HKLM\System\CurrentControlSet\Services\HealthService\Parameters
    Thanks.
    Nicholas Li
    TechNet Community Support

  • How to write the following CASE statement

    I need to calculate Retainer amount. The derivation of it is as follows:
    "A fixed value of 10000 per month for employees who have served for less than 2 years and 15000 for more than 2 years. This increment will be affected in two cycles a year, in the January or July."
    The years of service can be calculated using date_of_joining.

    Use this logic
    SELECT DECODE(SIGN(SYSDATE - ADD_MONTHS(HIREDATE,24)),-1,10000,1,15000) from <table_name>Is this you need
    In every Jan/July month employee get a Sal+Fixed allownce (10000 if less than 2 years in company/15000 if more than 2 years). In other month they get just the salary
    SELECT CASE WHEN TO_CHAR(SYSDATE,'MM') IN ('01','07') THEN
                   CASE WHEN SYSDATE- ADD_MONTHS(HIREDATE,24)> 0 THEN
                        SAL+15000
                    ELSE
                        SAL+10000
                    END
            ELSE
              SAL
            END  SAL
    FROM EMP
    /Edited by: Lokanath Giri on १७ सितंबर, २०१० ४:५६ अपराह्न

  • Union two tables with diffrent count of fields with null and float value

    Hello,
    i want to union two tables, first one with 3 fields and second one with 4 fields (diffrent count of fields).
    I can add null value at end of first select but it does not work with float values in second table. Value form second table is convert to integer.
    For example:
    select null v1 from sessions
    union
    select 0.05 v1 from sessions
    result is set of null and 0 value.
    As workaround i can type:
    select null+0.0 v1 from sessions
    union
    select 0.05 v1 from sessions
    or simple change select's order.
    Is any better/proper way to do this? Can I somehow set float field type in first select?
    Best regards,
    Lukasz.
    WIN XP, MAXDB 7.6.03

    Hi Lukasz,
    in a UNION statement the first statement defines the structure (number, names and types of fields) of the resultset.
    Therefore you have to define a FLOAT field in the first SELECT statement in order to avoid conversion to VARCHAR.
    Be aware that NULL and 0.0 are not the same thus NULL+0.0 does not equal NULL.
    In fact NULL cannot equal to any number or character value at all.
    BTW: you may want to use UNION ALL to avoid the search and removal of duplicates - looks like your datasets won't contain duplicates anyhow...
    Regards,
    Lars

  • AutoPopulating a field with a string plus value from another field

    Hi: I have a table with two fields
    create table LINKREQUEST (
    requestid int primary key,
    INCIDENT_NUMBER varchar2(16),
    REQUESTID is auto populated using a sequence. I want to auto populate the INCIDENT field with the string "INC" plus the value from REQUESTID. Can someone help.
    TIA
    Ravi

    REQUESTID is auto populated using a sequence. I want to auto populate the INCIDENT field with the string "INC" plus the value from REQUESTIDSomething like this?:
    SQL> create table linkrequest (
    requestid int primary key,
    incident_number varchar2(16)
    Table created.
    SQL> create sequence linkrequest_seq
    Sequence created.
    SQL> create or replace trigger linkrequest_trg before insert on linkrequest
    for each row
    begin
    select linkrequest_seq.nextval,
            case when :new.incident_number = 'INC' then 'INC' || linkrequest_seq.nextval else :new.incident_number end
       into :new.requestid, :new.incident_number from dual;
    end;
    Trigger created.
    SQL> insert into  linkrequest (incident_number) values ('XY')
    1 row created.
    SQL> insert into  linkrequest (incident_number) values ('INC')
    1 row created.
    SQL> insert into  linkrequest (incident_number) values ('VW')
    1 row created.
    SQL> insert into  linkrequest (incident_number) values ('INC')
    1 row created.
    SQL> select * from linkrequest
    REQUESTID INCIDENT_NUMBER
             1 XY             
             2 INC2           
             3 VW             
             4 INC4           
    4 rows selected.

  • How can I display only one value of a field with two or more values?

    Post Author: skiabox
    CA Forum: Crystal Reports
    I have a field in my report with 2 or more values (depending of another field id).For example for id = 1 the report gives me 2 names in that field.For id = 3 the report gives me 3 names in that field.I want to display only one of these names in the id row.The selection of name is random.Thanks!

    Hello Tim,  would barely fit in this situation since this code resides on the client’s interaction side of things. I’d recommend using JavaScript for this matter, e.g. var Data_FName1 = document.getElementById(‘Data_FName1’).value;. If you still opt for using CFML, then you’d go for proxying your JavaScript code to a CFC.

  • How to query a datetime field with a datetime offset value

    Hi Experts
    I have a problem where I want to query a date time field with a value with a date time offset, and convert the value to the stored timezone.
    The functionality I am after is that the user enters the value 30/08/2011 22:14:46 -4:00 and the report converts it into the timezone of the stored field and performs the query using the date time filter and the local equivilent of the date time entered.
    Has anyone had experience in date time offsets and OBIEE?
    suggestions appreciated.
    Cheers

    Hi,
    Use below one
    cast the timestampadd (sql_tsi_year, -1, timestampadd (sql_tsi_day, -1, current_timestamp)) to a date and see if it
    drops the current time stamp and makes it 00:00:00
    Thanks
    Deva

  • Performance issues with query input variable selection in ODS

    Hi everyone
    We've upgraded from BW 3.0B to NW04s BI using SP12.
    There is a problem encountered with input variable selection. This happens regardless of using BEx (new or old 3.x) or using RSRT. When using the F4 search help (or "Select from list" in BEx context) to list possible values, this takes forever for large ODS (containing millions of records).
    Using ST01 and SM50 to trace the code in the same query, we see a difference here:
    <u>NW04s BI SQL command</u>
    SELECT                                                                               
    "P0000"."COMP_CODE" AS "0000000032" ,"T0000"."TXTMD" AS "0000000032_TXTMD"                             
    FROM                                                                               
    ( "/BI0/PCOMP_CODE" "P0000" ) LEFT OUTER JOIN "/BI0/TCOMP_CODE" "T0000" ON  "P0000"."COMP_CODE" = "T0000
      "."COMP_CODE"                                                                               
    WHERE                                                                               
    "P0000"."OBJVERS" = 'A' AND "P0000"."COMP_CODE" IN ( SELECT "O"."COMP_CODE" AS "KEY" FROM              
      "/BI0/APY_PP_C100" "O" )                                                                               
    ORDER BY                                                                               
    "P0000"."COMP_CODE" ASC#                                                                               
    <u>BW 3.0B SQL command:</u>
    SELECT ROWNUM < 500 ....
    In 3.0B, rownum is limited to 500 and this results in a speedy, though limited query. In the new NW04s BI, this renders the selection screen unusable as ABAP dumps for timing out will occur first due to the large data volume searched using sequential read.
    It will not be feasible to create indexes for every single query selection parameter (issues with oerformance when loading, space required etc.). Is there a reason why SAP seems have fallen back on a less effective code for this?
    I have tried to change the number of selected rows to <500 in BEx settings but one must reach a responsive screen in order to get to that setting and it is not always possible or saved for the next run.
    Anyone with similar experience or can provide help on this?

    here is a reason why the F4 help on ODS was faster in BW 3.x.
    In BW 3.x the ODS did not support the read mode "Only values in
    InfoProvider". So If I compare the different SQL statements I propose
    to change the F4 mode in the InfoProvider specific properties to
    "About master data". This is the fastest F4 mode.
    As an alternative you can define indexes on your ODS to speed up F4.
    So would need a non-unique index on InfoObject 0COMP_CODE in your ODS
    Check below for insights
    https://forums.sdn.sap.com/click.jspa?searchID=6224682&messageID=2841493
    Hope it Helps
    Chetan
    @CP..

  • Invoking a LiveCycle process with multiple input variables using REST

    Hi all,
    A simple scenario brushup:
    I have an LC process that takes as input 2 parameters:
    - formID
    - formXML
    The user starts out with an empty interactive form and fills out some info in this form. In the end he/she clicks submit and now I want to invoke my process to create a flattened form from the XML just entered in the interactive form.
    The process returns a flattened form with the data merged with the XDP template referenced in formID. Simple as that.
    What I basically need is to do something similar to the following in a LiveCycle Designer Form:
    ------------------------------------Snippet from http://help.adobe.com/en_US/livecycle/10.0/ProgramLC/WS624e3cba99b79e12e69a9941333732bac8- 7fe5.html ---------------------------------------------------------------------
    <html>
    <body>
    <form name="input" action="http://localhost:9080/rest/services/RestTest3" method="post" 
             enctype="multipart/form-data">
    Doc: <input type="file" name="inDoc">
    String 1: <input type="text" name="inListOfStrings" value="hello">
    String 2: <input type="text" name="inListOfStrings" value="privet">
    <input type="submit" value="Submit"/>
    </form>
    </body>
    </html>
    ------------------------------------End snippet from http://help.adobe.com/en_US/livecycle/10.0/ProgramLC/WS624e3cba99b79e12e69a9941333732bac8- 7fe5.html ---------------------------------------------------------------------
    Now my question to you is (hope you can help out here):
    How do I invoke this process from my form using the REST endpoint - I need to specify 2 input variables for my process. One string (formID) and some xml (formXML). How is this done in Designer?
    Thanks in advance
    Kim Christensen
    Dafolo A/S
    Denmark

    Bump...
    Nobody who has an idea of how to do this?
    Thanks in advance
    Kim Christenen
    Dafolo A/S
    Denmark

Maybe you are looking for