Tricky select statement ??
Hello everybody,
can anyone helps me?
I have a table with 2 columns, e.g. .
A A5
A 5
B 5
C 5
D 5
D D5
Now, i will select:
If column 2 is the combination of column 1 and 5, then select this record and not the record with only 5. Doesn't exist the combination I will select the record with the only 5.
My result should look:
A A5
B 5
C 5
D D5.
Thanks.
Best Regards
Sandra Koenig
One way would be:
SQL> SELECT * FROM t;
C1 C2
A A5
A 5
B 5
C 5
D 5
D D5
SQL> SELECT c1, c2
2 FROM t
3 WHERE c2 = c1||'5'
4 UNION ALL
5 SELECT c1, c2
6 FROM t o
7 WHERE c2 = '5' and
8 NOT EXISTS (SELECT 1
9 FROM t i
10 WHERE o.c1 = i.c1 and
11 i.c2 = i.c1||'5')
12 ORDER BY c1;
C1 C2
A A5
B 5
C 5
D D5Another, one-pass, way is:
SQL> SELECT c1, c2
2 FROM (SELECT c1, c2,
3 RANK() OVER (PARTITION BY c1 ORDER BY LENGTH(c2) DESC) rn
4 FROM t
5 WHERE c2 = '5' OR
6 c2 = c1||'5')
7 WHERE rn = 1;
C1 C2
A A5
B 5
C 5
D D5TTFN
John
Similar Messages
-
Video in button Selected state?
I'm rusty in DVDSP - been a couple of years since my last DVD menu project, but I have one now and the client has provided the menu art and description of what they want - but there are 2 items I'm not sure if I can include the way they want, and before I re-prep their art for Studio Pro (because, of course, it was prepped as if for print), I want to determine the best course to take, and reset their expectations, if necessary.
1) They want video to play in a button, but only when it is selected - I can drop video in using shapes, but it's playing in all states. I can't use a layered menu, because the background also is motion (subtle looping, but motion just the same). Thought maybe I'd build the motion in the buttons and mask over with a still in normal state, reveal in selected, but can't seem to get there. So any ideas, or am I spinning my wheels trying to accomplish?
2) They've prepped the buttons as if rollovers in a Flash presentation - the normal state has an image with a white border around it and title underneath - but the selected state has those as black PLUS a small arrow pointing at the title. I can't do a straight shape with text, because the arrow element will be present in all states. I can't do a layered menu because of the motion background. So what I've come up with is embedding the normal states of the button (white, without the arrow) into the motion background, and then create a shape with the text (black, with the arrow) but with only the highlight layer and no shape layer so that only the black highlight layer shows up over it when selected. This works, but I have to position it PRECISELY over the background normal button, or else white edges show and it gets ugly. So, is there a better way to do this that I'm overlooking?
Thanks very much to anyone who has taken the time to read this novel, and even more thanks to anyone who has some insight into any possible solutions (or lack thereof).There is another way, but it's tricky.
You create a series of menus that are almost identical. In the first, all buttons show still images, and the button auto activates to a copy of the menu where the appropriate button shows a video. If you have four buttons, you'll need five menus all exactly the same except in the last four, single buttons have video, others are stills. Every button that is a still auto activates to go to the appropriate menu where that button has video. Every video based button links to the relevant track.
In practice, the user will see a still image, and when they select it (or roll over with a mouse if on a computer) the menu will jump to the associated sub menu where that button now has video and the others show stills.
The problem here is that on some (most) players there will be a pause between the menus, and it won't be a slick and quick change.
Using the masking technique is better in lots of ways, however as Drew said it will be hard to match the colours. Also, the mask shape will need to be your 'still' image (which might help obscure the colour differences) and this can only have four colours.
A further consideration is that the mask shape will be part of the button highlight (well.... it will BE the button highlight) and as such will exist in the subpicture stream. It is impossible to guarantee that this will stay in place or be in place when leaving or entering a menu. In effect, when you click a button the first thing to disappear is the sub picture stream, which means all of your videos will be visible for a fraction of a second, or perhaps slightly longer. Secondly, when entering a menu, the last thing to be created is the subpicture stream, which means that all of your button videos will be showing for a fraction of a second until they get covered.
The amount of delay in each case will depend on the player. For some it'll be tiny. For others it'll be seconds and will ruin the effect you are looking for.
I would probably prefer the delay as you change menus to the revelation of all the button videos, but you'll have to decide for yourself which is better! -
Hi all,
I have a tricky SQL statement below:
SELECT field_x FROM TABLE B WHERE field_y=0;
will generate result = ABC. ABC is a field IN TABLE A
However WHEN I try TO USE the below query:
SELECT (SELECT field_x FROM TABLE B WHERE field_y=0) FROM TABLE A;
it gives me a different result (All records is ABC), compare to the below query result that I want:
SELECT (ABC) FROM TABLE A;
Please advise.
Thanks.> I appreciate if you please give the some dos and
donts which i have to keep in mind ( except from the
business requirement) while designing database
structure for an application.
My 2c's.
Relational design works. It is mature. It is well documented. It works. Thus if you step outside the bounds of relational design, you must question WHY and make sure that answers are sound and logical. (obviously I do not refer to OLAP designs here, before someone jumps on my case ;-) )
That was the 1st cent. Logical design.
The 2nd cent says that know Oracle. Physical implementation of the logical design. Know Oracle concepts and fundamentals and features. E.g. a partitioned table can make the difference between a system that still works next year because it was able to scale with volumes.
It should be this simple. -
I'm trying to do two fairly tricky SQL statements for a class, and been trying for a while and have gotten stuck.
The first one is, "Find Courses where more than half the students got an A"
I've basically been trying to use not exist statements to set it up and then compared the count(sid)/2 > count(sid) query of all A's and am just not getting decent results from it.
The REALLY tricky one is "Find the pairs of students who took exactly the same set of courses"
I can get a cross product from all students with something like this
SELECT s1.sname, s2.sname
FROM lab6_student s1, lab6_student s2
WHERE s1.sname > s2.sname
but after that I can't seem to get it to work properly at all.
Thanks for any pointers/advice!1.
SQL> WITH T
2 AS (SELECT 'mathematics' course_name, 'john' student_name, 'A' grade FROM DUAL
3 UNION ALL
4 SELECT 'mathematics' course_name, 'john1' student_name, 'A' grade FROM DUAL
5 UNION ALL
6 SELECT 'mathematics' course_name, 'john2' student_name, 'A' grade FROM DUAL
7 UNION ALL
8 SELECT 'mathematics' course_name, 'john3' student_name, 'B' grade FROM DUAL
9 UNION ALL
10 SELECT 'physics' course_name, 'john' student_name, 'A' grade FROM DUAL
11 UNION ALL
12 SELECT 'physics' course_name, 'john1' student_name, 'B' grade FROM DUAL
13 UNION ALL
14 SELECT 'physics' course_name, 'john2' student_name, 'B' grade FROM DUAL
15 UNION ALL
16 SELECT 'physics' course_name, 'john3' student_name, 'B' grade FROM DUAL)
17 SELECT *
18 FROM T;
COURSE_NAME STUDE G
mathematics john A
mathematics john1 A
mathematics john2 A
mathematics john3 B
physics john A
physics john1 B
physics john2 B
physics john3 B
8 rows selected.
SQL> WITH T
2 AS (SELECT 'mathematics' course_name, 'john' student_name, 'A' grade FROM DUAL
3 UNION ALL
4 SELECT 'mathematics' course_name, 'john1' student_name, 'A' grade FROM DUAL
5 UNION ALL
6 SELECT 'mathematics' course_name, 'john2' student_name, 'A' grade FROM DUAL
7 UNION ALL
8 SELECT 'mathematics' course_name, 'john3' student_name, 'B' grade FROM DUAL
9 UNION ALL
10 SELECT 'physics' course_name, 'john' student_name, 'A' grade FROM DUAL
11 UNION ALL
12 SELECT 'physics' course_name, 'john1' student_name, 'B' grade FROM DUAL
13 UNION ALL
14 SELECT 'physics' course_name, 'john2' student_name, 'B' grade FROM DUAL
15 UNION ALL
16 SELECT 'physics' course_name, 'john3' student_name, 'B' grade FROM DUAL)
17 SELECT *
18 FROM (SELECT course_name, COUNT (*) total_cnt, SUM (CASE WHEN grade = 'A' THEN 1 ELSE 0 END) grade_cnt
19 FROM T
20 GROUP BY course_name)
21 WHERE grade_cnt > total_cnt / 2;
COURSE_NAME TOTAL_CNT GRADE_CNT
mathematics 4 3
SQL>
2.
SQL> WITH T
2 AS (SELECT '1' course_id, 'john' student_name FROM DUAL
3 UNION ALL
4 SELECT '2' course_id, 'john' student_name FROM DUAL
5 UNION ALL
6 SELECT '1' course_id, 'peter' student_name FROM DUAL
7 UNION ALL
8 SELECT '2' course_id, 'peter' student_name FROM DUAL
9 UNION ALL
10 SELECT '3' course_id, 'king' student_name FROM DUAL
11 UNION ALL
12 SELECT '4' course_id, 'king' student_name FROM DUAL)
13 SELECT *
14 FROM t;
C STUDE
1 john
2 john
1 peter
2 peter
3 king
4 king
6 rows selected.
SQL> WITH T
2 AS (SELECT '1' course_id, 'john' student_name FROM DUAL
3 UNION ALL
4 SELECT '2' course_id, 'john' student_name FROM DUAL
5 UNION ALL
6 SELECT '1' course_id, 'peter' student_name FROM DUAL
7 UNION ALL
8 SELECT '2' course_id, 'peter' student_name FROM DUAL
9 UNION ALL
10 SELECT '3' course_id, 'king' student_name FROM DUAL
11 UNION ALL
12 SELECT '4' course_id, 'king' student_name FROM DUAL),
13 r
14 AS (SELECT student_name, SUM (POWER ( 2, course_id)) val
15 FROM T
16 GROUP BY student_name)
17 SELECT DISTINCT student_name NAME
18 FROM r
19 WHERE EXISTS
20 (SELECT 'x'
21 FROM r r1
22 WHERE r1.val = r.val
23 GROUP BY val
24 HAVING COUNT (*) > 1);
NAME
john
peterEdited by: G. on Feb 24, 2011 2:09 PM -
Embedded select statements 8.04
this is a tricky one to explain, I have sql statements that were written in oracle 8i and works, unfortunately it is now being run on oracle 8.04 and fails, we have tested out that field names are correct, and that all the functions like decode, avg, stddev etc all existed in version 7.0 (the oldest manual we have).
The only message we are get using sql worksheet and having the sql statement in a text file, is missing expression after the third embedded sql statement (there is then another) so my first questions is does this older version of oracle have a limit on statement sizes, no of times sql select statements can be embedded. Any help would be greatly appreciated as though I know SQL i know little about Oracle.the code is roughly as follows(cut down a bit) , we cant use procedures, so have to send whole code through vb to oracle, using an account with read only access , where it says Select Sum (( sql worksheet reports an error of missing expression on this select:
SELECT 0 AS Record_Type_Id,
'INCLUDE' AS EXCLUDE_STATUS,
Chrt.Chart_Number,
Cpsl.scale,
Othv.Chart_Version,
Chrt.W_Dimension
FROM
Chart Chrt,
Other_Version Othv,
(( SELECT chart_number, scale
FROM chart_panel
WHERE UPPER(panel_main_title) = 'MAIN PANEL'
UNION
SELECT chart_number,
(SELECT SUM((
SELECT Decode(STDDEV(CpnI.SCALE),0, AVG(CpnI.scale) / count(CpnI.scale),0)
FROM Chart_Panel CpnI
WHERE CpnI.Chart_Number = CpnO.Chart_Number
GROUP BY CpnI.Chart_Number))/count(cpno.chart_number) as AVGScale
FROM Chart_panel CpnO
WHERE Cpno.chart_number = Cpn2.Chart_number
GROUP BY CpnO.chart_number) AS Scale
FROM chart_panel Cpn2
WHERE UPPER(panel_main_title) <> 'MAIN PANEL'
AND Cpn2.chart_number NOT IN (SELECT Chart_number
FROM Chart_panel
WHERE UPPER(Panel_main_title) = 'MAIN PANEL'))
UNION
SELECT
CHART_NUMBER AS Chart_Number, 0
FROM
CH[i]Long postings are being truncated to ~1 kB at this time. -
If statement in select statement alias
I have the following select statement. It has the alias Survivors, Deaths and "All Cases". Is it posible to use :P_LANGUAGE variable to say that -- IF :P_LANGUAGE = FRENCH THEN alias are Survivants for survivors, Décès for Deaths, Tous_les_cas for All Cases. Please advise
SELECT ALL T_NTR_MULTIBAR.CAT, T_NTR_MULTIBAR.NUM_CASES_LEFTBAR AS Survivors,
T_NTR_MULTIBAR.NUM_CASES_MIDDLEBAR AS Deaths, T_NTR_MULTIBAR.NUM_CASES_RIGHTBAR AS "All Cases"
FROM T_NTR_MULTIBAR
WHERE INSTANCE_NUM = :P_INSTANCENUM
order by ORDERSYou may not be able to add this condition inside the SQL Statement. But you can add this condition outside the statement, if you're using PL/SQL...
IF :p_language = french THEN
SELECT ALL t_ntr_multibar.cat,
t_ntr_multibar.num_cases_leftbar AS survivors,
t_ntr_multibar.num_cases_middlebar AS deaths,
t_ntr_multibar.num_cases_rightbar AS "All Cases"
ELSE
END IF; -
How to get all values from an interval using select statement
Hi,
Is it possible to write a select statement that returns all values from an interval? Interval boundaries are variable.
something like this:
select (for x in 1,1024 loop x end loop) from dual
(this, of course, doesn't work)
The result in this example should be 1024 rows of numbers from 1 to 1024. These numbers are parameters, so it is not possible to create a table with predefined values.
Thanks in advance for your help,
Mia.For your simple case, with a lower boundary of 1, you can use:
SELECT rownum
FROM all_objects
WHERE rownum <= 1024For a set of number between say 50 - 100, you can use something like:
SELECT rownum + (50 - 1)
FROM all_objects
WHERE rownum <= (100 - 50 + 1)Note, that all_objects was used only because it generally has a lot of rows. Any table with at least the number of rows in your range will work.
TTFN
John -
Select statement operators in ecc 6.
Hi Experts,
I have a small doubt about the '>=' ( greater than or equal to ) operator usage in select statement. Is this operator by any chance perform not as desired in ECC 6.0. Is it a good option to use 'GE' instead of '>='. ?
It may sound a bit awkward, but still I would like to know. I am facing a situation, which could be related to this. An early response would be highly appreciated.
I would request,you NOT TO REPLY with links/explanations which says how to use select statement. Only answer if you have the answers related to this query.
Regards,
Sandipan>
Jaideep Sharma wrote:
> Hi,
> The only difference is GE will take a little more time than >= as system need to convert the keyword into actual operator when fetching data from Database.
>
> KR Jaideep,
????? Every Open SQL statements is translated to the SQL slang the underlying database is talking regardless if you type GE or >=
If the result differs using >= or GE i would open a call at SAP instead of asking in SDN. -
How to find the number of fetched lines from select statement
Hi Experts,
Can you tell me how to find the number of fetched lines from select statements..
and one more thing is can you tell me how to check the written select statement or written statement is correct or not????
Thanks in advance
santoshHi,
Look for the system field SY_TABIX. That will contain the number of records which have been put into an internal table through a select statement.
For ex:
data: itab type mara occurs 0 with header line.
Select * from mara into table itab.
Write: Sy-tabix.
This will give you the number of entries that has been selected.
I am not sure what you mean by the second question. If you can let me know what you need then we might have a solution.
Hope this helps,
Sudhi
Message was edited by:
Sudhindra Chandrashekar -
Use of LIKE in where clause of select statement for multiple records
Hi Experts,
I have a account number field which is uploaded from a file. Now this account numbers uploaded does not match fully with sap table account numbers but it contains all of the numbers provided in the file mostly in the upright positions.
For example in file we have account number as 2ARS1 while in sap table the value is 002ARS1.
And i want to fetch data from sap table based on account number uploaded. So, i am trying to use LIKE with for all entries but its not working as mentioned below but LIKE is not working with FOR ALL ENTRIES.
data : begin of t_dda occurs 0,
dda(19) type c,
end of t_dda.
data : begin of t_bukrs occurs 0,
bukrs type t012k-bukrs,
end of t_bukrs.
data : dda type t012k-bankn,
w_dda type t012k-bankn.
CONCATENATE '%'
'2ARS1'
INTO W_DDA.
MOVE W_DDA TO T_DDA-DDA.
APPEND T_DDA.
CLEAR T_DDA.
free t_bukrs.
SELECT BUKRS
FROM T012K
into TABLE t_bukrs
for all entries in t_dda
WHERE BANKN like t_dda-dda.
Can anybody suggest what should i use to get the data for multiple account numbers using one select statement only instead on using SELECT UP TO 1 ROWS in LOOP....ENDLOOP ?
Thanks in advance,
AkashHi,
yes, For All entries won't work for LIKE with '% '.
I think the other alternative is go for Native SQL by writing sub-query
sample code is here:
data: begin of i_mara occurs 0,
matnr like mara-matnr,
matkl like mara-matkl,
end of i_mara.
exec sql.
select matnr, matkl from mara where matnr in (select matnr from marc) and matnr like '%ma' into :i_mara
endexec.
loop at i_mara.
write:/ i_mara-matnr, i_mara-matkl.
endloop.
hope u got it.
regards
Mahesh
Edited by: Mahesh Reddy on Jan 21, 2009 2:32 PM -
What is the use of additon in up to 1 rows in SELECT statement
Hi All,
What is the use of up to 1 rows in select statement.
for example
SELECT kostl
FROM pa0001
INTO y_lv_kostl UP TO 1 ROWS
WHERE pernr EQ pernr
AND endda GE sy-datum.
ENDSELECT.
I'm unable to get in wat situations we hav to add up to 1 rows
please help me out...
Thanks,
santosh.Hi,
Use "select up to 1 rows" only if you are sure that all the records returned will have the same value for the field(s) you are interested in. If not, you will be reading only the first record which matches the criteria, but may be the second or the third record has the value you are looking for.
The System test result showed that the variant Single * takes less time than Up to 1 rows as there is an additional level for COUNT STOP KEY for SELECT ENDSELECT UP TO 1 ROWS.
The 'SELECT .... UP TO 1 ROWS' statement is subtly different. The database selects all of the relevant records that are defined by the WHERE clause, applies any aggregate, ordering or grouping functions to them and then returns the first record of the result set.
Regards,
Bhaskar -
Secondary Index Select Statement Problem
Hi friends.
I have a issue with a select statement using secondary index,
SELECT SINGLE * FROM VEKP WHERE VEGR4 EQ STAGE_DOCK
AND VEGR5 NE SPACE
AND WERKS EQ PLANT
%_HINTS ORACLE
'INDEX("&TABLE&" "VEKP~Z3" "VEKP^Z3" "VEKP_____Z3")'.
given above statement is taking long time for processing.
when i check for the same secondary index in vekp table i couldn't see any DB index name with vekp~z3 or vekp^z3 or vekp____z3.
And the sy-subrc value after select statement is 4. (even though values avaliable in VEKP with given where condition values)
My question is why my select statement is taking long time and sy-subrc is 4?
what happens if a secnodary index given in select statement, which is not avaliable in that DB Table?Hi,
> ONe more question: is it possible to give more than one index name in select statement.
yes you can:
read the documentation:
http://download.oracle.com/docs/cd/A97630_01/server.920/a96533/hintsref.htm#5156
index_hint:
This hint can optionally specify one or more indexes:
- If this hint specifies a single available index, then the optimizer performs
a scan on this index. The optimizer does not consider a full table scan or
a scan on another index on the table.
- If this hint specifies a list of available indexes, then the optimizer
considers the cost of a scan on each index in the list and then performs
the index scan with the lowest cost. The optimizer can also choose to
scan multiple indexes from this list and merge the results, if such an
access path has the lowest cost. The optimizer does not consider a full
table scan or a scan on an index not listed in the hint.
- If this hint specifies no indexes, then the optimizer considers the
cost of a scan on each available index on the table and then performs
the index scan with the lowest cost. The optimizer can also choose to
scan multiple indexes and merge the results, if such an access path
has the lowest cost. The optimizer does not consider a full table scan.
Kind regards,
Hermann -
BI Publisher : SELECT statement in RTF template
Hi Guys
I have written a BI Publisher Report using XML file created from Oracle Reports(in Oracle Apps).
Repors runs from Oracle Apps perfectly ok. Now I need to fetch some data from couple of tables and display on the Report.
I am wondering whether I can directly code SELECT statement in RTF file rather than messing with Oracle Report(.rdf) file.
Please advise.
Thanks and Regards
VijayHey Vijay,
You cannot query in RTF using select :)..
You have to mess/play with RDF to do it ;)
Oh wait, did i say , we cannot in RTF, we can , but that is difficult approach to go with., keep this as an end of the world option. -
Can we use is null in our select statement in ABAP program
hi,
I want to use 'is nul' or 'not null' in select statement of my ABAP program for any field. I have written below query but I am getting sy-subrc = 4 and getting no data. Can anyone resolve this.Hi,
I think you've posted your question on the wrong forum. This is the SAP Business One development forum which is not part of ERP and doesn't include any ABAP or Netweaver programming.
For a list of forums please see here:
http://forums.sdn.sap.com/index.jspa
Kind Regards,
Owen -
Performance issue - Select statement
Hi I am having the 10 lack records in the KONP table . If i am trying to see all the records in SE11 , it is giving the short dump TSV_TNEW_PAGE_ALLOC_FAILED . I know this is because of less memory in the Server . Is there any other way to get the data ? How to optimise the below SELECT statement if i have large data in the table .
i_condn_data - is having 8 lack records .
SELECT knumh kznep valtg valdt zterm
FROM konp
INTO TABLE i_condn_data_b
FOR ALL ENTRIES IN i_condn_data
WHERE knumh = i_condn_data-knumh
AND kschl = p_kschl.
Please suggest .Hi,
try to use "UP TO n ROWS" to control the quantity of selected data in each Loop step.
Something like this:
sort itab by itab-knumh.
flag = 'X'.
while flag = 'X'.
SELECT knumh kznep valtg valdt zterm
FROM konp
INTO TABLE i_condn_data_b UP TO one_million ROWS
WHERE knumh > new_value_for_selection
AND kschl = p_kschl.
describe table i_condn_data_b lines i.
read table i_condn_data_b index i.
new_value_for_selection = i_condn_data_b-knumh.
*....your logic for table i_condn_data_b
if one_million > i.
clear flag.
endif.
endwhile.
Regards
Maybe you are looking for
-
Hard drive died, how to get music off iPod
My hard drive died so I lost iTunes. I've reinstalled iTunes and need to move all my music from my iPod to iTunes. If I plug my iPod into my computer, will iTunes take all that music and create a new music library, or will it go the other way around
-
Can't share my subscription to new York post to husbands device
Can't share my subscription to new York post to husbands device
-
Accessing RSM information from powershell or command line
I am trying to get information on Tape Mount Counts to determine when tapes are nearing their life expectancy. I have been unable to find a way to access the information without using the Management GUI and doing a lot of clicking. I would prefer to
-
We have a few servers (different versions) that we run WMI but is always seems to use tcp 21880 according to all the MS documentation I can find it should not be using the port but all the practical information I can find says thats normal. I need t
-
HP Printer PSC 2175 Color Management
I use raw images from my Canon. In Photoshop Elements v 5, I have color management set to "optimised for printing". Tutorials on color management advise to de-activate any attempt by the printer driver to manage color. My HP printer driver (PSC 2175)