SQL command for conditional join for PROGRESS database - sql

Please bear with me new to SQL- I am trying to write an SQL command with a join in a PROGRESS db. I would like to then select only the first matching record from the join. I thought to use LIMIT but PROGRESS does not support that. MIN or TOP would also work I think but having trouble with the syntax.
Something like this?-
SELECT table1.field 1, table2.field 2
FROM table2
INNER JOIN table2
ON table1.field3=table2.field3
WHERE table1.field4 in (SELECT min(table1.field4) FROM table1)
BUt it appears I can't use MIN there as saying can't do an aggregate there.
Any help would be huge.

try:
SELECT
t1.field1, t2.field2
FROM table1 t1
INNER JOIN table2 t2 ON t1.field3=t2.field3
WHERE t1.field4=(SELECT min(t.field4) FROM table1 t WHERE t1.field4=t.field4)

Related

Selecting from multiple tables in SQL Server

What does this syntax in SQL Server mean?
select top 10 *
from
table1 as t1,
table2 as t2
Is this a union or join? Does it combine two tables together? Not sure exactly what this syntax means? does anyone have online resources for this?
, is archaic syntax for cross join. This is more colloquially written as:
select top 10 *
from table1 as t1 cross join
table2 as t2;
That said, using top without order by is suspicious.

Best way to use Where Clause in SQL SERVER Query (for best performance)?

I have written 2 queries in SQL Server and used SUB Query in join with Where Clause, Please let me know which is the best way to do in below Queries.
Query 1. SELECT column1,column2,column3,JOINTAB.column5 FROM Table1
INNER JOIN (SELECT column1,column4,column5 FROM Table2 WHERE
column4='xxxx') AS JOINTAB ON Table1.column1=JOINTAB.column1
Query 2. SELECT column1,column2,column3,JOINTAB.column5 FROM Table1
INNER JOIN (SELECT column1,column4,column5 FROM Table2) AS JOINTAB
ON Table1.column1=JOINTAB.column1 WHERE JOINTAB.column4='xxxx'
For the best performance Query 1 or 2?
Your best option here is not to use a subquery at all. It's not needed. Either of thsese will give you the same results:
SELECT t1.column1,t1.column2,t1.column3,t2.column5
FROM Table1 t1
INNER JOIN Table2 t1 on t2.column1 = t1.column1 and t2.column4 = 'xxxx'
--or
SELECT t1.column1,t1.column2,t1.column3,t2.column5
FROM Table1 t1
INNER JOIN Table2 t1 on t2.column1 = t1.column1
WHERE t2.column4 = 'xxxx'
Query Performance is a great concern for all the developers.
To find the performance and optimization simply you can view 'query execution plans'.
You can use the Query Analyzer, to which execution plan is chosen by the optimizer. Simply type an SQL statement in the Query window and press the Ctrl+L key. The query is displayed graphically.
SQL Server offers is the ability to see query execution plans
Run the query and find the performance.

Optimization of DB2 query which uses joins and takes 1.5 hours to execute

when i run SELECT stataement on my view it takes around 1.5 hours to run, what can i do to optimize it.
Below is the sample structure of how my view looks like
CREATE VIEW SCHEMANAME.VIEWNAME
{
COL, COL1, COL2, COL3 }
AS SELECT
COST.ETA,
CASE
WHEN VOL.CURR IS NOT NULL
THEN COALESCE {VOL.COMM,0}
END CASE,
CASE
WHEN...
END CASE
FROM TABLE1 t1 inner join TABLE2 t2 ON t1.ETA=t2.ETA
INNER JOIN TABLE3 t3 on t2.ETA=t3.ETA
LEFT OUTER JOIN TABLE4 t4 on t2.ETA=t4.ETA
This is your query:
SELECT COST.ETA,
(CASE WHEN VOL.CURR IS NOT NULL THEN COALESCE {VOL.COMM,0}
END) as ??,
. . .
FROM TABLE1 t1 inner join
TABLE2 t2
ON t1.ETA = t2.ETA INNER JOIN
TABLE3 t3
on t2.ETA = t3.ETA LEFT OUTER JOIN
TABLE4 t4
on t2.ETA = t4.ETA;
First, I will the fact that the select clause references tables that are not in the from clause. I assume this is a typo.
Second, you should be able to use indexes to improve this query: table1(eta), table2(eta),table3(eta), andtable4(eta).
Third, I am highly suspicious on seeing the same column used for joining so many tables. I suspect that you might have cartesian products occurring, because there are multiple values of any given eta in several tables. If that is the case, you need to fix the query to better reflect what you really need. If so, ask another question with sample data and desired results, because your query is probably not correct.

comparison with more than one value in sql

I have two tables. In the first one there is a column called id. I want to select some of these IDs, and to choose from the other table the rows that have these IDs. How do I do that?
You need to learn SQL joins.
SELECT SecondTable.*
FROM SecondTable
INNER JOIN FirstTable ON (FirstTable.ID = SecondTable.ID)
WHERE FirstTable.SomeField = 'Something Else'
Here is a good link to get you started
Inner Joins
Basically for your example you would use something like
SELECT table1.id, table1.value, table2.value
FROM table1
INNER JOIN table2
ON table1.id=table2.id

How to join all columns from one table

I tried doing this but it failed.
SELECT table2.ID, table1.* FROM table2
LEFT JOIN table1 ON table1.ID = table2.table1ID
How do you select all columns from a table?
EDIT: There is no error in the above query. I don't know what caused the error but the code is now working.
You had field names conflict as both tables have ID field. You must to
SELECT table2.ID as t2_id, table1.* FROM table2
LEFT JOIN table1 ON table1.ID = table2.table1ID
What you have is syntactically correct, exactly what did you mean by it failed? Did you get an error message or just not the results you wanted? (BTW it is a bad practice to select *, only return the columns you need. In this case you do not need all the columns as the id field in table1 will have the exact same data as the file din table 2 it is joined to)
SELECT t2.ID, t1.* FROM table2 t2
LEFT JOIN table1 t1 ON t1.ID = t2.table1ID
this works on sql 2000+
If I am working inside a stored procedure where I have a defined #Table data type, there is no issue with using select table.* especially if I am using it for an output SELECT at the end. So the comment about production servers and network traffic in this case is meaningless as the entire stored procedure executes in memory. A select.* in this case is merely returning all the columns which have been defined ahead of time.