Syntax for multiple unpivot

I have a table like the below, which I brought to a worksheet, where twelve U-P-columns and are representative of both age groups (U30 and P30), three genders (F, M and the two (not f or m)) and two types of client (the 1 and 2 at the end).

CREATE TABLE AGESEX_RAW

(DATE OF THE 'M_DATE',

'REGION' VARCHAR2 (20 BYTE),

'QUARTER' VARCHAR2 (30 BYTE),

"U30FA1" NUMBER (5.0).

"U30MA1" NUMBER (5.0).

"U30A1" NUMBER (5.0).

"P30FA1" NUMBER (5.0).

"P30MA1" NUMBER (5.0).

"P30A1" NUMBER (5.0).

"U30FA2" NUMBER (5.0).

"U30MA2" NUMBER (5.0).

"U30A2" NUMBER (5.0).

"P30FA2" NUMBER (5.0).

"P30MA2" NUMBER (5.0).

("P30A2" NUMBER (5.0));

I would like to use UNPIVOT to transform each of the twelve original lines (with the columns m_date, region, district, age_grp, sex and client_type), but I'm a hard time figuring what the syntax would be.

I he managed quite easily by making the UNION ALLs (code below), but I figure I should probably learn how to do multiple unpivots.

Some examples of data (for a month) is:

Insert in many CASES. The values AGESEX_RAW (M_DATE, REGION, DISTRICT, U30FA1, U30MA1, U30A1, P30FA1, P30MA1, P30A1, U30FA2, U30MA2, U30A2, P30FA2, P30MA2, P30A2) (to_date (1 April 06 ',' DD-MON-RR'), "Avalon", "Harbour Grace", 276, 129, 405, 929, 869, 1798, 28, 30, 58, 318, 141, 459);

Insert in many CASES. The values AGESEX_RAW (M_DATE, REGION, DISTRICT, U30FA1, U30MA1, U30A1, P30FA1, P30MA1, P30A1, U30FA2, U30MA2, U30A2, P30FA2, P30MA2, P30A2) (to_date (1 April 06 ',' DD-MON-RR'), 'Avalon', 'Bell Island', 60, 46, 106, 194, 229, 423, 4, 2, 6, 62, 25, 87);

Insert in many CASES. Values AGESEX_RAW (M_DATE, REGION, DISTRICT, U30FA1, U30MA1, U30A1, P30FA1, P30MA1, P30A1, U30FA2, U30MA2, U30A2, P30FA2, P30MA2, P30A2) (to_date (1 April 06 ',' DD-MON-RR'), 'Avalon',' St. John "s Center ', 1180, 779, 1959, 3487, 2954, 6441, 62, 44, 106, 400, 441, 841);

Insert in many CASES. Values AGESEX_RAW (M_DATE, REGION, DISTRICT, U30FA1, U30MA1, U30A1, P30FA1, P30MA1, P30A1, U30FA2, U30MA2, U30A2, P30FA2, P30MA2, P30A2) (to_date('01-APR-06','DD-MON-RR'), 'Avalon', 'Placentia', 35,20,55,163,164,327,2,5,7,38,17, 56);

Insert in many CASES. The values AGESEX_RAW (M_DATE, REGION, DISTRICT, U30FA1, U30MA1, U30A1, P30FA1, P30MA1, P30A1, U30FA2, U30MA2, U30A2, P30FA2, P30MA2, P30A2) (to_date (1 April 06 ',' DD-MON-RR'), 'Avalon', 'Mt Pearl', 228, 132, 360, 731, 585, 1316, 6, 10, 16, 117, 77, 194);

Insert in many CASES. Values AGESEX_RAW (M_DATE, REGION, DISTRICT, U30FA1, U30MA1, U30A1, P30FA1, P30MA1, P30A1, U30FA2, U30MA2, U30A2, P30FA2, P30MA2, P30A2) (to_date('01-APR-06','DD-MON-RR'), 'Central', 'Springdale', 44,21,65,143,152,295,7,4,11,45,30, 75);

Insert in many CASES. The values AGESEX_RAW (M_DATE, REGION, DISTRICT, U30FA1, U30MA1, U30A1, P30FA1, P30MA1, P30A1, U30FA2, U30MA2, U30A2, P30FA2, P30MA2, P30A2) (to_date (1 April 06 ',' DD-MON-RR'), 'Central', "Grand Falls", 105, 299, 597, 600, 194, 1197, 36, 26, 62, 216, 137, 353);

Insert in many CASES. Values AGESEX_RAW (M_DATE, REGION, DISTRICT, U30FA1, U30MA1, U30A1, P30FA1, P30MA1, P30A1, U30FA2, U30MA2, U30A2, P30FA2, P30MA2, P30A2) (to_date('01-APR-06','DD-MON-RR'), 'Central', 'Twillingate', 31,20,51,111,145,256,7,4,11,57,17, 74);

Insert in many CASES. Values AGESEX_RAW (M_DATE, REGION, DISTRICT, U30FA1, U30MA1, U30A1, P30FA1, P30MA1, P30A1, U30FA2, U30MA2, U30A2, P30FA2, P30MA2, P30A2) (to_date('01-APR-06','DD-MON-RR'), 'Central', 'Lewisporte', 35,30,65,128,181,309,2,4,6,81,31, 112);

Insert in many CASES. Values AGESEX_RAW (M_DATE, REGION, DISTRICT, U30FA1, U30MA1, U30A1, P30FA1, P30MA1, P30A1, U30FA2, U30MA2, U30A2, P30FA2, P30MA2, P30A2) (to_date('01-APR-06','DD-MON-RR'), 'Central', 'Clarenville', 74,56,130,251,300,551,7,6,13,98,44, 142);

Insert in many CASES. Values AGESEX_RAW (M_DATE, REGION, DISTRICT, U30FA1, U30MA1, U30A1, P30FA1, P30MA1, P30A1, U30FA2, U30MA2, U30A2, P30FA2, P30MA2, P30A2) (to_date('01-APR-06','DD-MON-RR'), 'Central', 'Bonavista', 63,50,113,238,204,442,9,5,14,45,42, 87);

Insert in many CASES. Values AGESEX_RAW (M_DATE, REGION, DISTRICT, U30FA1, U30MA1, U30A1, P30FA1, P30MA1, P30A1, U30FA2, U30MA2, U30A2, P30FA2, P30MA2, P30A2) (to_date('01-APR-06','DD-MON-RR'), 'Central', 'Marystown', 156,85,241,425,419,844,24,12,36,123,76, 199);

Insert in many CASES. Values AGESEX_RAW (M_DATE, REGION, DISTRICT, U30FA1, U30MA1, U30A1, P30FA1, P30MA1, P30A1, U30FA2, U30MA2, U30A2, P30FA2, P30MA2, P30A2) (to_date('01-APR-06','DD-MON-RR'), 'Central', 'Milltown', 43,15,58,111,139,250,2,5,7,44,20, 64);

Insert in many CASES. Values AGESEX_RAW (M_DATE, REGION, DISTRICT, U30FA1, U30MA1, U30A1, P30FA1, P30MA1, P30A1, U30FA2, U30MA2, U30A2, P30FA2, P30MA2, P30A2) (to_date('01-APR-06','DD-MON-RR'), 'Central', 'Gander', 178,97,275,490,558,1048,21,13,34,229,112, 341);

Insert in many CASES. The values AGESEX_RAW (M_DATE, REGION, DISTRICT, U30FA1, U30MA1, U30A1, P30FA1, P30MA1, P30A1, U30FA2, U30MA2, U30A2, P30FA2, P30MA2, P30A2) (to_date (1 April 06 ',' DD-MON-RR'), 'Central', 'Green Bay', 33, 16, 49, 83, 122, 205, 5, 3, 8, 50, 13, 63);

Insert in many CASES. The values AGESEX_RAW (M_DATE, REGION, DISTRICT, U30FA1, U30MA1, U30A1, P30FA1, P30MA1, P30A1, U30FA2, U30MA2, U30A2, P30FA2, P30MA2, P30A2) (to_date (1 April 06 ',' DD-MON-RR'), 'Western', 'St Anthony', 20, 24, 44, 64, 54, 118, 2, null, 2, 6, 8, 14);

Insert in many CASES. Values AGESEX_RAW (M_DATE, REGION, DISTRICT, U30FA1, U30MA1, U30A1, P30FA1, P30MA1, P30A1, U30FA2, U30MA2, U30A2, P30FA2, P30MA2, P30A2) (to_date('01-APR-06','DD-MON-RR'), 'Western', 'Channel', 42,21,63,154,185,339,5,3,8,59,23, 82);

Insert in many CASES. The values AGESEX_RAW (M_DATE, REGION, DISTRICT, U30FA1, U30MA1, U30A1, P30FA1, P30MA1, P30A1, U30FA2, U30MA2, U30A2, P30FA2, P30MA2, P30A2) (to_date (1 April 06 ',' DD-MON-RR'), 'Western', 'Stephenville Crossing", 39, 27, 66, 232, 265, 497, 9, 2, 11, 79, 53, 132);

Insert in many CASES. Values AGESEX_RAW (M_DATE, REGION, DISTRICT, U30FA1, U30MA1, U30A1, P30FA1, P30MA1, P30A1, U30FA2, U30MA2, U30A2, P30FA2, P30MA2, P30A2) (to_date('01-APR-06','DD-MON-RR'), 'Western', 'Stephenville', 179,88,267,532,418,950,20,16,36,104,108, 212);

Insert in many CASES. The values AGESEX_RAW (M_DATE, REGION, DISTRICT, U30FA1, U30MA1, U30A1, P30FA1, P30MA1, P30A1, U30FA2, U30MA2, U30A2, P30FA2, P30MA2, P30A2) (to_date (1 April 06 ',' DD-MON-RR'), 'Western', 'Corner Brook", 123, 397, 834, 676, 274, 1510, 25, 19, 44, 170, 148, 318);

Insert in many CASES. The values AGESEX_RAW (M_DATE, REGION, DISTRICT, U30FA1, U30MA1, U30A1, P30FA1, P30MA1, P30A1, U30FA2, U30MA2, U30A2, P30FA2, P30MA2, P30A2) (to_date (1 April 06 ',' DD-MON-RR'), 'Western', 'Port Saunders', 61, 35, 96, 168, 220, 388, 6, 6, 12, 74, 12, 86);

Insert in many CASES. Values AGESEX_RAW (M_DATE, REGION, DISTRICT, U30FA1, U30MA1, U30A1, P30FA1, P30MA1, P30A1, U30FA2, U30MA2, U30A2, P30FA2, P30MA2, P30A2) (to_date('01-APR-06','DD-MON-RR'), 'Western', 'Piccadilly', 23,7,30,142,190,332,4,4,8,72,54, 126);

Insert in many CASES. Values AGESEX_RAW (M_DATE, REGION, DISTRICT, U30FA1, U30MA1, U30A1, P30FA1, P30MA1, P30A1, U30FA2, U30MA2, U30A2, P30FA2, P30MA2, P30A2) (to_date (1 April 06 ',' DD-MON-RR'), 'Labrador','Mary "s port ', 24, 17, 41, 52, 62, 114, 3, null, 3, 15, 14, 29);

Insert in many CASES. The values AGESEX_RAW (M_DATE, REGION, DISTRICT, U30FA1, U30MA1, U30A1, P30FA1, P30MA1, P30A1, U30FA2, U30MA2, U30A2, P30FA2, P30MA2, P30A2) (to_date (1 April 06 ',' DD-MON-RR'), 'Labrador', 'Happy Valley', 70, 30, 100, 108, 94, 202, 9, 7, 16, 9, 17, 26);

Insert in many CASES. Values AGESEX_RAW (M_DATE, REGION, DISTRICT, U30FA1, U30MA1, U30A1, P30FA1, P30MA1, P30A1, U30FA2, U30MA2, U30A2, P30FA2, P30MA2, P30A2) (to_date('01-APR-06','DD-MON-RR'), 'Labrador', 'Hopedale', 23,13,36,28,32,60,2,3,5,6,16, 22).

Insert in many CASES. Values AGESEX_RAW (M_DATE, REGION, DISTRICT, U30FA1, U30MA1, U30A1, P30FA1, P30MA1, P30A1, U30FA2, U30MA2, U30A2, P30FA2, P30MA2, P30A2) (to_date('01-APR-06','DD-MON-RR'), 'Labrador', 'Dwarf', 27,15,42,32,33,65,1,3,4,5,9, 14);

Insert in many CASES. Values AGESEX_RAW (M_DATE, REGION, DISTRICT, U30FA1, U30MA1, U30A1, P30FA1, P30MA1, P30A1, U30FA2, U30MA2, U30A2, P30FA2, P30MA2, P30A2) (to_date('01-APR-06','DD-MON-RR'), 'Labrador', 'Shesahtshiu', 27,9,36,29,20,49,1,3,4,7,10, 17);

Insert in many CASES. Values AGESEX_RAW (M_DATE, REGION, DISTRICT, U30FA1, U30MA1, U30A1, P30FA1, P30MA1, P30A1, U30FA2, U30MA2, U30A2, P30FA2, P30MA2, P30A2) (to_date('01-APR-06','DD-MON-RR'), 'Labrador', 'Wabush', 46,11,57,64,26,90,null,2,2,6,4, 10);

Insert in many CASES. Values AGESEX_RAW (M_DATE, REGION, DISTRICT, U30FA1, U30MA1, U30A1, P30FA1, P30MA1, P30A1, U30FA2, U30MA2, U30A2, P30FA2, P30MA2, P30A2) (to_date('01-APR-06','DD-MON-RR'), 'Total', 'Total', 3485,2021,5506,10520,9896,20416,309,241,550,2535,1699, 4235);

Select *.

Of

(select *)

Of

(select m_date, region, district, u30fa1, u30ma1, u30a1, age_grp 'U', '1' client_type

of agesex_raw

)

UNPIVOT (ct for equality of the sexes at the ((u30fa1) as 'F', (u30ma1) as ' me, (u30a1) as ' t '))

UNION ALL

Select *.

Of

(select m_date, region, district, p30fa1, p30ma1, p30a1, age_grp 'P', '2' client_type

of agesex_raw

)

UNPIVOT (ct for equality of the sexes at the ((p30fa1) as 'F', (p30ma1) as ' me, (p30a1) as ' t '))

UNION ALL

Select *.

Of

(select m_date, region, district, u30fa2, u30ma2, u30a2, age_grp 'U', '2' client_type

of agesex_raw

)

UNPIVOT (ct for equality of the sexes at the ((u30fa2) as 'F', (u30ma2) as ' me, (u30a2) as ' t '))

UNION ALL

Select *.

Of

(select m_date, region, district, p30fa2, p30ma2, p30a2, age_grp 'P', '2' client_type

of agesex_raw

)

UNPIVOT (ct for equality of the sexes at the ((p30fa2) as 'F', (p30ma2) as ' me, (p30a2) as ' t '))

)    ;

Thank you

Jon

Hello

Maybe you want something like this:

SELECT *.

Of agesex_raw

UNPIVOT (ct

(Age_group, client_type, sex)

IN (u30fa1 ('u', 1, 'F')

, u30ma1 AS ('U', 1,')

, u30a1 AS ('U', 1, 't')

, u30fa2 ('u', 2, 'F')

, u30ma2 AS ('U', 2 am ')

, u30a2 AS ('U', 2, 't')

, p30fa1 ('p', 1, 'F')

, p30ma1 AS ('P', 1,')

, p30a1 AS ('P', 1, 't')

, p30fa2 ('p', 2, 'F')

, p30ma2 AS ('P', 2 am ')

, p30a2 AS ('P', 2, 't')

)

)

;

Tags: Database

Similar Questions

  • Numbers gives an error of syntax for all equations

    I don't know what is happening, but I keep getting errors in syntax for multiple equations and no matter where I put it or how, he always gives the syntax error. Example B2 has 500 in there. Enter B3 B2x2 and it gives an error of syntax instead of 1000.  I leave numbers and re-open the file with the same results. I even opened a new worksheet, and when you enter anything except the Scriptures, I always get the syntax error. It seems that there is something wrong with the numbers himself. Can someone explain why I can't use the formulas at all. They used to work in the past, so I am confused, what can happen.

    Salvation of,

    Did you B2x2 = or = B2 * 2?

    Number change * to x, but seems not to recognize x as the multiplication operator when he entered directly.

    Kind regards

    Barry

  • Active Directory - join the domain for multiple devices

    Hi all

    I need your expertise to advice me how join domain for multiple devices.

    Currently my organization have more than 10,000 computers are made up of Windows XP, 7, 8 and 10.

    We will deploy new Active Directory server in the data center.

    Currently, we plan to go every computer/devices to perform a field joints. This method will take much time to complete the 10,000 devices.

    is there another method to do this?

    is there a method that all devices will join automatically field when it is connected to the corporate network.

    Thank you.

    Hello

    Post your question in the TechNet Server Forums, as your question kindly is beyond the scope of these Forums.

    http://social.technet.Microsoft.com/forums/WindowsServer/en-us/home?category=WindowsServer

    See you soon.

  • Is it possible to set up for multiple users on the same computer to avoid entering the e-mail and the password

    Original title: Multi users

    Is it possible to set up for multiple users on the same computer to avoid entering the email and password every time that a new user want to check emails?

    Hello

    Welcome to the community forums of Microsoft and thanks for posting the question. According to the description, you need help in the creation of multiple user accounts. I've surely you will help find a solution on the issue.

    What is the operating system installed on the computer?

    User accounts, several people can easily share a single computer. Each person can have a separate user account with unique settings and preferences, such as a screen saver or desktop background. User accounts control files and programs users can access and what types of changes users can make to the computer. As a general rule, you'll want to create standard accounts for most computer users.

    If you use Windows Vista or Windows 7, see the article below:

    Create a user account

    http://Windows.Microsoft.com/en-in/Windows7/create-a-user-account

    http://Windows.Microsoft.com/en-in/Windows-Vista/create-a-user-account

    For Windows XP users:

    How to create and configure user accounts in Windows XP

    http://support.Microsoft.com/kb/279783?WA=wsignin1.0

    Hope this information helps. Answer please if you have more queries about Windows.

  • is it possible to save the Office XP for multiple monitors and restore after using the laptop in mobile mode?

    Is it possible to save the Office XP for multiple monitors and restore after using the laptop in mobile mode?

    Hi Kenelms,

    You can follow this link & check if it helps.

    HOW to: Set up hardware profiles for laptop computers in Windows XP

    Hope the helps of information.

    Please post back and we do know.

  • Problem with the extended help display option for multiple displays.

    I have a laptop with an internal (#1) designated as my screen main screen. At work, I like to use the 'extended display' option for several screens to use the external desktop as #2 display monitor. However, at home I have the screen internal one on my laptop. So when I open an application like Excel at home, the window does not appear, but the application is running, as I confirmed with the Task Manager. So far, the only way I know to solve this problem at home is to change my display settings for multiple views to show on display #1 only. Is there a practical way to deal with this issue in addition to change the display settings whenever I move from work to home and vice versa?

    Hello

    In Windows 7, you can simply press the Windows Logo + P key.

    In the menu, select the item from the computer only .

    Concerning

  • I prefer SEPARATE (for multiple open documents) instead of tedious proceedings hover over the icon and then choose the one I want.

    Notepad or WORD or EXCEL, Lotus SAMETIME, I prefer SEPARATE (for multiple open documents) instead of tedious proceedings hover over the icon and then choose the one I want.  I can't figure out how to change that, and it's a boring true change to XP. Does anyone know how to do this?

    Go to the Start menu button, right-click on it, select Properties, click the task bar .

    Click the drop-down list buttons Taskbar & select never combine.

    That's all.  You should now see an entry of the separate taskbar for each open file on your PC.

  • Unique portfolio for multiple schemas in a database

    Hello

    Is it possible to create a unique portfolio for multiple schemas for the same database.

    for example: I have several users Test1, Test2 and so on in a Testdb database, then I create a unique portfolio for these users to the same database and connect?

    The PES is designed to be used on the client side - each customer can have its own PES with its letters of nobility.

    Contains the credentials: tbs_alias, username, password. Having the right (wallet_location and sqlnet.wallet_override = true) sqlnet.ora parameters, when you try to authenticate to the database at: sqlplus / @tns_alias, then the portfolio is looking for tns_alias and the username and pwd will be taken.

    This means that the tns_alias must be unique in the wallet. If you want multiple users to use the same portfolio and connect to the same database, you will need to create multiple aliases tns (different names but pointing to the same listener) for each user

  • Using filters Essbase to restrict access to OBIEE dashboards for multiple users

    Hello

    You can use Essbase filters to restrict access to the data in OBIEE dashboards so that users with no access to specific members are not able to see all data for multiple users.

    Any suggestions on how to go about it.

    Thank you!

    Hello

    Like any data source as an essbase.

    You can filter the data by the user, use a NQSESSION. to get the session the correct access.

    Kind regards

  • The plug-in for multiple clients

    Y at - he gave a plugin for multiple access to the client in an environment?

    For example:

    I have 50 clients to access the data on the server.

    I'm doing a layer of connection manual socket to manage the database reads and inserts?

    Or y at - it a way to connect directly to the environment of the clients to the server?

    It is unclear in your question what database you're talking about.   Berkeley DB is an embedded database and not a client server database.  It doesn't have a client plugin.

    Thank you

    Mike

  • How can I configure alerts group for multiple servers?

    Hello

    We have deployed 100 + agents and are very happy with it so far. But is there anyway to create group alerts? We run Hyperic 5.8.4

    Otherwise, we have to all alerts on each server manually.

    HQAPI will be what you are looking for.

    It allows to create scripts that can generate alerts for multiple platforms at once.

    vFabric Documentation Center

    The tip before gui using swing will take some time.

  • Why is there not a highlight of the syntax for ColdFusion?

    It is a community of support Adobe ColdFusion. Why is there not highlight the syntax for ColdFusion even? There are other but NOT of ColdFusion? Is just not that popular language ColdFusion more able to provide a code syntax highlighter? Or am I missing something here?

    coldFusion-highlighter.png

    Yes, this forum is built on the commercial platform of Jive, which isn't really a product Adobe.  If no direct ColdFusion syntax is supported.

  • Installation of Photoshop Lightroom for multiple platforms

    I have Photoshop Lightroom for multiple platforms. I just bought a new computer and wanted to download Lightroom on it.

    After downloading a trial version, because it was the only way I could download the program, I entered the serial number I had for the purchased product, but it comes up saying that the serial number is not valid.

    How to solve this problem?

    According to suggestion of John, check in the serial number is correct, you can also try: error: "serial number is not valid for this product". Adobe Creative Suite

    Let us know if that helps.

  • Is there a way to set reminders for multiple documents at the same time?

    Is there a way to set reminders for multiple documents at the same time? Now he must go through a list, manually click through a series of options, then select it. If I send more than 300 different documents to individuals at a time, it becomes tedious. Quick. A way to save me the trouble would be so so so appreciated.

    Hello Lindsayb123,

    We do have an option to set a generic reminder under choice of account in the accounts of the company and can be set by going to the tab account-> account settings-> send parameters and set reminders.

    Let me know if you need more information.

    -Usman

  • Can I purchase a single license for multiple software? Buy one for a single application, buy another one for another, etc.

    Can I purchase a single license for multiple software? Buy one for a single application, buy another one for another, etc.

    You can purchase the license but on different emails if you need to use them on multiple computers. From an e-mail, you can install software on both computers. These two computers can be Windows, Mac OS, or everyone. If you install on a third computer, you will be asked to turn off this feature on the other two computers. You can then re-enable one of the two previous computers and use Cloud Creative apps on it.

    Kind regards

    Hervé Khare

Maybe you are looking for