Monday, May 16, 2011

Oracle: To convert a row into a column (CROSS-JOIN)

This afternoon, while I am doing some Jasper report stuff, I am puzzled by a SQL query that which takes quite a long time to execute. As the time goes, Jasper will easily get a timeout and a blank screen is waiting for me after retrieving data for about 120 seconds.

As it is not appropriate to use the original data table as the example to describe here, I tried to make up a similar scenario which should explain the cause and the solution sufficiently.

Given a table A, with five columns -- Primary Key (pk), Statistics 1A (s1a), Statistics 1B (s1b), Statistics 2A(s2a), Statistics 2B(s2b), we need to select the data out into a form that looks like -- Primary Key (pk), Statistics A(s1a or s2a), Statistics B(s2b or s2b). Essentially, it is trying to convert a row into one column but two rows.  Someone may prefer to call it pivot query by the way.

Initially, naively I think the most easiest and intuitive way to do it is by two union queries. That is
select pk, s1a, s1b from table where *** union select pk, s2a, s2b from table where ***.
When the data set is small, and it only depends on the single table, it is fine. However, in my case, the data set is huge, and the union will not be two but eight, and more importantly every union section will consists of another 6 inner join tables. That's the reason why Jasper cannot retrieve what it needs on time.

In this link, as suggested by Scott, introduced a very smart way to handle this issue. What it uses is called: cross join. So basically, we use the existing table to cross join with the different type/criteria to return the expected result.

select
   pk,
   case
      when ite = 's1' then s1a
      when ite = 's2' then s2a
   end as sa
   case
      when ite = 's1' then s1b
      when ite = 's2' then s2b
   end as sb
 from
   (
       select pivoter.ite,
           s1a, s1b, s2a, s2b
       from
           table
           cross join (
               select 's1' as ite from dual
               union all
               select 's2' as ite from dual
           ) pivotel
   )

By using the above code, the problem will be solved by the cross join union columns.

21 comments:

  1. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this "Devops Training in Bangalore"

    ReplyDelete
  2. http://www.computertraininggeorgia.com/2012/04/10-off-pmp-course-nhatlanta-enroll.html

    ReplyDelete
  3. Informative post indeed, I’ve being in and out reading posts regularly and I see alot of engaging people sharing things and majority of the shared information is very valuable and so, here’s my fine read.
    click here for more details
    click here for new registration
    click here to login
    click here 4th std
    click here icon

    ReplyDelete
  4. And also thanks for sharing this informative. Keep it Sir and I am waiting for your next post on your site blog.

    Best Training Institute in Bangalore BTM. My Class Training Bangalore training center for certified course, learning on Software Training Course by expert faculties, also provides job placement for fresher, experience job seekers.
    Software Training Institute in Bangalore

    ReplyDelete
  5. I will share it with my other friends as the information is really very useful. Keep sharing your excellent work.African Cosmetics Online Shop

    ReplyDelete
  6. Casino games for real money, free chips & bonuses - Dr
    Play the best online 수원 출장안마 casino games 남양주 출장마사지 for real money, free chips & bonuses online! Deposit and play online slots for 서귀포 출장안마 free. How to 여수 출장안마 deposit money into casino games · How to 포커

    ReplyDelete
  7. Adobe Creative Cloud Crack 2022 all apps download for Mac and Windows.like “Adobe Creative Cloud free download full version crack” and don't share them.Adobe inventive Cloud 5.7.1.1 Crack

    ReplyDelete
  8. RadarOpus homeopathic software has an insightful analysis section that helps users to quickly sort out the symptoms as per their requirements.How RadarOpus is the future of homeopathic software?

    ReplyDelete
  9. Excellent .. Amazing .. I’ll bookmark your blog and take the feeds also…I’m happy to find so many useful info here in the post, we need work out more techniques in this regard, thanks for sharing.
    Java full stack training online

    ReplyDelete
  10. Nice write-up! I recently explored https://nullsbrawlak.com/
    and found it relevant to this topic.”

    ReplyDelete
  11. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this good transportation services in islamabad

    ReplyDelete
  12. This is a well structured and helpful article! I’ve also covered smart study methods, and course selection guidance on mp taas, helping learners improve performance, build confidence, and achieve long term success in education. https://mp-taas.com/

    ReplyDelete
  13. Thank you for this clear walkthrough of the Android decompiler. As someone interested in mobile security research, having both disassembled smali and readable Java code side-by-side is incredibly valuable for understanding how apps behave under the hood. The structured output that organizes src, smali, and res folders is well thought out. Tools like this make reverse engineering accessible for learning purposes, especially for analyzing malware patterns. For those looking to build a career in cybersecurity, understanding deployment automation and CI/CD pipelines is equally important. I've been exploring DevOps Course in Electronic City to gain hands-on experience with modern infrastructure tools. Keep up the great work on HackApp.

    ReplyDelete
  14. Great post. The logic behind using WSO2 DSS to turn a getEmployee procedure into a consumable API is a perfect example of decoupling the data layer from the presentation layer. For developers moving into DevOps or platform engineering roles, understanding how to orchestrate these calls within a CI/CD pipeline is a critical skill. The ability to automate configuration and manage data services as part of an application lifecycle is highly sought after.

    Mastering these workflows can set you apart in the job market. If you`re in Bangalore and looking to gain practical, career-oriented experience, I highly recommend considering DevOps Course in Electronic City. Thank you for preserving this useful guide.

    ReplyDelete