How can I split one row to multiple rows
Table like this
CREATE TABLE T(ID NUMBER(12),START_DATE DATE,END_DATE DATE,ORDER_ID NUMBER(12),PROD_ID NUMBER(12));
data like this
ID START_DATE END_DATE ORDER_ID PROD_ID
1 2013-01-01 2013-03-31 12 123
2 2013-04-01 2013-06-30 12 123
3 2013-01-01 2013-05-30 12 234
4 2013-02-01 2013-04-30 13 123
5 2013-07-01 2013-09-30 13 345
I want the result like this
ID DIFF_DATE ORDER_ID PROD_ID
1 201301 12 123
2 201302 12 123
3 201303 12 123
4 201304 12 123
5 201305 12 123
6 201306 12 123
7 201301 12 234
8 201302 12 234
9 201303 12 234
10 201304 12 234
11 201305 12 234
12 201302 13 123
13 201303 13 123
14 201304 13 123
15 201307 13 345
16 201308 13 345
17 201309 13 345
how to write the sql ?
Edited by: 990390 on 2013-3-31 下午11:42
SQL> WITH t(iD ,START_DATE ,END_DATE ,ORDER_ID ,PROD_ID) AS(
2 SELECT 1, to_date('2013-01-01','yyyy-mm-dd'), to_date('2013-03-31','yyyy-mm-dd'), 12, 123 FROM dual UNION ALL
3 SELECT 2, to_date('2013-04-01','yyyy-mm-dd'), to_date('2013-06-30','yyyy-mm-dd'), 12, 123 FROM dual UNION ALL
4 SELECT 3, to_date('2013-01-01','yyyy-mm-dd'), to_date('2013-05-30','yyyy-mm-dd'), 12, 234 FROM dual UNION ALL
5 SELECT 4, to_date('2013-02-01','yyyy-mm-dd'), to_date('2013-04-30','yyyy-mm-dd'), 13, 123 FROM dual UNION ALL
6 SELECT 5, to_date('2013-07-01','yyyy-mm-dd'), to_date('2013-09-30','yyyy-mm-dd'), 13, 345 FROM dual
7 )
8 SELECT add_months(MY_DATE , ROW_NUMBER() OVER(PARTITION BY Q.ORDER_ID, Q.PROD_ID ORDER BY ROWNUM)-1) ddate,
9 Q.ORDER_ID,
10 Q.PROD_ID
11 FROM (SELECT MIN(T.START_DATE) MY_DATE,
12 MAX(T.END_DATE),
13 MONTHS_BETWEEN(MAX(T.END_DATE),MIN(T.START_DATE))+1 CNT,
14 T.ORDER_ID,
15 T.PROD_ID
16 FROM t
17 GROUP BY T.ORDER_ID, T.PROD_ID) Q,
18 TABLE (SELECT COLLECT(ROWNUM) FROM DUAL CONNECT BY LEVEL <= Q.CNT)
19 ORDER BY Q.ORDER_ID, Q.PROD_ID
SQL> /
DDATE ORDER_ID PROD_ID
01.01.2013 12 123
01.02.2013 12 123
01.03.2013 12 123
01.04.2013 12 123
01.05.2013 12 123
01.06.2013 12 123
01.01.2013 12 234
01.02.2013 12 234
01.03.2013 12 234
01.04.2013 12 234
01.05.2013 12 234
01.02.2013 13 123
01.03.2013 13 123
01.04.2013 13 123
01.07.2013 13 345
01.08.2013 13 345
01.09.2013 13 345
17 rows selected
SQL> ----
Ramin Hashimzadeh
Edited by: Ramin Hashimzadeh on Apr 1, 2013 12:15 PM
Similar Messages
-
How can I separate one column into multiple column?
How can I separate one column into multiple column?
This is what I have:
BUYER_ID ATTRIBUTE_NAME ATTRIBUTE_VALUE
0001 PHONE_NUMBER 555-555-0001
0001 EMAIL [email protected]
0001 CURRENCY USD
0002 PHONE_NUMBER 555-555-0002
0002 EMAIL [email protected]
0002 CURRENCY USD
0003 PHONE_NUMBER 555-555-0003
0003 EMAIL [email protected]
0003 CURRENCY CAD
This is what I would like to have:
BUYER_ID PHONE_NUMBER EMAIL CURRENCY
0001 555-555-0001 [email protected] USD
0002 555-555-0002 [email protected] USD
0003 555-555-0003 [email protected] CAD
Any help would be greatly appreciated.This is another solution. Suppose your actual table's name is test(which has the redundant data). create a table like this:
CREATE TABLE test2 (BUYER_ID number(10),PHONE_NUMBER varchar2(50),EMAIL varchar2(50),CURRENCY varchar2(50));
then you will type this procedure:
declare
phone_number_v varchar2(50);
EMAIL_v varchar2(50);
CURRENCY_v varchar2(50);
cursor my_test is select * from test;
begin
for my_test_curs in my_test loop
select ATTRIBUTE_VALUE INTO phone_number_v from test
where person_id=my_test_curs.person_id
and attribute_name ='PHONE_NUMBER';
select ATTRIBUTE_VALUE INTO EMAIL_v from test
where person_id=my_test_curs.person_id
and attribute_name ='EMAIL';
select ATTRIBUTE_VALUE INTO CURRENCY_v from test
where person_id=my_test_curs.person_id
and attribute_name ='CURRENCY';
INSERT INTO test2
VALUES (my_test_curs.person_id,phone_number_v,EMAIL_v,CURRENCY_v);
END LOOP;
END;
Then you will create your final table like this:
create table final_table as select * from test2 where 1=2;
After that write this code:
INSERT ALL
into final_table
SELECT DISTINCT(BUYER_ID),PHONE_NUMBER,EMAIL,CURRENCY
FROM TEST2;
If you have a huge amount of data in your original table this solution may take a long time to do what you need. -
During import, how to auto split one project into multiple albums based on date?
I believe I've done enough aperture reserach to get all my terminology right, so here goes...
I've created a file structure within the aperture inspector the the tune of:
Year [folder], Type of event (Occasion, special occasion, holiday, vacation) [folder], Project (Wedding), Album (i.e. Day 1, Day 2 or Pre wedding week, event, honeymoon).
I also have entire months with 7 or 8 pictures each day, that are of the same type/project (random dog pictures) but are distinctly different days (albums - day in park, in bed at night, watching movie).
My question is, can I import one project with multiple albums already created based on the date they're taken. As I can see it, all I can do is split individual projects automatically by date. But what if one project is over one month, but has multiple distinct days that would be ideal for albums? One more example. We did a trip to San Fran. One day was touring the city, one day was a concert, another day was touring the city. All one project, but 3 different albums (in my mind). I had to import as one project, then separately go in and make albums. I would like that done automatically.
My suspicion is that I can not natively do this within aperture, and that I would need something called an "applescript." I haven't done any reserach into that area, so if that's the case and you know where I can find a script that would do this, I'd appreciate the help.
Sorry for the wordiness, just wanted to be clear. Thanks for any and all help!Hi,
You need to create a new requirement in tcode VOFM for that, together with your ABAP consultant. This requirement will be used in copy contol.
http://saptechsolutions.com/pdf/VOFMCopyRequirementRoutines.pdf
MdZ -
How can I seperate one string into multiple strings using PowerShell?
New to PowerShell...how can I read/separate parts of a file name? For instance if I have a file name like "part1_part2_part3_part4.xls", how to separate this file name to four different parts? So I basically want to go from that file name,
to four different strings like part1, part2, part3, and part4...as four separate strings. How can I get this done in PowerShell?
ThanksYou can try something like this
PS> (Get-ChildItem part1_part2_part3_part4.xls).BaseName.Split('_') -
How can I install one app on multiple iPads at the same time?
We have a volume purchasing account, but when purchasing an app we get multiple serial numbers that all seem to need to be entered individually. The iPhone config Utility will not sync multiple iPads at the same time. Am I missing something? This cannot be the method for mass deployment, it seem too complicated.
You can do a backup of you iPad then restore the backup to many devices.
How to use multiple iPods, iPads, or iPhones with one computer
http://support.apple.com/kb/HT1495
Mobile Device Management Solutions for iOS
http://www.apple.com/iphone/business/integration/mdm/
http://www.apple.com/ipad/business/resources/
"iOS mobile device management (MDM)
With an unmatched breadth of features, a Self Service console forover-the-air (OTA) distribution of in-house and app store apps, and integration with Apple's Volume Purchase Plan (VPP), the Casper Suiteallows administrators to easily achieve the four major tenets of iOSmobile device management: Inventory, Configuration, Security Management, and App Distribution."
http://jamfsoftware.com/products/casper-suite/ -
https://www.icloud.com/iw/#numbers/BAI31boMQXsGKc8NH22BlqWRzbs8PdgWBs-F/2014-15_ Curriculum_Planning_w_CCSS_(Web_Shared)
My document is above, if anyone wants to help this crazy teacher invent a better lesson planner.
Here's my predicament. The state in which I teach requires teachers to keep up with which of the Common Core State Standards (CCSS) their instruction is addressing and when the standards were addressed. I got the bright idea to do this through a spreadsheet in Numbers because our school is getting iPads this year, so it'd be pretty sweet to be able to assemble all of my lesson plans in one massive spreadsheet. On the "AP Lesson Plans" sheet, I've got a few weeks worth of lesson plans entered in. I'm planning on going in and using the drop-down menu next to each lesson plan to add all the CCSS on a per-day basis. This is simple enough, but here's where I run into a problem.
I want the "AP Standards" sheet to concatenate, in cells D3 through G76, the dates on which a standard was covered. What I need is some way for, say, Cell D3 to look at the table on the "AP Lesson Plans" sheet and concatenate the dates of every lesson which has the standard in column A (In this instance, RL.11-12.1). At the end of the day, the functionality I need is that I can enter my lessons and CCSS on the "AP Lesson Plans" sheet and go to the other sheet and see a summary of which standards have been addressed, when, and how often.
If that's as clear as mud, well, I teach literature, not technical writing, so my apologies. I'm more at home with Jonathan Swift than spreadsheets. I'll gladly help you help me if you'd let me know what you need to know. Oh, and to illustrate my knowledge level (just enough to be dangerous), I've left you the formula of my best stab at making cell D3 behave as desired. It's the gray box on the "AP Standards" sheet. Yikes.Hi Arrdenet,
Congratulations on the nested IFs. I haven't tested that formula, but it is impressive. It will be very difficult to debug if it doesn't work, and I think it would make Numbers very slow.
Here is a different approach. After you enter all the CCSS, add another column with HLOOKUP.
Edit:
B2 (and Fill Down)
A2 (and Fill Down)
=IFERROR(HLOOKUP(A$1,C2:E2,1,0),"")
HLOOKUP looks for the value in A1 in all the CCSS columns.
IFERROR is to stop the error triangles appearing and returns "" (NULL) instead.
Then sort by column A.
Not a complete answer, but it might get you started.
Regards,
Ian. -
How can I Make one column into Multiple columns
Id
SCM-01-015
SCM-01-020
SFA-02-021
SFC-02-042
STB-03-035
STP-04-167
SVF-06-150
I want the output like this
Id 01 02 03 04 06
SCM-01-015 5
SCM-01-020 10
SFA-02-021 15
SFC-02-042 15
STB-03-035 20
STP-04-167 50
SVF-06-150 5
thank you in advanceHi,
801668 wrote:
Id
SCM-01-015
SCM-01-020
SFA-02-021
SFC-02-042
STB-03-035
STP-04-167
SVF-06-150
I want the output like this
Id 01 02 03 04 06
SCM-01-015 5
SCM-01-020 10
SFA-02-021 15
SFC-02-042 15
STB-03-035 20
STP-04-167 50
SVF-06-150 5
thank you in advanceWhenever you post formatted text (such as query output) on this site, type these 6 characters:
\(small letters only, inside curly brackets) before and after formatted text, to preserve spacing.
Do you want output something like this, which is from the scott.emp table?ENAME ANALYST CLERK MANAGER PRESIDENT SALESMAN
ALLEN 1600
JONES 2975
FORD 3000
CLARK 2450
MILLER 1300
SMITH 800
WARD 1250
MARTIN 1250
SCOTT 3000
TURNER 1500
ADAMS 1100
BLAKE 2850
KING 5000
JAMES 950
(ALLEN is a SALESMAN, JONES is a MANAGER, ..., JAMES is a CLERK.)
If so, do a pivot, somehting like this:SELECT ename
, MIN (CASE WHEN job = 'ANALYST' THEN sal END) AS analyst
, MIN (CASE WHEN job = 'CLERK' THEN sal END) AS clerk
, MIN (CASE WHEN job = 'MANAGER' THEN sal END) AS manager
, MIN (CASE WHEN job = 'PRESIDENT' THEN sal END) AS president
, MIN (CASE WHEN job = 'SALESMAN' THEN sal END) AS salesman
FROM scott.emp
GROUP BY ename
Always say which version of Oracle you're using.
The query above will work in Oracle 8.1 or higher, but starting in Oracle 11 there's a better way: SELECT ... PIVOT. -
Easy Question: How to split concatenated string into multiple rows?
Hi folks,
this might be an easy question.
How can I split a concatenated string into multiple rows using SQL query?
INPUT:
select 'AAA,BBB,CC,DDDD' as data from dualDelimiter = ','
Expected output:
data
AAA
BBB
CCC
DDDDI'm looking for something kind of "an opposite for 'sys_connect_by_path'" function.
Thanks,
TomasHere is the SUBSTR/INSTR version of the solution:
SQL> WITH test_data AS
2 (
3 SELECT ',' || 'AAA,BBB,CC,DDDD' || ',' AS DATA FROM DUAL
4 )
5 SELECT SUBSTR
6 (
7 DATA
8 , INSTR
9 (
10 DATA
11 , ','
12 , 1
13 , LEVEL
14 ) + 1
15 , INSTR
16 (
17 DATA
18 , ','
19 , 1
20 , LEVEL + 1
21 ) -
22 INSTR
23 (
24 DATA
25 , ','
26 , 1
27 , LEVEL
28 ) - 1
29 ) AS NEW_STRING
30 FROM test_data
31 CONNECT BY LEVEL <= LENGTH(REGEXP_REPLACE(DATA,'[^,]','')) - 1
32 /
NEW_STRING
AAA
BBB
CC
DDDD -
How can we assign one attribute set to multiple business partners at a time ?
How can we assign one attribute set to multiple business partners at a time ? Is it possible ? Can anyone explain me ?
Hello,
please refer to the following thread:
How can we assign one Attribute Set to multiple Business Partners at a time? Is it possible?
best regards,
Johannes -
If I have multiple email accounts, how can I prevent one of them from sending email?
If set up multiple email accounts in Thunderbird, how can I prevent one of them from sending email, while allowing others to send?
Simple answer:
do not use the email address when sending or replying or forwarding emails. Sending is not automatic, you have to generate the email and click on a Send button.
You could use an smtp server that will not allow sending using the incorrect server for the email address, so that it cannot send, but if you accidentally use the email address you may get an error message, but at least the email will not be sent. Do this here:
Tools > Account Settings for the mail account.
bottom right Outgoing Server - select one that will not accept sending using wrong email address
OR
in Outgoing Server(SMTP) select the server you are using for that account
click on 'Edit'
deliberately make an error in the server name - remove the port details etc.
So the details are wrong and cannot send. -
How can I forward a mail to multiple mail accounts that I have in a group without having to add them one by one?
I don't think that you can. Yahoo's basic email features do not include mail forwarding.
-
How can I get an album with multiple artists to display in my Nano as one album insteadof multiple albums?
See this page for helpful information.
http://samsoft.org.uk/iTunes/grouping.asp
B-rock -
If I add artwork by dragging a jpg to the artwork area after selecting several rows, in some instances a single icon is created in the flipper; in others one is created for each song. Why? How can I get one image per album every time?
If I add artwork by dragging a jpg to the artwork area after selecting several rows, in some instances a single icon is created in the flipper; in others one is created for each song. Why? How can I get one image per album every time?
-
HT201066 How can I split a long recording audio file into multiple audio files?
Hi,
How can I split a long recording audio file into multiple audio files?
With Windows I was using Nero software, which is not available for Mac. Is there any similar app for free for Mac?
Thanks!Hello ingiorgio
You can import the track into GarageBand and then split the tracks there. Once that it is done you can highlight the ruler section and then share the song to iTunes to get them separated out.
GarageBand - Split regions in the Tracks area
http://help.apple.com/garageband/mac/10.0/#gbnd76fcce04
GarageBand - Share songs to iTunes
http://help.apple.com/garageband/mac/10.0/#gbndfb96a96f
Regards,
-Norm G. -
How Can I Create One Entry Sheet For Multiple Line Items of A PO
Please Tell Me How Can I Create One Entry Sheet For Multiple Line Items of A PO
hi,
It is not possible to create single Service Entry sheet for multiple PO line items. It is 1 to 1 relation.
you have to make different service entry sheets for different item of service master.
regards
Manish Joshi
Maybe you are looking for
-
How can we search data in flat files
I need to search for a particular list of account numbers whose data is stored in a flat file. Each line represents data for one account number. I also need to put all data for the searched account number into a new flat file. Can anyone help me on t
-
Dynamic configuration multiple folders
Hi all, I have a scenario in which the file adapter should pick multiple files from multiple directories and at the receiver side it should put these files in different folders with different file names. There is no content conversion involved and no
-
Photoshop cs3 will not open any files when I try to open them?
Can somebody please help me? I have tried resetting my preferences in photoshop and it still does not work. I go to file < open < and the image does show up or anything I try to open in photoshop. I have windows xp and I am using photoshop cs3.
-
Whiten Teeth Problem with Perfect Portrait Tool or Smart Brush Tool
The "whiten teeth" tool does not work ... while using either the "Perfect Portrait With Guided Edit" mode or the "Smart Brush - Quick Fix" mode. When I select the toothbrush and drag the curser across the teeth (as the on-line instructor did on the
-
How to import a multiple page pdf into Pages
is there a simple and efficient (not every single page by itself) way to convert/copy/insert a multiple page pdf document into Pages? Thank you.