Search condition in where clause postgres - sql

I am new to postgres, can anyone help me understand the role of ? in the where clause?
Select * from tablename1 as a
join tablename2 as e
on a.column1 = ? and a.id = e.id
and e.range between ? and ?;

These look like place holders for parameters that you need to assign in code outside of a SQL editor. The statement probably gets compiled by Postgres and you could call it many times with different sets of parameters.

? just represents that you should place appropriate id in a.colum1=? and two values after between clause if you want to get date in some range.

Related

SQL: Is the newly selected temp table in FROM clause not passed to the sub-query in WHERE clause?

here's a question I'm in trouble with. Basically, there are originally two tables: "a" and "b". I firstly "joined" (without using JOIN clause) them together with some conditions: "a.id=b.id", "b.class="xxx"". Then I name that temp table as A, and want to select the data with the highest income within the people in A.
The error returns "the relation A doesn't exist." And the error arrow turns to the clause "select max(A.income) from A". Therefore, I suspect that the temp table A created in FROM clause will not be passed to the sub-query in WHERE clause?
select * from
(select * from a,b where a.id=b.id and b.class='xxx') as A
where A.income = all
(select max(A.income) from A)
I've encountered this problem while using Postgres, but I think it may also happen in other languages like MYSQL or MSSQL. Are there any possible solutions to solve that? Without using WITH clause? Thanks. (The reason why I say "sub-query" instead of "query" is because I've tried terms like "where A.income>1000" and they all work)
The problem is that your alias a hides the table with the same name. Use a different alias name.
It is unclear whether you want to select from the original table a in the subquery or from the alias. If it is the former, then the above will solve your problem.
If you want to reference the alias in the subquery, you had better use a common table expression:
WITH alias_name AS (/* your FROM subquery */)
SELECT ... /* alias_name can be used in a subquery here */
You can try the below -
select * from a join b on a.id=b.id where b.class='xxx'
and income = all (select max(income) from a join b on a.id=b.id where b.class='xxx')

Getting value from a table and add value from table 2 if excist

I'm new to SQL and im wondering if something like this is possible.
It's probably a super simple solution but i cant seem to solve it. I'm using an Oracle database.
Employee_Table:
employeeNr
username
address
epost
Computer_Table:
employeeNr
ComputerNumber
ComputerUpdated
When I'm using
SELECT *
From Employee_Table,
Computer_Table
WHERE Employee_Table.EmployeeNr = Computer_Table.EmployeeNr
AND Employee_Table.username LIKE ('%SomeUsername%')
When I'm using this sqlstring I only get users with computers. I would like to get all users and the computers of those who has one.
Now the million dollar question. What modifications do I need to make?
Never use commas in the FROM clause. Always use explicit JOIN.
In this case, you want a LEFT JOIN:
SELECT *
FROM Employee_Table e LEFT JOIN
Computer_Table c
ON c.EmployeeNr = c.EmployeeNr
WHERE e.username LIKE '%SomeUsername%';
When you use LEFT JOIN, conditions on the first table go on the WHERE clause. Conditions on the second table go in the ON clause.

Invalid Object Name on Openquery update with join

I have the below query which is giving me an "Invalid object name 'E1'"
As far as I can tell my query is correct, I am wondering if it is as simple as I can not do an update to an open query with a join. any suggestions?
update E1 Set e1.LILRCJ = GRDATE from Openquery(E1_PROD_ORA, 'Select * From Proddta.F41021') E1
Inner Join dbo.Temp2
On E1.LiITM = IMITM
and ltrim(rtrim(E1.LIMCU)) = E1BU
and E1.LILOCN = Storage
where GRDate <> '0'
The documentation says:
Any call to OPENDATASOURCE, OPENQUERY, or OPENROWSET in the FROM
clause is evaluated separately and independently from any call to
these functions used as the target of the update, even if identical
arguments are supplied to the two calls. In particular, filter or join
conditions applied on the result of one of those calls have no effect
on the results of the other.
This would suggest that you cannot do what you want.
You can rephrase this query, but it would help to know where GRDATE and the other variables with no table aliases come from.

Update query based on two conditions

I have the following table based on this query:
SELECT
repName.repID, repName.Rep_Name, repName.Job_Code, GenItems.Item_Name,
repName.Entered
FROM
GenItems
INNER JOIN repName
ON GenItems.Job_Code = repName.Job_Code
ORDER BY
repName.Rep_Name
I want to add an update routine to it. I want to update the entered field if the user entry matches the rep.ID and the Item Name. and finally return the Max value for the Entered field. Can I add this to this query or is it better to write another.
I just started working with sql, so if my questions seems basic, please forgive me. I am self taught and stumbling greatly.
Thank you
I don't understand fully your question.
You are showing us a SELECT statement. It can only be used to return a table-like result. If you want to upate a table you must use an UPDATE query. For the SQL-Server (and SQL CE) the query looks like this:
UPDATE repName
SET repName.Entered = x
FROM
GenItems
INNER JOIN repName
ON GenItems.Job_Code = repName.Job_Code
WHERE
repName.repID = x AND GenItems.Item_Name = 'y'
The difficulty is that tables have to be joined in the UPDATE statement. This not supported in Oracle for instance, where you have to do it with sub-selects.

Why does my update(select from when) statement refuse to recognize my table alias?

I was looking through the AskTom site and found what should be a very powerful tool for update statments but my statement refuses to accept the alias after the subquery.
Can someone please explain this for me and possibly show a solution?
Update (SELECT T.Date_,T.Name_
FROM TableA T, TableB P
WHERE P.Date_ = T.Date_
AND P.Name_ = T.Name_) SET P.ID = T.Name_
There may be other issues as to how to run this type of update and That would be a bonus. I am more interested at this point in trying to understand the alias problem. I have tried to sub in
TableB.ID = TableA.Name_
but no luck
Specifically the error is ORA-00904 invalid identifier.
As always thanks in advance.
The above issue has been answered for me but I do have some others as I try to understand it.
I now get ORA-01779 cannot modify a column which maps to a non-key preserved table.
I presume this is referring to the table I'm trying to update correct? As I could update from a view or any other proper source that may not have a pk or fk.
Can this type of update statement work with Oracle temp tables or can I expect problems?
Can I use this type of statement with a case statement to update multiple columns in TableA or will there be problems?
Thanks again.
When you perform an UPDATE (query) operation, the only columns you can use outside the parentheses are the names of columns returned by the query - in this case Date_ and Name_. The table aliases you used in the query are not valid outside the parentheses either.
What you therefore need is:
Update (SELECT P.ID ,T.Name_
FROM TableA T, TableB P
WHERE P.Date_ = T.Date_
AND P.Name_ = T.Name_) SET ID = Name_