Trying to obtain filtered results matching TableA and a TableB with both a column col1 and WHERE a python variable "Bingo" NOT IN TableB col3
bingo = 'Data1'
SQL = 'SELECT a.* FROM TABLEA a WHERE a.col1 NOT IN (SELECT col1 FROM TABLEB) AND (?) NOT IN (SELECT col3 FROM TABLEB);'
rows = cur.execute(SQL,bingo).fetchall()
You cannot parameterize the name of the field. You are better off using Python's substitutions/formatting with str.format() such as this:
bingo = 'Data1'
SQL = 'SELECT a.* FROM TABLEA a WHERE a.col1 NOT IN (SELECT col1 FROM TABLEB) AND {} NOT IN (SELECT col3 FROM TABLEB);'.format(bingo)
rows = cur.execute(SQL).fetchall()
Related
I'm trying to solve a problem that requires me to get rows from a table based on the values of other rows from the same table (which I also need in the output).
I'm looking to do the equivalent of the below:
SELECT a.id,a.col1,a.col2 FROM tbl a WHERE col1 = #col
UNION
SELECT b.id,b.col1,b.col2 FROM tbl b WHERE b.col2 IN (SELECT a.col1)
UNION
SELECT c.id,c.col1,c.col2 FROM tbl c WHERE c.col1 IN (SELECT b.col1)
or
(SELECT id,col1,col2 FROM tbl WHERE col1 = #val) a
UNION
SELECT id,col1,col2 FROM tbl b WHERE b.col2 IN (SELECT col1 FROM a)
UNION
SELECT id,col1,col2 FROM tbl c WHERE c.col1 IN (SELECT col1 FROM b)
But both of these are not allowed. Is there some way to achieve this that avoid tediously recursive statements when extended, like the functional statement below:
SELECT * FROM #tbl WHERE col1 = #val
UNION
SELECT * FROM #tbl WHERE col2 = (SELECT col1 FROM #tbl WHERE col1 = #val)
UNION
SELECT * FROM #tbl WHERE col1 = (SELECT col1 FROM #tbl WHERE col2 = (SELECT col1 FROM #tbl WHERE col1 = #val))
fiddle
Is this what you want?
with cte as (
SELECT a.id, a.col1, a.col2
FROM tbl a
WHERE col1 = #col
UNION ALL
SELECT b.id, b.col1, b.col2
FROM tbl b JOIN
cte
ON b.col2 = cte.col1
)
select *
from cte;
You might want select distinct in the outer query or union in the inner one.
The query itself might be more complicated if your relationships have cycles.
I would like to
Select columns from table a
Where extension table a = extension in table b
And department in both table b and table c match
Based on that match that deptartment in table c should = yes
Select col1, col2, col3, col4, col5 from table a
Where col2 table a = col2 table b
And col3 in table b = col3 in table c
And col4 tabel c = yes
can anyone help.
Although your question is ambiguous have you tried:
SELECT A.COL1, A.COL2, A.COL3, A.COL4, A.COL5
FROM TABLE_A A
JOIN TABLE_B B
ON A.Extension = B.Extension
JOIN TABLE_C C
ON B.Department = C.Department
AND C.Department = 'yes'
You can do this in different ways. As following:
Way 1
SELECT col1, col2, col3, col4, col5
FROM tableA
JOIN tableB ON tableA.col2 = tableB.col2
JOIN tableC ON tableB.col3 = tableC.col3 AND tableC.col4 = 'yes'
Way 2
SELECT col1, col2, col3, col4, col5
FROM tableA
JOIN tableB ON tableA.col2 = tableB.col2
JOIN tableC ON tableB.col3 = tableC.col3
WHERE tableC.col4 = 'yes'
Way 3
SELECT *
INTO #col4yesTable
FROM tableC
Where col4 = 'yes'
SELECT tableA.col1, tableA.col2, tableA.col3, tableA.col4, tableA.col5
FROM #col4yesTable
JOIN tableB ON #col4yesTable.col3 = tableB.col3
JOIN tableB ON tableA.col2 = tableB.col2
Depends on your need you can use one of them. The last one, creates a temporary table to store all tableC objects that has col4 = 'yes'
I have a requirement to update a column in table A if the count of records in table B grouped by 3 columns (matching between A and B) is less than 7. I have written below query, but it is running long. Please suggest any optimal query or tune this.
update /*+ parallel(A) */ A set A.col4=0
where exists
(select 1
from B
where A.col1=B.col1 and A.col2=B.col2
and A.col3=B.col3
group by col1,col2,col3
having count(*) < 7)
Try this,
MERGE INTO A
USING (
SELECT col1, col2, col3
FROM B
GROUP BY col1, col2, col3
HAVING COUNT(*) > 7
) b ON (A.col1 = b.col1 AND A.col2=b.col2 AND A.col3= b.col3)
WHEN MATCHED THEN UPDATE
SET A.col4 = 0;
View it on SQL Fiddle: http://www.sqlfiddle.com/#!4/dcdf1/17
Let me know if it worked or not!
My first suggestion is to create an index on B: B(col1, col2, col3).
The next attempt would be to switch this to a join:
update A
set col4 = 0
from (select col1, col2, col3
from B
group by col1, col2, col3
having count(*) < 7
) B
where A.col1 = B.col1 and A.col2 = B.col2
and A.col3 = B.col3 ;
I'm trying to determine if there is a better way to do this in SQL. My goal is to run one query which returns two values that are to be used for another query. See below
select *
from table2
where col1 =
( select col1
from table1
where id = 123 )
and col2 =
( select col2
from table1
where id = 123 );
Is there a way to simplify this code by either doing a where clause that checks both values against one nested query, or by running the first querying and somehow setting the values of col1 and col2 to variables that I can use in the second query?
You can do
select *
from table2
where (col1, col2) = (select col1, col2
from table1
where id = 123)
SELECT DISTINCT a.*
FROM table2 a
INNER JOIN table1 b
ON a.col1 = b.col1
AND a.col2 = b.col2
WHERE b.id = 123
you can simply use query as below
select t2.* from table2 t2,table1 t1 where t1.col1=t2.col1 and
t1.col2=t2.col2 and t1.id=123
Seems like you've got it backwards. Since you know exactly what you want from table1 (so, presumably, the query is smaller), you should start by getting the data from table1, then join the releveant rows from table2:
select table2.*
from table1
inner join table2
on table2.col1 = table1.col1
and table2.col2 = table1.col2
where table1.id = 123
I'm trying to update multiple columns in a MS SQL statement using a sub-query. A search led me to something like:
UPDATE table1
SET col1 = a.col1, col2 = a.col2, col3 = a.col3 FROM
(SELECT col1, col2, col3 from table2 where <expression>) AS a
WHERE table1.col1 <expression>
Link
My problem is that in the inner WHERE expression I need a reference to a specific field in table1:
UPDATE table1
SET col1 = a.col1, col2 = a.col2, col3 = a.col3 FROM
(SELECT col1, col2, col3 from table2 where table1.col0 = table2.col0) AS a
WHERE table1.col1 <expression>
When I run that query I get "The multi-part identifier "table1.col0" could not be bound.
". Apparently when using that syntax SQL cannot bind the current table1 record in the subquery. Right now I am repeating the subquery for each field and using the syntax:
UPDATE table1
SET col1 = (subquery), col2 = (subquery)...
But that executes the subquery (which is very expensive) once per column, which I would like to avoid.
Any ideas?
in sql server, you can use a from clause in an update query. Join the tables as you would in a select. The table you are updating must be included in the joins.
update table_1
set field_1 = table_2.value_1
from table_1
inner join table_2
on (table_1.id = table_2.id)
Or if you dislike the join syntax this will also work:
UPDATE table1
SET col1 = a.col1, col2 = a.col2, col3 = a.col3
FROM table1, table2 as a
WHERE table1.col0 = a.col0
AND table1.col1 <expression>
Your car use CROSS APPLY command to update multiple columns from sub select
UPDATE t1
SET t1.col1 = a.col1, t1.col2 = a.col2, t1.col3 = a.col3
FROM table1 t1
CROSS APPLY
(SELECT col1, col2, col3 from table2 where table1.col0 = table2.col0) a(col1,col2,col3)