Pivot control, can the IN clause be dynamic instead of hard-coded values?

I want to have a number of statements that use the PIVOT command can run dynamically without having to hardcode the values I want to be selected within the 'IN' of sql clause. Here's a simple example.

REGION_ID COLUMNS SALES_MONTH NUM_SALES

1 02/01/2010 12
2010-01-03 1, 11
1 04/01/2010 10
02/01/2010 2, 22
03/01/2010 2, 21
2 04/01/2010 20

The PIVOT command, I used is the following:

Select * from
(select * from sales)
PIVOT
(
Sum (num_sales)
for columns sales_month in (February 1, 2010", March 1, 2010", 1 April 2010 "))

This works very well for now, but as new months are created, I don't want to have to redo all my sql monthly statements. Any suggestions on how I can do this?

No, unless you use swivel XML:

select  *
  from  (
         select  job,
                 deptno,
                 sal
           from  emp
        ) pivot xml(sum(sal) for job in (select distinct job from emp))
/

    DEPTNO JOB_XML
---------- ------------------------------------------------------------
        10 ANALYSTCLERK1300MANAGER2450PR
           ESIDENT5000SALESMAN

        20 ANALYST6000CLERK1900<
           /item>MANAGER2975PRESIDENTSALESMAN

        30 ANALYSTCLERK950MANAGER2850PRE
           SIDENTSALESMAN5600

SQL> 

However, if you want it to "format" you still need to know the number of columns returned:

with t as (
           select * from (
                          select  job,
                                  deptno,
                                  sal
                            from  emp
                        ) pivot xml(sum(sal) for job in (select distinct job from emp))
          )
select  deptno,
        extractvalue(job_xml,'/PivotSet/item[1]/column[2]') job1,
        extractvalue(job_xml,'/PivotSet/item[2]/column[2]') job2,
        extractvalue(job_xml,'/PivotSet/item[3]/column[2]') job3,
        extractvalue(job_xml,'/PivotSet/item[4]/column[2]') job4,
        extractvalue(job_xml,'/PivotSet/item[5]/column[2]') job5
  from  t
/

    DEPTNO JOB1       JOB2       JOB3       JOB4       JOB5
---------- ---------- ---------- ---------- ---------- ----------
        10            1300       2450       5000
        20 6000       1900       2975
        30            950        2850                  5600

SQL> 

SY.

Tags: Database

Similar Questions

  • How can I loop instead of hard-coding this info

    Hi all

    I have the following code, how can I cfloop and dynamically out this info instead of having each time meeting? Thank you

    < name cfselect = "MEETINGTIME" width = "90" label = "Time" onchange = "UsersGrid.dataProvider.editField (UsersGrid.selectedIndex, 'MEETINGTIME', MEETINGTIME.selectedItem.data); ">

    < option value = "None" > no < / OPTION >

    < option value = '05:30 ' > 05:30 < / option >

    < option value = "06:00" > 06:00 < / option >

    < option value = "06:30" > 06:30 < / option >

    < option value = "07:00" > 07:00 < / option >

    < option value = '07:30 ' > 07:30 < / option >

    < option value = "08:00" > 08:00 < / option >

    < option value = '08:30 ' > 08:30 < / option >

    < option value = "09:00" > 09:00 < / option >

    < option value = "09:30" > 09:30 < / option >

    < option value = "10:00" > 10:00 < / option >

    < option value = "10:30" > 10:30 < / option >

    < option value = "11:00" > 11:00 < / option >

    < option value = "11:30" > 11:30 < / option >

    < option value = "12:00 AM" > 12:00 AM < / option >

    < option value = "12:30 AM" > 12 h 30 < / option >

    < option value = "01:00" > 01:00 < / option >

    < option value = "12:00" > 12:00 < / option >

    < option value = "12:30" > 12:30 < / option >

    < option value = "13:00" > 13:00 < / option >

    < option value = "13:30" > 13:30 < / option >

    < option value = "14:00" > 14:00 < / option >

    < option value = "14:30" > 14:30 < / option >

    < option value = "15:00" > 15:00 < / option >

    < option value = "15:30" > 15:30 < / option >

    < option value = "16:00" > 16:00 < / option >

    < option value = "16:30" > 16:30 < / option >

    < option value = "17:00" > 17:00 < / option >

    < option value = "17:30" > 17:30 < / option >

    < option value = '18:00 ' > 18:00 < / option >

    < option value = "18:30" > 18:30 < / option >

    < option value = "19:00" > 19:00 < / option >

    < option value = "19:30" > 19:30 < / option >

    < option value = "20:00" > 20:00 < / option >

    < option value = "20:30" > 20:30 < / option >

    < option value = "21:00" > 21:00 < / option >

    < option value = "21:30" > 21:30 < / option >

    < option value = "2200" > 22:00 < / option >

    < option value = "22:30" > 22:30 < / option >

    < option value = "23:00" > 23:00 < / option >

    < option value = "23:30" > 23:30 < / option >

    < / cfselect >

    I receive all the time into intervals of half an hour of date 8 - 5.30 but in evil! {ts ' 1899-12-30 08:00 '}

    It isn't the wrong date.  Given that you have specified a time only (IE '07:30 ') the "date part" default to 0 or 12/30/1899 .  If you need a time and a date, you must specify when you create the variables. For example:

    ... or

    BUT in your case, it does not have a difference because you do not use the date for what it is. So just use timeFormat() to display time only.

    That is to say #timeFormat (yourVariableName, 'HH tt') #.

  • Pass Pl/sql table in the USING clause in the EXECUTE IMMEDIATE statement

    Getting error when I try to pass the PL/SQL table in the USING clause in the EXECUTE IMMEDIATE statement:

    Declare
    result NUMBER;
    TYPE values_tab IS TABLE OF NUMBER INDEX OF directory;
    lv_tab values_tab;
    lv_exp varchar2 (300);
    lv_exec varchar2 (300);
    BEGIN
    lv_tab (1): = 5;
    lv_tab (2): = 48;
    lv_tab (3): = 7;
    lv_tab (4): = 6;
    lv_exp: = ': + b1: b2 + (: b3 *: b4)';
    lv_exec: = 'SELECT'. lv_exp | ' THE DOUBLE '.

    IMMEDIATE EXECUTION
    lv_exec
    IN
    result
    USING
    lv_tab;
    DBMS_OUTPUT. Put_line (result);

    END;
    /

    Error on line 1
    ORA-06550: line 20, column 12:
    PLS-00457: expressions must be SQL types
    ORA-06550: line 15, column 8:
    PL/SQL: Statement ignored


    I am trying to evaluate the expression ': + b1: b2 + (: b3 *: b4) "which is stored in the table. This table has different expressions (expressions about 300). I want to use the bind variable in the expression because each expression evaluated thousands of time may be more in some cases. If I use bind variable can he fill pool.

    Is there a way I can pass parameters with the HELP of (IN) dynamically instead of write "help lv_tab (1), lv_tab (2), lv_tab (3), lv_tab (4)? As number of change of the input parameters depend on the expression in the table.

    If it is possible please suggest any other ideas/approaches

    Help, please...

    Published by: satnam on June 11, 2009 11:50

    Well, you keep changing faster reqs that I can follow. In any case, assuming that N-th variable bind (left to right) corresponds to n-th collection item:

    Declare
        result NUMBER;
        lv_tab values_tab := values_tab();
        lv_exp varchar2(300);
        lv_exec varchar2(300);
        lv_i number := 0;
    BEGIN
        lv_tab.extend(4);
        lv_tab(1) := 5;
        lv_tab(2) := 48;
        lv_tab(3) := 7;
        lv_tab(4) := 6;
        lv_exp := ':5000135+:5403456+(:5900111*:5200456)';
        lv_exec := lv_exp;
        While regexp_like(lv_exec,':\d+') loop
          lv_i := lv_i + 1;
          lv_exec := REGEXP_REPLACE(lv_exec,':\d+',':b(' || lv_i || ')',1,1);
        end loop;
        lv_exec := 'BEGIN :a := ' || lv_exec || '; END;';
    DBMS_OUTPUT.PUT_LINE(lv_exec);
    EXECUTE IMMEDIATE lv_exec USING OUT result,IN lv_tab;
    DBMS_OUTPUT.PUT_LINE(result);
    END;
    /
    BEGIN :a := :b(1)+:b(2)+(:b(3)*:b(4)); END;
    95
    
    PL/SQL procedure successfully completed.
    
    SQL> 
    

    SY.

  • Indicator/control of the chain can be created running?

    I have a lettering that I need to create a number of indicators of chain / orders based on a particular digital selection made by the user. One thing I can do is create the number max of controls & use the Visible property node to display only selected No.. controls. But by doing this my lettering would become very complex. So if someone could suggest me how it can be running, because this will save memory also.

    Thanks in advance

    Agree with Ben and Thoric, you can create controls running, easily.

    How to handle your situation - it depends on your final goals - you can create enough channels, then toggle with the visible property, or maybe reorganize your application - use the list tables/multicolumn place of the strings, using arrays of strings, etc.

    In any case, a possible solution for creating control Runtime as an attachment. Here used XControl with linked list and this VI you can 'create' Boolean buttons dynamically. This idea is useful when you do not initially - know buttons how much you need in your application (I started with that for the dynamic creation of the toolbar). You can replace the button with the string in this example. Maybe it will be useful to you.

    Andrey.

  • Using Firefox 7.0.1; having problems with hotmail. Cannot click on controls at the top of MSG, which is New, reply, delete, etc.. Can click on the same commands @ the bottom of the problem w/no MSG. Also cannot click on Inbox folder once I have access to

    Using Firefox 7.0.1; having problems with hotmail. Cannot click on controls at the top of MSG, which is New, reply, delete, etc.. Can click on the same commands @ the bottom of the problem w/no MSG. Also cannot click on Inbox folder once I have access to another folder. All known solutions?

    This problem may be caused by the Yahoo! toolbar as scopes as well down and covers the top of the browser window, allowing links in this part of the screen not clickable.

    Start Firefox in Firefox to solve the issues in Safe Mode to check if one of the extensions or if hardware acceleration is the cause of the problem (switch to the DEFAULT theme: Firefox (Tools) > Add-ons > appearance/themes).

  • Mail app screen complete fillings of 27 inches. Cannot reduce it. Tried to restart the computer, but no change. All covered controls can then stop just hit control Q

    Mail app screen complete fillings of 27 inches. Cannot reduce it. Tried to restart the computer, but no change. All covered controls can then stop just hit control Q

    You should be able to move your cursor to the top of the screen and the menu bar will fall.  So, you start with

    Drag the slider to the top of the screen against the edge and hold that and you should see

    Click the button of "green".  This should take you out of mode full-screen Mail.

    Command + control + F should do it too.

    Also when you have the cursor at the top of the screen and the menu bar comes down, you could also use Mail-> view-> full screen output

    You should also be able to use the command + Tab to switch to another application

  • I connect my macbook pro to monitor via HDMI, but there is no sound control can do with the macbook, can you help me

    I connect my macbook pro to monitor via HDMI, but there is no sound control can do with the macbook, can anyone help?

    You must use the volume control on the monitor. That's how Apple means yo do and does not provide any method using Mac

  • Can the Format date/timestring service that can control the execution of the loop to a VI?

    Can the Format date/timestring service that can control the execution of the loop to a VI?

    Example,

    a VI is running at August 25, 2010 and print a txtdata(3)) '100825.

    Then follow when time system running at 2359hrs, August 25, 2010 at 0000hrs, August 26, 2010.

    and print a txtdata(3)) '100826.

    How?

    When the system changes a new date, problems occur.


  • How can I do a multiple selection in Enum control or control of the ring or control Combo box

    How can I do a multiple selection in Enum control or control of the ring or control Combo box

    You can not.

    You can use a listbox control.

    An alternative solution is to write the code and each selection, the user gives an enum, for example, can be send to a table and invite the user to continue to select...

  • I can't resize any object inside a tab control and control of the tab itself

    Hi all

    Since this morning I noticed that I can't always resize the object placed inside a tab control or the control tab itself.

    I have a few objects (controls, indicators, etc.) outside of the tab control, and they are resizable (I can see the dark blue squares and I am able to adjust the object); Instead, any object inside the tab control is still not resizable (when I select the dark blue boxes are not visible and I can't access the properties of the object by clicking with the right button of the mouse over it). If I have access to the properties of the object to the block diagram, the appearance of her-> frame size is gray and I can't change the size fields.

    It seems that something was changed when I try to adjust the minimum window size and appearance of windows in the proposals of VI, but I restore the previous values...

    How can I solve this problem?

    Sure, tab control is not locked.

    If the tab control is locked that resize is not possible

    Lucast85 wrote:

    Hi all

    Since this morning I noticed that I can't always resize the object placed inside a tab control or the control tab itself.

    I have a few objects (controls, indicators, etc.) outside of the tab control, and they are resizable (I can see the dark blue squares and I am able to adjust the object); Instead, any object inside the tab control is still not resizable (when I select the dark blue boxes are not visible and I can't access the properties of the object by clicking with the right button of the mouse over it). If I have access to the properties of the object to the block diagram, the appearance of her-> frame size is gray and I can't change the size fields.

    It seems that something was changed when I try to adjust the minimum window size and appearance of windows in the proposals of VI, but I restore the previous values...

    How can I solve this problem?

  • How can I disable my control of the administrator?

    HOW CAN I TURN OFF MY CONTROL ADMINISTRATOR. I have TRIED TO SIMPLY clicking the OTHER OPTION, BUT IT DO WILL NOT CHANGE. I HAVE TRIED TO CLOSE the COMPUTER towards the BOTTOM FOR RESTART AND THAT DOES NOT WORK EITHER

    By administrator, do you mean the user access control?  If so, I strongly recommend that you don't do that despite the indonvenience because it is an important element of the security of your system.

    But if you're going to do it anyway, here's the procedure; http://www.howtogeek.com/howto/windows-vista/disable-user-account-control-uac-the-easy-way-on-windows-vista/.

    I hope this helps and if you do against my strong recommendation that you don't have, you don't live to regret it later.

    Good luck!

    Lorien - MCSA/MCSE/network + / has + - if this post solves your problem, please click the 'Mark as answer' or 'Useful' button at the top of this message. Marking a post as answer, or relatively useful, you help others find the answer more quickly.

  • How can I disable the popup control in the user account that appears on my desktop to allow or reject the changes when I open a program without changing my user account settings.

    Original title: uac

    How can I disable the popup control in the user account that appears on my desktop to allow or reject the changes when I open a program without changing my user account settings.

    You want should not do that. These pop-up UAC is there to ensure the interaction of the user before executing a command or program. Disabling, allows even unwanted programs (= malware, viruses, Trojans) go ahead and implement themselves on your system without being noticed.

  • Clause type SQL how can the others cannot?

    Hi all

    I met a topic called the type Clause when I was reading Pro Oracle SQL by Karen Morton, Kerry Osborne, Robyn SandsRiyaj Ntanguand Jared still. I just read the chapter and did some practice on my local machine. It is obvious that it is a very powerful feature. And I also reachered on the web and found out that it is used for spreadsheets (excel etc.). Can someone please give me a simple example which, in which case one must use the clauses types SQL Analytic Functions or group Advanced functions cannot cure?

    Either said by the way, I recommend strongly Pro SQL Oracle book to all who want to learn the knowledge of SQL.

    Thanks in advance

    Hello

    In short Yes, the TYPE clause allows you to perform calculations of "spreadsheet-like". With the help of the clause TYPE is quite rare. Now with the introduction of the WITH recursive clause in 11g 2, you can almost everything in SQL, do not even talk about the pattern match feature introduced in 12 c.

    You can probably do now with analytical functions / recursive clause the clause of MODEL can do. And I must say that it is a shame that the TYPE clause has never been improved since its introduction in 10g.

    In my view, the clause TYPE can come in handy when you need cross reference calculations (typically dealing with periods such as dates, years, weeks). For example, I personally used a MODEL clause for an analysis of sales time, where I watch the sales of previous years to find a pattern for the current. References of cells in a MODEL clause allows to easily, something like [my_year, my_week] model = local_pattern [(y ENTRE CV()-2 ET CV) (-1, w BETWEEN (CV-2) RESUME AND () + 2)]

  • A way to return to the controls at the top of the files and thumbnails when hovering over the name of the file?  I can't even read the sticker because it has been moved to a separate column on the right.

    A way to return to the controls at the top of the files and thumbnails when hovering over the name of the file?

    I can't even read the sticker because it has been moved to a separate column on the right.

    Hi Bobca,

    I'm sorry, but the forecast of nail has been deprecated in the latest version due to security problems.

    Kind regards

    Nicos

  • You can move the inserted text to a different location on the picture, if yes how?  How can I put the character and the Panel clause up in the photo shop when you type test?

    You can move the inserted text to a different location on the picture, if yes how?  How can I put the character and the Panel clause up in the photo shop when you type test?

    Yes.  I find it easier to type text, and then use the move tool (shortcut V) (looks like an arrow) to place it where you want.

    To bring up the character and paragraph panels go to WINDOW > CHARACTER or WINDOW > PARAGRAPH.

    I hope this helps.

Maybe you are looking for

  • Changing the date of a video of iOS

    I have a problem that I have not been able to solve. I downloaded a few scanned Super 8 movies from the late seventies to my photos using iCloud for Windows. It seems that icloud sorts the date they have been downloaded. I used a winning tool to chan

  • Satellite L300-1AQ - 9 Will stack need more space?

    Hi someone Genius! Please can you convert my confusion. My current battery is a unit of 6 in Satellite L300-1AQ. It became dead and hard to sustain for 30 minutes. I decided to replace it and ordered a new 9-cell PA3535U-1BRS battery. Now I am confus

  • Lenovo Y50-70: Crash suddenly

    The machine is not even 2 weeks after the first start, and he already has a few problems... Laughing out loud 3-4 accidents were from the days where there is no blue screen or overheating or suspended upward, just a pop hard and instant stop. It happ

  • Trackpad/point does not not after the withdrawal of the keyboard

    Earlier today I opened my T510 for maintenance. In doing so, I removed the keyboard. After all together, the trackpoint/trackpad didn't work. This includes the the * beep *-all the buttons of the mouse mouse and trackpad. The trackpad was not yet rem

  • Please help with the problem of deployment OTA

    Hello I'm trying to deploy my application (for the first time) using a live approach. First of all, let me mention that I developed this application using the BB for Eclipse plugin.  It generated files jad and cod. I have 5 files of cod and jad for t