POSTGRES SELECT AS - postgresql

I am joining two tables house and tower, both have some of the same column names such as id, created_at, deleted, address etc. I wonder if it is possible to return the columns in the following fashion: house.created_at, house.id, tower.created_at, tower.id etc. I know I can query with AS, I was wondering if it is possible to query something like this: SELECT house.* AS house, tower.* AS tower. I tried it like this, but it was not valid SQL. Any idea how I can chase the column names prefix easily ?

Related

How to query an ampersand symbol in Postgres

I have a Postgres table that has names and addresses. Some of these name fields are both names of a couple -- for example, "John & Jane".
I am trying to write a query that pulls out only those rows where this is the case.
When I run this query, it selects 0 rows even though I know that they exist in the table:
SELECT count(*) FROM name_list where namefirst LIKE '%&%';
Does anyone know how to address this?

PostgreSQL: Address matching using fuzzymatch from two tables

What I want to do;
I have two tables with two address columns , both stored as text I want to create a view returning the matching rows.
What I've tried;
I've created and index on both columns and tables as below;
CREATE INDEX idx_table1_fulladdress ON table1 (LOWER(fulladdress_ppd));
Then run the following;
CREATE OR REPLACE VIEW view_adresscheck AS
SELECT
--from table1
table1.postcode,
table1.fulladdress_ppd,
--from table2
table2.epc_postcode,
table2.fulladdress_epc
FROM
table1,
table2
WHERE
table1.postcode = table2.epc_postcode
AND
table2.fulladdress_epc = table1.fulladdress_ppd ::text;
What hasn't worked
The above returned fewer records than I know to be there. On inspection this is because the address format is not consistent between the two tables ie.
table1.fulladdress_ppd = Flat 2d The building the street
table2.fulladdress_epc = Flat 2/d The building the street, the town
The address isn't consistently formatted within the table either ie in table not all addresses include town so I can't use regex or trim to bulk clean.
I've then seen the fuzzystrmatch module in postgres and this sounds like it might resolve my problem.
Question
Which of Soundex, Levenshtein, Metaphone is most appropriate. Most records are in English by some place names are Gaelic running on 9.6.
talking from experience of matching address from different sources. What you could do is index each address. Regardless of formatting the above address would return the same number. You then match on these indexes.
eg in the UK you have what are called UDPRN numbers for each postal address in the country.

Create a query to select two columns; (Company, No. of Films) from the database

I have created a database as part of university assignment and I have hit a snag with the question in the title.
More likely I am being asked to find out how many films each company has made. Which suggests to me a group by query. But I have no idea where to begin. It is only a two mark question but the syntax is not clicking in my head.
My schema is:
CREATE TABLE Movie
(movieID CHAR(3) ,
title CHAR(36),
year NUMBER,
company CHAR(50),
totalNoms NUMBER,
awardsWon NUMBER,
DVDPrice NUMBER(5,2),
discountPrice NUMBER(5,2))
There are other tables but at first glance I don't think they are relevant to this question.
I am using sqlplus10
The answer you need comes from three basic SQL concepts, I'll step through them with you. If you need more assistance to create an answer from these hints, let me know and I can try to keep guiding you.
Group By
As you mentioned, SQL offers a GROUP BY function that can help you.
A SQL Query utilizing GROUP BY would look like the following.
SELECT list, fields, aggregate(value)
FROM tablename
--WHERE goes here, if you need to restrict your result set
GROUP BY list, fields
a GROUP BY query can only return fields listed in the group by statement, or aggregate functions acting on each group.
Aggregate Functions
Your homework question also needs an Aggregate function called Count. This is used to count the results returned. A simple query like the following returns the count of all records returned.
SELECT Count(*)
FROM tablename
The two can be combined, allowing you to get the Count of each group in the following way.
SELECT list, fields, count(*)
FROM tablename
GROUP BY list, fields
Column Aliases
Another answer also tried to introduce you to SQL column aliases, but they did not use SQLPLUS syntax.
SELECT Count(*) as count
...
SQLPLUS column alias syntax is shown below.
SELECT Count(*) "count"
...
I'm not going to provide you the SQL, but instead a way to think about it.
What you want to do is select where the company matches and count the total rows returned. That count is the number of films made by the specified company.
Hope that points you in the right direction.
Select company, count(*) AS count
from Movie
group by company
select * group by company won't work in Oracle.

nested sql statement using and

how to make this work in mysql?
select ID,COMPANY_NAME,contact1, SUBURB, CATEGORY, PHONE from Victoria where (city in ( select suburb from allsuburbs)) and CATEGORY='Banks'
this below statement is working:
select ID,COMPANY_NAME,contact1, SUBURB, CATEGORY, PHONE from Victoria where city in ( select suburb from allsuburbs)
if I add "and" , it gives me an empty resultset,
thanks
Learn how joins work.
select
v.ID,v.COMPANY_NAME,v.contact1,v.SUBURB,v.CATEGORY,v.PHONE
from
Victoria v
inner join allsuburbs s on s.suburb = v.city
where
v.CATEGORY='Banks'
Apart from that, your query does not make a whole lot of sense.
Your table is namend Victoria, but it contains a field named city?! Do your other cities have their own table too?
You have a table named allsuburbs, but your criterion is that Victoria.city equals allsuburbs.suburb, even though a field named Victoria.suburb exists?! What's Victoria.suburb for, then?
Your table is named allsuburbs. Do you have another table that contains suburbs or is this your only one? If it is your only one, the name is redundant.
You have a field contact1. Do you have contact2...contact10 as well? Bad database design.
Why is half of your fieldnames in caps, and not all of them (or none of them)?
Oh, and the usual format for SQL is: SQL keywords in caps, the field names etc. in mixed case/lower case. Much easier to read.
I think you might have misplaced a parentheses?
.. PHONE from Victoria where
(city in ( select suburb from allsuburbs)) and CATEGORY='Banks'
I'm guessing should be:
.. PHONE from Victoria where
city in ( select suburb from allsuburbs) and CATEGORY='Banks'
Not sure if that makes more sense, but the first case is not an ok SQL-statement I believe.

How can I write a SQL select statement to include a lookup from another table?

I'm copying data from one database to another and massaging the data while I'm at it. Both databases have tables called Clients and Jobs.
However, in database "Alpha" the Jobs table does not have a relationship to the Clients table, where database "Epsilon" does. Alpha's Jobs table just has the Clients name in an nvarchar column.
I need a select statement to lookup the Client's ID in the Client table by their name while I am inserting it into the Jobs table in Epsilon.
My unfinished SQL statement looks like this:
insert into Epsilon.dbo.Jobs (ClientId, Name, Location, DateCreated)
select ????, Name, Location, DateCreated from Alpha.dbo.Jobs
How can I modify this so that the ???? contains the ClientId from the Clients table in Epsilon? I know I need to lookup the data using the Name column in Jobs, but I can't figure out the syntax for this.
What you need is a join. Joins, contrary to what pretty much everybody thinks when starting out, don't require defined relationships in the schema of the database. They just require that the two columns you're comparing have the same type (edit see comments).
The question is which join do you want. Because there isn't a relationship defined, there may be clients that have jobs and clients that don't, and jobs that have clients and jobs that don't.
I'm assuming that you want all JOBS that exist, and where a ClientId matches the CLIENTS table bring in the ClientId, and where that relationship doesn't exist to leave the ClientId null. We can do this with a LEFT JOIN. Jobs LEFT JOIN Clients will bring in all records on the LEFT, even where the relationship defined with Clients on the right doesn't exist. We could reverse the two and do a RIGHT JOIN, but that's not what people usually do. I'll leave it to you to read up on other types of joins and how they work.
So your select statement would look like:
select ClientId, Name, Location, DateCreated
from Alpha.dbo.Jobs as J LEFT JOIN
Alpha.dbo.Clients as C ON j.ClientName = c.ClientName
If Jobs.ClientName is not the same data type as c.ClientName, you can edit the schema before running the query to bring them in line with each other.
insert into Epsilon.dbo.Jobs (ClientId, Name, Location, DateCreated)
select c.ClientID, a.Name, a.Location, a.DateCreated from Alpha.dbo.Jobs a
join Epsilon.dbo.Client c on c.Name = a.ClientName
This is a pretty optimistic join, but even if it needs to be modified this should give you the general idea.
insert into Epsilon.dbo.Jobs
(ClientId, Name, Location,
DateCreated)
select c.ClientId, Name, Location, DateCreated from Alpha.dbo.Jobs as j
inner join Epsilon.dbo.Clients as c On
(j.ClientId = c.ClientId)