Table PIVOT query need help
I have a requirement as below-The output I want as below.
EMP_ID LAPTOP DESK LAPTOP LCD MONITOR ROUTER
------------------------------------------------------------------------------------------
100 Y Y Y Y
101 Y N Y N
102 N Y N N
-----------------------------------------------------------------------------------------
I wrote a query as below with hard-code the code produced.SELECT EMP_ID,
NVL(MAX(DECODE(PROD_ID,10,'Y',NULL)),'N') LAPTOP,
NVL(MAX(DECODE(PROD_ID,11,'Y',NULL)),'N') DESK_LAPTOP,
NVL(MAX(DECODE(PROD_ID,12,'Y',NULL)),'N') LCD_MONITOR,
NVL(MAX(DECODE(PROD_ID,13,'Y',NULL)),'N') ROUTER
FROM XX_EMP a
GROUP BY EMP_ID;
I want a solution without Hardcoding code product, because if we need to introduce a new product in the product table, then the output will be fit dynamically...Is it possible to write the query like this?
Is my version of DB - Oracle 10 g
Table creation script-
CREATE TABLE XX_EMP (
EMP_ID NUMBER,
PROD_ID NUMBER,
SALE_DT DATE
);
INSERT INTO XX_EMP VALUES(100,10,sysdate-15);
INSERT INTO XX_EMP VALUES(100,11,sysdate-14);
INSERT INTO XX_EMP VALUES(100,12,sysdate-14);
INSERT INTO XX_EMP VALUES(100,13,sysdate-13);
INSERT INTO XX_EMP VALUES(101,11,sysdate-11);
INSERT INTO XX_EMP VALUES(101,13,sysdate-10);
INSERT INTO XX_EMP VALUES(102,12,sysdate-10);
COMMIT;
CREATE TABLE XX_PRODUCT(PROD_ID NUMBER,
PROD_DES VARCHAR2(50)
);
INSERT INTO XX_PRODUCT VALUES(10,'LAPTOP');
INSERT INTO XX_PRODUCT VALUES(11,'DESK LAPTOP');
INSERT INTO XX_PRODUCT VALUES(12,'LCD MONITOR');
INSERT INTO XX_PRODUCT VALUES(13,'ROUTER');
COMMIT;
Hello
See this thread for more options:
Re: County report and the sum of the number of rows by multiple columns
It seems that the aggregation of the chain might be the best for this work.
For example:
I guess you don't want to view the whole xx_emp table in all reports. In the first auxiliary request, emp_summary, below I've limited the output lines with the sale_dt in the last 30 days. You can change this to anything else, or omit the WHERE all clause if you do not want to include the entire table.
I assume that you will not necessarily interested in the whole xx_product. In the second auxiliary request, product_summary, I've limited the output to products that appear effectively in emp_summary. Again, you can use any conditions you want, or include all the lines.
The main request is the UNION of 3 games: 2 produce header lines, and the third produces the bulk of the production. Note that (as far as Oracle is concerned) there are only 2 columns in the output. The last of them will be foramatted to resemble a variable number of columns.
VARIABLE column_width NUMBER
EXEC :column_width := 12;
SET PAGESIZE 0
WITH emp_summary AS
(
SELECT emp_id, prod_id
FROM xx_emp
WHERE sale_dt >= TRUNC (SYSDATE - 30) -- or whatever
GROUP BY emp_id, prod_id
)
, product_summary AS
(
SELECT prod_id
, SUBSTR (prod_des, 1, :column_width) AS short_prod_des
, ROW_NUMBER () OVER (ORDER BY prod_id) AS r_num
FROM xx_product
WHERE prod_id IN (
SELECT prod_id
FROM emp_summary
)
)
--
-- ===== First Header Line: Product Names =====
--
SELECT NULL AS emp_id
, REPLACE ( SYS_CONNECT_BY_PATH ( LPAD ( short_prod_des
, :column_width
)
, '~'
)
, '~'
, ' '
) AS txt
FROM product_summary
WHERE CONNECT_BY_ISLEAF = 1
START WITH r_num = 1
CONNECT BY r_num = PRIOR r_num + 1
--
UNION ALL -- ===== Second Header Line: Hyphens =====
--
SELECT NULL AS emp_id
, SYS_CONNECT_BY_PATH ( LPAD ( '-'
, :column_width
, '-'
)
, ' '
) AS txt
FROM product_summary
WHERE CONNECT_BY_ISLEAF = 1
START WITH r_num = 1
CONNECT BY r_num = PRIOR r_num + 1
--
UNION ALL -- ===== Body of Report =====
--
SELECT e.emp_id
, REPLACE ( SYS_CONNECT_BY_PATH ( LPAD ( NVL2 ( e.prod_id
, 'Y'
, 'N'
)
, :column_width
)
, '~'
)
, '~'
, ' '
) AS txt
FROM product_summary p
LEFT OUTER JOIN emp_summary e PARTITION BY (e.emp_id)
ON p.prod_id = e.prod_id
WHERE CONNECT_BY_ISLEAF = 1
START WITH p.r_num = 1
CONNECT BY p.r_num = PRIOR p.r_num + 1
AND e.emp_id = PRIOR e.emp_id
--
-- ===== Common ORDER BY Clause =====
--
ORDER BY emp_id NULLS FIRST
, txt
;
Output:
` LAPTOP DESK LAPTOP LCD MONITOR ROUTER
------------ ------------ ------------ ------------
100 Y Y Y Y
101 N Y N Y
102 N N Y N
There are a few things you might want to change.
For example, if the first product_name is: characters column_width, the ORDER BY clause will put hyphens, first. Which can be fixed by putting the UNION in a subquery, so that you can ORDER BY columns that do not appear in the output.
You could also include all product_des, dividing each item into pieces of no. more: column_width characters. I'll leave that as an exercise.
Published by: Frank Kulash, April 12, 2011 17:49
Example of grouping of extra chain
Tags: Database
Similar Questions
-
Paging query needed help for large table - force a different index
I use a slight modification of the pagination to be completed request to ask Tom: [http://www.oracle.com/technology/oramag/oracle/07-jan/o17asktom.html]
Mine looks like this to extract the first 100 lines of everyone whose last name Smith, ordered by join date:
The difference between this and ask Tom is my innermost query returns just the ROWID. Then, in the outermost query we associate him returned to the members table ROWID, after that we have cut the ROWID down to only the 100 piece we want. This makes it MUCH more (verifiable) fast on our large tables, because it is able to use the index on the innermost query (well... to read more).SELECT members.* FROM members, ( SELECT RID, rownum rnum FROM ( SELECT rowid as RID FROM members WHERE last_name = 'Smith' ORDER BY joindate ) WHERE rownum <= 100 ) WHERE rnum >= 1 and RID = members.rowid
The problem I have is this:
It will use the index for the column predicate (last_name) rather than the unique index that I defined for the column joindate (joindate, sequence). (Verifiable with explain plan). It is much slower this way on a large table. So I can reference using one of the following methods:SELECT rowid as RID FROM members WHERE last_name = 'Smith' ORDER BY joindate
SELECT /*+ index(members, joindate_idx) */ rowid as RID FROM members WHERE last_name = 'Smith' ORDER BY joindate
Whatever it is, it now uses the index of the column ORDER BY (joindate_idx), so now it's much faster there not to sort (remember, VERY large table, millions of records). If it sounds good. But now, on my outermost query, I join the rowid with the significant data in the members table columns, as commented below:SELECT /*+ first_rows(100) */ rowid as RID FROM members WHERE last_name = 'Smith' ORDER BY joindate
As soon as I did this join, this goes back to the use of the index of predicate (last_name) and perform the sort once he finds all the corresponding values (which can be a lot in this table, there is a cardinality high on some columns).SELECT members.* -- Select all data from members table FROM members, -- members table added to FROM clause ( SELECT RID, rownum rnum FROM ( SELECT /*+ index(members, joindate_idx) */ rowid as RID -- Hint is ignored now that I am joining in the outer query FROM members WHERE last_name = 'Smith' ORDER BY joindate ) WHERE rownum <= 100 ) WHERE rnum >= 1 and RID = members.rowid -- Merge the members table on the rowid we pulled from the inner queries
My question therefore, in the query full above, is it possible that I can get to use the ORDER of indexing BY column to prevent having to sort? The join is what makes go back to using the predicate index, even with notes. Remove the join and just return the ROWID for these 100 records and it flies, even over 10 millions of documents.
It would be great if there was some generic hint that could accomplish this, such as if we change the table/column/index, do not change the indicator (indicator FIRST_ROWS is a good example of this, while the INDEX indicator is the opposite), but any help would be appreciated. I can provide explain plans for the foregoing, if necessary.
Thank you!
-
Thanks in advance
Table (Data):
-----
Date of Bank_id Device_type State Station_status
1011 N O 23/03/2012 DOWN
1011 N Y until 23/03/2012
1011 N there until 23/03/2012
1011 E Y low 23/03/2012
1011 E I have low 23/03/2012
1011 W Y until 23/03/2012
1012 N O 23/03/2012 DOWN
1012 N Y until 23/03/2012
1012 N there until 23/03/2012
1012 E Y low 23/03/2012
1012 E I have low 23/03/2012
1012 W Y until 23/03/2012
1013 N O 23/03/2012 DOWN
1013 N Y until 23/03/2012
1013 N there until 23/03/2012
1013 E Y low 23/03/2012
1013 E I have low 23/03/2012
1013 W Y until 23/03/2012
(SELECT QUERY RESULT SHOULD BE LIKE THIS): result report:
Bank_id - Date - TOTAL - UP_STATUS - DOWN_STATUS
1011 3/23 / 2012 6 3 3
1012 3/23 / 2012 6 3 3
1013 3/23 / 2012 6 3 3Try this:
with t as ( select 1011 Bank_id , 'N' Device_type,'Y' Status ,to_date('3/23/2012' ,'mm/dd/yyyy') l_date , 'down' Station_status from dual union all select 1011,'N', 'Y', to_date('3/23/2012','mm/dd/yyyy') l_date , 'up' Station_status from dual union all select 1011 , 'N','Y' ,to_date('3/23/2012','mm/dd/yyyy'), 'up' Station_status from dual union all select 1011, 'E' ,'Y' ,to_date('3/23/2012','mm/dd/yyyy'), 'down' Station_status from dual union all select 1011,'E' ,'I ',to_date('3/23/2012','mm/dd/yyyy'), 'down' Station_status from dual union all select 1011 ,'W','Y' ,to_date('3/23/2012','mm/dd/yyyy'), 'up' Station_status from dual union all select 1012 , 'N', 'Y' ,to_date('3/23/2012','mm/dd/yyyy'), 'down' Station_status from dual union all select 1012 ,'N', 'Y' ,to_date('3/23/2012','mm/dd/yyyy'), 'up' Station_status from dual union all select 1012 ,'N', 'Y' ,to_date('3/23/2012','mm/dd/yyyy'), 'up' Station_status from dual union all select 1012 ,'E' ,'Y' ,to_date('3/23/2012','mm/dd/yyyy'), 'down' Station_status from dual union all select 1012, 'E', 'I' ,to_date('3/23/2012','mm/dd/yyyy'), 'down' Station_status from dual union all select 1012 ,'W' ,'Y' ,to_date('3/23/2012','mm/dd/yyyy'), 'up' Station_status from dual union all select 1013 ,'N','Y' ,to_date('3/23/2012','mm/dd/yyyy'), 'down' Station_status from dual union all select 1013, 'N','Y' ,to_date('3/23/2012','mm/dd/yyyy'), 'up' Station_status from dual union all select 1013 , 'N','Y' ,to_date('3/23/2012','mm/dd/yyyy'), 'up' Station_status from dual union all select 1013, 'E' ,'Y' ,to_date('3/23/2012','mm/dd/yyyy'), 'down' Station_status from dual union all select 1013, 'E','I',to_date('3/23/2012','mm/dd/yyyy'), 'down' Station_status from dual union all select 1013 ,'W' ,'Y' ,to_date('3/23/2012','mm/dd/yyyy'), 'up' Station_status from dual ) SELECT bank_id, COUNT (*) total, MAX (l_date) ldate, SUM (CASE WHEN Station_status = 'down' THEN 1 ELSE 0 END) down_status, SUM (CASE WHEN Station_status = 'up' THEN 1 ELSE 0 END) up_status FROM t GROUP BY bank_id
Published by: 846773 on March 27, 2012 11:03
-
VMFS partition table corruption? Need help to retrieve data
My House ESXi 5.1 was installed on HP Microserver G7. Hypervisor on USB-flash, datastore on Adaptec 3405.
After some problems of power electro Microserver is will not start and I have chosen to build the new server on Microserver Gen8. I moved Adaptec and disks in the new box, installed new ESXi 5.5 U1 (HP image) on USB and after reboot did not store data :-(.
After some research, I found that ESXi is not see any partition table on this raid array:
partedUtil getptbl /vmfs/devices/disks/mpx.vmhba2:C0:T0:L0
unknown
728422 255 63 11702108160
BUT! I tried to start the server of linux liveCD with vmfs-tools 0.2.5 installed partition mounted with vmfs-fuse and able to see the data! But cannot copy all important vmdk (see below).
debugvmfs is also show me some useful information:
Volume Version: 14
Version: 54
Label: STORE
Mode: public
UUID: 4f2ac538-58c181cf-arms-441ea13ee615
Creation date: 2012-02-02 23:17:44
Block size: 1 MiB
Subblock size: 8 KiB
Size of header of the FDC: 64 KiB
County of FDC Bitmap: 64
and it's very strange
debugvmfs/dev/sda1 see the lvm.extent [0]
Device: / dev/sda1
UUID: 4f2ac4da-8de4b166-a-441ea13ee615 848
LUN: 0
Version: 5
Name:
Size: 459.99 GiB
NUM. Segments: 22319
First Segment: 0
Last Segment: 22318
because I don't know that I don't create scopes.
Also, there are two vmdk files on more than 256 GB data store and it cannot copy because of the limitation of vmdk-tools :-(.
I tried to do a magic (like this - partedUtil setptbl "/ vmfs/devices/disks/mpx.vmhba2:C0:T0:L0" GPT "1 2048 11702099429 AA31E02A400F11DB9590000C2911D1B8 0""), but without success;-(.)
All the suggestions, guys?
Finally!
Found that was an "Intel VT - d" enabled in the BIOS. After disable it everything is cool.
It looks like Adaptec 3405 working wrong with it. Will try to change it to P222.
-
simple aggregate query - need help: o)
Hi people, received what should be a simple requirement of a report, but for one reason or another (Friday afternoon, long week, etc.) I can't get my head around the solution.
Can someone take a look if you please and let me know what the answer is, I'm sure it's very simple, but my mind was white!
Here is a scenario of random that I created, which is a summary of the real problem that I have...
3 guys are working between 00:00-03:00 creating operations, I want just a result set of 3 lines showing me the name, time, and the no. some Trad., for the hour in which they create their max don't. transactions, if that makes sense? : o).
with q as)
so I just want the results are:
Select 'Paul' FIRST_NAME, '00', 50 double OPERATIONS TIME_OF_DAY
Union select 'Paul', '01', 20 of the double
Union select 'Paul', '02', 30 of the double
Union select 'John', '00', 40 of the double
Union select 'John', '01', 50 of the double
Union select 'John', '02', 60 of the double
Union select 'Dave', '00', 20 of the double
Union select 'Dave', '01', 30 of the double
Union select 'Dave', '02', 80 of the double
)
Select * q
Paul | 00 | 50
I can get to halfway it easily by
John | 02. 60
Dave | 02. 80
Select first_name, max (transactions)
just not do the next step, getting time in the results!
q
Group name
Thanks in advance: o)
Published by: smon May 14, 2010 08:58
Published by: smon 14 May 2010 09:00SQL> with q as ( 2 select 'Paul' FIRST_NAME, '00' TIME_OF_DAY, 50 TRANSACTIONS from dual 3 union select 'Paul', '01', 20 from dual 4 union select 'Paul', '02', 30 from dual 5 union select 'John', '00', 40 from dual 6 union select 'John', '01', 50 from dual 7 union select 'John', '02', 60 from dual 8 union select 'Dave', '00', 20 from dual 9 union select 'Dave', '01', 30 from dual 10 union select 'Dave', '02', 80 from dual 11 ) 12 select first_name, time_of_day, transactions 13 from (select first_name, time_of_day, transactions, 14 DENSE_RANK() OVER (PARTITION BY first_name 15 ORDER BY transactions DESC) rn 16 from q) 17 where rn = 1; FIRS TI TRANSACTIONS ---- -- ------------ Dave 02 80 John 02 60 Paul 00 50
would be another way. This, as a Tubby will result in more than one line if twice the maximum operations of a name.
John
Published by: John Spencer, on May 14, 2010 12:13
Solution of Frank, who was not visible when I posted., returns a single line in the case of links with the most early who had the maximum transaction.
Published by: John Spencer, on May 14, 2010 12:15
Frank and I seem to do montages Duel :-). His edition, stating that it returns only the first row of links incase wasn't there when I edited my post, honest :-) -
Charger (need help to convert AS2 AS3)
Because I know that this code works very well and that I use it in one of my flash AS3, I need it in one of my flash AS2 and I do not know how to adapt. I searched more than 100 threads and I can not find something similar... Thank you to help me make it work in AS2! As I need in AS2, I thought it would be the right place to post.
I posted the entire process of loading code, but I need help especially with the charger part. How to do this in AS2? Thank you!
var img = 0;
var image_total = 0;var myImages_array:Array = new Array();
var myBitmaps_array:Array = new Array();function Init();
{
Image URLS is loaded into a table before this call
LoadImage();}
function LoadImage()
{
If (img, myImages_array.length) / / img is the index of the current image and myImages_array is my URL table
{I need help with this framework, you can
var loader: Loader = new Loader();/ / Returns the full path of the image and load it
Loader.Load (new URLRequest (my_site_url + myImages_array [img]));
loader.contentLoaderInfo.addEventListener (Event.COMPLETE, imageLoaded);
}
on the other
{
If (count == 0)
{When everything is loaded, I'll start loading my Bitmaps in a short slide show
Count += 1;
init_slideshow();
}
}
}function imageLoaded(e:Event):void
{
Bitmap of the image: var = e.target.content;Bitmap manipulation (deleted) here
image_total = myBitmaps_array.push (image);
If (img < myImages_array.length)
{
IMG += 1;Call the following image
LoadImage();
}
}function init_slideshow (): void
{The current index for the first Reserts
IMG = 0;Start the slide show, since everything is loaded
animate_slideshow();
}Look at the MoveClipLoader class and the addListener method that supports.
-
Need help for query flat_file type clobdata oracle table data.
Hi Sir,
I need help to query oracle table flat file data having given clob type.
COLUMN FOR CHAR_DATA FLAT_FILEOracle Version: Oracle Database 10g Express Edition Release 10.2.0.1.0 - Product PL/SQL Release 10.2.0.1.0 - Production "CORE 10.2.0.1.0 Production" TNS for 32-bit Windows: Version 10.2.0.1.0 - Production NLSRTL Version 10.2.0.1.0 - Production Source table CREATE TABLE order_details ( QUEUE_SEQNUM NUMBER(10,0) NOT NULL ENABLE, LINE_SEQNUM NUMBER(10,0) NOT NULL ENABLE, CHAR_DATA CLOB, OPTIMISTIC_LOCK_KEY NUMBER(20,0) )
EXPECTED RESULTS:EU,6067AT,AT10,000000402004,NexiumGERDManagementProject,Z435,,ZZ29,NIS-GOLD,AT EU,6067AT,AT10,000000402038,NIS-OEU-ARI-2007/1,Z450,,ZZ29,NIS-OEU-ARI-2007/1,AT EU,6067AT,AT10,000000402039,SymbicortNISinCOPD,Z450,,ZZ29,NIS-REU-DUM-2007/1,AT EU,6067AT,AT10,000000402040,D1443L00044SeroquelXRRuby,Z450,,ZZ29,D1443L00044,AT EU,6067AT,AT10,000000402041,NIS-GEU-DUM-2008/1,Z450,,ZZ29,NIS-GEU-DUM-2008/1,AT EU,6067AT,AT10,000000402042,SonstigeAktivitätenLCM,Z450,,ZZ29,.,AT EU,6067AT,AT10,000000402134,D1680L00002Saxagliptin,Z450,,ZZ29,D1680L00002,AT EU,6067AT,AT10,000000402199,SeroquelWaveNIS,Z450,,ZZ29,NIS-NEU-DUM-2009/1,AT EU,6067AT,AT10,000000402313,SeroquelExtra(D1443L00082),Z450,,ZZ29,D1443L00082,AT EU,6067AT,AT10,000000402517,AtlanticD5130L00006(AZD6140),Z450,,ZZ29,D5130L00006,AT EU,6067AT,AT10,000000554494,ArimidexSt.Gallen(13+2),Z142,,ZZ09,,AT EU,6067AT,AT10,000000554495,ArimidexASCO(5delegates),Z142,,ZZ09,,AT EU,6067AT,AT10,000000554496,ArimidexSanAntonio6delegates,Z142,,ZZ09,,AT EU,6067AT,AT10,000000554497,ArimidexBreastCancerSummit(13+2),Z130,,ZZ09,,AT EU,6067AT,AT10,000000554498,ArimidexEIH(15delegates),Z130,,ZZ09,,AT EU,6067AT,AT10,000000554499,ArimidexNIFA(200delegates),Z135,,ZZ09,,AT EU,6067AT,AT10,000000554500,ArimidexNIFAworkshops(8x25),Z135,,ZZ09,,AT EU,6067AT,AT10,000000554501,ArimidexPraktischeGyn.Fortbildung,Z147,,ZZ09,,AT EU,6067AT,AT10,000000554502,ArimidexAGO,Z147,,ZZ09,,AT EU,6067AT,AT10,000000554503,ArimidexHämato/OnkologieKongress,Z147,,ZZ09,,AT EU,6067AT,AT10,000000554504,ARIMIDEXGYNäKOLOGENKONGRESS,Z147,,ZZ09,,AT EU,6067AT,AT10,000000554505,ArimidexChirurgenkongress,Z147,,ZZ09,,AT
AFFIRM_CODE COMPANY_CODE INTERNAL_ORDER_CODE INTERNAL_ORDER_DESC ENIGMA_ACTIVITY SUB_ACTIVITY_CODE IN_AFF_IND ORDER_TYPE EXTERNAL_ORDER COUNTRY EU 6067AT AT10 000000402004 NEXIUMGERDMANAGEMENTPROJECT Z435 NULL ZZ29 NIS-GOLD AT EU 6068AT AT11 000000402005 NEXIUMGERDMANAGEMENTPROJECT Z435 NULL ZZ29 NIS-GOLD AT
Sorry, my bad. Without database at hand, I'll try 'baby steps' (borrowed from Frank) so you don't confuse it with errors that I might add (happens far too often already, but at least you won't "swallow" as forum members think is one of the main goals of this fighter - help her learn - providing not only the proverbial fish.)
Search the Forum - your problem is one of its best sellers. Watching {message identifier: = 10694602} ("split string into" was the key word used in research) you can try something asselect table_row, level clob_row, regexp_substr(char_data,'[^' || chr(13) || chr(10) || ']+',1,level) the_line from (select to_char(queue_seqnum)||':'||to_char(line_seqnum) table_row, char_data from order_details ) connect by regexp_substr(char_data,'[^' || chr(13) || chr(10) || ']+',1,level) is not null and prior char_data = char_data and prior table_row = table_row and prior sys_guid() is not null
to get all the s the_lineall CLOB and after that the use of the example even to get your columns of each the_line.
Concerning
Etbin
Edited by: Etbin on 3.2.2013 09:01
.. .but I m connected to do things according to the instructions, I can't do something.
Used to happen to me too and I did as told to the but only after explaining any disadvantages, I was aware of in time. The last sentence is usually: "O.K. now be just and Don't come back with that kind of thing when it turns out that this isn't the right thing."
rp0428 post - something to remember. -
need help on creating pivot query
SELECT * FROM TEST1
----------------
VALUE OF PERSON COMPUTERNAME
COMP1 ABC 3
COMP2 ABC 5
COMP1 CAD 3
COMP3 CAD 5
COMP2 TES 1
COMP1 TES 5
COMP3 ABC 2
myQuery
----------------------------
Select the link null, label, value1 COUNT (VALUE)
from 'test1 '.
CONTROL group PER PERSON
Results
---------
Link label value1
- ABC 3
- CAD 2
- TES 2
My requirement
---------------
can we have something like that out using the concept of pivot? If so can you share an example query pls.
Link label value1
-ABC ORDI1, COMP2, COMP3
-CAD COMP1, COMP2
-YOUR ORDI1, COMP3
Hello
Subhash C-Oracle wrote:
need help on creating pivot query
SELECT * FROM TEST1
----------------
VALUE OF PERSON COMPUTERNAME
COMP1 ABC 3
COMP2 ABC 5
COMP1 CAD 3
COMP3 CAD 5
COMP2 TES 1
COMP1 TES 5
COMP3 ABC 2
myQuery
----------------------------
Select the link null, label, value1 COUNT (VALUE)
from 'test1 '.
CONTROL group PER PERSON
Results
---------
Link label value1
- ABC 3
- CAD 2
- TES 2
My requirement
---------------
can we have something like that out using the concept of pivot? If so can you share an example query pls.
Link label value1
-ABC ORDI1, COMP2, COMP3
-CAD COMP1, COMP2
-YOUR ORDI1, COMP3
This sounds like a job for LISTAGG:
SELECT NULL AS link
label
LISTAGG (comp_name, ',')
THE Group (ORDER BY ComputerName) AS value1
OF test1
GROUP BY label
;
If you would care to post CREATE TABLE and INSERT statements for your sample data, then I could test it.
Are you sure that the results you posted are what you want from data provided?
Is of the order of the elements in a significant list? In other words, when you say you want to get the results:
COMP1, COMP2
you'd be just as happy with
ORDI1, COMP2
? If the order is important, explains what this order.
-
Oracle Version: 11.2.0.2.0
I need assistance with the output of the query. Here is the table.
With Tbl_Nm as
(
Select 'ABC1' SYSTEM_ID, REGION 'US', 'CHI' SUB_REGION 4000 BALANCE, to_date('1-JUN-2012 10:45:00 am', 'dd-mon-yyyy hh:mi:ss am') LAST_UPD_TIME, 'A' FLAG of union double all the
Select 'PQR2', 'UK', 'LN', 2000, To_Date('1-JUL-2012 10:46:00 am', 'dd-mon-yyyy hh:mi:ss am'), has ' starting from dual Union All
Select 'ABC1', 'IND","MAMA", 3500, To_Date('1-AUG-2012 11:47:00 am', 'dd-mon-yyyy hh:mi:ss am'), 'A' from dual Union All
Select "LMN3", "US", "NJ", 2500, To_Date('1-SEP-2012 09:49:00 am', 'dd-mon-yyyy hh:mi:ss am'), 'A' from dual Union All
Select "PQR2", "UK", "MC", 2600, To_Date('1-OCT-2012 04:45:00 am', 'dd-mon-yyyy hh:mi:ss am'), 'A' from dual Union All
Select 'ABC1', 'US', 'NY', 3200, To_Date('1-OCT-2012 06:45:00 am', 'dd-mon-yyyy hh:mi:ss am'), has ' starting from dual Union All
Select "LMN3", "UK", "BT", 2400, To_Date('1-NOV-2012 07:45:00 am', 'dd-mon-yyyy hh:mi:ss am'), has ' From Dual
)
Select * from tbl_nm
I need the output below.
PQR2 UK MC 2600 1 OCTOBER 2012 04:45
ABC1 US NY 3500 October 1, 2012 06:45
LMN3 UK BT 2500 November 1, 2012 07:45
The need the disc according to this system_id flagged as "A". But if the last disc of 'd' then it must show that the amount, but the file should be displayed in 'A '.
I've tried a few and got stuck. Help, please. Not able to get a balance '.
This question is a bit similar to needing help with a query result
With Tbl_Nm as
(
Select 'ABC1' System_Id, region 'US', 'CHI' Sub_Region, 4000 balance, To_Date('1-JUN-2012 10:45:00 am', 'dd-mon-yyyy hh:mi:ss am') Last_Upd_Time, 'A' flag of double Union All
Select 'PQR2', 'UK', 'LN', 2000, To_Date('1-JUL-2012 10:46:00 am', 'dd-mon-yyyy hh:mi:ss am'), has ' starting from dual Union All
Select 'ABC1', 'IND","MAMA", 3500, To_Date('1-AUG-2012 11:47:00 am', 'dd-mon-yyyy hh:mi:ss am'), 'A' from dual Union All
Select "LMN3", "US", "NJ", 2500, To_Date('1-SEP-2012 09:49:00 am', 'dd-mon-yyyy hh:mi:ss am'), 'A' from dual Union All
Select "PQR2", "UK", "MC", 2600, To_Date('1-OCT-2012 04:45:00 am', 'dd-mon-yyyy hh:mi:ss am'), 'A' from dual Union All
Select 'ABC1', 'US', 'NY', 3200, To_Date('1-OCT-2012 06:45:00 am', 'dd-mon-yyyy hh:mi:ss am'), has ' starting from dual Union All
Select "LMN3", "UK", "BT", 2400, To_Date('1-NOV-2012 07:45:00 am', 'dd-mon-yyyy hh:mi:ss am'), has ' From Dual
)
Select System_Id, region, Sub_Region, Balance, Last_Upd_Time of Tbl_Nm T1
where t1. Last_Upd_Time = (select max (Last_Upd_Time) in the Tbl_Nm T2 where T1.) SYSTEM_ID = T2. SYSTEM_ID)
So maybe you'd then
ORDER BY DECODE(flag,'D',9,1) ASC...
to get the Ds at the end of the list.
or
ORDER BY CASE WHAT flag = has ' (your other filters) AND then 9 or 1 end CSA,...
HTH
-
Hello
I have a data in table (raj_table) with columns (char11) raj_id, raj_number (varchar2 (15)), raj_format (NUMBER), Primary_ID (identity with the values of the primary key column)
Primary_ID raj_id Raj_number Raj_format
1 raj rajvend 1
2 raj rajvend 1
3 raj rajvendor1 2
4 raj rajvendor1 2
5 raj rajvendor1 2
6 raj rajvendor2 3
I used under SQL to get query output as below, but has not achieved the required result:
Select client_id vendor_number, vendor_format, primary_id, row_number() on sl_no (client_id partition, primary_id, vendor_format order of client_id primary_id, vendor_format, vendor_number, vendor_number)
from raj_table by sl_no asc
SL_NO raj_id raj_number raj_format primary_id
1 1 raj rajvendor 1
1 2 raj rajvendor 1
2 3 raj rajvendor1 2
2 4 raj rajvendor1 2
2 5 raj rajvendor1 2
3 6 raj rajvendor2 3
I need help with a SQL query to get the result as above without using the group by clause. I want to bring together the combination of separate line of the three columns (raj_id, raj_number, raj_format) and add a unique serial number for each online game (SL_NO column below). So, above there are 3 unique set of (raj_id, raj_number, raj_format) I can get in a group by clause, but I can not add prmiary_id, SL_NO values if I group by clause. I used the analytical functions like row_number() but no luck. Need solution for this.
with t as)
Select 'raj' raj_id, 'rajvend' raj_number, 1 raj_format, 1 primary_id Union double all the
Select option 2, 'raj', 'rajvend', 1 double Union all
Select 3, 'raj', 'rajvendor1', 2 double Union all
Select 4, 'raj', 'rajvendor1', 2 double Union all
Select 5, 'raj', 'rajvendor1', 2 double Union all
Select 6, 'raj', 'rajvendor2', 3 double
)
Select dense_rank() over (order of raj_id, raj_number, raj_format) sl_no,
t.*
t
order by primary_id
/
PRIMARY_ID RAJ RAJ_NUMBER RAJ_FORMAT SL_NO
---------- ---------- --- ---------- ----------
1 1 raj rajvend 1
1 2 raj rajvend 1
2 3 raj rajvendor1 2
2 4 raj rajvendor1 2
2 5 raj rajvendor1 2
3 6 raj rajvendor2 36 selected lines.
SQL >
SY.
-
Need help with query between 2 dates
Hello
I did not SEE in a long time and need help with a simple query.
I have a table of DB access with 3 fields, name, date and number
What I want is to create a query to retrieve all the names between 2 dates
When I ask the date field, the results are showing in this formats 2013-07-12 00:00:00
Here's my query
< cfquery datasource = 'mydb' name = 'test' >
SELECT name from myTable
where edate between ' 2011-01-01 00:00:00 ' AND ' 2013-01-01 00:00:00 '
< / cfquery >
< cfoutput query = 'test' >
#name #.
< / cfoutput >
What I get is this error
ODBC = 22005 (assignment error) error code
[Microsoft] [ODBC Microsoft Access driver] Type mismatch of data in the expression of the criteria.
Don't know what I'm doing wrong here.
Please let me know.
Thank you
SELECT ename
FROM MyTable
WHERE edate BETWEEN
AND
#ename #.
-
Hello
I need help to form a query. I have two tables Support_issues and Support_comments with the information below.
I want to create a query to view the account of the situation for the last 15 days until today.
Sample Input data support_issues: ISSUE_ID ISSUE_DESC CREATION_DATE STATUS 1 AAA 01/NOV/2011 Open 2 BBB 02/NOV/2011 Closed 3 CCC 02/NOV/2011 Open 4 DDD 03/NOV/2011 Reopened 5 EEE 03/NOV/2011 Reopened support_comments: COMMENT_ID ISSUE_ID COMMENT_DESC STATUS UPDATE_TIME 101 1 aaa Open 01/NOV/2011 102 2 bbb Open 02/NOV/2011 103 2 bbbbbb Closed 03/NOV/2011 104 2 bbbb11 Reopened 03/NOV/2011 105 3 ccc Open 02/NOV/2011 106 2 bbbbb Closed 03/NOV/2011 107 4 ddddd Open 03/Nov/2011 108 5 eeeee Open 03/NOV/2011 109 4 343434 Closed 06/NOV/2011 110 4 dfdf Reopened 07/NOV/2011 111 5 dfdfdf Closed 08/NOV/2011 112 5 udehjk Reopened 10/NOV/2011
For "Created" count of status must be taken Support_issues table, for others it must be taken from table Support_comments.Sample output: DATE Created Reopened Closed 28/OCT/2011 0 0 0 29/OCT/2011 0 0 0 30/OCT/2011 0 0 0 31/OCT/2011 0 0 0 01/NOV/2011 1 0 0 02/NOV/2011 2 0 0 03/NOV/2011 2 1 2 04/NOV/2011 0 0 0 05/NOV/2011 0 0 0 06/NOV/2011 0 0 1 07/NOV/2011 0 1 0 08/NOV/2011 0 0 1 09/NOV/2011 0 0 0 10/NOV/2011 0 1 0 11/NOV/2011 0 0 0
Please help me to form a query for this!
Thank you
MukeshHello
Try the following query
with t1 as ( select to_char(sysdate-level+1,'dd-mm-yyyy') as date_range from dual where level<16 connect by sysdate-16
Cannot test because I did not create, insert commands...
-
need help for a conditional query
guys this is just. as an extension of this post that Frank was helping me. IM reposting because my needs change slightly and im having a hell of a time trying to change the query.
Here are the previous post.
need help with query can find data back please help.
dataCREATE TABLE "FGL" ( "FGL_GRNT_CODE" VARCHAR2(60), "FGL_FUND_CODE" VARCHAR2(60), "FGL_ACCT_CODE" VARCHAR2(60), "FGL_ORGN_CODE" VARCHAR2(60), "FGL_PROG_CODE" VARCHAR2(60), "FGL_GRNT_YEAR" VARCHAR2(60), "FGL_PERIOD" VARCHAR2(60), "FGL_BUDGET" VARCHAR2(60) )
I need to find the year of greater subsidy for the granting by a period setting.Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7600','4730','02','11','00','400'); Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','1','100'); Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','1','0'); Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7600','4730','02','11','1','400'); Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('360055','360055','7200','4730','02','10','1','400'); Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('360055','360055','7600','4730','02','10','1','400'); Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','14','200'); Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7600','4730','02','10','14','100'); Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','14','200'); Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','10','2','100'); Insert into FGL (FGL_GRNT_CODE,FGL_FUND_CODE,FGL_ACCT_CODE,FGL_ORGN_CODE,FGL_PROG_CODE,FGL_GRNT_YEAR,FGL_PERIOD,FGL_BUDGET) values ('240055','240055','7240','4730','02','11','2','600');
Once I found the great year, I need to check the value of the period 14 this grant for the previous year and add it to the amount of the budget for this grant. However if there is an entry in the biggest year for period 00 so I need to ignore the period 14 of the previous year and the current calculation period + (current period - more great year 00)
hope that makes sense, so in other words, with the new data above. If I asking me a second period of the grant year 11. I'd end up with $800
because the greatest year is 11 it contains one point 0 with $ 400, so my total should be
amount of period $2,600
period $0 400 - period $2 600 = $200
600 + 200 = $800
If I have question period 1 grant 360055 I'd end up just with 800 grnt year 10.
I tried to edit this query you have provided me with no luck. I tried for several embarrassed day but I'm saying I can just do what im trying to do.
can you please help me.
Here's the query provided by frank kulash who graciously put it together for me.
MiguelWITH got_greatest_year AS ( SELECT fgl.* -- or whatever columns are needed , MAX ( CASE WHEN fgl_period = :given_period THEN fgl_grnt_year END ) OVER () AS greatest_year FROM fgl ) SELECT SUM (fgl_budget) AS total_budget -- or SELECT * FROM got_greatest_year WHERE ( fgl_grnt_year = greatest_year AND fgl_period = :given_period ) OR ( fgl_grnt_year = greatest_year - 1 AND fgl_period = 14 ) ;
Hi, Miguel.
You are waying that when larger year who has: given_period also a period = '00' (or '0', or whatever it is you want to use), then you want to double the budget of the given_period (subtract the '00' budget and do not count '14' year pevious)? If so, add another condition to the CASE statement that decides what you've summarized:
WITH got_greatest_year AS ( SELECT TO_NUMBER (fgl_grnt_year) AS grnt_year , fgl_period , TO_NUMBER (fgl_budget) AS budget , MAX ( CASE WHEN fgl_period = :given_period THEN TO_NUMBER (fgl_grnt_year) END ) OVER () AS greatest_year FROM fgl ) , got_cnt_00 AS ( SELECT grnt_year , fgl_period , budget , greatest_year , COUNT ( CASE WHEN grnt_year = greatest_year AND fgl_period = '00' THEN 1 END ) OVER () AS cnt_00 FROM got_greatest_year ) SELECT SUM ( CASE WHEN grnt_year = greatest_year -- New AND fgl_period = :given_period -- New AND cnt_00 > 0 THEN budget * 2 -- New WHEN grnt_year = greatest_year AND fgl_period = :given_period THEN budget WHEN grnt_year = greatest_year AND fgl_period = '00' THEN -budget WHEN grnt_year = greatest_year - 1 AND fgl_period = '14' AND cnt_00 = 0 THEN budget END ) AS total_budget FROM got_cnt_00 ;
You will notice it is the same as the previous query, I posted, with the exception of 3 lines marked 'new '.
-
Hi gurus
need help with this query,
I want to display the records in the table emp
I want just the recordsSQL> Select Deptno,sal,sal/SUMSAL Percent,rn 2 From ( Select emp.*,Sum(Sal) Over() "SUMSAL",Row_number() Over(Order by sal Desc) rn 3 From emp 4 ) 5 / EMPNO DEPTNO SAL PERCENT RN --------- ---------- ---------- ---------- ---------- 7839 10 5000 .172265289 1 7902 20 3000 .103359173 2 7788 20 3000 .103359173 3 7566 20 2975 .102497847 4 7698 30 2850 .098191214 5 7782 10 2450 .084409991 6 7499 30 1600 .055124892 7 7844 30 1500 .051679587 8 7934 10 1300 .044788975 9 7521 30 1250 .043066322 10 7654 30 1250 .043066322 11 7876 20 1100 .037898363 12 7900 30 950 .032730405 13 7369 20 800 .027562446 14 14 rows selected.
Thank youEMPNO DEPTNO SAL PERCENT RN ---------- ---------- ---------- ---------- ---------- 7839 10 5000 .172265289 1 7902 20 3000 .103359173 2 7788 20 3000 .103359173 3 7566 20 2975 .102497847 4 7698 30 2850 .098191214 5 with sum (Percent) of remaing records..... Others .420327304
Published by: SeenuGuddu on February 27, 2011 03:39with a as ( Select Empno, Deptno ,sal, sal/SUMSAL Percent, case when rn<=5 then rn else null end rnm From (Select emp.*, Sum(Sal) Over() "SUMSAL", Row_number() Over(Order by sal Desc) rn From emp) ) select case when max(rnm) is not null then to_char(max(empno)) else 'Others:' end empno, case when max(rnm) is not null then max(deptno) else null end deptno, case when max(rnm) is not null then max(sal) else null end sal, to_char(sum(percent), '90.99') percent, max(rnm) rn from a group by rnm order by rnm
EMPNO DEPTNO SAL PERCENT RN ---------------------------------------- ---------------------- ---------------------- ------- ---------------------- 7839 10 5000 0.17 1 7902 20 3000 0.10 2 7788 20 3000 0.10 3 7566 20 2975 0.10 4 7698 30 2850 0.10 5 Others: 0.42 6 rows selected
-
Need help to write a sub query
Our environment - Oracle 10 g
Hi all
Need help to write a sub query to reach him here are examples of data using which iam trying to replace the value column in the table based on two other columns in the same table
Examples of data
ClaimNo flag LineNo Procedurecode
100 01 N MN4567
100 02 Y 7863
100 03 N MN8976
100 04 Y 9000
101 01 Y 8954
101 02 N MN6754
101 03 N MN7654
101 04 Y 8976
102 01 Y 1234
102 02 Y 2345
102 03 Y 3456
102 03 Y 4567
Each column of ClaimNo has several rows of data. But if column procedurecode for a claimNo starts with MN then all values associated with the claimno for the flag column should replace N
If the data must become like below
ClaimNo flag LineNo Procedurecode
100 01 N MN4567
100 02 N 7863
100 03 N MN8976
100 04 N 9000
101 01 N 8954
101 02 N MN6754
101 03 N MN7654
101 04 N 8976
102 01 Y 1234
102 02 Y 2345
102 03 Y 3456
102 03 Y 4567
Thank youSee the example:
with t as ( select 100 ClaimNo, '01' LineNo, 'N' Flag, 'MN4567' Procedurecode from dual union all select 100, '02', 'Y', '7863' from dual union all select 100, '03', 'N', 'MN8976' from dual union all select 100, '04', 'Y', '9000' from dual union all select 101, '01', 'Y', '8954' from dual union all select 101, '02', 'N', 'MN6754' from dual union all select 101, '03', 'N', 'MN7654' from dual union all select 101, '04', 'Y', '8976' from dual union all select 102, '01', 'Y', '1234' from dual union all select 102, '02', 'Y', '2345' from dual union all select 102, '03', 'Y', '3456' from dual union all select 102, '03', 'Y', '4567' from dual ) select claimno, lineno, flag, case when count(decode(substr(procedurecode,1,2),'MN',1)) over(partition by claimno)>0 then 'N' else flag end new_flag, procedurecode from t
Kind regards
Sayan M.
Maybe you are looking for
-
Going crazy trying to select and move multiple points of tempo
I tried to move a whole bunch of tempo of a bar points to a spot 4 bars later in my project. First time this... For the life of me I can't figure out what I'm doing wrong as I * try * follow the steps here: Logic Pro X: move and copy points of tempo
-
What is a good second iMac monitor 4 k
Hi all I just upgraded iMac iMac 2011 2015 4 k, my second screen worked fine on older iMac on my new iMac, the display is tense, I'm on love at first sight to VGA btw. but my second monitor has dvi, my question is there a software that would allow me
-
How to UN-restrict the movement of the regions to take
The use of Logic Pro 10.2.3, I want to spend an excerpt from a plug to another track (which is not in the same folder to take). This is the procedure I use: Make sure the quick slide is off Cuts in the region to isolate the part I want to spend Drag
-
OfficeJet 5740: efax for officejet 5740
How to configure the fax using efax?
-
CPU/map change mother fails to start
I've had a rig that was sitting in my garage for a long time which was an old Intel platform and I found some better hardware at home and installed it. The old motherboard was of unknown brand - no marking on the Board of Directors. The new hardware