Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Dynamic row-to-column transfer through sql

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

In the previous chapter, we talked about fixed row transfer. In this chapter, we will show how to dynamically implement row transfer. Because sometimes there are thousands of values in a row-specific column, it is impossible to use a fixed row-to-column method, otherwise you will certainly collapse. All right, let's cut the crap and let's go!

A common table, tmp_test, contains the following:

Implementation code:

Create or replace procedure p_test is

V_sql varchar2 (2000)

Cursor cursor_1 is

Select distinct subject from tmp_test order by subject

Begin

V_sql: = 'select username'

For v_subject in cursor_1 loop

V_sql: = v_sql | |','| 'sum (decode (subject,''' | | v_subject.subject | |'', source)) as'| | v_subject.subject

Dbms_output.put_line (v_sql)

End loop

V_sql: = vql | | 'from tmp_test group by username'

Dbms_output.put_line (v_sql)

V_sql: = 'create or replace view test_result as' | | v_sql

Execute immediate v_sql

End

The stored procedure has been written and started to be called and saved:

Begin

P_test

End

Query View test_result:

Code implementation:

Select * from test_result

The data are as follows:

At this point, dynamic row rotation has been realized! I hope it will be helpful to the little friends!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

  • Oracle 10g installation prompt: operating system version: must be 5.1 or 5.2 what to do?

    Today, this problem occurred during the installation of oracle10g. After multi-party verification, we got this solution. Share it! 1. Search for refhost.xml in the installation directory, and then add the following in the appropriate location. Notice the parenthesis pairing

    © 2024 shulou.com SLNews company. All rights reserved.

    12
    Report