The table structure is as follows:
NOTE: The names used are for illustrative purposes only.
Table T1 - col1 INT, col2 CHAR, col3 VARCHAR
Table T2 - col1 INT, col2 VARCHAR col3 CHAR
Table T3 - col1 INT - col1 from Table 2 col2 INT - col2 from Table T col3 INT
SELECT tt1.col2, COUNT(tt1.col1) FROM T1 tt1, T2 tt2, T3 tt3 WHERE tt2.col1 = tt3.col1 AND
tt3.col2 = tt1.col1 GROUP BY tt1.col1, tt1.col2 HAVING EVERY (tt2.col3 = 'something');
This shows an error that matches the title of the question; However, no error is reported if I remove the HAVING clause.
Is the query syntactically correct?
SELECT tt1.col2, COUNT(tt1.col1)
FROM T1 tt1
JOIN T3 tt3 ON (tt3.col2 = tt1.col1)
JOIN T2 tt2 ON (tt2.col1 = tt3.col1)
WHERE tt2.col3 = 'something'
GROUP BY tt1.col2;
It doesn't appear that you need a HAVING clause. You use a HAVING clause when you need to filter results after your GROUP BY has been executed. For example, if you wanted only the records with a count(tt1.col1) greater than 10, you would use a HAVING clause. (HAVING count(tt1.col1) > 10)
Related
I am using Informix Dynamic Server Version 12.10.FC9W1X2.
I need to update a table with fields from a different table. I am attempting to use the
MERGE INTO
statement, but I am not able to use a WHERE CLAUSE to filter out information from the table that is being updated as it's causing a syntax error. I also tried adding AND to the WHEN MATCHED and it isn't working.
Is there any way to do this?
This is my statement so far,
MERGE INTO table1 as t1
USING table2 as t2
ON t1.some_no = t2.some
WHEN MATCHED
THEN
UPDATE SET t1.some_other_no = t2.some_other_no, is_processed = 'Y', resolution = 'AAA'
The additional filters on table1 can be placed in the ON clause. For example:
create table t1(col1 int, col2 int, col3 int);
insert into t1 values(1,1,1);
insert into t1 values(1,2,0);
insert into t1 values(1,3,0);
insert into t1 values(2,1,0);
insert into t1 values(3,1,1);
create table t2 (col1 int, col2 int);
insert into t2 values(1,5);
insert into t2 values(2,5);
insert into t2 values(3,5);
merge into t1
using t2
on
t1.col1 = t2.col1
AND t1.col3 = 1
when matched then
update set t1.col2 = t2.col2;
The above results in the following output when selecting from the t1 table
col1 col2 col3
1 5 1
1 2 0
1 3 0
2 1 0
3 5 1
I have two tables. I am making a query on one table, and would like to join the result with a second table to get the final result.
My tables are:
create table table1 (col1 int, col2 int)
create table table2 (col3 int, col4 int)
insert into table1 values
(1, NULL), (2,10), (3, 20)
insert into table2 values
(1,100),(2,200),(3,300)
The query
SELECT col1 FROM table1 WHERE col2 IS NOT NULL
gives me
col1
2
3
How do I extend my query to receive the result as follows:
col1 col4
2 200
3 300
I put this example on SQL Fiddle http://sqlfiddle.com/#!3/9e89e/1 to quickly test queries.
SELECT t1.col1,t2.col4
FROM table1 t1
join table2 t2 on
t1.col1 = t2.col3
WHERE t1.col2 IS NOT NULL
You need to join the tables as per your expected output.
Fiddle
The have the following 2 table2:
Table1(col1 integer, col2)
1 "This is a string"
2 "This is another string"
5 "This is yet another string"
3 "a"
4 "b"
6 "Some other string"
Table2(col3 integer, col4 integer, col5 integer)
1 2 5
3 4 6
Now I want to find all the values from Table2 where col4=2. This gives me col3=1 and col5=5. Now I want to join this result with Table1 such that I obtain the string values(col2) corresponding to these integers.
That is, I want the result as: "This is a string", "This is yet another string"
The SQL query which I wrote in postgresql is given below:
select d1.col2, d2.col2
from Table1 d1, Table1 d2
where (select col3, col5 from Table2 where col4=0);
However, the above query is giving me error. Can someone please help me write an efficient query for the same.
You could use an INNER JOIN with two conditions on the ON clause:
SELECT Table1.*
FROM
Table1 INNER JOIN Table2
ON Table1.col1 = Table2.col3 OR Table1.col1 = Table2.col5
WHERE
Table2.col4=2
Please see fiddle here.
Try
SELECT t2.col2, t3.col2
FROM Table1 AS t1
INNER JOIN Table2 AS t2 ON t1.col1 = t2.col3
INNER JOIN Table2 AS t3 ON t1.col1 = t2.col5
WHERE t1.col4 = 2
if you want your result as two rows with one column:
select t1.col2
from Table2 as t2
inner join Table1 as t1 on t1.col1 in (t2.col3, t2.col5)
where t2.col4 = 2;
-- output
-- 'This is a string'
-- 'This is yet another string'
if you want your result as one row with two columns:
select t13.col2, t15.col2
from Table2 as t2
inner join Table1 as t13 on t13.col1 = t2.col3
inner join Table1 as t15 on t15.col1 = t2.col5
where t2.col4 = 2
-- output
-- 'This is a string', 'This is yet another string'
sql fiddle demo
try it as a union
select col2 from table1 where col1 in (
select col3 from table2 where col4 = 2
union
select col5 from table2 where col4 = 2
)
I was wondering if there exists code to accomplish the following in SQL-Server 2008?
Table 1:
id column name
-------------------
1 col1
2 col2
3 col3
4 col2
Table 2:
col1 col2 col3
--------------------
a b c
Result Table:
id data
--------------------
1 a
2 b
3 c
4 b
Thanks in advance, I really have no idea how to do this.
You can use UNPIVOT table2 to access the data from the columns:
select t1.id, t2.value
from table1 t1
left join
(
select value, col
from table2
unpivot
(
value
for col in (col1, col2, col3)
) u
) t2
on t1.name = t2.col
see SQL Fiddle with Demo
Or you can use a UNION ALL to access the data in table2:
select t1.id, t2.value
from table1 t1
left join
(
select col1 value, 'col1' col
from table2
union all
select col2 value, 'col2' col
from table2
union all
select col3 value, 'col3' col
from table2
) t2
on t1.name = t2.col
see SQL Fiddle with Demo
I dont see how you do it withou a column connection them:
Table1:
ID
ColumnName
Table2:
Table1ID
Letter
Select table1.id, table2.Letter
from table1
inner join table2 on table1.ID = table2.Table1ID
You can do this with a case statement and cross join:
select t1.id,
(case when t1.columnname = 'col1' then t2.col1
when t1.columnname = 'col2' then t2.col2
when t1.columnname = 'col3' then t2.col3
end) as data
from table1 t1 cross join
table2 t2
I have a select statement returning 5 columns:
select col1,col2,col3,col4,col5 from table1;
col1 col2 col3 col4 col5
9 A B C D
8 E F G H
I have another select statement from table2 which returns col1 alone;
col1
8
9
Based on the two select queries, is there a way to write a single select query to return the result as:
col1 col2 col3 col4 col5
8 E F G H
9 A B C D
ie. basically sort the output of I query based on col1 from II query. (this is in Mysql)
PS:II table column1 is used to for sorting & that is coming from table 2. Table2's col1 is not static, its changing for every user action & based on a call i will get col1 of table 2 & need to sort with table1's output.
Use an ORDER BY:
SELECT col1,col2,col3,col4,col5
FROM table1
ORDER BY col1
By default, ORDER BY is ASC.
SELECT col1,col2,col3,col4,col5
FROM table1
ORDER BY col1 DESC
...will put 9 from col1 as the first record returned.
For this to work, you seriously need a sort column on table2. Just having the IDs in table2 is not enough. You can have the records 7,8,9, then delete 8 and add it back. But no, that doesn't order it as 7,9,8. Maybe temporarily if there is no primary key on the table, but when the table gets large, even that "implicit" order is lost.
So, assuming you have such a sort column
Table2
Sort, Col1
1, 9
2, 8
Your query becomes
SELECT a.*
FROM table1 a
INNER JOIN table2 b ON a.col1 = b.col1
ORDER BY b.sort ASC
If you still want to rely on MySQL undocumented features or the way it currently works, then you can try this.
# test tables
create table table1 (col1 int, col2 int, col3 int);
insert table1 select 8, 1,2; # in this order
insert table1 select 9, 3,4;
create table table2 (col1 int);
insert table2 select 9; # in this order
insert table2 select 8;
# select
SELECT a.*
FROM table1 a
INNER JOIN table2 b ON a.col1 = b.col1
----output----
col1 col2 col3
9 3 4
8 1 2
This works at least for small tables, only because size(table2) < size(table1) so it collects in that order, preserving the filesort on table2.col1.
Not sure what the relationship is between t1.col1 and t2.col2. Probably looking for something like this though:
SELECT t2.col1, t1.col2, t1.col3, t1.col4, t1.col5
FROM table2 t2
INNER JOIN table1 t1 ON t1.col1 = t2.col1
ORDER BY t2.col1 ASC