I am trying to find the moments in which column5 data is greater than column6. The following command works for me for results in one table
SELECT * FROM table1 WHERE column5 > column6;
But I would like to query a return from other tables using the same conditional column5 > column6 and still select all because some columns provide additional relevant information and reference. For example, something like
SELECT * FROM table1, table2 WHERE column5 > column6;
but I get an error complaining from an ambiguous named table column5... I switched the command over to something like
SELECT * FROM table1, table2 WHERE table1.column5 > table1.column6 AND table2.column5 > table2.column6;
The above command does not produce and error but the query returns an empty result. I have also tried the INNER JOIN command as such
SELECT * FROM table1 INNER JOIN table2 ON table1.seconds = table2.seconds AND table1.column5 > table1.column6 AND table2.column5 > table2.column6;
This yields a result that is similar to the first command but the query results in all rows from table1 from that operator conditional repeated twice in the same row, but table2 results are not shown.
Is there another way to achieve this? Most of my attempts did not return the desired results. Again, I just wanted my query to result the rowid of multiple tables WHERE *.column5 > *.column6
.sql file of the database pasted on codeshare.io to provide sample-data to this problem
Put sample data here:
Table1:
Column5
"139062.6"
"115080"
"279718.5"
"106184"
"109483"
"152253"
"159030.3333"
"144092.5"
"154913.8333"
"52166.83333"
"18257.5"
"8907"
Column6
"224340.8"
"154723.6667"
"202486.8333"
"107184.8333"
"110674"
"257038.6667"
"151057"
"190702.6667"
"229714"
"37816.16667"
"18061.83333"
"6606.666667"
Table2:
Column5
"7544.6"
"10165.16667"
"11574.16667"
"9400.833333"
"11421.5"
"11368.5"
"11925.83333"
"9108.833333"
"8276.666667"
"8650.5"
"14998.16667"
"16229.83333"
Column6
"10109"
"14526.83333"
"12070.66667"
"7819.333333"
"9247.833333"
"7201.833333"
"8166.833333"
"4928"
"9135.5"
"9666.166667"
"8201.166667"
"10186"
Expected Result "WHERE column5 > column6" on table 1 and 2:
TABLE 1
Column5
"279718.5"
"159030.3333"
"52166.83333"
"18257.5"
"8907"
Column6
"202486.8333"
"151057"
"37816.16667"
"18061.83333"
"6606.666667"
TABLE 2
Column5
"9400.833333"
"11421.5"
"11368.5"
"11925.83333"
"9108.833333"
"14998.16667"
"16229.83333"
Column6
"7819.333333"
"9247.833333"
"7201.833333"
"8166.833333"
"4928"
"8201.166667"
"10186"
I think you are looking for the union operator (assuming your tables have the same number of columns):
SELECT * FROM table1 WHERE column5 > column6
UNION
SELECT * FROM table2 WHERE column5 > column6;
If you only want the rowids listed, SELECT rowid instead (it depends on how you want to use the result of the query later, as this way you have no way of telling which table the rowid refers to).
Related
I am facing some problems and I got stuck in building this query.
I would like to stack 2 columns from the same table into a long single one (I use UNION statement), and then, I would like to produce a new variable to tell me if the number (stack of column1 and column2, organism_id) comes from column 1 or comes from column 2. For now, I have been trying this approach but I have a problem which I do not understand in the following query:
SELECT u.organism_id, case when u.organism_id IN cpl.column1 then 1
else 0
end as is_column1
FROM
(select column1 as organism_id
from table1
UNION
select column2
from table1) as u,
table1 as cpl;
Does someone have a clue on how to solve this problem?
Thanks in advance!
In general, and if I understand you correctly, you can throw a source column on the tables before unioning them. I'd also suggest UNION ALL to avoid accidental removal of duplicates:
SELECT
*
FROM
(
SELECT
'Column1' AS Source,
Column1
FROM
Table1
UNION ALL
SELECT
'Column2' AS Source,
Column2
FROM
Table1
) u
Id like to find records with (.XX) extension at the end which don't have corresponding records without an extension (.XX) at the end. Id like to use the "exists" or "not exists" solution if possible as I'm puzzled why mine gives no output.
input
col_a
value1.XX
value1
value2.XX
value3
** expected output**
col_a
value2.XX
code
SELECT *
FROM table1 as a
where
right (table1.[col_a],3) = ".XX"
and exists(
select 1 from table1 b where Left(a.[col_a], Len(a.[col_a]) - 3) = b.[col_a]
)
Hmmm. You
select t1.*
from table1 t1
where t1.col_a like '%.XX' and
not exists (select 1
from table1 tt1
where t1.col_a = tt1.col_a || '.XX'
);
Note: You have not specified your database so this uses the ISO/ANSI standard || for string concatenation. You can check your RDBMs's documentation for the correct concatenation technique.
Hello everyone I am having trouble and I can't seem to find an answer that will work for my problem.
I am using Microsoft Access and running a Select query. What I want to then do is change the values in a column for this query.
for example
SELECT Table.column1, Table.Column2
FROM Table
I get the following:
|Column1|Column2|
A 1
B 2
C 3
D 2
E 1
F 2
G 3
H 3
I 1
J 2
K 2
I want it so that the query now replaces all the 1s in column2 with 100, all the 2s in column2 with 200 and all the 3s in column2 with 300.
What I dont want to do is update the values in column2 in the original table, I want to change the values just in the query.
If your column2 data are only 1, 2 and 3 you can multiply by 100
SELECT Table.column1
, Table.Column2 * 100 as Column2
FROM Table
If your data contains other values than 1,2 and 3 you can use IIF
SELECT Table.column1
, IIF(Table.Column2 in (1,2,3),Table.Column2 * 100, Table.Column2) as Column2
FROM Table
You can do calculations in your select queries, e.g.:
SELECT Table.column1, Table.Column2 * 100 As Column2
FROM Table
This displays the value of Column2 multiplied by 100, but doesn't modify the underlying table.
You can't modify specific rows just in a query. If you want to modify specific rows, you can just copy the table, or use a make-table query to insert the results of a query in a table and then modify that table.
Other option with IIF() :
select Col1, iif ( Col2 = 1, 100,
iif ( Col2 = 2, 200,
iif ( Col2 = 3, 300, col2 )
)
) as col2
from table t;
Use the switch() function:
select column1,
switch(column2 = 1, 100,
column2 = 2, 200,
column2 = 3, 300,
column2
) as new_column2
from t;
I have 2 rows from 2 tables in a database that I want to compare.
Column1 is on table1 and is an Integer field with entries like the following
column1
147518
187146
169592
Column2 is on table2 and is a Varchar(15) field with various entries but for this example lets use these 3:
column2
169592
00010000089
DummyId
For my query part of it relies on checking if rows from table1 are linked to the rows in table2, but to do this, I need to compare column1 and column2.
SELECT * FROM table1 WHERE column1 IN (SELECT column2 FROM table2)
The result of this using the data above should be 1 row - 169592
Obviously this wont work (A character to numeric conversion process failed) as they cannot be compared as is, but how do I get them to work?
I have tried
SELECT * FROM table1 WHERE column1 IN (SELECT CAST(column2 AS INTEGER) FROM table2)
and
SELECT * FROM table1 WHERE column1 IN (SELECT (column2::INTEGER) column2 FROM table2)
Using Server Studio 9.1 if that helps.
Try casting the int to a string:
SELECT * FROM table1 WHERE cast(column1 as varchar(15)) IN (SELECT column2 FROM table2)
You can try to use ISNUMERIC in following:
SELECT * FROM table1 WHERE column1 IN (SELECT CASE WHEN ISNUMERIC(column2) = 1 THEN CAST(column2 AS INT) END FROM table2)
For this purpose there is no need to create a special function that you'll not find on other environments.
Let's create a test case for your example:
CREATE TABLE tab1 (
col1 INT,
col2 INT
);
CREATE TABLE tab2 (
col1 VARCHAR(15)
);
INSERT INTO tab1 VALUES(147518,1);
INSERT INTO tab1 VALUES(187146,2);
INSERT INTO tab1 VALUES(169592,3);
INSERT INTO tab2 VALUES(169592);
INSERT INTO tab2 VALUES('00010000089');
INSERT INTO tab2 VALUES('DummyId');
The first query you run was like:
SELECT t1.*
FROM tab1 AS t1
WHERE t1.col1 IN (SELECT t2.col1 FROM tab2 AS t2);
This will raise an error because it tries to compare an INT with a VARCHAR
[infx1210#tardis ~]$ finderr 1213
-1213 A character to numeric conversion process failed.
A character value is being converted to numeric form for storage in a
numeric column or variable. However, the character string cannot be
interpreted as a number. It contains some characters other than white
space, digits, a sign, a decimal, or the letter e; or the parts are in
the wrong order, so the number cannot be deciphered.
If you are using NLS, the decimal character or thousands separator
might be wrong for your locale.
[infx1210#tardis ~]$
Then you've tried to cast a VARCHAR into a INT which resulted in the same error, you should tried the other way:
> SELECT t1.*
> FROM tab1 AS t1
> WHERE t1.col1::CHAR(11) IN (SELECT t2.col1 FROM tab2 AS t2);
>
col1 col2
169592 3
1 row(s) retrieved.
>
Check also if you don't get faster results using the EXISTS:
> SELECT t1.*
> FROM tab1 AS t1
> WHERE EXISTS (
> SELECT 1
> FROM tab2 AS t2
> WHERE t1.col1::CHAR(11) = t2.col1
> );
col1 col2
169592 3
1 row(s) retrieved.
>
Another way possible is to just join the tables:
> SELECT t1.*
> FROM tab1 AS t1
> INNER JOIN tab2 AS t2
> ON (t1.col1 = t2.col1);
col1 col2
169592 3
1 row(s) retrieved.
>
Part of this question was answered by #Stanislovas Kalašnikovas where he said to use the following:
SELECT * FROM table1 WHERE column1 IN (SELECT CASE WHEN ISNUMERIC(column2) = 1 THEN CAST(column2 AS INT) END FROM table2)
But informix does not have a built in function for ISNUMERIC, so the following created it:
create function isnumeric2(inputstr varchar(15)) returning integer;
define numeric_var decimal(15,0);
define function_rtn integer;
on exception in (-1213)
let function_rtn = 0;
end exception with resume
let function_rtn = 1;
let numeric_var = inputstr;
return function_rtn;
end function;
And then the first query above worked for me.
I want to build columns that calculated with each other. (Excuse my English)
Example:
Id Column1 Column2 Column3
1 5 5 => Same as Column1 5 => Same as Column2
2 2 12 => column1 current + column2.prev + column3.previous = 2+5+5 17 => column2.current + column3.prev = 12+5
3 3 32 => 3+12+17 49 => 32+17
easier way to see:
Id Column1 Column2 Column3
1 5 5 => Same as Column1 5 => Same as Column2
2 2 12 => 2+5+5 17 => 12+5
3 3 32 => 3+12+17 49 => 32+17
so complicated??? :-(
The previous issue was calculating Column3 with the new calculated column as Column2. But now, it must be renew with the just calculated Column2 and the previous record of Column3 as well. If you want to have a look at the previous post, here it is.
Here is my previous recursive CTE code. It works like, 1st, calculate column2 with previous record of current column (c.Column2) in cteCalculation, and then calculate new column3 in cte2 with just calculated column2 from cteCalculation.
/copied from that previous post/
;with cteCalculation as (
select t.Id, t.Column1, t.Column1 as Column2
from table_1 t
where t.Id = 1
union all
select t.Id, t.Column1, (t.Column1 + c.Column2) as Column2
from table_1 t
inner join cteCalculation c
on t.Id-1 = c.id
),
cte2 as(
select t.Id, t.Column1 as Column3
from table_1 t
where t.Id = 1
union all
select t.Id, (select column2+1 from cteCalculation c where c.id = t.id) as Column3
from table_1 t
inner join cte2 c2
on t.Id-1 = c2.id
)
select c.Id, c.Column1, c.Column2, c2.column3
from cteCalculation c
inner join cte2 c2 on c.id = c2. id
Now I wanna extend it like calculate 2 columns with the data from each other. Means, use 2nd to calc the 3rd, and use 3rd to get new 2nd column data. Hope you can get it.
This is an example how to achive this using recursive CTE
create table #tmp (id int identity (1,1), Column1 int)
insert into #tmp values(5)
insert into #tmp values(2)
insert into #tmp values(3);
with counter as
(
SELECT top 1 id, Column1, Column1 as Column2, Column1 as Column3 from #tmp
UNION ALL
SELECT t.id, t.Column1,
t.Column1 + counter.Column2 + counter.Column3,
(t.Column1 + counter.Column2 + counter.Column3) + counter.Column3 FROM counter
INNER JOIN #tmp t ON t.id = counter.id + 1
)
select * from counter
You'll need to use a Recursive CTE since the values of subsequent columns are dependent upon earlier results.
Do this in pieces, too. Have your first query just return the correct values for Column1. Your next (recursive CTE) query will add the results for Column2, and so on.
OK I'm assuming you're doing inserts into column 1 here of various values.
Essentially col2 always = new col1 value + old col2 value + old col 3 value
col3 = new col2 value + old col3 value
so col3 = (new col1 value + old col2 value + old col 3 value) + old col3 value
So an INSTEAD OF Insert trigger is probably the easiest way to implement.
CREATE TRIGGER tr_xxxxx ON Tablename
INSTEAD OF INSERT
AS
INSERT INTO Tablename (Column1, Column2, Column3)
SELECT ins.col1, ins.col1+t.col2+t.col3, ins.col1+t.col2+t.col3+t.col3
FROM Tablename t INNER JOIN Inserted ins on t.Id = ins.Id
The trigger has access to both the existing (old) values in Tablename t, and the new value being inserted (Inserted.col1).