How to form a query for this requirment
Hi Friends,
I have a database table in which I store the employee data along with his phone number. Now this row of data can repeat for different phone number depending on if it is office phone or home phone or cell phone.
But in the output of query, I need name of empoyee and other three columns namely "Home Phone", "Office Phone" and "Cell Phone".
If employee has three rows for each kind of phone, then in the result of query all three columns for phone numbers should be filled, otherwise as many columns should be filled with data as different phone numbers employee has.
Can any one please post SQL query for this scenario ?
Thanks in Adavance
I cannot imagine, that second and third query are
under any circumstances faster as first one, but i
can imagine that they are slower You stated that query without the inline view can only be faster. Did you check on optimizer plans or use tkprof to verify your claim?
A quick check on those two selects
SELECT object_id, object_type, object_name
FROM user_objects;
SELECT object_id, object_type, object_name
FROM (SELECT object_id, object_type, object_name
FROM user_objects)
;didn't show any changes in the explain plan unter 10g1.
C.
Similar Messages
-
How to write select query for this
Hi,
I had a html form and in the for i had drop down box and it needs to select multiple values from the drop down box. When i select multiple values then i have to write the SQL select statement query .
When i try to write the select statement and trying to run i am getting error.
select * from Table
where emo_no = '1,2,3'
this is how i write query please suggest me how to write query for selecting multiple values from the drop down box.
Thanksselect * from Table
where emo_no in ( 1,2,3)
for integer values
select * from Table
where emo_no in ('1','2','3')
for characters
If we talk about large scale applications that may have millions of records, I would suggest this.
declare @t table (v int)
insert into t (v) values (1)
insert into t (v) valves (2)
insert into t (v) values (3)
select *
from table
inner join @t t on table.emo_no = t.v
Using "in" for a where clause is not so bad for filtering on a few values, but if you are filtering a lot of rows and a lot of values (emo_no) the performance degrades quickly for some reasons beyond the scope of this.
This is just one solution, I'll through this out as well, instead of an in memory (@t) table, doing a disk based temp table (#t) and creating an index on the column "v".
create table #t (v int)
insert into #t (v) values (1)
insert into #t (v) valves (2)
insert into #t (v) values (3)
create index ix_t on #t (v)
select *
from table
inner join #t t on table.emo_no = t.v
drop table #t
Pardon any syntax errors and careful using a drop statement.
Sometimes in memory tables work better than disk temp tables, it takes some testing and trial and error depending on your datasets to determine the best solution.
Probably too much info ;-)
Byron Mann
[email protected]
[email protected]
Software Architect
hosting.com | hostmysite.com
http://www.hostmysite.com/?utm_source=bb -
How to use dynamic query for this ??
hi , i am new to ABAP. i got a requirement to write dynamic query for the following code.
kindly address. two set of queries are same.but condition is different.
.IF p_psd EQ ' '.
*C--End of change DF 1232137- (Transport # :CIDK980530 )
*C--FETCH THE Deliverd Quantiity and Material Number
SELECT aufnr "ORDER number
wemng "Quantity of goods received for the order item
matnr "MATERIAL NUMBER
pwerk "PLANT
dauat "Order Type
FROM afpo
INTO TABLE t_afpo
WHERE aufnr IN s_order
AND wemng IN s_dqt
AND matnr IN s_matnr
AND pwerk IN s_plant
AND dauat = c_ro.
IF sy-subrc = 0.
SORT t_afpo BY aufnr matnr pwerk.
*C--FETCH THE OBJECT NUMBER
SELECT aufnr "ORDER number
objnr "Object number
FROM aufk
INTO TABLE t_aufk
FOR ALL ENTRIES IN t_afpo
WHERE aufnr = t_afpo-aufnr.
IF sy-subrc = 0.
SORT t_aufk BY aufnr objnr.
*C--FETCH THE Target Quantiity
SELECT aufnr "ORDER number
gamng "Total order quantity target quantity
FROM afko
INTO TABLE t_afko
FOR ALL ENTRIES IN t_afpo
WHERE aufnr = t_afpo-aufnr
AND gamng IN s_tqt.
IF sy-subrc = 0.
SORT t_afko BY aufnr .
ENDIF.
ENDIF.
ELSE.
MESSAGE text-e03 TYPE c_s. " No data for the selection criteria
LEAVE LIST-PROCESSING.
ENDIF.
*C--Begin of change DF 1232137- (Transport # :CIDK980530 )
ENDIF.
IF p_psd EQ c_x.
SELECT aufnr "ORDER number
wemng "Quantity of goods received for the order item
matnr "MATERIAL NUMBER
pwerk "PLANT
dauat "Order Type
FROM afpo
INTO TABLE t_afpo
WHERE aufnr IN s_order
AND wemng > 0
AND matnr IN s_matnr
AND pwerk IN s_plant
AND dauat = c_ro.
if sy-subrc = 0.
*C--FETCH THE OBJECT NUMBER
IF NOT t_afpo[] IS INITIAL.
SORT t_afpo BY aufnr matnr pwerk.
SELECT aufnr "ORDER number
objnr "Object number
FROM aufk
INTO TABLE t_aufk
FOR ALL ENTRIES IN t_afpo
WHERE aufnr = t_afpo-aufnr.
IF sy-subrc = 0.
SORT t_afko BY aufnr gamng.
ELSE.
MESSAGE text-e03 TYPE c_s. " No data for the selection criteria
LEAVE LIST-PROCESSING.
ENDIF.
ENDIF.
IF NOT t_afpo[] IS INITIAL.
*C--FETCH THE Target Quantiity
SELECT aufnr "ORDER number
gamng "Total order quantity target quantity
FROM afko
INTO TABLE t_afko
FOR ALL ENTRIES IN t_afpo
WHERE aufnr = t_afpo-aufnr
AND gamng <> t_afpo-wemng .
IF sy-subrc = 0.
SORT t_afko BY aufnr gamng.
ELSE.
MESSAGE text-e03 TYPE c_s. " No data for the selection criteria
LEAVE LIST-PROCESSING.
ENDIF.
ENDIF.
ELSE.
MESSAGE text-e03 TYPE c_s. " No data for the selection criteria
LEAVE LIST-PROCESSING.
ENDIF.
Edited by: Thomas Zloch on Jan 5, 2011 1:30 PM please use code tagsHi friend,
Try using MACRO and dynamic WHERE condition.
Group simialr Select statements under a Macro.
Build a dynamic where by checking conditions
Call macro passing dynamic where condition.
TABLES afpo.
DATA: str TYPE string.
*Macro definition
DEFINE operation.
select single *
from afpo into afpo
where (&1). " Dynamic condition
END-OF-DEFINITION.
*Build dynamic WHERE by checking some conditions
*If conditon
CONCATENATE 'AUFNR = ''000000700008''' 'AND POSNR = ''0001''' INTO str SEPARATED BY space.
*Else
CONCATENATE 'AUFNR = ''000000700008''' 'AND POSNR = ''0002''' INTO str SEPARATED BY space.
*Endif.
*Call Macro passing dynamic WHERE condition
operation str. -
How to prepare a query for this validation
I am supposed to implement the below validation using query:
retrieve emp_no from employee table where emp_desgn='MANAGER' and emp_status='OFFSHORE'
1. if the emp_no exists display emp_first_name,emp_last_name,emp_remarks.
2. if the emp_no doesn't exists then display emp_first_name, emp_last_name as blanks and emp_remarks as "EMPLOYEE not available in OFFSHORE'.
tablename: employee
can any one please give me the query with this condition. the validation should be implemented in queries and not using functions or procedures.i have tried with this
select emp_no,(
case emp_no
when null then emp_first_name
else 'SPACE'
end) empfirst_name,
(case emp_no
when null then emp_last_name
else 'SPACE '
end
) emplast_name,
(case emp_no
when null then emp_remarks
else 'EMPLOYEE NOT AVAILABLE IN OFFSHORE'
end
) empremarks
from employee
where emp_design='MANAGER' and emp_status='OFFSHORE'
here emp_no is PK so the emp_no in case statement will execute only if the where condition satisifies. but i am looking the results for the below data.
EMP_NO EMP_FIRST_NAME EMP_LAST_NAME EMP_DESIGN EMP_STATUS EMP_REMARKS
1 john Andrey SRDEVELOPER ONSITE DOING GOOD
2 Peter Andrey MANAGER ONSITE DOING GOOD
3 Samuel Andrey MANAGER OFFSHORE DOING GOOD
4 Joseph Andrey DELIVERYHEAD OFFSHORE DOING GOOD
5 Paul Andrey SRARCHITECT OFFSHORE DOING GOOD
i am expecting the results like for emp_no 3 it should display firsname, last name as blank spaces and remarks should be "EMPLOYEE Not availabe in OFFSHORE' but for all it should show the firstnname,lastname,remarks. -
How to write a query for this data??
In this table i have column dob which is DATE type,for example 12-jan-89 I want output like this
Year Day
12-jan-89 Thusrs_day
12-jan-90 Friday
upto
12-jan-14 sundaykrishnagopi wrote:
No not like that ,I want up to 2014 year
Ok, so lets say you have table like this
SQL> create table my_table(dob date);
Table created.
SQL> insert into my_table (dob) values( to_date('12-jan-1989', 'dd-mon-yyyy'));
1 row created.
SQL> commit;
Commit complete.
SQL> select * from my_table;
DOB
12-JAN-89
You can write a query like this
select dob + (level -1) dob
, to_char(dob + (level -1), 'fmDay') day_
from my_table
connect
by level <= (dob + interval '25' year) - dob + 1; -
How to write a query for this output
I have a string ' if i know good acting , I am a hero '
now if 'hero' is present in the string it will return ' i am hero' else
'you are hero'
How to write a sql query to return the same .SQL> select (case
2 when '&a' like '%hero%' then 'I am hero'
3 ELSE 'u r hero'
4 end) output from dual;
Enter value for a: if i know good acting , I am a hero
old 2: when '&a' like '%hero%' then 'I am hero'
new 2: when 'if i know good acting , I am a hero ' like '%hero%' then 'I am hero'
OUTPUT
I am hero
SQL> /
Enter value for a: jkhkh
old 2: when '&a' like '%hero%' then 'I am hero'
new 2: when 'jkhkh' like '%hero%' then 'I am hero'
OUTPUT
u r hero
Hope this helps. -
Give the query for this requirement can we get it in correlated sub query
i have to query avg(someinfo) group by dayofthemonth for last three days
select someinfo from
(select avg(someinfo)from table tab
group by dayofdate1
where exists(select * from table
having max(date1)-tab.date1<3 )
how can we group day of the date1
someinfo date1
10 28-jun-2006
20 28-jun-2006
30 29-jun-2006
30 29-jun-2006
10 30-jun-2006
20 30-jun-2006
30 1-july-2006
40 1-july-2006
30 2-july-2006
40 2-july-2006
i have to query avg(someinfo) group by day for last three days
avg(someinfo) date1
35 2-jun-2006
35 1-july-2006
15 30-june-2006
here no need of union
Message was edited by:
user517983
Message was edited by:
user517983
Message was edited by:
user517983
Message was edited by:
user517983SQL> CREATE TABLE t_table
2 AS
3 (SELECT 10 day ,'28-jun-2006' ft FROM dual
4 UNION
5 SELECT 20,'28-jun-2006' FROM dual
6 UNION
7 SELECT 30,'29-jun-2006' FROM dual
8 UNION
9 SELECT 30,'29-jun-2006' FROM dual
10 UNION
11 SELECT 10,'30-jun-2006' FROM dual
12 UNION
13 SELECT 20,'30-jun-2006' FROM dual
14 UNION
15 SELECT 30,'1-jul-2006' FROM dual
16 UNION
17 SELECT 40,'1-jul-2006' FROM dual
18 UNION
19 SELECT 30,'2-jul-2006' FROM dual
20 UNION
21 SELECT 40,'2-jul-2006' FROM dual)
22 .
SQL> /
Table created.
SQL> DESC t_table
Name Null? Type
DAY NUMBER
FT VARCHAR2(11)
SQL> SELECT * FROM t_table;
DAY FT
10 28-jun-2006
10 30-jun-2006
20 28-jun-2006
20 30-jun-2006
30 1-jul-2006
30 2-jul-2006
30 29-jun-2006
40 1-jul-2006
40 2-jul-2006
9 rows selected.
SQL> SELECT day,ft FROM (SELECT AVG(day) day,ft FROM t_table GROUP BY ft ORDER BY ft DESC)
2 WHERE ROWNUM<=3;
DAY FT
15 30-jun-2006
30 29-jun-2006
15 28-jun-2006Khurram -
What can be the SQL Query for this requirement ?
Hi,
I have a table with fields like this:
ID DESC PARENT
01 ABC 02
02 ABC1 01
03 ABC2 01
04 ABC4 02
In the above table PARENT column refers to ID column , but actually in SQL query i want to have ID, DESC , and PARENTDESC (i.e., desc value of the corresponding ID)
Actual output that i need is
select ID , DESC , ?? from table where ID=someValue . Now if i provide ID=01 then output should be like this:
ID DESC PARDESC
01 ABC ABC1
Can anyone help what can be the required sql query ?
Edited by: bootstrap on Apr 29, 2011 6:15 AMSELECT T1.ID, T1.DESC, T2.DESC
FROM TABLEA T1, TABLEA T2
WHERE T1.ID='01'
AND T2.ID = T1.PARENT; -
How to use Start Routine for this requirement
Hello,
I am having data in 2 ods and should load that data into cube, but there isn't any common object in both the ods's. I need to analyze based on these ods objects. so I thought of pulling some objects into 1ods which are used for analysis and added some objects to ods and cube. Now I have to write a start routine in update rules to fetch the data from the info objects which are present in another ods. Can any one help me in this?
Rajesh.Hi Surya,
Extract all info from table BKPF
select * into it_bkpf
from bkpf
where gjahr = p_fisyr.
Select record based on internal table it_BKPF
The reason why FOR ALL ENTRIES syntax is used because of its cluster table.
select * into it_bseg
from bseg
for all entries in it_bkpf
where bukrs = it_bkpf-bukrs
and belnr = it_bkpf-belnr.
Delete duplicate record based on Company Code and GL No.
delete adjacent duplicates from it_bseg
comparing bukrs belnr. -
I have 2 tables
emp_master (emp_id, emp_name)
dept_master (dept_id, dept_name, emp_id (references emp_id in emp_master)Now there are 30 departments and each department can have maximum 5 employees.
Now I want a query that would list a department and all its employees in one line and if there are less than 5 employees it would show null for the rest of columns.
Dept_id emp_1 emp_2 emp_3 emp_4 emp_5
1 1 2 -- -- --
2 3 4 5 6 7
3 8 -- -- -- --
Please help me out
Thanks...One solution may be
Connected to Oracle Database 10g Express Edition Release 10.2.0.1.0
Connected as hr
SQL>
SQL> SELECT e.department_id, e.employee_id
2 FROM employees e
3 WHERE e.department_id IN (10, 20, 40, 60, 90)
4 /
DEPARTMENT_ID EMPLOYEE_ID
90 100
90 101
90 102
60 103
60 104
60 105
60 106
60 107
10 200
20 201
20 202
40 203
12 rows selected
SQL>
SQL> SELECT ee.department_id, ee.emp_1, ee.emp_2, ee.emp_3, ee.emp_4, ee.emp_5
2 FROM (SELECT MAX(CASE
3 WHEN MOD(r, 5) = 1 THEN
4 e.employee_id
5 ELSE
6 NULL
7 END) emp_1,
8 MAX(CASE
9 WHEN MOD(r, 5) = 2 THEN
10 e.employee_id
11 ELSE
12 NULL
13 END) emp_2,
14 MAX(CASE
15 WHEN MOD(r, 5) = 3 THEN
16 e.employee_id
17 ELSE
18 NULL
19 END) emp_3,
20 MAX(CASE
21 WHEN MOD(r, 5) = 4 THEN
22 e.employee_id
23 ELSE
24 NULL
25 END) emp_4,
26 MAX(CASE
27 WHEN MOD(r, 5) = 0 THEN
28 e.employee_id
29 ELSE
30 NULL
31 END) emp_5,
32 e.department_id
33 FROM (SELECT row_number() over(PARTITION BY department_id ORDER BY employee_id) r,
34 department_id,
35 employee_id
36 FROM employees
37 WHERE department_id IN (10, 20, 40, 60, 90)) e
38 GROUP BY e.department_id) ee
39 /
DEPARTMENT_ID EMP_1 EMP_2 EMP_3 EMP_4 EMP_5
10 200
20 201 202
40 203
60 103 104 105 106 107
90 100 101 102
SQL> Regards... -
How to write a query for this situation
Hi ,
Can any one help me to write a query.I have a table with three columns like char_id_1,char_id_2,char_id_3,these data have to insert into some other table but in the target table the data should be comma seperated like (10,8,2),here blank space indicates null.In source table the columns are numbers,but in target table it is varchar2.
source table:
CHAR_ID_1 CHAR_ID_2 CHAR_ID_3
10 8 2
7 1 5
4 11
6 1
2 8
6 12 7
Target table:
CHAR_IDS
10,8,2
7,1,5
4,11
6,1
2,8
6,12,7I started doing it to_char way, and thought of some different version, and ended up with an ugly solution which works only if you have three columns. Thought of not posting, but posting it anyway:
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options
SQL> with t as(
2 select 10 c1,8 c2,2 c3 from dual union all
3 select 7,1,5 from dual union all
4 select 4,null,11 from dual union all
5 select null,6,1 from dual union all
6 select 2,8,null from dual union all
7 select null,8,null from dual union all
8 select null,null,10 from dual union all
9 select 2,null,null from dual union all
10 select null,null,null from dual union all
11 select 6,12,7 from dual )
12 --
13 select nvl2(c1,
14 c1 || nvl2(c2,',' || c2 || nvl2(c3,',' || c3,null),nvl2(c3,',' || c3,null)),
15 nvl2(c2, c2 || nvl2(c3,','|| c3,null),nvl2(c3,c3,null))) col_concat
16 from t;
COL_CONCAT
10,8,2
7,1,5
4,11
6,1
2,8
8
10
2
6,12,7
10 rows selected.
SQL> -
How to write select query for below requirement
Hi sir,
i have a table x have one column y which containing value like below
Y
a
b
c
d
I want out put like below kindly help me:
Y
a
d
c
bHi ,
Please check:
select y from (
select case when y='a' then 1 else 2 end no, y from table_x
) order by no asc, y desc
with table_x as(
select 'a' y
from dual
union
select 'b' y
from dual
union
select 'c' y
from dual
union
select 'd' y
from dual
select y from (
select case when y='a' then 1 else 2 end no, y from table_x
) order by no asc, y desc
Thank you -
How to write complex sql for this
Hi ALL,
I have a requirement like this
I have 5 tables which i have to join to get the result
but there no join column to 2 other table.
I want to get all the applications using cobal,running on UNIX.
How to write the query for this
1.APP
APP_i DESC
1 Accounts
2 Payments
3 order transfer
4 Order processing
2.Techgy
techid techdesc
1 cobal
2 Java
3.APP_Techgy
APP_I Techid
1 1
2 1
3 1
4 2
4.Pltfrm
pltfmid pltfrmdesc
1 Windows NT
2 UNIX
5.APP_Pltfrm
APP_I pltfrmid
1 1
2 1
3 2
4 2
ouput must be
APP_i Desc techDESC pltfrmdesc
3 ordertranfer Cobal UNIX
Thanks in advanceThis ('descr' in place of 'desc')?
SQL> select a.app_i, a.descr, t.techdesc, p.pltfrmdesc
from app_techgy atc,
app a,
techgy t,
app_pltfrm ap,
pltfrm p
where atc.techid = t.techid
and atc.app_i = a.app_i
and atc.app_i = ap.app_i
and ap.pltfrmid = p.pltfmid
order by a.app_i
APP_I DESCR TECHDESC PLTFRMDESC
1 accounts cobal windows nt
2 payments cobal windows nt
3 order transfer cobal unix
4 order processing java unix
4 rows selected. -
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 query for this in TopLink ?
I am doing a simple search in jsp where the search will the based on the choices chosen by user.
I had given 3 check boxes for those choices.
The problem is, query will be based on the choice or choices chosed by the user.
How to write query for this in TopLink ?
Thanks in Advance..
JayaganeshTry below solution, it is NOT best solution but might work:
Declare @Questions TABLE (QuestionID INT, QuestionText Varchar(100))
INSERT INTO @Questions
VALUES (1, 'Comment'), (2, 'Score')
DECLARE @Answers TABLE (authkey INT, QuestionID INT, questiontext VARCHAR(100), answertext VARCHAR(100))
INSERT INTO @Answers
VALUES (101, 1, 'comment', 'hi!!'), (101, 2, 'score', '4'), (102, 1, 'comment', 'excellent'), (102, 2, 'score', '5'), (103, 2, 'score', '6'), (104, 2, 'score', '8')
SELECT
A.AuthKey
,Q.QuestionID
,Q.QuestionText
,A.AnswerText
FROM
@Questions Q
INNER JOIN @Answers A ON Q.QuestionID = A.QuestionID
UNION
SELECT
A.AuthKey
,Q.QuestionID
,Q.QuestionText
,Null
FROM
@Questions Q
CROSS JOIN @Answers A
WHERE
NOT EXISTS (SELECT 1 FROM @Answers SubQry WHERE SubQry.AuthKey = A.AuthKey AND SubQry.QuestionID = Q.QuestionID)
Output
AuthKey | QuestionID
| QuestionText
| AnswerText
101 | 1 | Comment | hi!!
101 | 2 | Score | 4
102 | 1 | Comment | excellent
102 | 2 | Score | 5
103 | 1 | Comment | NULL
103 | 2 | Score | 6
104 | 1 | Comment | NULL
104 | 2 | Score | 8
Best Wishes, Arbi; Please vote if you find this posting was helpful or Mark it as answered.
Maybe you are looking for
-
Error while testing SAP Screen Personas Service!
Hi, I am getting an error message "No system available for your user.Please contact your IT team". While testing the service "mainapp" under services SAP->bc->bsp->persos->mainapp. Please reply asap.. Thanks in advance.. Regards, Chandrababu Katta
-
My screen died!! Help!
Last night I was using my unplugged MacBook pro, I was monitoring the battery left and when it got to 22%, the laptop turned itself off. (is that normal?) I rebooted it but the screen hasn't recovered, I've restarted a few times and I hear the engine
-
How can I get Hebrew to read RTL in Illustrator CC?
I already changed the language preference to English/Hebrew in CC. I was able to get it to work in Indesign by changing the setting in the paragraph dialogue box to 'Adobe World-Ready Paragraph Composer, but the same option is not available in Illust
-
Hi just downloaded itunes 10.5.1.42 on windows vista, since then cannot connect to itunes store and cannot update iphone. Did try to uninstall itunes and to reinstall but no changes still the same issue. Also tried to install older version of Itunes.
-
New fright condition type required
Hi My client requested to create new condition type for fright which should be caliculated with percentage of (Basic + ED) how to create the conditon type