I am having issue in group by using decode and case in pl/sql

my query is some thing like this but having more column in select. when i am firing this query it is giving result but that is not proper
my problem is , like if there are 3 more values for uh.sflowtype (0,1,2) then group by is not working for them and those are coming in different row , i need them to be combined
query is :
select substr(uh.sstartdatetime,1,8) DateTime,
( case
when uh.sflowtype=7 then 'sms'
when uh.sflowtype=9 then 'mms'
when uh.sflowtype=10 then 'gprs'
else 'voice'
end )
as flowtype from e_vpn_usagehistory uh where 1=1 and uh.nspid='1' AND ((substr(uh.sstartdatetime,1,8) >= 20130507 )
AND (substr(uh.sstartdatetime,1,8) <= 20130606)) GROUP BY substr(uh.sstartdatetime,1,8),uh.sflowtype
order by substr(uh.sstartdatetime,1,8) DESC
result :
DATETIME FLOWTYPE
20130507 voice
20130507 voice
20130507 voice
20130507 sms
20130507 mms
but i need
20130507 voice
20130507 sms
20130507 mms
so what should i do?
please suggest me

996962 wrote:
my query is some thing like this but having more column in select. when i am firing this query it is giving result but that is not proper
my problem is , like if there are 3 more values for uh.sflowtype (0,1,2) then group by is not working for them and those are coming in different row , i need them to be combined
query is :
select substr(uh.sstartdatetime,1,8) DateTime,
( case
when uh.sflowtype=7 then 'sms'
when uh.sflowtype=9 then 'mms'
when uh.sflowtype=10 then 'gprs'
else 'voice'
end )
as flowtype from e_vpn_usagehistory uh where 1=1 and uh.nspid='1' AND ((substr(uh.sstartdatetime,1,8) >= 20130507 )
AND (substr(uh.sstartdatetime,1,8) <= 20130606)) GROUP BY substr(uh.sstartdatetime,1,8),uh.sflowtype
order by substr(uh.sstartdatetime,1,8) DESC
result :
DATETIME FLOWTYPE
20130507 voice
20130507 voice
20130507 voice
20130507 sms
20130507 mms
but i need
20130507 voice
20130507 sms
20130507 mms
so what should i do?
please suggest meWell, you are grouping by substr(uh.sstartdatetime,1,8),uh.sflowtype
so different values of sflowtype that aren't 7,9 or 10 will be grouped separately.
You therefore need to group by your case statement instead of just the sflowtype.
The most worrying thing though is that you have sstartdatetime that appears to be a varchar2 and not a date.
If it is a date, you are applying a substr (which applies to strings) to a date. you then compare the result
of the substr to a number.
This is a recipe for future problems: sort it out using the correct conversion functions with format masks.
Edited by: Paul Horth on May 14, 2013 1:25 PM

Similar Messages

  • More effecient way of using Decode and CASE

    Is it possible to use both case and decode together? I feel I have too many decode and I am not sure how to write this in a more efficient way.
    I would really appreciate any help.
    Thank you so much in advance!!!
    SELECT  Person_ID,Work_ID,
        CASE WHEN NBR = 1 THEN
                MIN(DECODE(NBR, 1, field1)) aug_one_field1;
                MIN(DECODE(NBR, 1, field2)) aug_one_field2;
                MIN(DECODE(NBR, 1, field3)) aug_one_field3;
                MIN(DECODE(NBR, 1, field4)) aug_one_field4;
                MIN(DECODE(NBR, 1, field5)) aug_one_field5;
                MIN(DECODE(NBR, 1, field6)) aug_one_field6;
        CASE WHEN NBR = 2 THEN
                MIN(DECODE(NBR, 2, field1)) aug_two_field1;
                MIN(DECODE(NBR, 2, field2)) aug_two_field2;
                MIN(DECODE(NBR, 2, field3)) aug_two_field3;
                MIN(DECODE(NBR, 2, field4)) aug_two_field4;
                MIN(DECODE(NBR, 2, field5)) aug_two_field5;
                MIN(DECODE(NBR, 2, field6)) aug_two_field6;
        CASE WHEN NBR = 3 THEN
                MIN(DECODE(NBR, 3, field1)) aug_three_field1;
                MIN(DECODE(NBR, 3, field2)) aug_three_field2;
                MIN(DECODE(NBR, 3, field3)) aug_three_field3;
                MIN(DECODE(NBR, 3, field4)) aug_three_field4;
                MIN(DECODE(NBR, 3, field5)) aug_three_field5;
                MIN(DECODE(NBR, 3, field6)) aug_three_field6;
        CASE WHEN NBR = 4 THEN
                MIN(DECODE(NBR, 4, field1)) aug_four_field1;
                MIN(DECODE(NBR, 4, field2)) aug_four_field2;
                MIN(DECODE(NBR, 4, field3)) aug_four_field3;
                MIN(DECODE(NBR, 4, field4)) aug_four_field4;
                MIN(DECODE(NBR, 4, field5)) aug_four_field5;
                MIN(DECODE(NBR, 4, field6)) aug_four_field6;
    END
       FROM (SELECT Person_ID,Work_ID, NBR
                   fiel1, field2, field3,field4,field5, field6,
                  FROM field_rep
       GROUP BY Person_ID,Work_ID

    Thanks alot John and Frank.
    John to answer your question, I just felt the 24 or more decode will slow down the systems performance, hence I was trying to find a better way to do it.
    Frank
    This is a sample data but I want it pivoted hence the reason for using the decode. I have oracle 10g. These are sample data
    Person_id work_id NBR      field1     field2     field3     field4 field5 field6
    1       ao334   1     1/2/2009   1/9/2010     block_A        HH       55667      1
    1       ao334   2     5/2/2011   9/9/2013     block_Z        HL       11111      3
    1       ao334   2     1/2/2009   1/9/2010     block_A        HH       22222      1
    1       ao334   4     1/2/2009   1/9/2010     block_A        HH       zzzzz      7
    1       z5521   1     10/5/2006  12/31/2012     block_C        SS       33322      1
    1       z5521   2     1/2/2009   1/9/2010     block_C        SS       21550      1
    1       z5521   3     1/2/2009   1/9/2010     block_R        SS       10000      1
    1       z5521   4     1/2/2009   1/9/2010     block_D        SS       99100      5
    1       z5521   5     1/2/2009   1/9/2010     block_P        SS       88860      1
    1       z5521   6     1/2/2009   1/9/2010     block_G        SS       99660      8
    1       ob114   1     1/2/2009   1/9/2010     block_A        HH       52333      1
    1       ob114   2     1/2/2009   1/9/2010     block_A        HH       88888      1Output will look like this
    Person_id work_id  aug_one_field1 aug_one_field2 aug_one_field3 aug_one_field4 aug_one_field5 aug_one_field6 aug_two_field1 aug_two_field2 aug_two_field3 aug_two_field4 aug_two_field5  aug_two_field6
    1       ao334        1/2/2009       1/9/2010       block_A       HH             55667          1              5/2/2011       9/9/2013       block_Z        HL                      11111          3

  • Hi I'm having issue to sign in using iPod touch cause it keep showing check network connection as I use wifi.. Can please hel me . Thanks

    Hi I'm having issue to sign in using iPod touch cause it keep showing check network connection as I use wifi.. Can please hel me . Thanks

    Sign in where?
    Maybe:
    FaceTime, Game Center, Messages: Troubleshooting sign in issues

  • TS2634 Having issues with touch screen selecting keys and buttons I don't touch

    I'm having issues when messaging or using the browser with the key pad it will jump up and down the screen,  select buttons I don't want or the wrong messages. It basically wigs out at times, usually when I'm charging it, or listening to a radio station and trying to message at the same time. Any suggestions on why this is happening or how to fix it?

    thana88,
    Sorry to hear that you have had multiple service events, and yet problems persist.
    Please send me a private message with your contact information - name, phone number and the type / serial number of your flex 14.  If you have any of the service case numbers, please include these as well..
    We can work to escalate this
    Mark
    ThinkPads: S30, T43, X60t, X1, W700ds, IdeaPad Y710, IdeaCentre: A300, IdeaPad K1
    Mark Hopkins
    Program Manager, Lenovo Social Media (Services)
    twitter @lenovoforums
    English Community   Deutsche Community   Comunidad en Español   Русскоязычное Сообщество

  • Is anyone else having issues if they purchased a Season and it is NOW not allowing downloads without charging again

    is anyone else having issues if they purchased a Season and it is NOW not allowing downloads without charging again
    I get my email to download then ... it won't.   I go to the store and its showing charges again... rather than PURCHASED

    Not just you. I too am having this issue including with a movie I just purchased.
    If you follow mario49's advice they will eventually help you by posting the purchases to your account again (not actually fixing the current issue) but be VERY specific with them and be prepared to explain your issue about 4 times (I have taken to just copy and pasting) before they actually read what you told them and help out.
    e-mail 1: Response was to explain the difference between a season pass and individual orders (even though I attached the season pass order number to the issue message).
    e-mail 2: Reponse explained how to redownload episodes again (even though I had clearly stated it was a latest episode on a season pass).
    e-mail 3: I may have yelled a little bit here.
    e-mail 4: This one was a complaint about the pre-order that was charged but also now giving me the "you haven't paid for this" message. I attached the earlier complaint # to this one and got they same service person. This time he re-posted every episode from the above season pass to my account and my movie so I got the new episode I was looking for and about 5 others I was not.
    Now I just have to go through all of this for my other 11 season passes that won't download either.
    Good Luck.
    Lee
    PS is it just me or does everyone get the impression Apple has now farmed out thier customer service to Mircosoft?

  • What is better and fast to use between decode and case

    Hi friends,
    i wanted to know what is better to use decode or case in sql,which gives faster result.
    thks
    sonal....

    Here's a very simple timing comparison. This table (actually it's a partition) has a little over 1 million rows.
    As you can see, the timing difference is trivial. (I ran an earlier query to fetch the blocks from disk).
    SQL> select sum(decode(balloon_flag
      2                   ,'Y',1
      3                   ,0
      4                   )
      5            ) bal_count
      6  from   prod.loan_master
      7  where  report_date = to_date('31-DEC-2005');
               BAL_COUNT
                    9036
    Elapsed: 00:00:07.65
    SQL> select sum(case balloon_flag
      2                  when 'Y' then 1
      3                  else  0
      4             end
      5            ) bal_count
      6  from   prod.loan_master
      7  where  report_date = to_date('31-DEC-2005');
               BAL_COUNT
                    9036
    Elapsed: 00:00:07.68
    SQL> select sum(case
      2                  when balloon_flag = 'Y' then 1
      3                  else  0
      4             end
      5            ) bal_count
      6  from   prod.loan_master
      7  where  report_date = to_date('31-DEC-2005');
               BAL_COUNT
                    9036
    Elapsed: 00:00:07.46

  • Having Issues with iTunes 10.5.3 and uninstalled. Cannot reinstall without upgrading to newest Snow Leopard? Using MacBook Pro running 10.5.8.

    Running OS X Leopard 10.5.8 and was having issues with my version of iTunes 10.5.3 (freezing and getting rainbow spinny wheel, not allowing me to do anything, while importing music from a different folder into the iTunes). Anyway, so I uninstalled iTunes and went to download the newest version (10.7 i believe) and after downloading and attempting to install, i was alerted that I cannot install and run this application without having the newest version of OS X 10.6 i believe. So, what I'm ultimately trying to figure out is if I need to spend $130(that i do not have) on Amazon to get the Snow Leopard 10.6.3 then pay another $20 in the App Store to upgrade to Mountain Lion as well? I need to have iTunes on this laptop due to having an iPhone 4 that needs to be able to be synced on this computer. Bottom Line is, I don't have really have $150 to upgrade all this at the current moment just to get iTunes back on my system. Any suggestions or am I basically having to drop the $150 just to get iTunes back so I can sync my iPhone 4 again? Ugh, this *****, really wish i hadn't uninstalled that version of iTunes..HELP!!! thanks in advance..

    Click here and install the software. If you decide to upgrade to 10.6 at some future point, phone the online Apple Store.
    (70951)

  • Mac Pro 6,1 STILL having issues with Open CL (4K scaling and SpeedGrade direct link grades)

    I've seen the staff responses to the issues of the new Mac Pro GPU saying that 10.9.4 fixes everything. For me, it has fixed my export issues, so I'm glad. I'm no longer getting lines and artifacts on my renders.
    That's great.
    However, I'm still having several issues, and they seem to be linked to Adobe and Open CL.
    Here's a video that shows what's going on:
    mac pro issues july 7 - YouTube
    Issue #1
    Like most of you probably, I'm doing 1080p exports of most things I'm working on. The first project shows a 1080p timeline with a mix of 4k, 5k, and 720p footage. Because zooming in so much on a 720p file in a 4k timeline looks so crappy, I'm doing 1080p timelines and scaling everything to the size it needs to be. I started in a 4k timeline then switched to 1080p because the 720p footage looked so terrible. However, in the 4k timeline, I didn't have any of the issues I'm showing in this first part of the video.
    When I switched to the 1080p timeline, I discovered a weird quirk: When I resize the 4k (or 5k) footage to the size it needs to be (which varies based on the frame I want), it goes back to the 100% view when I'm scrubbing through it. When I pause, the size of the video goes back to the way I set it. I show this in the video. Then I show what was my working solution for the time being: "scale to frame size", which brings all the videos to the size of the sequence frame size and works fine. Later, I turn off OpenCL in premiere and when I scrub through the video, it doesn't zoom back in or only stay at the size I set when stopped. For some reason all of these issues only happen at 50% scale or smaller. It doesn't happen at, say, 60%.
    I've tested this with raw R3D files and other 4K (or 5K) clips (even clips exported from After Effects) and the same thing happens. It doesn't seem to happen with 2.5k footage or 1080p footage.
    Issue #2
    This is the second project you'll see in the video.
    After grading in speedgrade (through direct link) I open my project back up in premiere and start playing it back. The video plays back with delays and frame drops. Now normally, this would be expected. But this is 1080p pro res footage, and this is just an effect added, and the yellow bar appears indicating that open CL should be accelerating the playback and it should do so smoothly.
    Maybe I was just wrong in assuming that a simple grade from speedgrade would be able to playback smoothly. I turn off the grade, and it plays normally.
    But then, when I turn off open cl and leave the grade on, the video plays back without ANY issues.
    So to me, it seems I'm still having issues with Open CL and adobe. I've found fixes that make it work temporarily, I don't need that kind of answer. However, I would like this to work. Is there something I'm doing wrong? Something I need to do to get this to work?
    Here are my specs:
    Mac Pro 6,1 (Late 2013)
    3 GHz 8-Core Intel Xeon E5
    64 GB 1867 MHz DDR3 ECC
    AMD FirePro D700 6144 MB (x2)
    OSX 10.9.4
    Firmware: 2.20f18
    What you're seeing is latest version of Premiere (CC 2014, v8.0.0 [169] Build)
    Everything here is ProRes 422, running from a Promise Pegasus2 R4 RAID (drives: 4x Seagate Desktop HDD 4 TB SATA 6Gb/s NCQ 64MB Cache 3.5-Inch Internal Bare Drive ST4000DM000) (that's thunderbolt 2).
    I've done everything: I've restarted my computer hundreds of times, I've reset NVRAM and PRAM and done power cycles.
    This isn't just some issue that started with 10.9.3 of OSX, the 4k issues happened when I first switched to a 1080p sequence back in February (2014) or so, which means it was an issue even with 10.9.2 and the previous firmware release.
    I've reported this issue to adobe.

    That crash appears to be casued by the Facebook plug-in.
    Create a new account (systempreferences -> accounts or Users & Groups on 10.7 and 10.8), make a new Library in that account, import some shots  and see if the problem is repeated there. If it is, then a re-install of the app might be indicated. If it's not, then it's likely the app is okay and the problem is something in the main account.

  • I have Snow Leopard 10.6 and I am having issues with M/S Word 2004 and G-mail. I can only open docs sent to me in g-mail on google doc?

    Is there a compatability issue with Microsoft Word 2004 and Snow Leopard or is it G-Mail? I am using Safari with a G-mail account.

    Will upgrading to 2011 correct my issue that I can not open a pdf or word doc in G-mail or could it be a g-mail setting? I can open these docs outside of G-mail, but I am also having issues sending them without the docs becoming slightly modified enroute.

  • After the recent update I am having issues with Flash based games freezing and not responding.

    After the recent update I am having issues with flash based programs not running or loading correctly.  The programs will not load or take an extremely long time to load compared to jsut a few days ago.  This happens across the board on anything flash based.  Also, once they do load they will freeze, stop responding or need to be constantly refreshed to come back alive which is short lived at best.  I went thru the help and did all the file deleting, cache clearing, disabling hardware acceleration, removing then reinstalling flash, and even removing and reinstalling my browsers.  None of this has helped at all.  I am using Chrome and it is up to date and I checked my Flash player and it is up to date.  I am running windows 7 64bit.

    Here's what you're up against guys,
    Adobe writes the base code for Flash Player, and then hands it off to Google, who adapt it to a PPAPI plug-in (it's NPAPI when Adobe writes it). It's then "PepperFlash" and Google embeds it in their Chrome browser. While Google is pushing a "webwide" move to PPAPI Flash Content, a reinvention of the wheel so to speak, not everyone "got the memo", and Google is slowly making it so that PepperFlash will block NPAPI Flash content from running properly, and you have to disable PepperFlash to force the NPAPI plug-in to work, which sometimes fixes it, sometimes not. I have a feeling this is only going to get worse. HTML5 will eventually resolve the video end but game developers need to come up with something that won't require a plug-in.
    And to the first post:  Since PepperFlash is embedded in Chrome by Google, you can uninstall and reinstall Flash Player from here until your hard drive literally seizes up.... guess what? It has NO EFFECT at all on the PepperFlash plug-in in Chrome because it's a separate file in a separate folder and the uninstaller & installers don't have anything to do with it.

  • I was having issues with my 80G Video iPod and had to restore it, but now it seems all of my music on it previously is "corrupt" . How did all my songs get corrupted and how do I fix them?

    I was having issues with syncing my 80G video iPod, so I restored it, and now all my songs in my library are "corrupted" and I can't get them back on my iPod. I keep getting error(-69). How did my songs all get corrupted, and how do I fix this without having to purchase these songs all over again?

    Go to the iTunes Store, log into your account, and click the Purchases link under the Quick Links. From there you should be able to re-download some or all of your purchased content. Note that not all content has been licensed for re-downloading in all countries at this time; you can see what content you can download here:
    http://support.apple.com/kb/HT5085
    You can also re-download content using an iOS device.
    For more information, see:
    http://support.apple.com/kb/ht2519
    This if course will not apply to songs you did not buy through the iTunes Store.
    Regards.
    Forum Tip: Since you're new here, you've probably not discovered the Search feature available on every Communities page, but next time, it might save you time (and everyone else from having to answer the same question multiple times) if you search a couple of ways for a topic, both in the relevant forums and in the Apple Knowledge Base, before you post a question.

  • Having issues making GarageBand work with iPad and line 6 midi mobilizer. Any suggestions?

    I am having issues making the line 6 midi mobilizer for keyboard work with GarageBand for ipad2.

    Check out the iRig Midi iRig MIDI

  • I'm having issues with my apple wireless keyboard and bootcamp 5

    I'm having issues with my apple wireless keyboard.
    I just bought an Apple Wireless Keyboard for my ThinkPad E430 but the keys are not compatible.
    I installed BootCamp 5 and it worked great but whenever I restart, it changes my system's time.

    What do you mean by racing around in circles? Have you done any damage to your phone? Like drop it or get it wet?

  • Having issues clicking buttons on the apple and adobe webpage

    Hi,
    Recently Safari (last version, mountain lion, all the updates installed) started to act in a really weird way. I have issues clicking buttons on the apple and adobe webpage, The buttons you can see on the image attacched.
    The weirdest thing is that if I change the user agent in the Develop menu with IE 9, everything works fine. I tried to switch on and off Java and I did install even the java version from the oracle website. Still got the same problem. Any help? Thanks!

    Please read this whole message before doing anything.
    This procedure is a diagnostic test. It’s unlikely to solve your problem. Don’t be disappointed when you find that nothing has changed after you complete it.
    The purpose of the test is to determine whether the problem is caused by third-party software that loads automatically at startup or login. 
    Disconnect all wired peripherals except those needed for the test, and remove all aftermarket expansion cards. Boot in safe mode* and log in to the account with the problem. The instructions provided by Apple are as follows:
    Shut down your computer, wait 30 seconds, and then hold down the shift key while pressing the power button.
    When you see the gray Apple logo, release the shift key.
    If you are prompted to log in, type your password, and then hold down the shift key again as you click  Log in.
    *Note: If FileVault is enabled under OS X 10.7 or later, or if a firmware password is set, or if the boot volume is a software RAID, you can’t boot in safe mode. Safe mode is much slower to boot and run than normal, and some things won’t work at all, including wireless networking on certain Macs.  The next normal boot may also be somewhat slow.
    The login screen appears even if you usually log in automatically. You must know your login password in order to log in. If you’ve forgotten the password, you will need to reset it before you begin. Test while in safe mode. Same problem? After testing, reboot as usual (i.e., not in safe mode) and verify that you still have the problem. Post the results of the test.

  • Query to find out sum by using decode or case

    Hi, I have data like below and required an output as given below, could you please help me to get the same by using decode / case .....Thanks for your help
    INSNAME INSID STATUS
    AAA 1000 Scheduled
    AAA 1000 Processing
    BBB 1001 Inspector
    BBB 1001 Scheduled
    BBB 1001 Processing
    BBB 1001 Inspector
    CCC 1002 Scheduled
    CCC 1002 Processing
    CCC 1002 Inspector
    CCC 1002 Scheduled
    CCC 1002 Processing
    CCC 1002 Inspector
    Required Output...
    INSNAME INSID sum_of_scheduled sum_of_Processing sum_of_Inspector
    AAA 1000 1 1 0
    BBB 1001 1 1 2
    CCC 1002 2 2 2

    And if you want it with CASE statement:
    WITH test_table AS
       SELECT 'AAA' insname, 1000 insid, 'Scheduled'   status FROM DUAL UNION ALL
       SELECT 'AAA' insname, 1000 insid, 'Processing'  status FROM DUAL UNION ALL
       SELECT 'BBB' insname, 1001 insid, 'Inspector'   status FROM DUAL UNION ALL
       SELECT 'BBB' insname, 1001 insid, 'Scheduled'   status FROM DUAL UNION ALL
       SELECT 'BBB' insname, 1001 insid, 'Processing'  status FROM DUAL UNION ALL
       SELECT 'BBB' insname, 1001 insid, 'Inspector'   status FROM DUAL UNION ALL
       SELECT 'CCC' insname, 1002 insid, 'Scheduled'   status FROM DUAL UNION ALL
       SELECT 'CCC' insname, 1002 insid, 'Processing'  status FROM DUAL UNION ALL
       SELECT 'CCC' insname, 1002 insid, 'Inspector'   status FROM DUAL UNION ALL
       SELECT 'CCC' insname, 1002 insid, 'Scheduled'   status FROM DUAL UNION ALL
       SELECT 'CCC' insname, 1002 insid, 'Processing'  status FROM DUAL UNION ALL
       SELECT 'CCC' insname, 1002 insid, 'Inspector'   status FROM DUAL
    SELECT insname
          ,insid
          ,SUM(CASE WHEN status = 'Scheduled'
                    THEN 1
                    ELSE 0
                END
              ) sum_of_scheduled
          ,SUM(CASE WHEN status = 'Processing'
                    THEN 1
                    ELSE 0
                END
              ) sum_of_processing
          ,SUM(CASE WHEN status = 'Inspector'
                    THEN 1
                    ELSE 0
                END
              ) sum_of_inspector         
      FROM test_table
    GROUP BY insname
            ,insid;
    Regards
    Arun

Maybe you are looking for