compare the two lines - sql

There are two columns, in the same table.
I need to find values that are in the second column but not in the first.
All I've been able to think of so far:
SELECT DISTINCT [column],
FROM [table]
WHERE column2 LIKE "2";
I know the task is simple, but do you have any ideas?

I need to find values that are in the second column but not in the first.
This suggests not exists or a similar approach:
select t.*
from t
where not exists (select 1 from t t2 where t2.column2 = t.column1);

Related

Write a where clause that compares two columns to the same subquery?

I want to know if it's possible to make a where clause compare 2 columns to the same subquery. I know I could make a temp table/ variable table or write the same subquery twice. But I want to avoid all that if possible. The Subquery is long and complex and will cause significant overhead if I have to write it twice.
Here is an example of what I am trying to do.
SELECT * FROM Table WHERE (Column1 OR Column2) IN (Select column from TABLE)
I'm looking for a simple answer and that might just be NO but if it's possible without anything too elaborate please clue me in.
I updated the select to use OR instead of AND as this clarified my question a little better.
The example you've given would probably perform best using exists, such as:
select *
from t1
where exists (
select 1 from t2
where t2.col = t1.col1 and t2.col = t1.col2
);
To prevent writing the complicated subquery twice, you can use a CTE (Common Table Expression):
;WITH MyFirstCTE (x) AS
(
SELECT [column] FROM [TABLE1]
-- add all the very complicated stuff here
)
SELECT *
FROM Table2
WHERE Column1 IN (SELECT x FROM MyFirstCTE)
AND Column2 IN (SELECT x FROM MyFirstCTE)
Or using EXISTS:
;WITH MyFirstCTE (x) AS
(
SELECT [column] FROM [TABLE1]
-- add all the very complicated stuff here
)
SELECT *
FROM Table2
WHERE EXISTS (SELECT 1 FROM MyFirstCTE WHERE x = Column1)
AND EXISTS (SELECT 1 FROM MyFirstCTE WHERE x = Column2)
I used deliberately clumsy names, best to pick better ones.
I started it with a ; because if it's not the first command in a larger script then a ; is needed to separate the CTE from the commands before it.

SUM and SELECT ALL

I need a correction please
SELECT *, SUN(mytable2.quantite)
FROM mytable1
INNER JOIN mytable2
ON mytable1.id = mytable2.id_table_article
I want to select all columns and SUM one column. How I can do that please ?
I have a problem because I think SUM works with ExecuteScalar and SELECT * works with ExecuteReader()
:( how I cant fusion this result because I need to show this result at on my listview so I need one request :/
I work with SQLIte
I suspect you want every column from mytable1 and the corresponding sum from mytable2. If so, you can use a subquery:
SELECT t1.*,
(SELECT SUM(t2.quantite)
FROM mytable2 t2
WHERE t1.id = t2.id_table_article
) as quantite
FROM mytable1 t1;

Check if combination of fields in Table1 exists in another Table2 (SQL)

I need to check whether a combination of values in my table A exists in the specified corresponding set of columns in a different table, B.
So far, I'm doing this, which doesn't seem very elegant or efficient:
select * from table1 where
colX_table_1 NOT IN (select colX_table_2 from table2)
and
colY_table_1 NOT IN (select colY_table_2 from table2)
Is there a better/faster way to do this combination check (colX_table_1,colY_table_1) -> (colX_table_2,colY_table_2)?
The query you gave evaluates each field separately:
select * from table1 where
colX_table_1 NOT IN (select colX_table_2 from table2)
and
colY_table_1 NOT IN (select colY_table_2 from table2)
This is not merely unelegant, as you claim, it is wrong, as it does not enforce combinations. E.g., consider the following tables:
table1:
------
colX colY
1 1
table2:
------
colX colY
1 2
2 1
According to your post, you are looking for a query that would return the row in table1, since such a combination does not exist in table2. However, in the given query, each part of the where clause evaluates to false, and the row is not returned.
Instead, in order to check the combination and not each column individually, you could use an exists condition:
SELECT *
FROM table1
WHERE NOT EXISTS (SELECT *
FROM table2
WHERE table1.colx = table2.colx AND
table1.coly = table2.coly)
It depends a bit on your data but this worked in my case:
select *
from table1
where colx||coly not in (select colx||coly from table2)
This notation is just a string concatenation: ||

Combining several query results into one table, how is the results order determined?

I am retuning table results for different queries but each table will be in the same format and will all be in one final table. If I want the results for query 1 to be listed first and query2 second etc, what is the easiest way to do it?
Does UNION append the table or are is the combination random?
The SQL standard does not guarantee an order unless explicitly called for in an order by clause. In practice, this usually comes back chronologically, but I would not rely on it if the order is important.
Across a union you can control the order like this...
select
this,
that
from
(
select
this,
that
from
table1
union
select
this,
that
from
table2
)
order by
that,
this;
UNION appends the second query to the first query, so you have all the first rows first.
You can use:
SELECT Col1, Col2,...
FROM (
SELECT Col1, Col2,..., 1 AS intUnionOrder
FROM ...
) AS T1
UNION ALL (
SELECT Col1, Col2,..., 2 AS intUnionOrder
FROM ...
) AS T2
ORDER BY intUnionOrder, ...

SQL "In" Statement Match Anything

If I have a query like this
SELECT * FROM table1 WHERE col1 IN ({SUBS})
Is there anything I can replace {SUBS} with that will return all rows in the table?
Further details:
I am building the SQL dynamically in my app, so I cannot (should not) edit other parts of the query except what's in braces. So,
SELECT * FROM table1
will not do.
Also,
SELECT * FROM table1 WHERE col1 IN (SELECT col1 FROM table1)
would be hackish and highly inefficient. Consider the table have more than 50k rows.
This would do it:
select col1 from table1
Edit: There seems to be a bit of confusion - the OP asked what value could be used to replace {SUBS} that would return all rows from table1. My answer above is what you could use in place of {SUBS} that would return all the rows.
This works for me in SQL Server:
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN (COLUMN_NAME)
Have you tried just using COL1 for {SUBS}?
e.g.
SELECT * FROM table1 WHERE col1 IN (col1)
If you replaced {SUBS} with SELECT col1 FROM table1, you would end up with
SELECT * FROM table1 WHERE col1 IN (SELECT col1 FROM table1);
which would return all rows from table1. This is, of course, simply a more roundabout way of saying:
SELECT * FROM table1;
You're right,
SELECT * FROM table1 WHERE col1 IN (SELECT col1 FROM table1)
does work, but is highly inefficient; requiring a merge join to return all rows.
Use the following which is just as efficient as regular SELECT * FROM table1
SELECT * FROM table1 WHERE col1 IN (col1)
However, that said; I suggest you have a chat to the person who is trying to impose the SELECT * FROM table1 WHERE col1 IN ({SUBS}) structure. There is no good reason to do so.
It unnecessarily complicates queries.
Creates risk of highly inefficient queries.
Potentially even limits developers to use certain techniques.
I suspect the person imposing this is trying to implement some sort of silver-bullet framework. Remember, the golden rule in software development is that there are no silver-bullets.
If you're simply trying to retrieve every row in the table, then:
select * from table1
If you're trying to prove a point or win a bet or something, then:
select * from table1 where col1 in (select col1 from table1)
If the query requires some WHERE condition, then I would try to replace it with an EXISTS statement:
select
*
from
table1 t1
where
exists ( {subs} )
Then {subs} can be replaced with any expression that does not yield NULL.
This works in Oracle:
select * from table1 where col1 in (col1)