COUNT the same column multiple time with 2 different condition

Hi everyone,
I have a small query but I don't find the answer. Can anyone help me with this one ? Look at this example:
WITH T AS (
  SELECT 'a' as Name, 1 as v FROM DUAL
  UNION ALL
  SELECT 'a' as Name, 2 as v FROM DUAL
  UNION ALL
  SELECT 'a' as Name, 2 as v FROM DUAL
    UNION ALL
  SELECT 'b' as Name, 1 as v FROM DUAL
    UNION ALL
  SELECT 'b' as Name, 1 as v FROM DUAL
    UNION ALL
  SELECT 'b' as Name, 2 as v FROM DUAL
) SELECT DISTINCT name,
         COUNT(DECODE(v, 1, 1, null)) OVER (PARTITION BY name ORDER BY v) as v1,
         COUNT(DECODE(v, 2, 1, null)) OVER (PARTITION BY name ORDER BY v) as v2
    FROM T
  ORDER BY 1;This returns :
NAME V1                     V2                    
a    1                      0                     
a    1                      2                     
b    2                      0                     
b    2                      1                      But what I need is only one row with the count of 1 and 2 by name. How can I achieve that without using subqueries like (the following query returns the correct output)
WITH T AS (
  SELECT 'a' as Name, 1 as v FROM DUAL
  UNION ALL
  SELECT 'a' as Name, 2 as v FROM DUAL
  UNION ALL
  SELECT 'a' as Name, 2 as v FROM DUAL
    UNION ALL
  SELECT 'b' as Name, 1 as v FROM DUAL
    UNION ALL
  SELECT 'b' as Name, 1 as v FROM DUAL
    UNION ALL
  SELECT 'b' as Name, 2 as v FROM DUAL
) SELECT DISTINCT t.name,
         (SELECT COUNT(*) FROM T z WHERE z.name = t.name and z.v = 1),
         (SELECT COUNT(*) FROM T z WHERE z.name = t.name and z.v = 2)
    FROM T t
  ORDER BY 1;The table I'm working on has millions of records and with subqueries it takes ages to executes. The one with analytical function takes less than 3 sec.
Thanks for your help!

Hi,
user13117585 wrote:
Hi everyone,
I have a small query but I don't find the answer. Can anyone help me with this one ? Look at this example:
WITH T AS (
SELECT 'a' as Name, 1 as v FROM DUAL
UNION ALL
SELECT 'a' as Name, 2 as v FROM DUAL
UNION ALL
SELECT 'a' as Name, 2 as v FROM DUAL
UNION ALL
SELECT 'b' as Name, 1 as v FROM DUAL
UNION ALL
SELECT 'b' as Name, 1 as v FROM DUAL
UNION ALL
SELECT 'b' as Name, 2 as v FROM DUAL
) SELECT DISTINCT name,
COUNT(DECODE(v, 1, 1, null)) OVER (PARTITION BY name ORDER BY v) as v1,
COUNT(DECODE(v, 2, 1, null)) OVER (PARTITION BY name ORDER BY v) as v2
FROM T
ORDER BY 1;But what I need is only one row with the count of 1 and 2 by name. How can I achieve that without using subqueries Use the aggregate COUNT function instead of the analytic COUNT:
SELECT   name,
          COUNT(DECODE(v, 1, 1, null))  as v1,
          COUNT(DECODE(v, 2, 1, null))  as v2
     FROM T
GROUP BY  name
ORDER BY  name;

Similar Messages

  • Running the same code multiple times with different paramters automatica​lly

    Hi guys,
    I want to run the same code multiple times with different paramters automatically. Actually, I am doing some sample scans which takes around 2 hours n then I have to be there to change the paramters and run it again. Mostly I do use folowing paramters only
    X_Intial, X_Final, X-StepSize
    Y_Intial, Y_Final, Y-StepSize
       Thanks,
    Dushyant

    All you have to di is put all of the parameters for each run into a cluster array. Surround your main program with a for loop and wire the cluster array through the for loop. A for loop will autoindex an input array so inside the for loop you just have to unbundle the cluster to get the parameters for each run.
    Message Edited by Dennis Knutson on 07-13-2006 07:50 AM
    Attachments:
    Cluster Array.JPG ‏9 KB

  • How do I use the same track multiple times in different playlists? I want to rename it multiple times.

    Hi, I am an entertainer that want rename a track a music a number of different ways so it will come up in playlist that work alphabeticly.
    for instance:
    Good Song I want to change to:
    A Good Song
    B Good Song
    C Good Song
    D Good Song
    So I can place them in different playlist and keep the order straight.
    Todd

    You can't give a single track multiple sets of properties, but if you want four copies of a song with different metadata then find the original and copy/paste it three times into the Automatically Add to iTunes folder, then edit the properties of each in turn.
    tt2

  • Is it possible to purchase the same song multiple times with iTunes?

    Now that I have purcahsed a good deal of songs thru itunes it is difficult to keep track of what I have already bought. Will itunes alert me to songs that I have already purchased if I try to buy them again? I know I can keep checking back in the "Purchased Music" playlist but this takes a lot of clicking back and forth. Is there an easier way?

    If you try to buy a song you have already bought, it should pop up a warning and say you have done so. You can ignore the warning and buy the song anyway, however.
    Note that this only works for the same exact version of the song. If you have a different version, like the clean/explicit difference, you get no warning.

  • My itunes has got the same album 5 times with different songs on each one. How do i get all the songs from 1 album altogther?, My itunes has got the same album 5 times with different songs on each one. How do i get all the songs from 1 album altogther?

    I am having problems with my itunes. I have purchase an album from itunes but it has been seperated so i have 3 songs from the album on 1 album the 3 songs on another and the 1 song on another. How do i get all the songs on 1 album like it should normally do?

    Try these Discussions...
    https://discussions.apple.com/message/10990796#10990796
    https://discussions.apple.com/message/21989767#21989767

  • Is it possible to copy the same database multiple times simultaneously?

    This is the setup of my environment.
    I have a 'master' database that contains the full schema plus some pre-populated data such as default settings. When database schema changes are made, they get made to the master database. When a new customer signs up for the service, I make a copy of this
    master database (using SMO) that will now be the database containing all of the customer's information. The database is rather large, and the copy operation can take a couple minutes.
    Everything works fine as expected, unless I try to provision two new customers at the same time. It appears that Azure only allows for one DB copy operation at a time. Is there a way to allow for copying the same database multiple times simultaneously?
    Thanks for you input. I understand that this is likely not the optimal setup, and that there is many better ways of doing this, but I am somewhat heavily invested in the current process and I would like to find out how to make it work if I can.

    BTW, you can do this programatically by using PowerShell (https://msdn.microsoft.com/en-us/library/ee210569.aspx), SqlPackage (http://www.benday.com/2012/12/18/deploy-a-sql-server-database-projects-dacpac-with-sqlpackage-exe/)
    or the SQL Server binaries for .NET (http://www.vijayt.com/Post/Deploying-a-data-tier-application-in-SQL-Azure-programatically)
    Hope this helps.
    Alex

  • Why is Apple TV showing the same computer multiple times for Home Sharing?

    Why is my Apple TV showing the same computer multiple times for Home Sharing? Also have connectivity issues. Sometimes will lose signal.
    JonRod's music
    JonRod's music
    JonRod's music
    JonRod's music
    JonRod's music
    I recently updated to Yosemite and never had this issue before.

    I'm not 100% sure of the ins and outs, but it's some sort of known issue between Yosemite, Airport and wifi. All you can do is wait for a fix and keep restarting your equipment as and when needed, unless you have the ability to connect your Mac via ethernet.

  • Can I gift the same app multiple times to multiple users?

    Can I gift the same app multiple times to multiple users?
    Like, if I wanted to buy iMovie for 3 friends, could I buy the app as a gift 3 times?

    You should be able to gift it multiple times
    Gifting content : http://support.apple.com/kb/HT2736

  • TS3276 how do I get Mail to stop downloading the same email multiple times?

    My mail keeps downloading the same email multiple times.  I've had as many as 10 of the same emial.  How do I stop this?

    Me too - did you ever figure it out?
    You can't even delete from the server in mountain lion.... I did it from my old mac, but it didn't work. I keep having 11,000 new emails that can not even be deleted in blocks!!!
    Was it a gmail account? Mine is, and my other 2 are fine...

  • How to automate saving the same image multiple times?

    Hello. I wish to save the same image multiple times in a folder... is there a way to automate this function? I also need the flexibility of determining the starting number in this image sequence. Thanks.

    Good day!
    I would recommend asking for help over at
    Photoshop Scripting
    And going into more detail about what you want to achieve (maybe post a screenshot, diagram, mock-up to illustrate it).
    Regards,
    Pfaffenbichler

  • Apply the same LCR two times on different tables

    Hi all
    Just one question:
    Can I apply the same LCR two times on different tables using only one apply queue?
    For example executing two times LCR.EXECUTE(TRUE) in a procedure handler:
    LCR.EXECUTE(TRUE);
    LCR.EXECUTE(TRUE);
    ....

    Topas,
    You need to create new LCR and execute both LCRs.
    Regards,
    Serge

  • Executing a program multiple times with different variants

    Hi All,
    I want to create a tool which executes a program multiple times taking different variants (which I would be
    maintaining in an excel sheets) and for each time the program gets executed, its output would be
    written to a particular file.
    I understand that this requirement is quite vague but I am trying to figure out if a tool/program could
    be written in abap which does this or do I need to think of a different platform such as C#.Net
    Thanks,
    Faiz

    Hi Rahman,
    This class 'cl_salv_bs_runtime_info' will get the output of internal table.
       DO n times.
          cl_salv_bs_runtime_info=>set
          SUBMIT ZARCHER_0099 WITH VARIANT..(variant name can from your excel sheets?)
          cl_salv_bs_runtime_info=>GET_DATA
          call 'GUI_DOWNLOAD'  or OPEN DATASET...(down output to file)
       ENDDO.
    Hope can help you.
    regards,
    Archer

  • Can the RV180W have multiple SSIDs with different security configurations?

    I am trying to configure the RV180W with a guest network and regular wireless network. The regular wireless network is just a bridge to the wired network, using WPA2-Pers for authentication. I built and enabled another wireless SSID, using a different VLAN and no authentication. I can get both SSIDs to function at the same time if I turn off security. Once I turn on Security, the regular one no longer functions.

    It is actually all in the manual:
    SEE: PDF MANUAL
    Page 63 of PDF and onwards
    Do note that you need to assign multiple VLAN per SSID. Check the manual it is there :D
    and based on the manual you need to enable multiple VLAN support: See page 34 of the manual: Configuring Virtual LAN (VLAN) Membership
    Don't forget to rate and mark as answer helpful posts! :)

  • Can I use the "same" button multiple times for multiple galleries?

    OK so I am extremely untrained in CS4 and Actionscript. However I have managed to get along fairly well until I started to dynamically upload images as a gallery. This works great if I have one gallery, but for my site I have 9 galleries!!! I have a back and next button, but I want to be able to use those same buttons for all of the galleries so they look the same. I have split them up and renamed them, but I am clueless on how to script the buttons to work. Please help...and don't laugh at my poor scripting. This is what I have now because I do not know where to put the other button names without getting errors.
    stop();
    next_btn .addEventListener(MouseEvent.CLICK, nextImage);
    var imageNumber: Number=1;
    function checkNumber(): void{
        next_btn.visible=true;
        back_btn.visible=true;
        if(imageNumber==15){
            trace(imageNumber);
        next_btn.visible=false;
        if(imageNumber==1){
            trace(imageNumber);
        back_btn.visible=false;
    function nextImage(evtObj:MouseEvent):void {
        imageNumber++;
        mc_engagement.source= "photo/engagement/en0"+imageNumber+".jpg";
        mc_amish.source= "photo/amish/Amish"+imageNumber+".jpg";
        mc_chicago.source= "photo/chicago/ch"+imageNumber+".jpg";
        mc_landscapes.source= "photo/landscapes/land"+imageNumber+".jpg";
        mc_goodvsevil.source= "photo/goodvsevil/ge"+imageNumber+".png";
        mc_animals.source= "design/animals/an"+imageNumber+".png";
        mc_icons.source= "design/icons/icon0"+imageNumber+".png";
        mc_objects.source= "design/objects/pc"+imageNumber+".png";
        mc_typography.source= "design/typography/type"+imageNumber+".png";
        checkNumber();
    back_btn .addEventListener(MouseEvent.CLICK, backImage);
    function backImage(evtObj:MouseEvent):void {
        imageNumber--;
        mc_engagement.source= "photo/engagement/en0"+imageNumber+".jpg";
        mc_amish.source= "photo/amish/Amish"+imageNumber+".jpg";
        mc_chicago.source= "photo/chicago/ch"+imageNumber+".jpg";
        mc_landscapes.source= "photo/landscapes/land"+imageNumber+".jpg";
        mc_goodvsevil.source= "photo/goodvsevil/ge"+imageNumber+".png";
        mc_animals.source= "design/animals/an"+imageNumber+".png";
        mc_icons.source= "design/icons/icon0"+imageNumber+".png";
        mc_objects.source= "design/objects/pc"+imageNumber+".png";
        mc_typography.source= "design/typography/type"+imageNumber+".png";
        checkNumber();

    I'm still a novice with Flash myself, but I have two comments.
    First, at this point, won't your buttons control all galleries at the same time? Which means, if I go to image 5 in one gallery, then move to another gallery, I'll start at image 5, because the actions are all connected.
    Second, you can definitely use the same buttons for multiple galleries. It seems to me that as long as you've assigned the buttons an instance name, these actions should already work to control all the galleries (see the note above). I guess I would need to understand a bit more about how your project is built.

  • Printing the same page multiple times on one sheet

    So last year I wanted to print out minature versions of my card design and I was able to do that by printing out multiple copies of the same thing on one sheet of paper. Of course that was a while ago and now today I can't do the same thing. I remember printing the same page(copy) multiple times either in acrobat or indesign. I see that I have a front and back version for last years card and I'm assuming I must have done it in acrobat. Last year when I looked at the preview there was multiple copies of my page. This year I only see 1 even as I choose the other things. At first I thought it might have been the version I was using on my windows as opposed to the version on my mac. But now I'm on my mac and I still don't see how I did it last year. Please help! I keep reading that this can't be done but I remember doing the same thing last year. It was the 16 copies of the same thing option. I tried it again this time but it doesn't work. I tried printing the 4 copies only 1 copy was printed.(the preview didn't even show all copies like last year) The verson of acrobat is X. version 10.1.4

    What exact version of iPhoto 9 are you using? Version 9.5's solution will be different from the solution for earlier versions of iPhoto 9.
    OT

Maybe you are looking for

  • Macbook pro cannot connect to internet

    Hi, My Macbook pro 13" will not connect to the internet via wifi or ethernet. I have tried about everything I can think of. I reset the modem, tried another network other than mine. Compared the settings between my Imac that will connect and the macb

  • Problem with CVI blocking UART Callback

    Hi, I have been using LabWindows CVI 9.0 since a few years now and I have seen a problem that didn't blocked me until now. My LabWindows Software freezes, the code is no longer executed, when I hold a click on the interface, on a string for example.

  • Session state in websheets?

    Hi guys, I fear that it is not possible, just wanted to make sure. I haven't found a way to maintain session state. For example to include a combo box to store a selection in it (e.g. a Project identifier). Also, it looks like we cannot reference any

  • DRM Import, Action Script

    Hi All, I am new to Hyperion DRM 11.1.2, I am confused about concepts such as metadata and data still not able to understand difference between them. What I understand is all Versions hold is set of independent hierarchies which have nodes and their

  • Change Business Partner Number or copy

    Hi experts!, I need to change some business partner numbers or in another hand creates a copy of business partner but different number. It's possible? A lot of thanks in advance. Regards.