Output query to text: Too few parameters, expected 1 - sql

This is my sql code if anyone can help me look at it and why it is not responding and give the error message. Note that the relationships are: table1 and table2 are connected by ResetFrequency; table1 and table3 are connected by AccountNumber.
SELECT table1.EffectiveDate, table2.ResetFrequency, table3.Loan
FROM (table1 LEFT JOIN table2 ON table1.ResetFrequency = table2.ResetFrequency)
INNER JOIN table3 ON table1.AccountNumber = table3.AccountNumber
WHERE (table1.EffectiveDate) = [Enter Date: mm/dd/yyy];

Use following format:
SELECT z.EffectiveDate, z.ResetFrequency, table3.Loan
FROM (SELECT Table1.EffectiveDate, table2.ResetFrequency, table1.AccountNumber
FROM table1
LEFT JOIN table2 ON table1.ResetFrequency = table2.ResetFrequency)z
INNER JOIN table3 ON z.AccountNumber = table3.AccountNumber
WHERE (z.EffectiveDate) = [Enter Date: mm/dd/yyy];

just remove the parentheses or brackets
SELECT table1.EffectiveDate, table2.ResetFrequency, table3.Loan
FROM table1 LEFT JOIN table2 ON table1.ResetFrequency = table2.ResetFrequency
INNER JOIN table3 ON table1.AccountNumber = table3.AccountNumber
WHERE table1.EffectiveDate = mm/dd/yyy;

Related

Inner join the tables where the value of one column is different. But the meaning of the values are same

Table t1
Table t2
I tried running the query
select CountyCode, ContactPerson
from table1 t1
inner join select * from table 2 t2 on t1.CountyCode[1] = Code[0]
Any one help me please.
Use this syntax:
select CountyCode, ContactPerson
from t1
inner join t2 on t1.CountyCode = t2.Code

Postgres UPDATE using SELECT whit INNER JOIN multiple tables

I am just starting with Postgres and I find myself with the following Query made in Postgres, which seeks to bring me data from 4 different tables.
SELECT
table1.id,
table2.icon,
table3.title,
table4.description
FROM
table1
JOIN table5 ON id_t5 = id_t3
JOIN table3 ON id_t1 = id_t3
AND id_t3 = 816
LEFT JOIN table2 ON table5.id_t2_fk = table2.id_t2
LEFT JOIN table4 ON table4.id_t3_fk = table1.id_t1;
My problem is that I have to make an UPDATEof these 4 tables after generating the Query.
I can not think of how to solve the problem, since the UPDATE syntax of Postgres is different from that of MySQL or SQLserver.
I tried to do this:
UPDATE
table1
INNER JOIN table5 ON id_t5 = id_t3
INNER JOIN table3 ON id_t1 = id_t3
LEFT JOIN table2 ON table5.id_t2_fk = table2.id_t2
LEFT JOIN table4 ON table4.id_t3_fk = table1.id_t1
SET
table2.icon = "new icon",
table3.title = "new title",
table4.description = "new description"
WHERE
table1.id_t1= 816;
Postgres allows you to do updates in CTEs. Perhaps this does what you want:
with data as (
select t1.id, t2.id_t2, t2.icon, t3.id_t3, t3.title,
t4.id_t4, t4.description
from table1 t1 join
table5 t5
on id_t5 = id_t3 join
table3
on id_t1 = id_t3 and id_t3 = 816 left join
table2 t2
on t5.id_t2_fk = t2.id_t2 left join
table4 t4
on t4.id_t3_fk = t1.id_t1
where t1.id_t1= 816
),
u2 as (
update table2
set icon = 'new icon'
where t2.id_t3 in (select data.id_t2 from data)
returning *
),
u3 as (
update table3
set title = 'new title'
where id_t3 in (select data.id_t3 from data)
returning *
)
update table4
set description = 'new description'
where id_t4 in (select data.id_t4 from data);
If not, something similar will.

Joining on one column and if no match join on another

I am attempting to use multiple columns in my join like this:
FROM Table1 t
INNER JOIN Table2 s ON t.number = s.number OR t.letter = s.letter
Both of these tables have several hundred thousand rows of data and it is running infinitely.
Any ideas?
You mean something like:
FROM Table1 t
INNER JOIN Table2 s ON case
when t.number = s.number then 1
when t.letter = s.letter then 1
else 0 end = 1
The first matching condition wins.
One possibility is to use left join and fix the rest of the query:
FROM Table1 t LEFT JOIN
Table2 sn
ON t.number = sn.number LEFT JOIN
Table2 sl
ON t.letter = sl.letter and sn.number is null
For performance, you want indexes on Table2(number) and Table2(letter).
ORs are usually produce bad performance. I would go for:
SELECT *
FROM Table1 t
INNER JOIN Table2 s ON t.number = s.number
UNION
SELECT *
FROM Table1 t
INNER JOIN Table2 s ON t.letter = s.letter

SQL Server: join three tables

I was able to join 2 SQL tables use the following query:
SELECT *
FROM Table1, Table2 with (nolock)
WHERE Table1.field1 = Table2.field2
Then I tried to join 3 SQL tables like below:
SELECT *
FROM Table1, Table2, Table3 with (nolock)
WHERE Table1.field1 = Table2.field2, Table1.field2 = Table3.field3
But it didn't work. Did I miss anything here? Or how do I join 3 tables properly?
Thanks!
If you use the proper ANSI JOIN syntax, you won't have any of those issues:
SELECT *
FROM
Table1
INNER JOIN
Table2 ON Table1.field1 = Table2.field2
INNER JOIN
Table3 ON Table1.field2 = Table3.field3
You are joining table in old style and for multiple condition you have to use and instead of ,
Try to use inner join
Like this
SELECT *
FROM Table1
inner join Table2 on Table1.field1 = Table2.field2
inner join Table3 on Table1.field2 = Table3.field3
Try this :
SELECT * FROM Table1 WITH (NOLOCK)
inner join Table2 WITH (NOLOCK)
on Table1.field1 = Table2.field2
inner join Table3 WITH (NOLOCK)
on Table1.field2 = Table3.field3
Use AND instead of ',' in where condition .
Like this :
SELECT *
FROM Table1, Table2, Table3 with (nolock)
WHERE Table1.field1 = Table2.field2 AND
Table1.field2 = Table3.field3
Try to use this one.
Select * from table1
left join table2 on (table1.tablefield1 = table2.tablefield2)
left join table3 on (table2.tablefield2 = table3.tablefield3)

SQL join order and conditions

I've got 3 tables that I want to join and filter on some conditions.
I've first wrote this query:
select * from table1 t1
left join (select * from table2 where table2.fieldX=...) t2
on t1.id_12=t2.id_12
left join table3
on t2.id_23=t3.id_23
where t1.fieldY=...
Then I wanted to make it looks like more canonical by rewritting it like that:
select * from table1 t1
left join table2 t2
on t1.id_12=t2.id_12
left join table3
on t2.id_23=t3.id_23
where table2.fieldX=...
and t1.fieldY=...
But it does not give the same result.
I dont't understand why...
Do you?
Thanks in advance.
When you put table2.fieldX=... in the where clause you eliminate all rows from table1 that do not have a corresponding row in table2. Effectively you are changing the left join into an inner join.
Instead, you can apply the table2 filter in the join itself:
SELECT
*
FROM
Table1 t1
LEFT JOIN Table2 t2 ON t1.id_12 = t2.id_12 AND t2.fieldX = ...
LEFT JOIN Table3 t3 ON t2.id_23 = t3.id_23
WHERE
t1.fieldY = ...
Did you add the inner select where on the second query?
SELECT *
FROM table1 t1
LEFT JOIN table2 t2
on t1.id_12=t2.id_12
LEFT JOIN table3
on t2.id_23=t3.id_23
WHERE t1.fieldY=...
and t2.fieldX=...