I would like to know how to convert a select query with multiple select in ( ) into a delete query. Field1 is a primary key in all the tables.
Query:
select field1 from table1 where field1 in
(select field1 from table2 where field19<>777) and field1 in
(select field1 from table2 where field19=777) and field1 in
(select field1 from table3 where field8=0 or field8 is null)
Never mind, it’s to late ;) it will return all the field1 so I can have the total numbers of field1 but we don’t want to delete field1, so it’s not possible. This will need to be done with a Join.
Thanks we can close this post.
Related
How can I query for the distinct field1 instances that have multiple distinct corresponding field2 values?
field1
field2
a
apple
b
grape
c
banana
b
orange
a
apple
In this example I want to return "b", since there are at least 2 distinct values (grape and orange) for field2 that correspond to it. I don't wan't "a" since there is only 1 unique field2 value that corresponds, "apple".
I have tried
with all_unique_combos as (
select distinct field1, field2
from table
)
select field1
from all_unique_combos
group by field1
having count(field2) > 1
I actually think this is right and would give me what I need. But at the moment it's returning 0 rows so I kinda need a sanity check. Thanks for any input either way.
You can use aggregation:
select field1
from t
group by field1
having min(field2) <> max(field2);
A straight-forward approach uses group by and having:
select field1
from mytable
group by field1
having min(field2) <> max(field2)
Using COUNT(DISTINCT ...):
select field1
from tab
group by field1
having count(disitnct field2) > 1
First timer here, so if I am doing something wrong, please do not hesitate in telling me.
I have a situation where I know what to do, but I do not know how to do it. Situation is as follows:
Take records from one table - now each record has to be split into two parts into a new table. This means I take 1 record and it ends up being 2 in another table. For this purpose I have made a CTE with SQL. This works perfectly. It splits the record into two parts. This is what the CTE looks like :
WITH
cteFirstLine (Field1, Field2, Field3) AS
(
SELECT TOP 1000 T.Field1, T.Field2, T.Field3
FROM Table1 T
LEFT JOIN Table2 P ON T.Field1 = P.Field4
ORDER BY Field2 DESC
)
,
cteSecondLine(Field1, Field2, Field3) AS
(
SELECT TOP 1000 T.Field1, T.Field2, T.Field3
FROM Table1 T
LEFT JOIN Table2 P ON T.Field1 = P.Field4
ORDER BY Field2 DESC
)
,
cteUnified (Field1, Field2, Field3)
AS
(
SELECT Field1, Field2, Field3
FROM cteFirstLine
UNION
SELECT Field1, Field2, Field3
FROM cteSecondLine
)
INSERT INTO Table_TEST(Field1, Field2, Field3)
SELECT TOP 2000 Field1, Field2, Field3
FROM cteUnified
OK, so this works. The problem I now have is that The old table, the table from which I have to process these records contains almost a million records. I know it is a lot, but it used to be way more than that, hence this task.
I need to know: how can I use this CTE inside a loop? Will a loop suffice? How would I consturct the loop so that it can do everything automatically. By Automatically I mean instead of doing only 1000 records at a time, but more.
Can anyone please help?
I want to select all records where field contains values from another field. How do I do that? Here is a code that I am trying.
select field1 , field2, field3
from table1
where field1 like '%'+(select distinct field4 from table2)+'%'
Thanks in advance.
Just do your like as a join condition:
select field1 , field2, field3
from table1
join (select distinct field4 from table2) x
on field1 like '%'+field4+'%'
Using the original structure of your query, you can do:
select field1, field2, field3
from table1 t1
where exists (select 1
from table2
where t1.field1 like '%' + field4 + '%'
);
The advantage of this method is that it will not duplicate records in table1. For instance, if there are two rows in table2 with the values 'a' and 'b' respectively and one row in table1 with the value 'ab', then this method will only return the row from table1 once.
I'm trying to make an SQL query using INSERT INTO to copy a number of rows from a table, but I want one of the fields (which isn't a primary key) to have a new, unique, int (I know this is terrible database design, but I can't change the structure of the DB).
Here is a simplified example of what I'm trying to do:
INSERT INTO Mytable
(field1
,field2
,field3
,nonKeyUniqueInt)
SELECT
(field1
,field2
,field3
,(SELECT MAX(nonKeyUniqueInt)+1 FROM mytable)
FROM
mytable
WHERE
(conditions)
However this doesn't work because the SELECT MAX query only runs once, giving all my new rows the same value for that field. Given the following rows to copy:
field1 field2 field3 nonKeyUniqueInt
x y z 1
a b c 2
I get output of:
field1 field2 field3 nonKeyUniqueInt
x y z 1
a b c 2
x y z 3
a b c 3
Is what I'm trying to do possible?
The problem is that the subquery gets evaluated once for the insert, not once per row. The solution is to use row_number():
INSERT INTO Mytable(field1, field2, field3, nonKeyUniqueInt)
SELECT field1, field2, field3,
x.maxk + row_number() over (order by (select NULL))
FROM mytable CROSS JOIN
(SELECT MAX(nonKeyUniqueInt) as maxk FROM mytable) x
WHERE (conditions);
I moved the max calculation to the FROM clause to make it clear that it is evaluated only once.
I have a very large query that follows the format below:
select ...
from ( select field1,
field2
from some_table ) table1,
( select field1,
field3
from other_table ) table2
where .....
Is is possible for me to refer to one of the tables "defined" in the from clause, lets say table1, in one of the other table definitions in the from clause?
For example:
select ....
from ( select field1,
field2
from some_table ) table1,
( select table1.field1,
field3
from other_table,
table1 ) table2
where .....
Disclaimer: What I am trying to do is not as simple as the example above. The example is simply to illustrate the idea.
WITH
table1 AS
(
SELECT field1, field2
FROM some_table
),
table2 AS
(
SELECT field1, field2
FROM other_table, table1
)
SELECT *
FROM table2
If you are using SQL 2005, you can use Common Table Expressions for doing what you are trying; Quassnoi gives us an example, in Oracle I don't know how to achieve it though