3

My query:

CREATE VIEW SOME_VIEW2 AS 
(
  SELECT to_char(tbl_albums.album), COUNT(tbl_songs.id) AS "Songs in album"
  FROM tbl_songs 
  LEFT JOIN tbl_albums 
  ON tbl_songs.album_id = tbl_albums.id
  LEFT JOIN tbl_bands
  ON tbl_albums.band_id = tbl_bands.id
  WHERE to_char(LOWER(TRIM(tbl_bands.band))) = 'metallica'
  GROUP BY to_char(tbl_albums.album)
);

Error I got:

Error at Command Line:10 Column:12 Error report: SQL Error: ORA-00998: must name this expression with a column alias 00998. 00000 - "must name this expression with a column alias"

This doesn't work, however I previously tried this:

CREATE VIEW SOME_VIEW AS 
(
  SELECT * FROM tbl_albums
);

And this worked fine, want to ask, what is wrong with first query.

2
  • 4
    Give an alias to the column to_char(tbl_albums.album) in your select list.
    – Taryn
    Oct 28, 2013 at 19:57
  • 1
    There is absolutely no need for the parentheses around the actual select statement.
    – user330315
    Oct 29, 2013 at 9:13

2 Answers 2

10

All columns must be named in a view. Since you have a calculated column, i.e.

to_char(tbl_albums.album)

You need to give it a name, like so:

to_char(tbl_albums.album) "Album"
0

try this query :

CREATE VIEW SOME_VIEW2 (album , SongsInAlbum) AS 
(
  SELECT q.alb,q.counter FROM
  (SELECT to_char(tbl_albums.album) as alb, rownum as counter
  FROM tbl_songs 
  LEFT JOIN tbl_albums 
  ON tbl_songs.album_id = tbl_albums.id
  LEFT JOIN tbl_bands
  ON tbl_albums.band_id = tbl_bands.id
  WHERE to_char(LOWER(TRIM(tbl_bands.band))) = 'metallica')q
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.