Avatar billede fredand Forsker
29. december 2003 - 18:38 Der er 4 kommentarer og
2 løsninger

Help with tricky sql-questions

Hello!

I am struggling with some additional questions at a SQL-course with Oracle and I have 2 questions that I can't get right. I shall create 2 views from a table looking like this:

tbl_persons
-----------
p_id
fname
ename
street
city
zip
email
food

food is a char Y or null

The first question looks like this:

1) Create a view called pers_stat upon tbl_persons that lists HOW MANY that has wrote down address, email, and that they will have food. (Far from every one has done that). Call the colums PERSON_ATTRIBUT and PERSON_COUNT. The values in the first column shall be "Address", "Email", "Food".  In the second column shall be listing the count of every row that has something filled in.

I guess that the result shall look something like this:
PERSON_ATTRIBUT        PERSON_COUNT
Address            187
Email            65
Food            39   
----------------------------------------------
The second question looks like this:
2) Create a view that lists the 2 or 3 last letters from the column email and that calculates the count of each extension. As last row count how many email-addresses there is totataly.

I guess that the result shall look something like this:
EXT            CNT
se            10
dk            21
com            10
org            3
Total            44

My attempt so far is (BUT I CANT MANAGE TO INCLUDE THAT LAST ROW WITH THE total):

CREATE OR REPLACE VIEW extension_count
    AS
    SELECT SUBSTR( email, INSTR(email, '.', -1, 1)+1, LENGTH(email) ) AS EXT, count(*) AS CNT FROM personer
            WHERE
                email <> NULL
            OR
                email <> ' '
            GROUP BY SUBSTR( email, INSTR(email, '.', -1, 1)+1, LENGTH(email) );
           
   
       
        SELECT count(*) AS totalt FROM extension_count_part_1
            WHERE
                email <> NULL
            OR
                email <> ' ';
               
So if any one could help me out with this it would be great.

Best regards, and Happy New Year!

Fredrik
Avatar billede teepee Nybegynder
29. december 2003 - 19:02 #1
1)
Make unions..

select 'Address', count(*) from tbl_persons where address is not null
union
select 'Email', count(*) from tbl_persons where email is not null
union
select 'Food', count(*) from tbl_persons where food is not null
Avatar billede teepee Nybegynder
29. december 2003 - 19:04 #2
2) Also union...
Avatar billede teepee Nybegynder
29. december 2003 - 19:05 #3
Then you will have to write
      SELECT 'Total', count(*) FROM extension_count_part_1
            WHERE
                email <> NULL
            OR
                email <> ' ';

to make the two selects have equal amount of columns
Avatar billede Slettet bruger
29. december 2003 - 19:07 #4
In both cases, you might use a UNION ALL:

1. SELECT 'Address', COUNT(*)
  FROM tbl_persons
  WHERE STREET || CITY || ZIP IS NOT NULL
  UNION ALL
  SELECT 'Email', COUNT(*)
  FROM tbl_persons
  WHERE EMAIL IS NOT NULL
  UNION ALL
  SELECT 'Food', COUNT(*)
  FROM tbl_persons
  WHERE FOOD IS NOT NULL

2. CREATE OR REPLACE VIEW extension_count
    AS
    SELECT SUBSTR( email, INSTR(email, '.', -1, 1)+1, LENGTH(email) ) AS EXT, count(*) AS CNT FROM personer
            WHERE
                email <> NULL
            OR
                email <> ' '
            GROUP BY SUBSTR( email, INSTR(email, '.', -1, 1)+1, LENGTH(email) )
UNION ALL
        SELECT 'Total', count(*) AS totalt FROM extension_count_part_1
            WHERE
                email <> NULL
            OR
                email <> ' ';
Avatar billede teepee Nybegynder
29. december 2003 - 19:33 #5
Maybe you can use the ROLLUP aggregate function for your second question, but it's abit difficult to explain, and I dont use it myself. Try to look at this tip here:

http://www.bijoos.com/oratom/ot_200010.htm
Avatar billede fredand Forsker
01. januar 2004 - 15:37 #6
Thanks mates!

Now I understand UNION

Happy new Year!
Fredrik
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Computerworld tilbyder specialiserede kurser i database-management

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester