Convert rows to single column
Hi All
Need some assistance, i have a table where i want the output to be a single column
ex: select from t1;*
query output_
rownum col_1
1 8217
2 6037
3 5368
4 5543
5 5232
i would like the result to be : *8217,6037,5368,5543,5232*
thanks for your help in advance.
i did look on the web but can't find a solution that is easily understood.
Hi,
855161 wrote:
thanks for responding quickly.
the link example seems not to work for me:
below is the information you have requested:Below is some of what I requested. The CREATE TABLE and INSERT statements seem to be missing.
1. duplicate values have no effect
2. list doesn't need any order
3. Version 10.2.0.5.0 For that, I recommend the user-defined aggregate function called STRING_AGG in the Oracle Base page, but called STRAGG by most of the people who use it. You have to copy and run about 60 lines of code from the Oracle Base page, or from AskTom one time, but once you have it installed, the job is as simple as
SELECT MIN (rnum) AS rnum -- ROWNUM isn't a good column name, since it's the same as a pseudo-column
, STRAGG (count_1) AS count_1_list
FROM table_x
;Hundreds of other jobs you have in the future will be just as easy, and you won't have to go through the installation process again.
Relevant information:
1. table columns : count_1
all rows should become a single row with ',' in between
example:
select * from t1;
output:
Rownum count_1
1 8217
2 6037
3 5368
4 5543
5 5232
Desired results :
Rownum count_1
1 8217, 6037, 5368, 5543, 5232
Edited by: 855161 on Dec 11, 2012 1:25 PMThe main problem with STRAGG is that it doesn't create a list in order. You said that's not an issue in this case, but if you ever do need output in order, then the best option in Oracle 10 is the SYS_CONNECT_BY_PATH technique. The main Oracle-Base page shows how to use SYS_CONNECT_BY_PATH in Oracle 9. Of course, that works in all later versions too, but in version 10 a simpler way, using CONNECT_BY_ISLEAF, became available. See the Oracle-Base Comments Page for the eaiser Oracle 10 technique.
Similar Messages
-
Convert rows to single column using t-sql
Hi All,
I have a table with 7 columns, et say..
ID ,PatientName,Date,Time,Room_Num ,Specialized, DoctorName
My source date looks like this..
ID PatientName Date Time Room_Num Specialized DoctorName
1 Sam 10/02/2010 10:00 4 Heart
John
1 Sam 10/02/2010 10:00 4 Lungs
Harris
2 Jones 11/12/2011 11:00 1 Lungs
Bob
3 Jim 12/05/2001 01:00 2 Kidney
Greg
3 Jim 12/05/2001 09:00 2 Eye
Roby
1 Sam 12/22/2010 11:00 1 Heart
John
1 Sam 12/22/2010 11:00 1 Lungs
Harris
My out put should look like this..
ID PatientName Date DoctorName
1 Sam 10/02/2010 John,Harris ( Need to be in 1 row because - ID, PatientName, Date is same)
2 Jones 11/12/2011 Bob ( Need to show as it is due to no repeatition
of ID)
3 Jim 12/05/2001 Greg,Roby ( Need to be in 1 row because - ID, PatientName,
Date is same)
1 Sam 12/22/2010 John,Harris ( Need to be in 1 row because - ID, PatientName, Date is same)
I am using below query to get above results but I am getting wrong results..Need some help in tweaking the query...
SELECT DISTINCT ID ,PatientName , Date
,STUFF(( SELECT ',' + DoctorName
FROM SampleTable ST1
WHERE ST1.ID=ST2.ID
FOR XML PATH('')),1,1,' ') AS DoctorName
FROM SampleTable1 ST2
GROUP BY ID ,PatientName , Date
With above query I am getting results like this...
ID PatientName Date DoctorName
1 Sam 10/02/2010 John,Harris ,John,Harris ( here John,Harris are repeating twice because of 4th row)
2 Jones 11/12/2011 Bob
3 Jim 12/05/2001 Greg,Roby
1 Sam 12/22/2010 John,Harris ,John,Harris ( here John,Harris are repeating twice because of 1st row)
Create table statement:
Create table SampleTable
ID Int null, PatientName varchar(25) null, Date datetime null, Time varchar(10) null, Room_Num int null, Specialized varchar(20) null, DoctorName varchar(20) null
Insert statement:
Insert into sampletable(ID,PatientName,Date,Time,Room_Num,Specialized,DoctorName) Values ( 1,'Sam','10/02/2010' ,'10:00',4 ,'Heart','John')
Insert into sampletable(ID,PatientName,Date,Time,Room_Num,Specialized,DoctorName) Values ( 1, 'Sam','10/02/2010' ,'10:00' , 4 , 'Lungs','Harris')
Insert into sampletable(ID,PatientName,Date,Time,Room_Num,Specialized,DoctorName) Values ( 2, 'Jones' ,'11/12/2011' , '11:00',1, 'Lungs', 'Bob')
Insert into sampletable(ID,PatientName,Date,Time,Room_Num,Specialized,DoctorName) Values ( 3,'Jim' ,'12/05/2001' , '01:00',2,'Kidney' , 'Greg')
Insert into sampletable(ID,PatientName,Date,Time,Room_Num,Specialized,DoctorName) Values ( 3,'Jim' ,'12/05/2001' , '09:00',2,'Eye' , 'Roby')
Insert into sampletable(ID,PatientName,Date,Time,Room_Num,Specialized,DoctorName) Values ( 1,'Sam' ,'12/22/2010' , '11:00',1,'Heart', 'John')
Insert into sampletable(ID,PatientName,Date,Time,Room_Num,Specialized,DoctorName) Values ( 1,'Sam', '12/22/2010' , '11:00',1 , 'Lungs','Harris')
I need help to tweak my query.
Thanks,
RH
sqlWITH cte AS(
SELECT [ID]
,[PatientName]
,[Date]
,[Time]
,[Room_Num]
,[Specialized]
,[DoctorName]
,ROW_NUMBER() OVER(PARTITION BY [ID],[PatientName],[Date] ORDER BY Time) as RowID
FROM [test].[dbo].[SampleTable]
SELECT
cte.[ID]
,cte.[PatientName]
,CAST(cte.[Date] as DATE) as [Date]
,STUFF((SELECT ',' + c.[DoctorName]
FROM cte c
WHERE c.ID=cte.ID AND c.PatientName=cte.[PatientName] AND c.[Date]=cte.[Date]
FOR XML PATH(''),TYPE).value('.','varchar(max)')
,1,1,'') AS [DoctorName]
FROM cte
WHERE cte.RowID=1 -
Convert Rows into single column
Hi,
My employee details table has data like below,
employee_id position department
100 technician 50
100 IT 80
101 Accountant 60
101 Accounting Manager 70
Now i want to covert the rows into column. So my output should be like this.
output:
employee_id position department position1 department1
100 technician 50 IT 80
101 Accountant 60 Accounting Manager 70
Help me on this
Edited by: Vi on Mar 22, 2012 5:36 AM
Edited by: Vi on Mar 22, 2012 5:36 AMIgor.M wrote:
http:// website link removed /t_converting_rows_columns.htm
Please don't post links to commercial websites that are only trying to sell their products and services. It breaches the terms of use of the forums.
There are usually more suitable websites giving much more valuable information without all the self promotion (google adwords etc. are acceptable), or in the case of this particular question there is a FAQ post that relates to it...
{message:id=9360005} -
How to convert rows into single columns in Oracle?
I have table with data like shown below in Oracle database.
P_COLUMN
COLUMN_1
COLUMN_2
COLUMN_3
COLUMN_ 4
COLUMN_5
COLUMN_6
COLUMN_7
COLUMN_8
COLUMN_9
COLUMN_10
1
A1
A2
A3
A4
A5
A6
A7
A8
A9
A10
1
B1
B2
B3
B4
B5
B6
B7
B8
B9
B10
1
C1
C2
C3
C4
C5
C6
C7
C8
C9
C10
2
AA1
AA2
AA3
AA4
AA5
AA6
AA7
AA8
AA9
AA10
2
BB1
BB2
BB3
BB4
BB5
BB6
BB7
BB8
BB9
BB10
I need a query to get one row based on P_COLUMN's value i.e. for P_COLUMN =1, below should be output :-
C_1
C_2
C_3
C_4
C_5
C_6
C_7
C_8
C_9
C_10
C_11
C_12
C_13
C_14
C_15
C_16
C_17
C_18
C_19
C_20
C_21
C_22
C_23
C_24
C_25
C_26
C_27
C_ 28
C_29
C_30
C_31
1
A1
A2
A3
A4
A5
A6
A7
A8
A9
A10
B1
B2
B3
B4
B5
B6
B7
B8
B9
B10
C1
C2
C3
C4
C5
C6
C7
C8
C9
C10
2
AA1
AA2
AA3
AA4
AA5
AA6
AA7
AA8
AA9
AA10
BB1
BB2
BB3
BB4
BB5
BB6
BB7
BB8
BB9
BB10
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
i searched google and found PIVOT, CROSS JOIN etc but could not use those keyword properly.
Thanks in advance.
Note - My DB client version is 11g.Since you have 11G, here's an alternative with the PIVOT clause.
First, set up test data with up to 10 rows:
CREATE TABLE T(P1,C1,C2,C3,C4,C5,C6,C7,C8,C9,C10) AS SELECT
1,1,2,3,4,5,6,7,8,9,10 FROM DUAL;
INSERT INTO T
WITH DATA AS (SELECT LEVEL*10 N FROM DUAL CONNECT BY LEVEL <= 9)
SELECT P1, C1+N, C2+N, C3+N, C4+N, C5+N, C6+N, C7+N, C8+N, C9+N, C10+N
FROM T, DATA;
INSERT INTO T
SELECT P1+1,C1,C2,C3,C4,C5,C6,C7,C8,C9,C10
FROM T
WHERE C1 <= 11;
select * from t order by p1,c1;
P1
C1
C2
C3
C4
C5
C6
C7
C8
C9
C10
1
1
2
3
4
5
6
7
8
9
10
1
11
12
13
14
15
16
17
18
19
20
1
21
22
23
24
25
26
27
28
29
30
1
31
32
33
34
35
36
37
38
39
40
1
41
42
43
44
45
46
47
48
49
50
1
51
52
53
54
55
56
57
58
59
60
1
61
62
63
64
65
66
67
68
69
70
1
71
72
73
74
75
76
77
78
79
80
1
81
82
83
84
85
86
87
88
89
90
1
91
92
93
94
95
96
97
98
99
100
2
1
2
3
4
5
6
7
8
9
10
2
11
12
13
14
15
16
17
18
19
20
Now the SELECT statement using PIVOT:
SELECT * FROM (
SELECT T.*,
ROW_NUMBER() OVER (
PARTITION BY P1
ORDER BY C1,C2,C3,C4,C5,C6,C7,C8,C9,C10
) RN
FROM T
PIVOT(
MAX(C1) C1, MAX(C2) C2, MAX(C3) C3, MAX(C4) C4, MAX(C5) C5,
MAX(C6) C6, MAX(C7) C7, MAX(C8) C8, MAX(C9) C9, MAX(C10) C10
FOR RN IN (1 R1,2 R2,3 R3,4 R4,5 R5,6 R6,7 R7,8 R8,9 R9,10 R10)
P1
R1_C1
R1_C2
R1_C3
R1_C4
R1_C5
R1_C6
R1_C7
R1_C8
R1_C9
R1_C10
R2_C1
R2_C2
R2_C3
R2_C4
R2_C5
R2_C6
R2_C7
R2_C8
R2_C9
R2_C10
R3_C1
R3_C2
R3_C3
R3_C4
R3_C5
R3_C6
R3_C7
R3_C8
R3_C9
R3_C10
R4_C1
R4_C2
R4_C3
R4_C4
R4_C5
R4_C6
R4_C7
R4_C8
R4_C9
R4_C10
R5_C1
R5_C2
R5_C3
R5_C4
R5_C5
R5_C6
R5_C7
R5_C8
R5_C9
R5_C10
R6_C1
R6_C2
R6_C3
R6_C4
R6_C5
R6_C6
R6_C7
R6_C8
R6_C9
R6_C10
R7_C1
R7_C2
R7_C3
R7_C4
R7_C5
R7_C6
R7_C7
R7_C8
R7_C9
R7_C10
R8_C1
R8_C2
R8_C3
R8_C4
R8_C5
R8_C6
R8_C7
R8_C8
R8_C9
R8_C10
R9_C1
R9_C2
R9_C3
R9_C4
R9_C5
R9_C6
R9_C7
R9_C8
R9_C9
R9_C10
R10_C1
R10_C2
R10_C3
R10_C4
R10_C5
R10_C6
R10_C7
R10_C8
R10_C9
R10_C10
1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 -
Convert different rows into single column
DB : 11.1.0.7
OS : Solaris Sparc 5.10
I have one query which is joining few tables and giving me output like below.
personnum orgnm
======= =======
6 Keyholder
9 Sales
3 Mgmt
I would like to convert that into single column like below.
col1
========
6,Keyholder,9,Sales,3,Mgmt
I have tried with pivot and decode, but not getting resule which I am exepcting. Any suggesstions ?yashwanth437 wrote:
listagg() function could work.LISTAGG is not available in 11.1. It was introduced in 11.2.
Anyway, XML solution:
with sample_table as (
select 6 personnum,'Keyholder' orgnm from dual union all
select 9,'Sales' from dual union all
select 3,'Mgmt' from dual
select rtrim(xmlagg(xmlelement(e,personnum || ',' || orgnm,',').extract('//text()')),',') col1
from sample_table
COL1
6,Keyholder,9,Sales,3,Mgmt
SQL> SY. -
How to join 2 rows under single column header
i want to join 2 or more rows under a single column header. as shown "container details" is the column name with "h", "w", and "s" and the sub column names. so my header should contain all these four names and my 3 rows under h,w and s should be clubed under this container details.
anyone help me out. thanks in advance.
container details
| h | w | sBummer, I just tried the link (which I saved from a couple months ago) and it doesn't work. This was a great site for showing how to do various things in Swing. Sorry for the bad link. Maybe someone saved the sites info or knows where it may have moved. Dang...
-
How to convert Rows to different columns
Hi All,
I am having a table address with the following data.
ID REL_NAME REL_RELATION REL_PHONE
1--- kish_rel1--- wife--- 1111
1--- kish_rel2--- sister--- 2222
1--- kish_rel3--- brother--- 3333
2--- ram_rel1--- brother--- 4444
Now i want to display rows data into columns. See the output i want. Exactly, I dont know how many rows are there for each ID. It may increase or decrease.
ID REL_NAME REL_RELATION REL_PHONE
1---kish_rel1--- wife--- 1111---kish_rel2--- sister--- 2222---kish_rel3--- brother--- 3333
2---ram_rel1--- brother--- 4444
Thanks in advance,
PalHi,
I have found this is useful. But it is static. It wont work if we dont know the maximum number of rows present when grouped by ID.
Is there any other solution, which will give correct values, if we dont know the row count when grouped by ID.
SELECT hrid,
MAX(case when seq=1 then rel_name end) AS "rel_name1",
MAX(case when seq=1 then rel_relation end) AS "rel_relation1",
MAX(case when seq=1 then rel_phone end) AS "rel_phone1",
MAX(case when seq=2 then rel_name end) AS "rel_name2",
MAX(case when seq=2 then rel_relation end) AS "rel_relation2",
MAX(case when seq=2 then rel_phone end) AS "rel_phone2",
MAX(case when seq=3 then rel_name end) AS "rel_name3",
MAX(case when seq=3 then rel_relation end) AS "rel_relation3",
MAX(case when seq=3 then rel_phone end) AS "rel_phone3",
MAX(case when seq=4 then rel_name end) AS "rel_name4",
MAX(case when seq=4 then rel_relation end) AS "rel_relation4",
MAX(case when seq=4 then rel_phone end) AS "rel_phone4"
FROM (SELECT hrid, rel_name, rel_relation, rel_phone, ROW_NUMBER() over(partition by hrid ORDER BY hrid) AS seq FROM address)
GROUP BY hrid
Thanks,
Pal -
Converting multiple column rows into single column
Hi all Below is my requirement, can you anybody help me solve it. CREATE TABLE TYPE ( c1_type VARCHAR2 (10), c2_type VARCHAR2 (10), c3_type VARCHAR2 (10), c4_type VARCHAR2 (10), c5_type VARCHAR2 (10), c6_type VARCHAR2 (10), c7_type VARCHAR2 (10), c8_type VARCHAR2 (10), c9_type VARCHAR2 (10)); INSERT INTO TYPE VALUES ('Region_D', 'Region_E', 'Region_F', 'Region_D', 'Region_E', 'Region_D', 'Region_M', 'Region_D', 'Region_E'); commit; Acutal output on the below query select * from type; C1_TYPE C2_TYPE C3_TYPE C4_TYPE C5_TYPE C6_TYPE C7_TYPE C8_TYPE C9_TYPE Region_D Region_E Region_F Region_D Region_E Region_D Region_M Region_D Region_E Acutal i am expecting the below output,how to make it using query or using oracle built in function. Region_D Region_D Region_D Region_D Region_E Region_E Region_E Region_F Region_M
Hi,
So, you want to put the column values in alphabetic order, with the earliest value in c1_type and the last in c9_type, is that it?
The fact that you even want to do this says that this is a bad table design. If sorting the values makes sense, then putting them all in the same column on 9 different rows (rather than 9 different columns on 1 row) makes sense.
If you're stuck with the current design, then use SELECT ... UNPIVOT to put them on separate rows, then use the analytic ROW_NUMBER function to assign numbers 1-9 to each value, and finally use SELECT ... PIVOT to put them back into 1 row in that order. If you're not familiar with the built-in UNPIVOT and PIVOT features, look them up in the SQL Language manual, and see the forum FAQ:
https://forums.oracle.com/message/9362005
If you get stuck, post your best attempt (formatted), the exact results you want (formatted), and your Oracle version (e.g. 11.2.0.2.0).
Also, TYPE is an Oracle keyword, so it's not a good table name. Use something like location_type instead. -
Hi,
I've records like:
Officer ID Qualification Id Qualification Type Qualification
1 1 Academic 10th
1 2 Academic 12th
1 3 Academic B.Sc.
2 1 Academic 10th
2 2 Academic I.Sc.
2 3 Academic B.A.
I need to rotate the rocards and make it like:
Officer ID Qualification1 Qualification2 Qualification3
1 10th 12th B.Sc.
2 10th I.Sc. B.A.
The system accepts 60 different possible values of qualifications, but out of them an officer can have at max 6 different qualifications.
Can you please suggest me , how to acheive this conversion of rows into
columns.
Thanks And regards
Rajeev RanjanYou can use unpivot for that purpose.
Jörg -
How to convert row data into columns without using pivot table?
Hello guys
I have a report that has several columns about sales call type and call counts
It looks like this:
Calls Type Call Counts
Missed 200
Handled 3000000
Rejected 40000
Dropped 50000
Now I wanna create a report that look like this:
Missed call counts Handled Call counts Rejected Counts Drop counts Other Columns
200 300000000 40000 50000 Data
So that I can perform other calculations on the difference and comparison of handled calls counts vs other call counts..
I know pivot table view can make the report look like in such way, but they cant do further calculations on that..
So I need to create new solid columns that capture call counts based on call types..
How would I be able to do that on Answers? I don't have access to the RPD, so is it possible to do it sololy on answers? Or should I just ask ETL support on this?
Any pointers will be deeply appreciated!
Thank youThanks MMA
I followed your guidance and I was able to create a few new columns of call missed count, call handled counts and call abandoned counts.. Then I create new columns of ave missed counts, ave handled counts and so forth..
Then I went to the pivot view, I realized there is a small problem.. Basically the report still includes the column "call types" which has 3 different types of call "miss, abandon and handled". When I exclude this column in my report, the rest of the measures will return wrong values. When I include this column in the report, it shows duplicate values by 3 rows. It looks like this:
Queue name Call types Call handled Call missed Call abondoned
A Miss 8 10 15
A Handled 8 10 15
A Abandoned 8 10 15
in pivot table view, if I move call type to column area, the resulted measures will become 8X3, 10X3 and 15X3
So is there a way to eliminate duplicate rows or let the system know not to mulitply by 3?
Or is this as far as presentation service can go in terms of this? Or should I advice to provide better data from the back end?
Please let me know, thanks -
I have a SQL statement which outputs a two column table with a ProjectName and a MemberFullValue, both text types values:
SELECT proj.ProjectName, lt.MemberFullValue
FROM dbo.MSP_EpmProject_UserView AS proj
LEFT JOIN [dbo].[MSPCFPRJ_IT Pillar(s)_AssociationView] AS itpillar
ON proj.ProjectUID = itpillar.EntityUID
LEFT JOIN dbo.MSP_EpmLookupTable AS lt
ON itpillar.LookupMemberUID = lt.MemberUID
Both columns can contain duplicate values and both contain text.
When there are duplicate values for 'ProjectName' I want to convert the corresponding 'MemberFullValue' values to columns. I have looked at a lot of different solutions to questions already posted, but I am not a SQL genius so I cannot get any of them to work.
Can someone help me out? Thanks.
Sounds like this to me
SELECT ProjectName,
[1] AS Value1,
[2] AS value2,
[3] AS Value3,
[4] AS Value4,
[5] AS Value5,
[6] AS Value6
FROM
SELECT ROW_NUMBER() OVER (PARTITION BY proj.ProjectName ORDER BY proj.ProjectName) AS Seq,
proj.ProjectName, lt.MemberFullValue
FROM dbo.MSP_EpmProject_UserView AS proj
LEFT JOIN [dbo].[MSPCFPRJ_IT Pillar(s)_AssociationView] AS itpillar
ON proj.ProjectUID = itpillar.EntityUID
LEFT JOIN dbo.MSP_EpmLookupTable AS lt
ON itpillar.LookupMemberUID = lt.MemberUID
)t
PIVOT (MAX(MemberFullValue) FOR Seq IN ([1],[2],[3],[4],[5],[6]))p
to make it dynamic you can use this
http://beyondrelational.com/modules/2/blogs/70/posts/10840/dynamic-pivot-in-sql-server-2005.aspx
If this is not what you're after, post some sample data and explain the required output
Please Mark This As Answer if it solved your issue
Please Mark This As Helpful if it helps to solve your issue
Visakh
My MSDN Page
My Personal Blog
My Facebook Page -
Generating rows from single column
hi All,
I need u guys help for following issue.
Supose i have a table called parameters with following columns
TABLE - PARAMETERS;
desc PARAMETERS;
customer_id number(9)
params varchar2(3500)
say againt a customer id 100 data stored in params like below
X;Y;PP;CH;KK;RX:KY
My question is ..is it possible to have a SQL query that can split into multiple rows like below.
100 X
100 Y
100 PP
100 CH
100 KK
100 RX
100 KY
Basically i am looking for a solution with a general sql query with out writing plsql.
ThanksI had the following in an old code archive which does what you want - assuming the semi-colon is the separator.... (hope the code tags format this correctly!)
with tmp as (
SELECT 100 as customer_id, 'X;Y;PP;CH;KK;RX;KY' as params FROM DUAL
UNION ALL
SELECT 200 as customer_id, 'UI;RT;QE' as params FROM DUAL
select t1.*,
trim(replace(substr(t1.params,mstart,(t2.mlevel-mstart)+1),';','')) as data_split
from tmp t1
join (
select customer_id,substr(params,level,1) as mchar,
level as mlevel,nvl(lag(level) over (partition by customer_id order by level),0)+1 as mstart
from tmp
where substr(params,level,1)=';'
or level=length(params)
connect by level<=length(params)
and prior customer_id=customer_id
AND PRIOR DBMS_RANDOM.STRING ('p', 10) IS NOT NULL
) t2
on t1.customer_id=t2.customer_id -
Combine two rows in single column data
hi,
I have an requirement
for ex:
Division Resourcename PRojectname Projectstartdate Projectenddate allocated percentage
development A one '01/01/2014' '03/31/2014'
100%
development B one '01/01/2014' '03/31/2014'
100%
development C one '01/01/2014' '03/31/2014'
100%
development D one '01/01/2014' '03/31/2014'
100%
development E one '01/01/2014' '01/31/2014'
25%
development E one '02/01/2014' '03/31/2014'
25%
In the above "Resource E have multiple dates ,will work on separate dates on single project,but the resource allocated percentage will be always same(ie)=25%,so the report output look like
Division Resourcename PRojectname Projectstartdate Projectenddate allocated percentage
development A one '01/01/2014' '03/31/2014'
100%
development B one '01/01/2014' '03/31/2014'
100%
development C one '01/01/2014' '03/31/2014'
100%
development D one '01/01/2014' '03/31/2014'
100%
development E one '01/01/2014' '01/31/2014'
25%
'02/01/2014' '03/31/2014'
the above SSRS report output look after grouping on division,resource.
this same output i need from sql query.how to achieve this.
Thanks
r.bBhupesh, I do not have any idea how your actual data would look like. So the below snippet can
only taken as reference not as an actual.
[Join condition, CASE...WHEN everything would change as per your actual data]
Try the below:
create table Bhupesh_test(Division varchar(50), Res_Name varchar(50), Proj_Name varchar(50), startdate date, enddate date, allocated int)
Insert into Bhupesh_test Values
('development','A','one', '01/01/2014','03/31/2014',100),
('development','B','one', '01/01/2014','03/31/2014',100),
('development','C','one', '01/01/2014','03/31/2014',100),
('development','D','one', '01/01/2014','03/31/2014',100),
('development','E','one', '01/01/2014','01/31/2014',25),
('development','E','one', '02/01/2014','03/31/2014',25)
;With cte
as
Select *, ROW_NUMBER()OVER(Partition by Division,Res_Name,Proj_Name Order by startdate)Rn
From Bhupesh_test
Select Division,Res_Name,Proj_Name,startdate,enddate,allocated From
Select Case when A.Division = B.Division and A.Res_Name = B.Res_Name and A.Proj_Name = B.Proj_Name
and A.startdate <>B.startdate Then NULL Else A.Division end Division,
Case when A.Division = B.Division and A.Res_Name = B.Res_Name and A.Proj_Name = B.Proj_Name
and A.startdate <>B.startdate Then NULL Else A.Res_Name end Res_Name,
Case when A.Division = B.Division and A.Res_Name = B.Res_Name and A.Proj_Name = B.Proj_Name
and A.startdate <>B.startdate Then NULL Else A.Proj_Name end Proj_Name,
Case when A.Division = B.Division and A.Res_Name = B.Res_Name and A.Proj_Name = B.Proj_Name
Then A.startdate Else B.startdate end startdate,
Case when A.Division = B.Division and A.Res_Name = B.Res_Name and A.Proj_Name = B.Proj_Name
AND A.startdate <> B.startdate Then A.enddate Else B.enddate end enddate,
Case when A.Division = B.Division and A.Res_Name = B.Res_Name and A.Proj_Name = B.Proj_Name
and A.startdate <>B.startdate Then NULL Else A.Allocated end Allocated,1 Ordered
From cte A
Inner join cte B on A.Rn = B.Rn+1 and A.Res_Name = B.Res_Name
Union All
Select Division,Res_Name,Proj_Name,startdate,enddate,allocated,0 From cte A where Rn=1
) A
Order by Ordered
Drop table Bhupesh_test -
How to Convert Rows of the Columns in Multiple Columns
Hi,
I have Data in one of the table of Oracle Application.
Sample Data.....
Period_Start_date Period_Type Demand
09-22-2005 1 5
09-21-2005 1 10
09-20-2005 1 5
09-19-2005 1 2
09-18-2005 1 20
09-17-2005 1 19
09-16-2005 1 3
Output Required.
Period 6 Period 5 Period 4 Period 3 Period 2 Period 1 Current
3 19 20 2 5 10 5
This need to be Done in Using SQL Statement.
On the base of Current Date enter by the User ...it will pickup last six date and Demand on that Date in Required Format.
Pls Do let me know Any more Clarification
Pls can any body Help me
Regards
RoshanCREATE TABLE t1
( col1 VARCHAR2(10),
col2 VARCHAR2(100)
INSERT INTO t1 VALUES('A','123,456,789');
INSERT INTO t1 VALUES('B','012,345');
INSERT INTO t1 VALUES('C','678,901,234');
SELECT col1, col2, pos, pre, SUBSTR(col2, pre+1, pos-pre-1) token FROM (
SELECT row_num, col1, col2, decode(pos,0,LENGTH(col2)+1,pos) pos, pre FROM (
SELECT row_num, col1, col2, pos, nvl(LAG(pos) OVER (PARTITION BY col1 ORDER BY row_num),0) pre
FROM (
SELECT row_num, vw.col1, vw.col2, INSTR(vw.col2,',',1,row_num) pos
FROM (
SELECT ROWNUM row_num FROM (
SELECT LEVEL FROM dual connect by LEVEL <= 5
)) r,
(SELECT col1, col2 FROM t1) vw
WHERE pos <> 0 OR pre <> 0
i think some part it may be help u also -
Rows into single column with comma seperator
Hi Friends,
I have the following Query
select A.tradeid||','||A.TICKER||'|'||A.SUBORDINATION||'|'||A.CUSIP||'|'||A.DOCCLAUSE||'|'||A.CURRENCY from table A
where A.ticker in ('LYME','GAADF') and A.tradeid in('456777')
which is returning
456777,LYME|SENIOR UNSECURED|Z1700990|MM|USD
456777,GAADF|SENIOR UNSECURED|Z1246790|MM|USD
I want the result set as:
456777,LYME|SENIOR UNSECURED|Z1700990|MM|USD,456777,GAADF|SENIOR UNSECURED|Z1246790|MM|USD
Please help me to get the result set
Thanks,
ragu.
Edited by: user533548 on Apr 3, 2009 12:54 AM
Edited by: user533548 on Apr 3, 2009 12:55 AMyou could do
select max (ltrim (sys_connect_by_path (tradeid||'|'||TICKER||'|'||SUBORDINATION||'|'||CUSIP||'|'||DOCCLAUSE||'|'||CURRENCY, ','), ','))
from (
select tradeid
, ticker
, subordination
, cusip
, docclause
, currency
, row_number() over (partition by tradeid
order by null
) rn
from x)
start with rn = 1
connect by rn = prior rn + 1like in
SQL> with x as
2 (
3 select 456777 tradeid ,'LYOE' ticker ,'SENIOR UNSECURED' subordination,'Z1830990' cusip,'MM' docclause,'USD' currency from dual union all
4 select 456777 tradeid ,'GAZDF','SENIOR UNSECURED','Z8446790','MM','USD' from dual
5 )
6 select max (
7 ltrim
8 (sys_connect_by_path (tradeid||'|'||TICKER||'|'||SUBORDINATION||'|'||CUSIP||'|'||DOCCLAUSE||'|'||CURRENCY
9 , ',')
10 , ',')) str
11 from (
12 select tradeid
13 , ticker
14 , subordination
15 , cusip
16 , docclause
17 , currency
18 , row_number() over (partition by tradeid
19 order by null
20 ) rn
21 from x)
22 start with rn = 1
23 connect by rn = prior rn + 1
24
SQL> /
STR
456777|GAZDF|SENIOR UNSECURED|Z8446790|MM|USD,456777|LYOE|SENIOR UNSECURED|Z1830990|MM|USD
Maybe you are looking for
-
Can't get BOBJ MOBILE 4.0 SP2 to work
Hello, I'm trying to set up bobj moble 4.0 sp2 (without BES/MDS). I have bobj 4.0 sp2 server, running under linux, and mobile server components, installed on windows machine. My current config: VAS server config - $Id: server.config 27498 2007-10-01
-
Prevent creation of duplicate material master record
Hi Experts 1) Is there any control or validation to prevent creation of duplicate material? I mean, if the description of new material is 100% or 95% matching with an existing material, there should be a validation. 2) Is there any feature of usage
-
Help required in module insertion
Dear All, I have 6509 switch. In its first slot present 10/100 ethernet module is there. I have to remove this module and insert 10/100/1000 ethernet module. Can I have some valid suggestions: 1 Like these are the precautions needs to be taken 2 Like
-
Itunes freezes when i connect my iphone
also it wont sync and i cant repair itunes by using the repair option in the download page
-
My Incredible has taken to crashing and restarting randomly. It has also seemed to turn off the 3G radio at times, which is cured by turning the phone on and off......it didn't do this prior to the 2.2 update, but took about a month after to start t