SQL query for empty string
I am trying to execute the following SQl query, SELECT * FROM Failure WHERE ID = '123' AND RepairAction = ' ';, using the DB Tools Execute Query.vi. This query never finds the record in my database. My database contains a record where the ID filed contains the value of '123' and the RepairAction field is an empty string. If I remove the 'AND RepairAction ' ';' text from my query statement, the record is found. I believe my problem is that I am not using the correct syntax to describe and empty string. I have tried the following: '', ' ', "", " ", and NULL as empty string arguments, and none of these work.
I was hoping someone might be able to tell me what the correct syntax is for an empty string or if there is another approach I need to take.
Thank you in advance for your help,
Jim
Solved!
Go to Solution.
Hi,
While creating your table "Failure", was the column "ID" delclared as intiger or varchar? If it is intiger and you use '123' , it wont return the results. You will have to try without the inverted comas ' '.
Regards,
Nitzz
(Giver Kudos to good Answers, Mark it as a solution if your problem is Solved)
Similar Messages
-
Error while executing a sql query for select
HI All,
ORA-01652: unable to extend temp segment by 128 in tablespace PSTEMP i'm getting this error while i'm executing the sql query for selecting the data.I am having 44GB of temp space, while executing the below query my temp space is getting full, Expert please let us know how the issue can be resolved..
1. I dont want to increase the temp space
2. I need to tune the query, please provide your recomendations.
insert /*+APPEND*/ into CST_DSA.HIERARCHY_MISMATCHES
(REPORT_NUM,REPORT_TYPE,REPORT_DESC,GAP,CARRIED_ITEMS,CARRIED_ITEM_TYPE,NO_OF_ROUTE_OF_CARRIED_ITEM,CARRIED_ITEM_ROUTE_NO,CARRIER_ITEMS,CARRIER_ITEM_TYPE,CARRIED_ITEM_PROTECTION_TYPE,SOURCE_SYSTEM)
select
REPORTNUMBER,REPORTTYPE,REPORTDESCRIPTION ,NULL,
carried_items,carried_item_type,no_of_route_of_carried_item,carried_item_route_no,carrier_items,
carrier_item_type,carried_item_protection_type,'PACS'
from
(select distinct
c.REPORTNUMBER,c.REPORTTYPE,c.REPORTDESCRIPTION ,NULL,
a.carried_items,a.carried_item_type,a.no_of_route_of_carried_item,a.carried_item_route_no,a.carrier_items,
a.carrier_item_type,a.carried_item_protection_type,'PACS'
from CST_ASIR.HIERARCHY_asir a,CST_DSA.M_PB_CIRCUIT_ROUTING b ,CST_DSA.REPORT_METADATA c
where a.carrier_item_type in('Connection') and a.carried_item_type in('Service')
AND a.carrier_items=b.mux
and c.REPORTNUMBER=(case
when a.carrier_item_type in ('ServicePackage','Service','Connection') then 10
else 20
end)
and a.carrier_items not in (select carried_items from CST_ASIR.HIERARCHY_asir where carried_item_type in('Connection') ))A
where not exists
(select *
from CST_DSA.HIERARCHY_MISMATCHES B where
A.REPORTNUMBER=B.REPORT_NUM and
A.REPORTTYPE=B.REPORT_TYPE and
A.REPORTDESCRIPTION=B.REPORT_DESC and
A.CARRIED_ITEMS=B.CARRIED_ITEMS and
A.CARRIED_ITEM_TYPE=B.CARRIED_ITEM_TYPE and
A.NO_OF_ROUTE_OF_CARRIED_ITEM=B.NO_OF_ROUTE_OF_CARRIED_ITEM and
A.CARRIED_ITEM_ROUTE_NO=B.CARRIED_ITEM_ROUTE_NO and
A.CARRIER_ITEMS=B.CARRIER_ITEMS and
A.CARRIER_ITEM_TYPE=B.CARRIER_ITEM_TYPE and
A.CARRIED_ITEM_PROTECTION_TYPE=B.CARRIED_ITEM_PROTECTION_TYPE
AND B.SOURCE_SYSTEM='PACS'
Explain Plan
==========
Plan
INSERT STATEMENT ALL_ROWSCost: 129 Bytes: 1,103 Cardinality: 1
20 LOAD AS SELECT CST_DSA.HIERARCHY_MISMATCHES
19 PX COORDINATOR
18 PX SEND QC (RANDOM) PARALLEL_TO_SERIAL SYS.:TQ10002 :Q1002Cost: 129 Bytes: 1,103 Cardinality: 1
17 NESTED LOOPS PARALLEL_COMBINED_WITH_PARENT :Q1002Cost: 129 Bytes: 1,103 Cardinality: 1
15 HASH JOIN RIGHT ANTI NA PARALLEL_COMBINED_WITH_PARENT :Q1002Cost: 129 Bytes: 1,098 Cardinality: 1
4 PX RECEIVE PARALLEL_COMBINED_WITH_PARENT :Q1002Cost: 63 Bytes: 359,283 Cardinality: 15,621
3 PX SEND BROADCAST PARALLEL_TO_PARALLEL SYS.:TQ10001 :Q1001Cost: 63 Bytes: 359,283 Cardinality: 15,621
2 PX BLOCK ITERATOR PARALLEL_COMBINED_WITH_CHILD :Q1001Cost: 63 Bytes: 359,283 Cardinality: 15,621
1 MAT_VIEW ACCESS FULL MAT_VIEW PARALLEL_COMBINED_WITH_PARENT CST_ASIR.HIERARCHY :Q1001Cost: 63 Bytes: 359,283 Cardinality: 15,621
14 NESTED LOOPS ANTI PARALLEL_COMBINED_WITH_PARENT :Q1002Cost: 65 Bytes: 40,256,600 Cardinality: 37,448
11 HASH JOIN PARALLEL_COMBINED_WITH_PARENT :Q1002Cost: 65 Bytes: 6,366,160 Cardinality: 37,448
8 BUFFER SORT PARALLEL_COMBINED_WITH_CHILD :Q1002
7 PX RECEIVE PARALLEL_COMBINED_WITH_PARENT :Q1002Cost: 1 Bytes: 214 Cardinality: 2
6 PX SEND BROADCAST PARALLEL_FROM_SERIAL SYS.:TQ10000 Cost: 1 Bytes: 214 Cardinality: 2
5 INDEX FULL SCAN INDEX CST_DSA.IDX$$_06EF0005 Cost: 1 Bytes: 214 Cardinality: 2
10 PX BLOCK ITERATOR PARALLEL_COMBINED_WITH_CHILD :Q1002Cost: 63 Bytes: 2,359,224 Cardinality: 37,448
9 MAT_VIEW ACCESS FULL MAT_VIEW PARALLEL_COMBINED_WITH_PARENT CST_ASIR.HIERARCHY :Q1002Cost: 63 Bytes: 2,359,224 Cardinality: 37,448
13 TABLE ACCESS BY INDEX ROWID TABLE PARALLEL_COMBINED_WITH_PARENT CST_DSA.HIERARCHY_MISMATCHES :Q1002Cost: 0 Bytes: 905 Cardinality: 1
12 INDEX RANGE SCAN INDEX PARALLEL_COMBINED_WITH_PARENT SYS.HIERARCHY_MISMATCHES_IDX3 :Q1002Cost: 0 Cardinality: 1
16 INDEX RANGE SCAN INDEX PARALLEL_COMBINED_WITH_PARENT CST_DSA.IDX$$_06EF0001 :Q1002Cost: 1 Bytes: 5 Cardinality: 1 -
Problem writing a sql query for a select list based on a static LOV
Hi,
I have the following table...
VALIDATIONS
ID Number (PK)
APP_ID Number
REQUESTED Date
APPROVED Date
VALID_TIL Date
DEPT_ID Number (FK)
I have a search form with the following field item variables...
P11_DEPT_ID (select list based on dynamic LOV from depts table)
P11_VALID (select list based on static Yes/No LOV)
A report on the columns of the Validations table is shown based on the values in the search form. So far, my sql query for the report is...
SELECT v.APP_ID,
v.REQUESTED,
v.APPROVED,
v.VALID_TIL,
d.DEPT
FROM DEPTS d, VALIDATIONS v
WHERE d.DEPT_ID = v.DEPT_ID(+)
AND (d.DEPT_ID = :P11_DEPT_ID OR :P11_DEPT_ID = -1)
This query works so far. My problem is that I don't know how to do a search based on the P11_VALID item - if 'yes' is selected, then the VALID_TIL date is still valid. If 'no' is selected then the VALID_TIL date has passed.
Can anyone help me to extend my query to include this situation?
Thanks.Hello !
Let's have a look at my example:create table test
id number
,valid_til date
insert into test values( 1, sysdate-3 );
insert into test values( 2, sysdate-2 );
insert into test values( 3, sysdate-1 );
insert into test values( 4, sysdate );
insert into test values( 5, sysdate+1 );
insert into test values( 6, sysdate+2 );
commit;
select * from test;
def til=yes
select *
from test
where decode(sign(trunc(valid_til)-trunc(sysdate)),1,1,0,1,-1)
=decode('&til','yes',1,-1);
def til=no
select *
from test
where decode(sign(trunc(valid_til)-trunc(sysdate)),1,1,0,1,-1)
=decode('&til','yes',1,-1);
drop table test; It's working fine, I've tested it.
The above changes to my first idea I did because of time portion of the DATE datatype in Oracle and therefore the wrong result for today.
For understandings:
1.) TRUNC removes the time part of DATE
2.) The difference of to date-values is the number of days between.
3.) SIGN is the mathematical function and gives -1,0 or +1 according to an negative, zero or positiv argument.
4.) DECODE is like an IF.
Inspect your LOV for the returning values. According to my example they shoul be 'yes' and 'no'. If your values are different, you may have to modify the DECODE.
Good luck,
Heinz -
Extracting the Logical sql query for the specified report in OBIEE 11g
Hi ,
I want to extract the logical SQL Query for the Particular report in OBIEE 11.1.1.5.
Any pointers related to this will be very helpful.
Thanks,
Sonalifor a try please add Logical sql view to ur report it will dispaly the Logical sql for that Report..
Hope it will helps you. -
Oracle SQL query for getting specific special characters from a table
Hi all,
This is my table
Table Name- Table1
S.no Name
1 aaaaaaaa
2 a1234sgjghb
3 a@3$%jkhkjn
4 abcd-dfghjik
5 bbvxzckvbzxcv&^%#
6 ashgweqfg/gfjwgefj////
7 sdsaf$([]:'
8 <-fdsjgbdfsg
9 dfgfdgfd"uodf
10 aaaa bbbbz#$
11 cccc dddd-/mnm
The output has to be
S.no Name
3 a@3$%jkhkjn
5 bbvxzckvbzxcv&^%#
7 sdsaf$([]:'
8 <-fdsjgbdfsg
10 aaaa bbbbz#$
It has to return "Name" column which is having special characters,whereas some special chars like -, / ," and space are acceptable.
The Oracle query has to print columns having special characters excluding -,/," and space
Can anyone help me to get a SQL query for the above.
Thanks in advance.You can achieve it in multiple ways. Here are few.
SQL> with t
2 as
3 (
4 select 1 id, 'aaaaaaaa' name from dual union all
5 select 2 id, 'a1234sgjghb' name from dual union all
6 select 3 id, 'a@3$%jkhkjn' name from dual union all
7 select 4 id, 'abcd-dfghjik' name from dual union all
8 select 5 id, 'bbvxzckvbzxcv&^%#' name from dual union all
9 select 6 id, 'ashgweqfg/gfjwgefj////' name from dual union all
10 select 7 id, 'sdsaf$([]:''' name from dual union all
11 select 8 id, '<-fdsjgbdfsg' name from dual union all
12 select 9 id, 'dfgfdgfd"uodf' name from dual union all
13 select 10 id, 'aaaa bbbbz#$' name from dual union all
14 select 11 id, 'cccc dddd-/mnm' name from dual
15 )
16 select *
17 from t
18 where regexp_like(translate(name,'a-/" ','a'), '[^[:alnum:]]');
ID NAME
3 a@3$%jkhkjn
5 bbvxzckvbzxcv&^%#
7 sdsaf$([]:'
8 <-fdsjgbdfsg
10 aaaa bbbbz#$
SQL> with t
2 as
3 (
4 select 1 id, 'aaaaaaaa' name from dual union all
5 select 2 id, 'a1234sgjghb' name from dual union all
6 select 3 id, 'a@3$%jkhkjn' name from dual union all
7 select 4 id, 'abcd-dfghjik' name from dual union all
8 select 5 id, 'bbvxzckvbzxcv&^%#' name from dual union all
9 select 6 id, 'ashgweqfg/gfjwgefj////' name from dual union all
10 select 7 id, 'sdsaf$([]:''' name from dual union all
11 select 8 id, '<-fdsjgbdfsg' name from dual union all
12 select 9 id, 'dfgfdgfd"uodf' name from dual union all
13 select 10 id, 'aaaa bbbbz#$' name from dual union all
14 select 11 id, 'cccc dddd-/mnm' name from dual
15 )
16 select *
17 from t
18 where translate
19 (
20 lower(translate(name,'a-/" ','a'))
21 , '.0123456789abcdefghijklmnopqrstuvwxyz'
22 , '.'
23 ) is not null;
ID NAME
3 a@3$%jkhkjn
5 bbvxzckvbzxcv&^%#
7 sdsaf$([]:'
8 <-fdsjgbdfsg
10 aaaa bbbbz#$
SQL> -
How to create sql query for item master with operator LIKE with variables?
hi all,
How to create sql query for item master with
operator LIKE(Contains,Start With,End With) with variables using query generator in SAP B1 ?
JeyakanthanHi Jeyakanthan,
here is an example (put the like statement into the where field)
SELECT T0.CardCode, T0.CardName FROM OITM T0 WHERE T0.CardName Like '%%test%%'
The %% sign is a wildcard. If you need start with write 'test%%' and otherwise ends with '%%test'. For contains you need '%%test%%'. You also could combinate this statements like 'test%%abc%%'. This means starts with test and contains abc.
Regards Steffen -
How to write sql query for counting pairs from below table??
Below is my SQL table structure.
user_id | Name | join_side | left_leg | right_leg | Parent_id
100001 Tinku Left 100002 100003 0
100002 Harish Left 100004 100005 100001
100003 Gorav Right 100006 100007 100001
100004 Prince Left 100008 NULL 100002
100005 Ajay Right NULL NULL 100002
100006 Simran Left NULL NULL 100003
100007 Raman Right NULL NULL 100003
100008 Vijay Left NULL NULL 100004
It is a binary table structure.. Every user has to add two per id under him, one is left_leg and second is right_leg... Parent_id is under which user current user is added.. Hope you will be understand..
I have to write sql query for counting pairs under id "100001". i know there will be important role of parent_id for counting pairs. * what is pair( suppose if any user contains both left_leg and right_leg id, then it is called pair.)
I know there are three pairs under id "100001" :-
1. 100002 and 100003
2. 100004 and 100005
3. 100006 and 100007
100008 will not be counted as pair because it does not have right leg..
But i dont know how to write sql query for this... Any help will be appreciated... This is my college project... And tommorow is the last date of submission.... Hope anyone will help me...
Suppose i have to count pair for id '100002'. Then there is only one pair under id '100002'. i.e 100004 and 100005Sounds like this to me
DECLARE @ID int
SET @ID = 100001--your passed value
SELECT left_leg,right_leg
FROM table
WHERE (user_id = @ID
OR parent_id = @ID)
AND left_leg IS NOT NULL
AND right_leg IS NOT NULL
Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs -
How to write sql query for below example.
Hi,
I have requirement.
There are number of rows and I need the result through query as follows.Please help me to proved sql query for below mentioned requirement.
example: table TEST.
COLA COLB COLC COLD
MANAGER 5 NULL null
SR.MANAGE 6 3 NULL
VP 5 5 4
I have to write the sql query if COLA IS MANAGER THEN CONSIDER MANGER RECORD,AND IF ANY COLUMN FILED IS NULL FOR MANGER THEN CONSIDER COLA IS SR.MANGER, AND IF ANY COLUMN FILED IS NULL FOR SR,MANGER THEN CONSIDER VP records.
I need output as below.
COLB COLC COLD
5(MANGER) 3(sr.manger) 5(vp)
Please provide the for above mentioned output.
Thanks<<if COLA IS MANAGER THEN CONSIDER MANGER RECORD>>
What does this sentence means? COLA does not cnatin a single record but the number of records with diffrent values.
Regards
Arun -
How to write sql query for below mentioned eaxmple.
Hi,
I have requirement.
There are number of rows and I need the result through query as follows.Please help me to proved sql query for below mentioned requirement.
example: table TEST.
COLA COLB COLC COLD COLE COLF MANAGER 5 NULL NULL 3 NULL
SR.MANAGER 6 3 NULL NULL NULL
VP 5 5 4 5 5
I have to write the sql query if COLA IS MANAGER THEN CONSIDER MANGER RECORD,AND IF ANY COLUMN FILED IS NULL FOR MANGER THEN CONSIDER COLA IS SR.MANGER, AND IF ANY COLUMN FILED IS NULL FOR SR,MANGER THEN CONSIDER VP records.
I need output as below.
COLB COLC COLD COLE COLF
5(manager) 3(sr.manger) 4(vp) 3(manger) 3(vp)
Please provide the for above mentioned output.
ThanksDuplicate thread. please view the answer posted in your first thread.
how to write sql query.
And, please don't post any duplicate thread.
Regards.
Satyaki De. -
OIM sql Query for getting the status of the task which got failed
Hi Everyone,
We have a requirement like we need to get the status of a particular task(say Create User in OID resource - Completed\Rejected status) for the particular user.We are able to get the status of the resource provisioed to the user but not the status of the particular task getting trigerred for the user.can someone put some light on this.We need to get the SQL query for this.
Thanks in Advance.
Regards,
MKNHi
Use this sample query to get the task status, also check the cooments
SELECT USR.USR_LOGIN, OSI.SCH_KEY,SCH.SCH_STATUS,STA.STA_BUCKET FROM
OSI,SCH,STA,MIL,TOS,PKG,OIU,USR,OBJ,OST
WHERE OSI.MIL_KEY=MIL.MIL_KEY
AND SCH.SCH_KEY=OSI.SCH_KEY
AND STA.STA_STATUS=SCH.SCH_STATUS
AND TOS.PKG_KEY=PKG.PKG_KEY
AND MIL.TOS_KEY=TOS.TOS_KEY
AND OIU.USR_KEY=USR.USR_KEY
AND OIU.OST_KEY=OST.OST_KEY
AND OST.OBJ_KEY=OBJ.OBJ_KEY
AND OSI.ORC_KEY=OIU.ORC_KEY
AND OBJ.OBJ_NAME='AD User'
AND OST.OST_STATUS = 'Provisioning' -- filter accordinglly
AND STA.STA_BUCKET = 'Pending' -- filter accordinglly
AND PKG.PKG_NAME='AD User' -- filter accordinglly
AND MIL.MIL_NAME='System Validation' ---- filter accordinglly
Thanks,
Kuldeep -
Provide sql query for below one
hi everyone,
in my source table [finanace_dept] contains two columns [finance_id,r_mature_kd] with 2 million records and sample data given below.
finance_id r_mature_kd
1 H
1 T
1 T
2 T
3 H
4 S
4 T
4 T
5 X
6 H
6 L
6 L
6 M
please provide sql query for below expected output.
expected output :
finance_id r_mature_kd
1 H
1 T
4 S
4 T
6 H
6 L
6 M
for column finance_id: '1', contains three records, but two distinct r_mature_kd hence it should retrive only two records with H,T
for column finance_id: '2','3', contains only one record hence this records should not retrive.
for column finance_id: '6', contains four records, but three distinct r_mature_kd hence it should retrive only two records with H,L,M
please help on this.WITH T(finance_id,r_mature_kd) AS (
SELECT 1, 'H' FROM DUAL UNION ALL
SELECT 1, 'T' FROM DUAL UNION ALL
SELECT 1, 'T' FROM DUAL UNION ALL
SELECT 2, 'T' FROM DUAL UNION ALL
SELECT 3, 'H' FROM DUAL UNION ALL
SELECT 4, 'S' FROM DUAL UNION ALL
SELECT 4, 'T' FROM DUAL UNION ALL
SELECT 4, 'T' FROM DUAL UNION ALL
SELECT 5, 'X' FROM DUAL UNION ALL
SELECT 6, 'H' FROM DUAL UNION ALL
SELECT 6, 'L' FROM DUAL UNION ALL
SELECT 6, 'L' FROM DUAL UNION ALL
SELECT 6, 'M' FROM DUAL
SELECT DISTINCT finance_id,r_mature_kd,C FROM(
SELECT finance_id,r_mature_kd,COUNT(1) OVER (PARTITION BY finance_id) C
FROM T
WHERE C > 1
ORDER BY 1
FINANCE_ID
R_MATURE_KD
C
1
H
3
1
T
3
4
S
3
4
T
3
6
H
4
6
L
4
6
M
4
Ramin Hashimzade -
Better SQL Query for clients without boundaries?
I've been using and modifying/experimenting with Chris Nackers' SQL query for missing boundaries (http://myitforum.com/myitforumwp/2011/12/07/sql-query-to-identify-missing-smsconfigmgr-boundaries/)
below (changed to add aliases)--but this seems to mainly be showing us non-clients, as several computers that were indeed missing boundaries (using SCCM 2007 SP2 R3, and all our boundaries are protected, most are IP Range, a few IP Subnet, none AD Site) are
not being listed, and everything in the listing has NULL SYS.Client0.
Is there a better query to pinpoint this issue, or maybe using something (error code or log?) that would show computers that can't find a distribution point or some other evidence of not having a boundary?
Thanks!
SELECT DISTINCT SYS.Name0, SYS.Client0, IPA.IP_Addresses0, IPS.IP_Subnets0, SMSAS.SMS_Assigned_Sites0
FROM dbo.v_R_System SYS
LEFT OUTER JOIN dbo.v_RA_System_IPSubnets IPS ON SYS.ResourceID = IPS.ResourceID
LEFT OUTER JOIN dbo.v_RA_System_IPAddresses IPA ON SYS.ResourceID = IPA.ResourceID
LEFT OUTER JOIN dbo.v_RA_System_SMSAssignedSites SMSAS ON SYS.ResourceID = SMSAS.ResourceID
LEFT OUTER JOIN dbo.v_RA_System_SystemOUName SOU ON SYS.ResourceID = SOU.ResourceID
WHERE (SMSAS.SMS_Assigned_Sites0 IS NULL)
AND (NOT (IPA.IP_Addresses0 IS NULL))
AND (NOT (IPS.IP_Subnets0 IS NULL))
AND SYS.Operating_System_Name_and0 LIKE 'microsoft%server%'
ORDER BY IPS.IP_Subnets0, SYS.Name0I gotcha now... I think most people, myself included, rely on finding clients that are not assigned to determine if a boundary is missing. If you expect clients to not be assigned that's not going to work for you.
WHERE (SMSAS.SMS_Assigned_Sites0
IS NULL)
AND (NOT (IPA.IP_Addresses0
IS NULL))
AND (NOT
(IPS.IP_Subnets0 IS
NULL))
= This is saying show me all clients not assigned but they do have an IP address and they do have a subnet discovered.
In the case of CM12 it is actually possible for that not to work anyway because you can have separate boundaries for client assignment and content lookup.
I am not aware of any query that can compare the IP address, AD Site and IP subnet from each client to what's configured in boundaries and find machines that do not fall into any boundary.
John Marcum | http://myitforum.com/myitforumwp/author/johnmarcum/ -
I want sql query for this output
hi guys
could u tell how can i write sql query for this out put
i have one table like this
ID ACCOUTID TAX
1 1 A
2 1 B
3 2 C
4 2 D
5 3 E
7 NULL F
8 NULL G
MY OUT PUT MUST BE LIKE THIS
ID AID TAX
2 1 A
4 2 D
7 NULL F
8 NULL G
HERE IN THIS OUTPUT I SHOULD HAVE
MAXIMAM ID VALUE FOR A REPEATED AID VALUES
AND
THE ROWS AID VALUES IS NULL ALSO MUST PAPULATED IN THE OUTPUT.
I KNOW ONE SOLUTION LIKE THIS
SELECT MAX(ID),AID,TAX
FROM TABLE T
GROUP BY AID,TAX
UNION ALL
SELECT ID, AIC,TAX
FROM TABLE T
WHERE AID IS NULL;
BUT I WANT SAME RESULT WITH OUT USING LOGICAL OPERATORS.
COULD U PLZ TELL A SOL.Will this help:
SQL> with t as
2 (
3 select 1 ID, 1 ACCOUTID, 'A' TAX from dual union all
4 select 2, 1, 'B' from dual union all
5 select 3, 2, 'C' from dual union all
6 select 4, 2, 'D' from dual union all
7 select 5, 3, 'E' from dual union all
8 select 7, NULL, 'F' from dual union all
9 select 8, NULL, 'G' from dual
10 )
11 --
12 select id, ACCOUTID AID, Tax
13 from
14 (
15 select t.*
16 ,count(1) over (partition by t.ACCOUTID) cn
17 ,row_number() over (partition by t.ACCOUTID order by id desc) rn
18 from t
19 )
20 where cn > 1
21 and (rn = 1 or ACCOUTID is null)
22 /
ID AID T
2 1 B
4 2 D
8 G
7 F
-- If I leave out the OR condition then you'll get this:
SQL> ed
Wrote file afiedt.buf
1 with t as
2 (
3 select 1 ID, 1 ACCOUTID, 'A' TAX from dual union all
4 select 2, 1, 'B' from dual union all
5 select 3, 2, 'C' from dual union all
6 select 4, 2, 'D' from dual union all
7 select 5, 3, 'E' from dual union all
8 select 7, NULL, 'F' from dual union all
9 select 8, NULL, 'G' from dual
10 )
11 --
12 select id, ACCOUTID AID, Tax
13 from
14 (
15 select t.*
16 ,count(1) over (partition by t.ACCOUTID) cn
17 ,row_number() over (partition by t.ACCOUTID order by id desc) rn
18 from t
19 )
20 where cn > 1
21* and rn = 1
SQL> /
ID AID T
2 1 B
4 2 D
8 G
--which follows the description you've given, but not the output -
SQL Query for mapping a set of batches to a class rooms group
Hi All,
I am using Oracle 11g Release 2 database.
I have the following data set:
ClassRooms
ClassId ClassName Capacity Group
1 Babbage/Software Engg Lab 24 1
2 Basement - PG Block 63 1
3 Classroom 1 56 1
4 Classroom 10 24 1
5 Classroom 11 24 1
6 Classroom 12 35 1
7 Classroom 13 42 1
8 Classroom 14 42 1
9 Classroom 15 42 1
10 Classroom 2 35 1
11 Classroom 3 35 1
12 Classroom 4 35 1
13 Classroom 5 35 1
14 Classroom 6 25 1
15 Classroom 7 25 1
16 Classroom 8 24 1
17 Classroom 9 24 1
18 Control Sys Lab 24 1
19 Dig & Embd Sys Lab 20 1
20 DSP & Comm Lab 20 1
21 Electromechanical System Lab 28 1
22 Farabi/Web Tech Lab 36 1
23 Gen Purpose Lab 40 1
24 Shirazi/DB Tech Lab 36 1
25 Adv Elect Lab 30 2
26 Classroom 16 42 2
27 Classroom 17 49 2
28 Classroom 18 56 2
29 Classroom 19 42 2
30 Classroom 20 49 2
31 Classroom 21 35 3
32 Classroom 22 35 3
33 MDA Lab 20 3
DegreeBatches
BatchId BatchName Strength
1 BIT-11 79
2 BIT-12 28
3 BS(CS)-1 35
4 BS(CS)-2 78
5 BE(SE)-1 69
6 BE(SE)-2 84
7 BE(SE)-3 64
8 BICSE-7 84
9 BICSE-8 43
10 BEE-1 112
11 BEE-2 151
12 BEE-3 157
13 BEE-4 157
I want to map a degree batch combination to a class rooms group in such away that they fully utilize maximum capacity of the class rooms within a group (Ideal case) or as close to it as possible. Can it be done with a SQL query?
Any response will be highly appreciated.
SQL Scripts to generate the required tables and populate data is below:
CREATE TABLE ClassRooms (ClassId NUMBER, ClassName VARCHAR2(50), Capacity NUMBER, Group NUMBER);
INSERT INTO ClassRooms VALUES(1,'Babbage/Software Engg Lab',24,1);
INSERT INTO ClassRooms VALUES(2,'Basement - PG Block',63,1);
INSERT INTO ClassRooms VALUES(3,'Classroom 1',56,1);
INSERT INTO ClassRooms VALUES(4,'Classroom 10',24,1);
INSERT INTO ClassRooms VALUES(5,'Classroom 11',24,1);
INSERT INTO ClassRooms VALUES(6,'Classroom 12',35,1);
INSERT INTO ClassRooms VALUES(7,'Classroom 13',42,1);
INSERT INTO ClassRooms VALUES(8,'Classroom 14',42,1);
INSERT INTO ClassRooms VALUES(9,'Classroom 15',42,1);
INSERT INTO ClassRooms VALUES(10,'Classroom 2',35,1);
INSERT INTO ClassRooms VALUES(11,'Classroom 3',35,1);
INSERT INTO ClassRooms VALUES(12,'Classroom 4',35,1);
INSERT INTO ClassRooms VALUES(13,'Classroom 5',35,1);
INSERT INTO ClassRooms VALUES(14,'Classroom 6',25,1);
INSERT INTO ClassRooms VALUES(15,'Classroom 7',25,1);
INSERT INTO ClassRooms VALUES(16,'Classroom 8',24,1);
INSERT INTO ClassRooms VALUES(17,'Classroom 9',24,1);
INSERT INTO ClassRooms VALUES(18,'Control Sys Lab',24,1);
INSERT INTO ClassRooms VALUES(19,'Dig & Embd Sys Lab',20,1);
INSERT INTO ClassRooms VALUES(20,'DSP & Comm Lab',20,1);
INSERT INTO ClassRooms VALUES(21,'Electromechanical System Lab',28,1);
INSERT INTO ClassRooms VALUES(22,'Farabi/Web Tech Lab',36,1);
INSERT INTO ClassRooms VALUES(23,'Gen Purpose Lab',40,1);
INSERT INTO ClassRooms VALUES(24,'Shirazi/DB Tech Lab',36,1);
INSERT INTO ClassRooms VALUES(25,'Adv Elect Lab',30,2);
INSERT INTO ClassRooms VALUES(26,'Classroom 16',42,2);
INSERT INTO ClassRooms VALUES(27,'Classroom 17',49,2);
INSERT INTO ClassRooms VALUES(28,'Classroom 18',56,2);
INSERT INTO ClassRooms VALUES(29,'Classroom 19',42,2);
INSERT INTO ClassRooms VALUES(30,'Classroom 20',49,2);
INSERT INTO ClassRooms VALUES(31,'Classroom 21',35,3);
INSERT INTO ClassRooms VALUES(32,'Classroom 22',35,3);
INSERT INTO ClassRooms VALUES(33,'MDA Lab',20,3);
CREATE TABLE DegreeBatches (BatchId NUMBER, BatchName VARCHAR2(50), Strength NUMBER);
INSERT INTO DegreeBatches VALUES(1,'BIT-11',79);
INSERT INTO DegreeBatches VALUES(2,'BIT-12',28);
INSERT INTO DegreeBatches VALUES(3,'BS(CS)-1',35);
INSERT INTO DegreeBatches VALUES(4,'BS(CS)-2',78);
INSERT INTO DegreeBatches VALUES(5,'BE(SE)-1',69);
INSERT INTO DegreeBatches VALUES(6,'BE(SE)-2',84);
INSERT INTO DegreeBatches VALUES(7,'BE(SE)-3',64);
INSERT INTO DegreeBatches VALUES(8,'BICSE-7',84);
INSERT INTO DegreeBatches VALUES(9,'BICSE-8',43);
INSERT INTO DegreeBatches VALUES(10,'BEE-1',112);
INSERT INTO DegreeBatches VALUES(11,'BEE-2',151);
INSERT INTO DegreeBatches VALUES(12,'BEE-3',157);
INSERT INTO DegreeBatches VALUES(13,'BEE-4',157);
Best Regards
Bilal
Edited by: Bilal on 27-Dec-2012 09:52
Edited by: Bilal on 27-Dec-2012 10:07Bilal, thanks for the nice problem! Another possibility for duplicate checking is to write a small PL/SQL function returning 1 if a duplicate id is found, then equate it to 0: 'Duplicate_Token_Found (p_str_main VARCHAR2, p_str_trial VARCHAR2) RETURN NUMBER'. This would parse the second string and might use p_str_main LIKE '%,' || l_id || ',%' for each id. Anyway the full query (without that) is given below:
Solution with names
SQL> WITH rsf_itm (con_id, max_weight, nxt_id, lev, tot_weight, tot_profit, path, root_id, lev_1_id) AS (
2 SELECT c.id,
3 c.max_weight,
4 i.id,
5 0,
6 i.item_weight,
7 i.item_profit,
8 ',' || i.id || ',',
9 i.id,
10 0
11 FROM items i
12 CROSS JOIN containers c
13 UNION ALL
14 SELECT r.con_id,
15 r.max_weight,
16 i.id,
17 r.lev + 1,
18 r.tot_weight + i.item_weight,
19 r.tot_profit + i.item_profit,
20 r.path || i.id || ',',
21 r.root_id,
22 CASE WHEN r.lev = 0 THEN i.id ELSE r.nxt_id END
23 FROM rsf_itm r
24 JOIN items i
25 ON i.id > r.nxt_id
26 AND r.tot_weight + i.item_weight <= r.max_weight
27 ORDER BY 1, 2
28 ) SEARCH DEPTH FIRST BY nxt_id SET line_no
29 , rsf_con (nxt_con_id, nxt_line_no, con_path, itm_path, tot_weight, tot_profit, lev) AS (
30 SELECT con_id,
31 line_no,
32 To_Char(con_id),
33 ':' || con_id || '-' || (lev + 1) || ':' || path,
34 tot_weight,
35 tot_profit,
36 0
37 FROM rsf_itm
38 UNION ALL
39 SELECT r_i.con_id,
40 r_i.line_no,
41 r_c.con_path || ',' || r_i.con_id,
42 r_c.itm_path || ':' || r_i.con_id || '-' || (r_i.lev + 1) || ':' || r_i.path,
43 r_c.tot_weight + r_i.tot_weight,
44 r_c.tot_profit + r_i.tot_profit,
45 r_c.lev + 1
46 FROM rsf_con r_c
47 JOIN rsf_itm r_i
48 ON r_i.con_id > r_c.nxt_con_id
49 WHERE r_c.itm_path NOT LIKE '%,' || r_i.root_id || ',%'
50 AND r_c.itm_path NOT LIKE '%,' || r_i.lev_1_id || ',%'
51 AND r_c.itm_path NOT LIKE '%,' || r_i.nxt_id || ',%'
52 )
53 , paths_ranked AS (
54 SELECT itm_path || ':' itm_path, tot_weight, tot_profit, lev + 1 n_cons,
55 Rank () OVER (ORDER BY tot_profit DESC) rnk,
56 Row_Number () OVER (ORDER BY tot_profit DESC) sol_id
57 FROM rsf_con
58 ), best_paths AS (
59 SELECT itm_path, tot_weight, tot_profit, n_cons, sol_id
60 FROM paths_ranked
61 WHERE rnk = 1
62 ), row_gen AS (
63 SELECT LEVEL lev
64 FROM DUAL
65 CONNECT BY LEVEL <= (SELECT Count(*) FROM items)
66 ), con_v AS (
67 SELECT b.itm_path, r.lev con_ind, b.sol_id, b.tot_weight, b.tot_profit,
68 Substr (b.itm_path, Instr (b.itm_path, ':', 1, 2*r.lev - 1) + 1,
69 Instr (b.itm_path, ':', 1, 2*r.lev) - Instr (b.itm_path, ':', 1, 2*r.lev - 1) - 1)
70 con_nit_id,
71 Substr (b.itm_path, Instr (b.itm_path, ':', 1, 2*r.lev) + 1,
72 Instr (b.itm_path, ':', 1, 2*r.lev + 1) - Instr (b.itm_path, ':', 1, 2*r.lev) - 1)
73 itm_str
74 FROM best_paths b
75 JOIN row_gen r
76 ON r.lev <= b.n_cons
77 ), con_split AS (
78 SELECT itm_path, con_ind, sol_id, tot_weight, tot_profit,
79 Substr (con_nit_id, 1, Instr (con_nit_id, '-', 1) - 1) con_id,
80 Substr (con_nit_id, Instr (con_nit_id, '-', 1) + 1) n_items,
81 itm_str
82 FROM con_v
83 ), itm_v AS (
84 SELECT c.itm_path, c.con_ind, c.sol_id, c.con_id, c.tot_weight, c.tot_profit,
85 Substr (c.itm_str, Instr (c.itm_str, ',', 1, r.lev) + 1,
86 Instr (c.itm_str, ',', 1, r.lev + 1) - Instr (c.itm_str, ',', 1, r.lev) - 1)
87 itm_id
88 FROM con_split c
89 JOIN row_gen r
90 ON r.lev <= c.n_items
91 )
92 SELECT v.sol_id,
93 v.tot_weight s_wt, v.tot_profit s_pr, c.id c_id, c.name c_name, c.max_weight m_wt,
94 Sum (i.item_weight) OVER (PARTITION BY v.sol_id, c.id) c_wt,
95 i.id i_id, i.name i_name, i.item_weight i_wt, i.item_profit i_pr
96 FROM itm_v v
97 JOIN containers c
98 ON c.id = To_Number (v.con_id)
99 JOIN items i
100 ON i.id = To_Number (v.itm_id)
101 ORDER BY sol_id, con_id, itm_id
102 /
SOL_ID S_WT S_PR C_ID C_NAME M_WT C_WT I_ID I_NAME I_WT I_PR
1 255 255 1 SEECS UG Block 100 100 1 BIT-10 35 35
2 BIT-11 40 40
6 BICSE-7 25 25
2 IAEC Building 70 70 4 BSCS-3 40 40
7 BESE-3 30 30
3 RIMMS Building 90 85 3 BSCS-2 35 35
5 BEE-4 50 50
2 255 255 1 SEECS UG Block 100 95 4 BSCS-3 40 40
6 BICSE-7 25 25
7 BESE-3 30 30
2 IAEC Building 70 70 1 BIT-10 35 35
3 BSCS-2 35 35
3 RIMMS Building 90 90 2 BIT-11 40 40
5 BEE-4 50 50
3 255 255 1 SEECS UG Block 100 100 3 BSCS-2 35 35
4 BSCS-3 40 40
6 BICSE-7 25 25
2 IAEC Building 70 65 1 BIT-10 35 35
7 BESE-3 30 30
3 RIMMS Building 90 90 2 BIT-11 40 40
5 BEE-4 50 50
4 255 255 1 SEECS UG Block 100 100 3 BSCS-2 35 35
4 BSCS-3 40 40
6 BICSE-7 25 25
2 IAEC Building 70 70 2 BIT-11 40 40
7 BESE-3 30 30
3 RIMMS Building 90 85 1 BIT-10 35 35
5 BEE-4 50 50
5 255 255 1 SEECS UG Block 100 95 2 BIT-11 40 40
6 BICSE-7 25 25
7 BESE-3 30 30
2 IAEC Building 70 70 1 BIT-10 35 35
3 BSCS-2 35 35
3 RIMMS Building 90 90 4 BSCS-3 40 40
5 BEE-4 50 50
6 255 255 1 SEECS UG Block 100 100 2 BIT-11 40 40
3 BSCS-2 35 35
6 BICSE-7 25 25
2 IAEC Building 70 65 1 BIT-10 35 35
7 BESE-3 30 30
3 RIMMS Building 90 90 4 BSCS-3 40 40
5 BEE-4 50 50
7 255 255 1 SEECS UG Block 100 100 2 BIT-11 40 40
3 BSCS-2 35 35
6 BICSE-7 25 25
2 IAEC Building 70 70 4 BSCS-3 40 40
7 BESE-3 30 30
3 RIMMS Building 90 85 1 BIT-10 35 35
5 BEE-4 50 50
8 255 255 1 SEECS UG Block 100 100 1 BIT-10 35 35
4 BSCS-3 40 40
6 BICSE-7 25 25
2 IAEC Building 70 70 2 BIT-11 40 40
7 BESE-3 30 30
3 RIMMS Building 90 85 3 BSCS-2 35 35
5 BEE-4 50 50
9 255 255 1 SEECS UG Block 100 100 1 BIT-10 35 35
4 BSCS-3 40 40
6 BICSE-7 25 25
2 IAEC Building 70 65 3 BSCS-2 35 35
7 BESE-3 30 30
3 RIMMS Building 90 90 2 BIT-11 40 40
5 BEE-4 50 50
10 255 255 1 SEECS UG Block 100 100 1 BIT-10 35 35
3 BSCS-2 35 35
7 BESE-3 30 30
2 IAEC Building 70 65 2 BIT-11 40 40
6 BICSE-7 25 25
3 RIMMS Building 90 90 4 BSCS-3 40 40
5 BEE-4 50 50
11 255 255 1 SEECS UG Block 100 100 1 BIT-10 35 35
3 BSCS-2 35 35
7 BESE-3 30 30
2 IAEC Building 70 65 4 BSCS-3 40 40
6 BICSE-7 25 25
3 RIMMS Building 90 90 2 BIT-11 40 40
5 BEE-4 50 50
12 255 255 1 SEECS UG Block 100 95 1 BIT-10 35 35
3 BSCS-2 35 35
6 BICSE-7 25 25
2 IAEC Building 70 70 2 BIT-11 40 40
7 BESE-3 30 30
3 RIMMS Building 90 90 4 BSCS-3 40 40
5 BEE-4 50 50
13 255 255 1 SEECS UG Block 100 95 1 BIT-10 35 35
3 BSCS-2 35 35
6 BICSE-7 25 25
2 IAEC Building 70 70 4 BSCS-3 40 40
7 BESE-3 30 30
3 RIMMS Building 90 90 2 BIT-11 40 40
5 BEE-4 50 50
14 255 255 1 SEECS UG Block 100 100 1 BIT-10 35 35
2 BIT-11 40 40
6 BICSE-7 25 25
2 IAEC Building 70 65 3 BSCS-2 35 35
7 BESE-3 30 30
3 RIMMS Building 90 90 4 BSCS-3 40 40
5 BEE-4 50 50
98 rows selected.
Elapsed: 00:00:01.42Edited by: BrendanP on 20-Jan-2013 11:25
I found the regex needed to deduplicate:
AND RegExp_Instr (r_c.itm_path || r_i.path, ',(\d+),.*?,\1,') = 0 -
Hi, I am using a prepared statement and how can I perform string matching using that?
For example, I have
PreparedStatement pstat = con.prepareStatement( new String(sql))
what should my sql String look like to query for strings containing "ab"
so when the executeQuery() is called,
I get results such as "abcd", "abcdcdc","ccccabccc"??
Thanks a lot,try somethink like this:
SELECT * FROM table_name WHERE column_name LIKE
'%ab%'
or
SELECT * FROM table_name WHERE column_name LIKE ?
providing "%ab%" as parameterAdding to that - you MUST do the % yourself and then use setString. The following will NOT work....
SELECT * FROM table_name WHERE column_name LIKE '%?%'
Maybe you are looking for
-
Purchased movie sync-back to iTunes causes Freeze
iTunes consistently freezes while transferring a purchased movie from the Apple TV to the iTunes Library during a sync. iTunes always freezes at the same point in the transfer, and is 100% repeatable. In fact, I have not been able to to use iTunes si
-
Test Stand 3.5 install problem
I'm having problems installing Test Stand 3.5 Development Edition on a WinXP SP1 machine - would anyone have any suggestions. I'm performing a custom install and selecting all components other than "Device Drivers". The install fails on "NI TestStand
-
Problem with BAPI_PO_RELEASE in workflow
Hi Experts, I have a backgrouond task for PO releases. in the method of the task , iam calling a BAPI, 'BAPI_PO_RELEASE' .when iam executing the workflow through SWUS the BAPI returnig parameters like new release code etc successfully but release str
-
Lightning to micro USB adapter with Otterbox Armor
Does anyone know if the Apple Lightning to Micro USB adapter will fit in the hole on the bottom of the Otterbox Armor series case?
-
TS1717 iTunes tends to freeze MacBook in many different ways?
Since I moved from Belgium to Australia following problem occurs: - When my Macbook Pro (OS X Lion 10.7.5) gets out of (screen)sleepmode when running music on iTunes the computer freezes and the song that was playing gets stuck on the last note. - Wh