29. december 2003 - 18:38Der 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.
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
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
Synes godt om
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 <> ' ';
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:
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.