I have a table with Last Names, First Names, Hours and GPA's.
How do I create a view that displays a concatenated first name and last name, the StudentID and the GPA of the students who have passed at least 90 hours.
The concatenated names should be separated with one space.
The three column headings should be FullName, StudentID and GPA.
The rows should be sorted by last names, then first names.
Please help. I am lost as to how to approach this.
Use the operator || for concatenation (so you don't have to do nested CONCAT()).
Example:
create view v as
select (firstname || ' ' || lastname) "FullName", GPA, StudentId
from table
where Hours>90
order by lastname, firstname
Related
This question already has answers here:
Using an Alias column in the where clause in Postgresql
(6 answers)
access a column aliases in the where clause in postgresql
(2 answers)
How to use new created column in where column in sql?
(2 answers)
Closed 1 year ago.
I'm getting an error querying from the column alias and don't understand why. In the example below, if I run a query from the actual column, no problem. I concatenate first_name and last_name columns into a fullname column alias and then get the output.
SELECT first_name ||' '|| last_name AS fullname
FROM actor;
Output:
Now, if I create a column alias, I get the error. In this example, I'm concatenating first_name and last_name into a fullname column alias, and then query the names between value1 and value2.
SELECT first_name ||' '|| last_name AS fullname
FROM actor;
WHERE fullname BETWEEN 'Zero Cage' AND 'Fred Costner';
Output:
Thanks in advance for your taking the time to help!
In postgres document:
An output column's name can be used to refer to the column's value in ORDER BY and GROUP BY clauses, but not in the WHERE or HAVING clauses; there you must write out the expression instead.
That's according to the SQL standard and may not be very intuitive. The (historic) reason behind this is the sequence of events in a SELECT query. WHERE and HAVING are resolved before column aliases are considered, while GROUP BY and ORDER BY happen later, after column aliases have been applied.
Also note that conflicts between input and output names are resolved differently in ORDER BY and GROUP BY - another historic oddity (with a reason behind it, but potentially confusing nonetheless).
You can use one of the below manners:
Use full both column name
SELECT first_name || ' ' || last_name AS fullname
FROM actor
WHERE first_name || ' ' || last_name BETWEEN :conditio1 AND :conditio2
Use CTE
WITH data s (
SELECT first_name || ' ' || last_name AS fullname
FROM actor
)
SELECT *
FROM data
WHERE fullname BETWEEN :conditio1 AND :conditio2
Use subquery
SELECT *
FROM (
SELECT first_name || ' ' || last_name AS fullname
FROM actor
) tmp
WHERE tmp.fullname BETWEEN :conditio1 AND :conditio2
I have two tables and need to match all records by name. The problem is that in one table name format is FirstName LastName, in another table - LastName FirstName, and I cannot split into separate columns because some records might have few first names or last names, so I don't know where first or last name ends or starts.
Eg. in first table I have John Erick Smith and need to join all records from another table where the name is Smith John Erick.
Any solution in SQL?
I think you can use string functions to get the piece of string (in 'John Erick Smith' type column) after the last space as a surname and stick it to front. Then you could compare the strings. That is assuming you don't have spaces in surnames.
Here is MSDN article on how to do it.
DECLARE #string nvarchar(max) = 'FirstName SecondName Surname'
SELECT RIGHT(#string, NULLIF(charindex(' ', REVERSE(#string)),0)) + ' ' +
REVERSE(RIGHT(REVERSE (#string), len(#string) - NULLIF(charindex(' ', REVERSE(#string)),0)))
Returns:
Surname FirstName SecondName
Verify first if you still have other tables with "FirstName" and "LastName" that you can use instead of using the field with "FirstName LastName". Normally Oracle has this kind of tables use for persons/employees. You may have something like this.
But if the "LastName FirstName" uses "," (comma) in its data then you can do a substring to get the LastName from the FirstName.
Or another alternative is by using their IDs (eg. employee IDs) [if only available].
In a table I have columns FIRST_NAME and LAST_NAME. I have to concatenate the two names and retrieve names containing more than 12 characters.
I tried the following query:
select *
from (select first_name, last_name
from customer as name
)
where length(NAME) = 12
Select *
from (
select first_name||last_name as name
from customer
)
where length(name)>12
You need only one SELECT
SELECT first_name || last_name AS name
FROM customer
WHERE LENGTH(first_name||last_name) > 12
Optionally TRIM name and last_name from spaces.
As an alternative, if you rewrite your question to check if the total length of first name and last name is more than 12, you probably get a different response from people with a more efficient code. It is not necessary to make Oracle actually concatenate the columns.
Programmers are too literal these days. :)
You are not concatenating the first name and last name.
Also no need to write an inline view, below is the query.
select FIRST_NAME || LAST_NAME as NAME
from CUSTOMER
where length(FIRST_NAME || LAST_NAME) > 12;
This may have been answered similarly somewhere but I am still kind of confused. I need to create a view named A7T7 that will display the concatenated first name and last name of the students who have at least three letter Os or three letter Ts in their concatenated name (e.g., John Olson, Richard Hooton, Tina Trout). The column heading should be Student and the rows should be sorted by the length of the concatenated names from long to short.
Not really sure how to make my WHERE statement for this restriction.
You can use a LIKE query - for example (using a table variable for SQL Server):
CREATE TABLE #students (firstname varchar(20), lastname varchar(20));
INSERT ALL
INTO #students VALUES ('John','Olson')
INTO #students VALUES ('Richard','Hooton')
INTO #students VALUES ('Tina','Trout')
SELECT 1 FROM dual;
SELECT *
FROM #students s
WHERE CONCAT(s.firstname, s.lastname) LIKE '%o%o%o%';
DROP TABLE #students;
Which roughly translates to "select all the rows from students where the concatenation of firstname and lastname contains o three times".
I was looking at this tutorial from W3Schools, where they combine address, zip code and country together as a single column "Address". So I've been playing with the Northwind DB and wanted to do the same thing for employee name:
SELECT EmployeeID, FirstName +" "+ LastName AS Name FROM Employees;
But all I get is 0's in the "Name" column. I figure SQLite has implicitly converted the string to integer, but it doesn't make sense...
Replace + with || wich is the string concatenation operator in SQLite and many other databases.
SELECT EmployeeID, FirstName || ', ' || LastName AS Name FROM Employees;