SQL Help Reqd.
Hi All,
I have following requirement:
Table Name: CUSTOMER
Fields in this table: CUSTOMER_ID, TAG, SUBFIELD, VALUE
When TAG = '100' and SUBFIELD = 'a' VALUE represents Customer created date.
When TAG = '200' and SUBFIELD = 'a' VALUE represents Customer changed date.
When TAG = '300' and SUBFIELD = 'a' VALUE represents Customer birth date.
When TAG = '400' and SUBFIELD = 'a' VALUE represents Customer zip code.
Table Name: CUSTOMER2
Fields in this table: CUSTOMER_ID, FIRST_NAME, LAST_NAME
Now I want all the CUSTOMER_ID whose created date >= Todays date and changed date >= Todays date.
For all the above CUSTOMER_ID I want to retrieve CUSTOMER2-FIRST_NAME, CUSTOMER2-LAST_NAME, birth date and zip code in a CSV file.
Can some one please help me with the SQL Code for the above logic,
Thanks in advance,
Raja
Example...
Without the max and group by...
SQL> ed
Wrote file afiedt.buf
1 with t as (select 1 as cust_id, 'x' as category, 10 as value from dual union all
2 select 1, 'y', 20 from dual union all
3 select 1, 'z', 30 from dual union all
4 select 2, 'x', 40 from dual union all
5 select 2, 'y', 50 from dual union all
6 select 2, 'z', 60 from dual)
7 -- END OF TEST DATA
8 select cust_id
9 ,case when category = 'x' then value else null end as x
10 ,case when category = 'y' then value else null end as y
11 ,case when category = 'z' then value else null end as z
12 from t
13* order by cust_id, x, y, z
SQL> /
CUST_ID X Y Z
1 10
1 20
1 30
2 40
2 50
2 60
6 rows selected.As you can see the data has been put in the right columns but it's still split into individual rows.
So using the max and group by we can get all the data on one row for each cust_id...
SQL> ed
Wrote file afiedt.buf
1 with t as (select 1 as cust_id, 'x' as category, 10 as value from dual union all
2 select 1, 'y', 20 from dual union all
3 select 1, 'z', 30 from dual union all
4 select 2, 'x', 40 from dual union all
5 select 2, 'y', 50 from dual union all
6 select 2, 'z', 60 from dual)
7 -- END OF TEST DATA
8 select cust_id
9 ,max(case when category = 'x' then value else null end) as x
10 ,max(case when category = 'y' then value else null end) as y
11 ,max(case when category = 'z' then value else null end) as z
12 from t
13 group by cust_id
14* order by cust_id
SQL> /
CUST_ID X Y Z
1 10 20 30
2 40 50 60
SQL>;)
Similar Messages
-
I have setup new instance. Created p.o & done MIGO things worked fine. In MIRO typically error is seen in tax code. I have assigned Tax procedure TAXGB to country IN. In Fin Acct in tax on sales & purchases defined new taxcode VX with country option IN. Still during MIRO its showing error Tax code VN & Country IN doesnot exist in procedure TAXGB. In error msg its M8 249 & origin is shown as check invoice document.
Help reqd from SAP Gurus pls.
RegardsCheck these links
while doing MIRO, i am getting error:-Tax code V0 country IN does not exist
Tax code Z4 country IN does not exist in procedure TAXINJ
Tax code V0 country IN does not exist in procedure TAXINN
Tax code V0 country IN does not exist in procedure TAXINN -
Hi again
I need some PIVOT sql help
In this query:
SELECT
SUM([RATES]) as RATES
,SUM([CONVERSION])as CONVERSION
FROM REPORTING
outputs
RATES CONVERSION
23 234
How would change this query to display a table like:
Name Amount
RATES 23
CONVERSION 234
Keep up the good work !nikos101 wrote:
> How would change this query to display a table like:
Do you *HAVE* to change the query... you could just display
it in the
desired format if that it the ultimate and only goal.
<table>
<tr>
<td>name</td>
<td>amount</td>
</tr>
<cfoutput query="sumQry">
<tr><td>Rates</td><td>#rates#</td></tr>
<tr><td>Conversion</td><td>#conversion#</td></tr>
</cfoutput>
</table> -
Urgent Help Reqd: Delta Problem....!!!
Hi Experts,
Urgent help reqd regarding follwoing scenario:
I am getting delta to ODS 1 (infosource : 2LIS_12_VCHDR) from R/3. Now this ODS1 gives delta to Cube1. I got one error record which fails when going to cube (due to master data check).And this record would be edited later.
So I thought of re loading cube and using error handling to split request in PSA and load valid records in cube.Later when I have coorect info abt error, I would load erroneous record manually using PSA.
I did following steps:
1)Failed request is not in cube...(setting is PSA and then into target)....also it has been deleted from PSA.
2)Remove data status from source ODS1.
3)Change error handling in infopackage to valid " Valid records update, reporting possible".
4)start loading of cube.
But when I did this I got following message :
<b> "Last delta update is not yet completed.Therefore, no new delta update is possible.You can start the request again if the last delta request is red or green in the monitor"</b>
But both my requests in cube and ODS are Green...no request is in yellow...!!
Now the problem is :
1) I have lost ODS data mart status
2)how to load valid records to cube, since errorneous record can be edited only after 3- 4 days.
3)How not to affect delta load of tomorrow.
Please guide and help me in this.
Thanks in advance and rest assured I will award points to say thanks to SDNers..
Regards,
Sorabh
Message was edited by: sorabh aroraHi Joseph,
Thanks for replying....
I apolgize and I have modified the message above..
I just checked in cube...there is no failed request for today...and neither in PSA....Person who has handed this to me may have deleted this is PSA..and why req is not in failed state in cube...I am not sure..though setting is "PSA and then into data package".....
So how to go frm here....
Thanks
Sorabh -
Hi,
I require help for sql query. The query will be like
"SELECT * FROM XYZ WHERE TEXT = 'h*s' "
and the results will be come like
“his”
“homes”
“houses”
“horses”,
“horticulturalists”
“herbaceous”.
Thanks,
KapilHello,
SELECT *
FROM XYZ
WHERE TEXT LIKE ('h%s');Regards
Edited by: OrionNet on Feb 2, 2009 12:33 AM -
Hi,
I am using the following statement:
select * from table1 where field1 = 'a' and rownum <= 10;
After I execute this statement SQL Plus gets stuck,
I am not able to figure out what I am doing wrong here,
Please advice,
Thanks,
RajaAlright you may have to CTRL+C that thing. Anyway, here is a very simple example of how explain plan works:
SQL*Plus: Release 10.2.0.2.0 - Production on Mon May 12 10:51:46 2008
Copyright (c) 1982, 2005, Oracle. All Rights Reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.2.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options
SQL> explain plan for select * from dual;
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
Plan hash value: 3543395131
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1 | 2 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| DUAL | 1 | 2 | 2 (0)| 00:00:01 |
8 rows selected.
SQL> -
Sql query - help reqd (new)
Hi,
I have follwing dataset
"herb with garden"
"garden without herb"
"herb with zbc"
"garden with pqr"
Now i want sql query like
select * from table where text1 = "herb and garden"
This should populate
herb with garden
garden without herb
And
select * from table where text1 = "herb or garden"
This should populate
"herb with garden"
"garden without herb"
"herb with zbc"
"garden with pqr"SQL> with t
2 as
3 (
4 select 'herb with garden' str from dual union all
5 select 'garden without herb' str from dual union all
6 select 'herb with zbc' str from dual union all
7 select 'garden with pqr' str from dual
8 )
9 select *
10 from t
11 where lower(str) like '%herb%'
12 and lower(str) like '%garden%'
13 /
STR
herb with garden
garden without herb
SQL> with t
2 as
3 (
4 select 'herb with garden' str from dual union all
5 select 'garden without herb' str from dual union all
6 select 'herb with zbc' str from dual union all
7 select 'garden with pqr' str from dual
8 )
9 select *
10 from t
11 where lower(str) like '%herb%'
12 or lower(str) like '%garden%'
13 /
STR
herb with garden
garden without herb
herb with zbc
garden with pqr -
Hi Friends,
This may be a dumb question but please bear with me I am not a SQL person.
I want to pull all records of table A and matched records of table B. Is following syntax correct:
SELECT
TABLE1.FIELDA
TABLE2.FIELDB
FROM
TABLE1 LEFT JOIN
TABLE2 ON (TABLE1.FIELDA = TABLE2.FIELDA)Hi Friends,
This may be a dumb question but please bear with me I
am not a SQL person.
I want to pull all records of table A and matched
records of table B. Is following syntax correct:
SELECT
TABLE1.FIELDA
TABLE2.FIELDB
FROM
TABLE1 LEFT JOIN
TABLE2 ON (TABLE1.FIELDA = TABLE2.FIELDA)Depends what you mean by "matched" records.
If you only want those that match then you don't want the "LEFT" in the join.
If you want everything from TABLE1 even if there isn't a matching record in TABLE2 then your query will give you that. -
Help Reqd - on simple sql query
Hi,
I have two tables back_temp_ord_hist, temp_ord_hist of similar structures
create table <table_name>
order_number varchar2(10),
item_number integer,
sched_line_number integer,
dlv_doc_id varchar2(10),
mtrl_mstr_key integer,
cust_key integer,
wrhse_key integer,
sls_ord_type varchar2(4)
Now I have inserted same data set in the two tables. (same following three rows in both the tables)
0009032022 160 2 0091383073 27219 1694 20 ZISO
0009032022 160 3 0091383073 27219 1694 20 ZISO
0009032022 160 4 0091383073 27219 1694 20 ZISO
Then I am trying to use a query like below :
select
fact.order_number
, fact.item_number
, fact.sched_line_number
, bk_fact.sched_line_number as old_sched_line_number
, fact.dlv_doc_id
,dbms_utility.get_hash_value(fact.order_number||fact.item_number||fact.sched_line_number||fact.dlv_doc_id,1,100) ord_hist_hash
,dbms_utility.get_hash_value(bk_fact.order_number||bk_fact.item_number||bk_fact.sched_line_number||bk_fact.dlv_doc_id,1,100) back_ord_hist_hash
, fact.mtrl_mstr_key
, bk_fact.mtrl_mstr_key as old_mtrl_mstr_key
, fact.cust_key
, bk_fact.cust_key as old_cust_key
, fact.wrhse_key
, bk_fact.wrhse_key as old_wrhse_key
, fact.sls_ord_type
, bk_fact.sls_ord_type as old_sls_ord_type
from
temp_ord_hist fact
join
back_temp_ord_hist bk_fact
on fact.order_number = bk_fact.order_number
and fact.item_number = bk_fact.item_number
and fact.sched_line_number <> bk_fact.sched_line_number
and fact.dlv_doc_id = bk_fact.dlv_doc_id
order by ord_hist_hash, back_ord_hist_hash
But it is actually it is showing 6 rows. But ideally I want no rows as temp_ord_hist.sched_line_number = back_temp_ord_hist.sched_line_number in all the cases over here.
Practically it can be different then I want those rows but when it is same why the output is showing 6 rows.
What I am doing wrong over here?
Regards,
KoushikNot sure how you are getting 6 rows as the output. I did exactly the same thing that you did and I am getting the only 3 rows as the output:
see below:
SQL> create table temp_ord_hist
2 (
3 order_number varchar2(10),
4 item_number integer,
5 sched_line_number integer,
6 dlv_doc_id varchar2(10),
7 mtrl_mstr_key integer,
8 cust_key integer,
9 wrhse_key integer,
10 sls_ord_type varchar2(4)
11 );
Table created.
SQL> create table back_temp_ord_hist
2 (
3 order_number varchar2(10),
4 item_number integer,
5 sched_line_number integer,
6 dlv_doc_id varchar2(10),
7 mtrl_mstr_key integer,
8 cust_key integer,
9 wrhse_key integer,
10 sls_ord_type varchar2(4)
11 );
Table created.
SQL> insert into temp_ord_hist values ('0009032022',160,2,'0091383073',27219,1694,20,'ZISO');
1 row created.
SQL> insert into temp_ord_hist values ('0009032022',160,3,'0091383073',27219,1694,20,'ZISO');
1 row created.
SQL>
SQL> insert into temp_ord_hist values ('0009032022',160,4,'0091383073',27219,1694,20,'ZISO');
1 row created.
SQL> insert into back_temp_ord_hist values ('0009032022',160,2,'0091383073',27219,1694,20,'ZISO');
1 row created.
SQL> insert into back_temp_ord_hist values ('0009032022',160,3,'0091383073',27219,1694,20,'ZISO');
1 row created.
SQL> insert into back_temp_ord_hist values ('0009032022',160,4,'0091383073',27219,1694,20,'ZISO');
1 row created.
SQL> select
2 fact.order_number
3 , fact.item_number
4 , fact.sched_line_number
5 , bk_fact.sched_line_number as old_sched_line_number
6 , fact.dlv_doc_id
7 ,dbms_utility.get_hash_value(fact.order_number||fact.item_number||fact.sched_line_number||fact.dlv_doc_id,1,100) ord_hist_hash
8 ,dbms_utility.get_hash_value(bk_fact.order_number||bk_fact.item_number||bk_fact.sched_line_number||bk_fact.dlv_doc_id,1,100) back_ord_hist_hash
9 , fact.mtrl_mstr_key
10 , bk_fact.mtrl_mstr_key as old_mtrl_mstr_key
11 , fact.cust_key
12 , bk_fact.cust_key as old_cust_key
13 , fact.wrhse_key
14 , bk_fact.wrhse_key as old_wrhse_key
15 , fact.sls_ord_type
16 , bk_fact.sls_ord_type as old_sls_ord_type
17 from
18 temp_ord_hist fact
19 join
20 back_temp_ord_hist bk_fact
21 on fact.order_number = bk_fact.order_number
22 and fact.item_number = bk_fact.item_number
23 and fact.sched_line_number = bk_fact.sched_line_number
24 and fact.dlv_doc_id = bk_fact.dlv_doc_id
25 order by ord_hist_hash, back_ord_hist_hash
26
SQL> /
0009032022 160 3 3 0091383073 4 4 27219 27219 1694 1694 20 20 ZISO ZISO
0009032022 160 4 4 0091383073 55 55 27219 27219 1694 1694 20 20 ZISO ZISO
0009032022 160 2 2 0091383073 100 100 27219 27219 1694 1694 20 20 ZISO ZISO
SQL> -
Table name: xyz
Column1 Column2
x 1
x 2
x 3
x 7
y 1
y 3
y 6
z 4
z 2
z 0
I want a query to output all Column1 only if Column2 value not in (1,2,3) - all inclusive. In the above example, I want y & z in the output.I may be wrong but I think the OP is asking for something more like:
[email protected](161)> select * from xyz;
C COLUMN2
x 1
x 2
x 3
x 7
y 1
y 3
y 6
z 4
z 2
z 0
10 rows selected.
[email protected](161)> select column1
2 from xyz
3 minus
4 select column1
5 from xyz
6 where column2 in (1, 2, 3)
7 group by column1
8 having count(distinct column2) = 3
9 /
C
y
zI.e., values from column1 which don't have ALL of (1, 2, 3) in column2.
cheers,
Anthony -
Hello,
I am having a query as a part of my procedure block that extracts the following xml and stores it into a variable that is of xmltype:
<FeatureRoot>
<Feature>
<FeatureName>qaz</FeatureName>
<FeatureAction>Add</FeatureName>
</Feature>
<Feature>
<FeatureName>wsx</FeatureName>
<FeatureAction>Remove</FeatureAction>
</Feature>
</FeatureRoot>
Now what i want to do is loop through this variable (which is of xmltype) and print the values of the tags <FeatureName> and <FeatureAction> for every Feature.
(ie)
qaz Add
wsx Remove
Can you please tell me how i can loop through to extract the values?
Many Thanks,
Kalyaniuser11912174 wrote:
Hello,
Thank you. The solutions may hold good on a normal context. What my problem is I want to run a loop through an anonymous block in pl/sql where I am already using a cursor where each record contains an xml value as above. So i need to be extracting for each run in a cursor. So i need to be extracting the <FeatureName> and <featureaction> values in each cursor run. Any help on this?
ThanksCan you not incorporate it as part of the cursor itself?
It can return the values as part of the data from the cursor rather than using PL/SQL extracts on the XML to do it.
SQL> ed
Wrote file afiedt.buf
1 with myxml as
2 (select xmltype('<FeatureRoot>
3 <Feature>
4 <FeatureName>qaz</FeatureName>
5 <FeatureAction>Add</FeatureAction>
6 </Feature>
7 <Feature>
8 <FeatureName>wsx</FeatureName>
9 <FeatureAction>Remove</FeatureAction>
10 </Feature>
11 </FeatureRoot>') myxml from dual union all
12 select xmltype('<FeatureRoot>
13 <Feature>
14 <FeatureName>aaa</FeatureName>
15 <FeatureAction>Add</FeatureAction>
16 </Feature>
17 <Feature>
18 <FeatureName>bbb</FeatureName>
19 <FeatureAction>Remove</FeatureAction>
20 </Feature>
21 </FeatureRoot>') from dual)
22 select extractvalue(VALUE(t),'/Feature/FeatureName') featurename
23 , extractvalue(VALUE(t),'/Feature/FeatureAction') featureaction
24 from myxml x
25* , table(xmlsequence(extract(x.myxml,'/FeatureRoot/Feature'))) t
SQL> /
FEATURENAME FEATUREACTION
qaz Add
wsx Remove
aaa Add
bbb Remove
SQL> -
I do not know if this is the right place for thius type of post. If it is not please advise where the right place is.
I need help generating a report, hopefully with SQL in 8.1.7
SQL Statement which produced the data below the query :
SELECT CHANGE.change_number, CHANGE.route_date as DATE_TO_CCB, nodetable.description AS Approver_required, (TRIM(BOTH ',' FROM CHANGE.PRODUCT_LINES)) AS PRODUCT_LINES /*, PROPERTYTABLE.VALUE as PRODUCT_LINES */
FROM CHANGE, signoff, workflow_process, nodetable /*, PROPERTYTABLE */
WHERE ( (CHANGE.ID = signoff.change_id)
AND (CHANGE.process_id = signoff.process_id)
AND ((nodetable.id = signoff.user_assigned) or (nodetable.id=signoff.user_signed))
AND (CHANGE.process_id = workflow_process.ID)
AND (CHANGE.ID = workflow_process.change_id)
AND (CHANGE.workflow_id = workflow_process.workflow_id)
AND (SIGNOFF.SIGNOFF_STATUS=0 )/* in (0, 2, 3)) */ /* 0=request needs attention, 2=request approved, 3=request rejected */
AND (SIGNOFF.REQUIRED=5 or SIGNOFF.REQUIRED=1) /* 1=Approver 5= Ad Hoc Approver */
AND (CHANGE.IN_REVIEW=1)
AND (CHANGE.RELEASE_DATE IS NULL)
AND (CHANGE.CLASS != '4928')
/* AND (PROPERTYTABLE.PROPERTYID IN (SELECT TRIM(BOTH ',' FROM CHANGE.PRODUCT_LINES) FROM CHANGE)) */
order by change.route_date desc
**** Results **********
CHANGE_NUMBER|DATE_TO_CCB|APPROVER_REQUIRED|PRODUCT_LINES
C02190|11/14/2008 3:34:02 PM|Anurag Upadhyay|270354,270362|
C02190|11/14/2008 3:34:02 PM|Dennis McGuire|270354,270362|
C02190|11/14/2008 3:34:02 PM|Hamid Khazaei|270354,270362|
C02190|11/14/2008 3:34:02 PM|Mandy York|270354,270362|
C02193|11/14/2008 3:05:18 PM|Hamid Khazaei|274279,266339,266340,266341|
C02193|11/14/2008 3:05:18 PM|Rob Brogle|274279,266339,266340,266341|
C02193|11/14/2008 3:05:18 PM|Xavier Otazo|274279,266339,266340,266341|
C02193|11/14/2008 3:05:18 PM|san|274279,266339,266340,266341|
C02194|11/14/2008 2:51:34 PM|Diana Young|271503|
C02194|11/14/2008 2:51:34 PM|Carl Krentz|271503|
C02194|11/14/2008 2:51:34 PM|Dennis Yen|271503|
C02194|11/14/2008 2:51:34 PM|Gordon Ries|271503|
C02194|11/14/2008 2:51:34 PM|Sunil Khatana|271503|
M00532|11/13/2008 1:34:42 PM|Dennis Yen|270356,270354,270360,274279,266339,266340,266341,276780,260784|
M00532|11/13/2008 1:34:42 PM|Jin Hong|270356,270354,270360,274279,266339,266340,266341,276780,260784|
M00532|11/13/2008 1:34:42 PM|Sunil Khatana|270356,270354,270360,274279,266339,266340,266341,276780,260784|
Each value in the numeric comma delimited string has a corresponding ID for the actual test string value in another table as shown below.
PROPERTYID|VALUE
260775|product 1
260776|Product 2
260777|Product x
260778|Product y
260779|Internal
260780|ORCA
260781|Tiger
260782|Orange product
260783|Restricted
260784|Product zz
266259|Product YYY
266260|Hercules
266261|Tangerine
*****Desired output****
CHANGE_NUMBER|DATE_TO_CCB|APPROVER_REQUIRED|PRODUCT_LINES
C02190|Nov/14/2008 03:34:02 PM|Anurag Upadhyay, Dennis McGuire, Hamid Khazaei, Mandy York|Product Y,Product 1
C02193|Nov/14/2008 03:05:18 PM|Hamid Khazaei, Rob Brogle, Xavier Otazo, san|Hercules,Apple,Product 3,Product zz
C02194|Nov/14/2008 02:51:34 PM|Diana Young, Carl Krentz, Dennis Yen, Gordon Ries, Sunil Khatana|Product 2
M00532|Nov/13/2008 01:34:42 PM|Dennis Yen, Jin Hong, Sunil Khatana|Product 1,Product 4,product yy,product YYY,ORCA,Tiger,Orange product,RestrictedHi,
Here's how you can do it in Oracle 8.1.
To get the individual sub-strings from product_lines, join your current result set to this "counter table"
( SELECT ROWNUM AS n
FROM all_objects
WHERE ROWNUM <= 10 -- upper bound on number of items
) cntrIf you don't know the worst case of how many items might be in product_lines, it's a little more complicated:
( SELECT ROWNUM AS n
FROM all_objects
WHERE ROWNUM <= 1 +
SELECT MAX ( LENGTH (product_lines)
- LENGTH (REPLACE (product_lines, ','))
FROM table_name
) cntrJoin this to the existing result set like this
WHERE ...
AND INSTR ( product_lines || ',' -- one extra comma added
, 1
, n
) > 0When you do the join, you will have
one copy of all the rows with one item in producgt_lines,
two copies of all the rows with two items in producgt_lines,
three copies of all the rows with three items in producgt_lines,
and so on.
When a row has been copied, each copy will have a different value of cntr.n.
To extract the n-th substring from product_lines:
SELECT ...
SUBSTR ( product_lines
, INSTR ( ',' || product_lines, ',', 1, n)
, ( INSTR (product_lines || ',', ',', 1, n)
- INSTR (',' || product_lines, ',', 1, n)
) AS product_lines_itemWhen you have derived this column, you can join to the table with the translations
WHERE TO_NUMBER (product_lines_item) = propertyidTo combine these rows into one row with a comma-delimited list, GROUP BY all the columns you want to select except the property_value ('produc 1', 'tangerine', etv.), and SELECT:
LTRIM ( MAX (CASE WHEN n = 1 THEN ',' || property_value END) ||
MAX (CASE WHEN n = 2 THEN ',' || property_value END) ||
MAX (CASE WHEN n = 10 THEN ',' || property_value END)
)I don't know a good way to re-combine the rows in Oracle 8 without assuming some limit on the number of items. I assumed there would never be more than 10 in the example above. You can say 20 or 100, I suppose, if you want to. If you guess too high, everything will still work: the query will just be slower.
This is a just one example of why packing several values into a single column is a bad idea. -
Please help ...
Data Sample:
A1 B1C1 D1
01 A 06/30/2008 1
01 B 06/10/2008 1
01 C 06/09/2008 1
01 D 06/09/2008 1
02 A 06/20/2008 1
02 C 06/20/2008 1
03 A 06/20/2008 1
04 A 06/10/2008 2
04 B 06/10/2008 1
04 C 06/09/2008 1
04 C 06/09/2008 1
Output Should be:
01 A 06/30/2008 1
02 A 06/20/2008 1
02 C 06/20/2008 1
03 A 06/20/2008 1
04 A 06/10/2008 2
Rules:
- Top D1 should be selected.
- If there are tie in D1 per A1, should be looking for C1 for the latest date
- if there are tie for the D1 and C1 per A1 this data should be selected
- if there are only one D1 which is the latest then inly 1 record will be selected.Hi,
try this:
SQL> with t as ( -- "sample data"
2 select '01' a1, 'A' b1, to_date('06/30/2008','mm/dd/yyyy') c1, 1 d1 from dual union all
3 select '01', 'B', to_date('06/10/2008','mm/dd/yyyy'), 1 from dual union all
4 select '01', 'C' , to_date('06/09/2008','mm/dd/yyyy'), 1 from dual union all
5 select '01', 'D' , to_date('06/09/2008','mm/dd/yyyy'), 1 from dual union all
6 select '02', 'A' , to_date('06/20/2008','mm/dd/yyyy'), 1 from dual union all
7 select '02', 'C' , to_date('06/20/2008','mm/dd/yyyy'), 1 from dual union all
8 select '03', 'A' , to_date('06/20/2008','mm/dd/yyyy'), 1 from dual union all
9 select '04', 'A' , to_date('06/10/2008','mm/dd/yyyy'), 2 from dual union all
10 select '04', 'B' , to_date('06/10/2008','mm/dd/yyyy'), 1 from dual union all
11 select '04', 'C' , to_date('06/09/2008','mm/dd/yyyy'), 1 from dual union all
12 select '04', 'C' , to_date('06/09/2008','mm/dd/yyyy'), 1 from dual
13 ) -- "end sample data"
14 select a1, b1, c1, d1
15 from (select a1, b1, c1, d1,
16 max(c1) over(partition by a1) mxc1,
17 max(d1) over(partition by a1) mxd1,
18 min(d1) over(partition by a1) mind1
19 from t)
20 where 1 = case when mind1 = mxd1 then
21 case when c1 = mxc1 then 1 end
22 else case when d1 = mxd1 then 1 end
23 end
24 /
A1 B1 C1 D1
01 A 6/30/2008 1
02 A 6/20/2008 1
02 C 6/20/2008 1
03 A 6/20/2008 1
04 A 6/10/2008 2 -
SQL help please. !!!!!!!!!!
hi
i need some help on the following problem. let's say i have a sql select that return the following rows
ME
EL
EO
FA
these are 4 rows of data returned by executing "select column1 from myTable where somecondition = true". however, i need to display these results as follow
MW,EL,EO,FA
which is just one row and they are concatenated as one result. is there any way to modify the sql to do it? your help is much appreciated. thankshi
i need some help on the following problem. let's say i
have a sql select that return the following rows
ME
EL
EO
FA
these are 4 rows of data returned by executing "select
column1 from myTable where somecondition = true".
however, i need to display these results as follow
MW,EL,EO,FA
which is just one row and they are concatenated as one
result. is there any way to modify the sql to do it?
your help is much appreciated. thanksit would be easier if you ahead know how many rows you going to get...
select
"row1" = select blah from table where blah = '1',
"row2" = select blah from table where blah = '2',
"row3" = select blah from table where blah = '3',
"row4" = select blah from table where blah = '4'
from table
make sure the sub select must return one record only... -
SQL help: Selecting tickets with no open tasks
Hi Gurus,
I'm new to SQL. I need your help in a script where I need to fetch the tickets with no open tasks.
Say, following is the Task table
Table: Task
| ticketid | taskid | taskstatus |
| 1 | 1 | O |
| 1 | 2 | O |
| 1 | 3 | O |
| 2 | 4 | C |
| 2 | 5 | C |
| 3 | 6 | C |
| 3 | 7 | O |
The query should return the ticketid(s) with all its taskstatus = 'C'.
Any help would be highly appreciated.
ThanksHi Surya,
Here's an Oracle style example (Sorry, I just can't do that ansi stuff)
SQL> with ticket as (select 1 ticketid, 'C' reason from dual union all
select 2 ticketid, 'O' reason from dual union all
select 3 ticketid, 'O' reason from dual union all
select 4 ticketid, 'C' reason from dual)
,taskstatus as (select 1 taskstatusid, 'O' taskstatus from dual union all
select 2 taskstatusid, 'C' taskstatus from dual union all
select 3 taskstatusid, 'P' taskstatus from dual union all
select 4 taskstatusid, 'F' taskstatus from dual union all
select 5 taskstatusid, 'O' taskstatus from dual union all
select 6 taskstatusid, 'C' taskstatus from dual union all
select 7 taskstatusid, 'P' taskstatus from dual union all
select 8 taskstatusid, 'F' taskstatus from dual)
,task as (select 1 ticketid, 1 taskid, 2 taskstatusid from dual union all
select 1 ticketid, 2 taskid, 3 taskstatusid from dual union all
select 1 ticketid, 3 taskid, 4 taskstatusid from dual union all
select 1 ticketid, 4 taskid, 4 taskstatusid from dual union all
select 2 ticketid, 5 taskid, 1 taskstatusid from dual union all
select 2 ticketid, 6 taskid, 1 taskstatusid from dual union all
select 2 ticketid, 7 taskid, 1 taskstatusid from dual union all
select 3 ticketid, 8 taskid, 2 taskstatusid from dual union all
select 3 ticketid, 9 taskid, 2 taskstatusid from dual union all
select 3 ticketid, 10 taskid, 6 taskstatusid from dual union all
select 4 ticketid, 11 taskid, 2 taskstatusid from dual union all
select 4 ticketid, 12 taskid, 6 taskstatusid from dual union all
select 4 ticketid, 13 taskid, 4 taskstatusid from dual union all
select 4 ticketid, 14 taskid, 8 taskstatusid from dual)
select a.ticketid, c.taskstatus
from task a, ticket b, taskstatus c
where a.ticketid = b.ticketid
and c.taskstatusid = a.taskstatusid
and (c.taskstatus = 'C' or c.taskstatus = 'F')
and b.reason = 'C'
and not exists (select null
from task a1, taskstatus c1
where a1.ticketid = a.ticketid
and c1.taskstatusid = a1.taskstatusid
and (c1.taskstatus = 'O' or c1.taskstatus = 'P'))
order by 1,2
TICKETID T
4 C
4 C
4 F
4 F
4 rows selected.Please note, how I made test data, that way is much more helpful than your tables
Regards
Peter
Maybe you are looking for
-
Get IP address ... behind xDSL ...of the ISP
Hi i m looking for a cool way to get not my localhost address.... i need to get my IP ... i have the next code: import java.net.*; public class TestIP { public static void main(String args[]) { try { InetAddress[] address
-
Hello, I have 4 instances Oracle 9i in a server on linux, which have 700 Mb of SGA each one, and the server have 4 Gb RAM. When I reboot the server, this haven´t use of SWAP but after a time the SWAP memory arrive to 1.3 Gb, and I would like reduce t
-
G4 Hangs, Sounds like an Airplane Taking Off
My G4 has been running smoothly on Leopard so far. Lately, though, it has started hanging often... no spinning wheel, no opportunity to force quit... it literally freezes. I hold down the power button to turn it off and then on reboot the fan goes cr
-
It is table with 5 rows and 4 colunms and forget about height and width. By using only single <table> tag is it possible to draw such a table. I tried but felt. I am inserting a link for that figure just visit it . Please reply Thank you http://sites
-
My computer won't stop downloading a song
I purchased some songs yesterday. Its been about 24 hours and it says it still downloading. I try to delete or pause and nothing will stop it