So something like this:
SELECT *,
CASE
WHEN
....
I'm coding in Teradata
In Teradata if you are going to include more derived columns with your * then you must include the table or alias name with the *
So:
SELECT
myTable.*,
myOtherTable.*,
CASE WHEN... END
FROM myTable
INNER JOIN myOtherTable ON
mytable.id = myOtherTable.id;
If you have a thousand tables being joined together then just toss that mess into a subquery so you can SELECT * FROM and then add you case in the main query:
SELECT t1.*
CASE WHEN... END
FROM
(
SELECT *
FROM myTable
INNER JOIN myOtherTable ON
mytable.id = myOtherTable.id
) AS t1;
Related
I have the following sql statement:
WITH
subquery AS (
select distinct id from a_table where some_field in (1,2,)
)
select id from another_table where id in subquery;
Edit
JOIN is not an option (this is just a reduced example of a bigger query)
But that obviously does not work. The id field exists in both tables (with a different name, but values are the same: numeric ids). Basically what I want to do is filter by the result of the subquery, like a kind of intersection.
Any idea how to write that query in a correct way?
You need a subquery for the second operand of IN that SELECTs from the CTE.
... IN (SELECT id FROM subquery) ...
But I would recommend to rewrite it as a JOIN.
Are you able to join on ID and then filter on the Where clause?
select a.id
from a.table
inner join b.table on a.id = b.id
where b.column in (1,2)
Since you only want the id from another_table you can use exists
with s as (
select id
from a_table
where some_field in (1,2)
)
select id
from another_table t
where exists ( select * from s where s.id=t.id )
But the CTE is really redundant since all you are doing is
select id
from another_table t
where exists (
select * from a_table a where a.id=t.id and a.some_field in (1,2)
)
I have two versions of the same table and want to find the differences between both: which rows have changed? I use a minus query twice to print the changed rows as they appear in the old and new table.
Now I want to add a new query: one that shows me the rows that have changed on a specific column.
(select * from NewTable minus select * from OldTable) NewRows
inner join
(select * from OldTable minus select * from NewTable) OldRows
on NewRows.column1 = OldRows.column1
and NewRows.column2 <> OldRows.column2
where column1 is the unique row id and column 2 is the changed property.
When I execute Oracle SQL Developer I get error ORA-00933 "SQL command not properly ended", and he indicates the definition of NewRows as error. I have also tried with ") as NewRows" but it did not work.
The following query does work, so the NewTable and OldTable are compatible.
(select * from NewTable minus select * from OldTable)
union
(select * from OldTable minus select * from NewTable)
Try with additional select * from on the beginning:
select * from
(select * from NewTable minus select * from OldTable) NewRows
inner join
(select * from OldTable minus select * from NewTable) OldRows
on (NewRows.column1 = OldRows.column1 and NewRows.column2 <> OldRows.column2)
Also be aware of nulls your condition for difference <> will not cover situation where one value is null and second is not null. You should probably use:
on (nvl(NewRows.column1, 'UNIQUEVAL1') = nvl(OldRows.column1, 'UNIQUEVAL1')
and nvl(NewRows.column2, 'UNIQUEVAL2') <> nvl(OldRows.column2, 'UNIQUEVAL2') )
Trying to get an slightly more complex sql statement structured but can't seem to get the syntax right. Trying to select counts, of various columns, in two different tables.
SELECT
SUM(ColumninTable1),
SUM(Column2inTable1),
COUNT(DISTINCT(Column3inTable1))
FROM TABLE1
This works, however I can't for the life of me figure out how to add in a COUNT(DISTINCT(Column1inTable2) FROM TABLE2 with what syntax.
There are several solutions you can take:
Disjunct FULL OUTER JOIN
SELECT
SUM(MYTABLE.ID) as theSum,
COUNT(DISTINCT MYTABLE.SOMEVALUE) as theCount,
COUNT(DISTINCT MYOTHERTABLE.SOMEOTHERVALUE) as theOtherCount
FROM MYTABLE
FULL OUTER JOIN MYOTHERTABLE ON 1=0
UNION two queries and leave the column for the other table null
SELECT
MAX(theSum) as theSum,
MAX(theCount) as theCount,
MAX(theOtherCount) AS theOtherCount
FROM (
SELECT
SUM(ID) as theSum,
COUNT(DISTINCT SOMEVALUE) as theCount,
NULL as theOtherCount
FROM MYTABLE
UNION ALL
SELECT
NULL,
NULL,
COUNT(DISTINCT SOMEOTHERVALUE)
FROM MYOTHERTABLE
)
Query 'with a query per column' against a single record table (eg RDB$DATABASE)
SELECT
(SELECT SUM(ID) FROM MYTABLE) as theSum,
(SELECT COUNT(DISTINCT SOMEVALUE) FROM MYTABLE) as theCount,
(SELECT COUNT(DISTINCT SOMEOTHERVALUE) FROM MYOTHERTABLE) as theOtherCount
FROM RDB$DATABASE
CTE per table + cross join
WITH query1 AS (
SELECT
SUM(ID) as theSum,
COUNT(DISTINCT SOMEVALUE) as theCount
FROM MYTABLE
),
query2 AS (
SELECT
COUNT(DISTINCT SOMEOTHERVALUE) as theOtherCount
FROM MYOTHERTABLE
)
SELECT
query1.theSum,
query1.theCount,
query2.theOtherCount
FROM query1
CROSS JOIN query2
There are probably some more solutions. You might want to ask yourself if it is worth the effort of coming up with a (convoluted, hard to understand) single query to get this data were two queries are sufficient, easier to understand and in the case of large datasets: two separate queries might be faster.
In this case all "count" would return the same value.
Try to do the same using sub queries:
Select
(Select count (*) from Table1),
(Select count (*) from table2)
from Table3
How to combine many select queries into 1 statement in SQLite?
SELECT * FROM Table1 WHERE condition1
SELECT * FROM Table2 WHERE condition2
SELECT * FROM Table3 WHERE condition3
...
Use UNION to combine the results of the three queries.
UNION will remove duplicate leaving on unique rows on the final result. If you want to keep the duplicate rows, use UNION ALL.
SELECT * FROM Table1 WHERE condition1
UNION
SELECT * FROM Table2 WHERE condition2
UNION
SELECT * FROM Table3 WHERE condition3
caveat: the number of columns (as well as the data type) must match on each select statement.
UPDATE 1
based on your comment below, You are looking for JOIN,
SELECT a.*, b.*, c.*
FROM table1 a
INNER JOIN table2 b
ON a.ColName = b.ColName
INNER JOIN table3 c
ON a.ColName = c.ColName
-- WHERE .. add conditions here ..
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
Your can also query also like
SELECT * FROM users where bday > '1970-05-20'
UNION ALL SELECT * FROM users where bday < '1970-01-20'
I was wondering, is it possible to join the result of a query with itself, using PostgreSQL?
You can do so with WITH:
WITH subquery AS(
SELECT * FROM TheTable
)
SELECT *
FROM subquery q1
JOIN subquery q2 on ...
Or by creating a VIEW that contains the query, and joining on that:
SELECT *
FROM TheView v1
JOIN TheView v2 on ...
Or the brute force approach: type the subquery twice:
SELECT *
FROM (
SELECT * FROM TheTable
) sub1
LEFT JOIN (
SELECT * FROM TheTable
) sub2 ON ...
Do you mean, the result of a query on a table, to that same table. If so, then Yes, it's possible... e.g.
--Bit of a contrived example but...
SELECT *
FROM Table
INNER JOIN
(
SELECT
UserID, Max(Login) as LastLogin
FROM
Table
WHERE
UserGroup = 'SomeGroup'
GROUP BY
UserID
) foo
ON Table.UserID = Foo.UserID AND Table.Login = Foo.LastLogin
Yes, just alias the queries:
SELECT *
FROM (
SELECT *
FROM table
) t1
JOIN (
SELECT *
FROM table
) t2
ON t1.column < t2.other_column