I Have A Table Like This.

Cityno Cityname Mails
1 Bangalore 8km
2 Hsr Layout 20km
3 Mejistic 30km
4 Jayadeva 55km
5 Itpl 80km
6 Hebbal 115km

I Have Data Like This

I Want O/p Like This
Distance No.ofcity
0-50km 3
51-100km 2
101-150km 4

And So On




Answers were Sorted based on User's Feedback



Answer / prativa mishra

select range distance,count(range) no_of_city
from
(SELECT cityname,mails,
CASE WHEN MAILS BETWEEN 0 AND 50 THEN '0-50'
WHEN MAILS BETWEEN 51 AND 100 THEN '51-100'
ELSE '101-150'
END RANGE
FROM CITY1)
group by range

Is This Answer Correct ?    20 Yes 2 No

Answer / janani

select '0-50' distance, count(*) No.ofcity from table
where mails between 0 and 50
union
select '51-100' distance, count(*) No.ofcity from table
where mails between 51 and 100
union
select '101-150' distance, count(*) No.ofcity from table
where mails between 101 and 150

Is This Answer Correct ?    5 Yes 1 No

Answer / anil

data abc;
set xyz;
count=1;
length Distance$ 10;
if mails<=50 then Distance='0-50';
else if 50 < mails <= 100 then Distance='51-100';
else if 100<mails <= 150 then Distance='101-150';
run;
proc print;
run;

proc sql;
select Distance,sum(count)as NO_OFCITY
from abc
group by Distance;
quit;

Is This Answer Correct ?    1 Yes 0 No




Answer / js

SELECT R,COUNT(R) FROM
(
SELECT cityname,mail,
CASE WHEN SUBSTR(MAIL,1,INSTR(MAIL,'KM' )-1) BETWEEN 0 AND 50 THEN '0-50'
WHEN SUBSTR(MAIL,1,INSTR(MAIL,'KM' )-1) BETWEEN 51 AND 100 THEN '51-100'
ELSE '101-150' END R FROM CITY) GROUP BY R;

Is This Answer Correct ?    1 Yes 0 No

Answer / kavitha nedigunta

SELECT DISTANCE,COUNT(*) NO_OFCITY
from
(SELECT (CASE WHEN TO_NUMBER(RTRIM(MAILS,'KM')) BETWEEN 0
AND 50 THEN '0-50'
WHEN TO_NUMBER(RTRIM(MAILS,'KM')) BETWEEN 51 AND
100 THEN '51-100'
ELSE '101-150' END) DISTANCE
FROM TEST001) A
GROUP BY DISTANCE
order by to_number(replace(DISTANCE,'-',''))

Is This Answer Correct ?    0 Yes 0 No

Answer / shriram2012

SQL> select * from quest;

Cityno cityname mails
---------- -------------------- ----------
1 bangalore 8km
2 hsr layout 20km
3 mejistic 30km
4 jayadeva 55km
5 itpl 80km
6 hebbal 115km

6 rows selected.

SQL> with data as (
2 select level as lvl,lag(level,1,0) over(order by level) as pre_lvl
3 from dual
4 where mod(level,50) = 0
5 connect by level <=150)
6 select count(cityno),pre_lvl||'-'||lvl
7 from quest
8 ,data
9 where to_number(replace(mails,'KM',0))/10 between pre_lvl and lvl
10 group by lvl,pre_lvl;

Is This Answer Correct ?    0 Yes 0 No

Post New Answer



More SQL PLSQL Interview Questions

how to create a database in oracle?please gve anser with example

1 Answers  


what is difference between procedure and function, procedure and trigger?

8 Answers   iFlex,


How will we see framework of a table?

2 Answers   Accenture,


What is pragma exception and how, when, where us

5 Answers  


What is RAC in oracle?

7 Answers  






How would you go about increasing the buffer cache hit ratio? 0. Explain the difference between a hot backup and a cold backup and the benefits associated with each 1. You have just had to restore from backup and do not have any control files. How would you go about bringing up this database? 2. How do you switch from an init.ora file to a spfile? 3. Explain the difference between a data block, an extent and a segment. 4. Give two examples of how you might determine the structure of the table DEPT. 5. Where would you look for errors from the database engine? 6. Compare and contrast TRUNCATE and DELETE for a table. 7. Give the reasoning behind using an index. 8. Give the two types of tables involved in producing a star schema and the type of data they hold. 9. What type of index should you use on a fact table? 10. Give two examples of referential integrity constraints. 11. A table is classified as a parent table and you want to drop and re-create it. How would you do this without affecting the children tables? 12. Explain the difference between ARCHIVELOG mode and NOARCHIVELOG mode and the benefits and disadvantages to each. 13. What command would you use to create a backup control file? 14. Give the stages of instance startup to a usable state where normal users may access it. 15. What column differentiates the V$ views to the GV$ views and how? 16. How would you go about generating an EXPLAIN plan?

2 Answers  


Use The Implicit cursor to Query The Department table information Where Deptno is 30.check,if no record was found then print "Record Was Not Found".Else Print Deptname And Ename.Dept table Have Dname Varchar2(20),Deptno Number,EnameVarchar2(20).Please Answer In 2 mins,with in Maximum 15 lines.

5 Answers   Wipro,


What is Referential Integrity?

3 Answers   IBM,


What is Primary Key?

4 Answers   Ramco,


Give an example of any procedure.

5 Answers   Wipro, Accenture, iFlex,


how to insert the records in particular position

1 Answers  


types of exceptions and what is meant by pragma autonomous_transaction ?what is the use.

4 Answers   3i Infotech,






Categories