Syntax Error in SQL - sql

SELECT *
INTO Temp3
from
( SELECT B.Name
FROM [Temp2] as B
WHERE B.Name
Not IN (
SELECT E.WorkerName
FROM WorkerDetail as E ) )
Why does this produce an error?

If you want to use a derived table you need to alias it:
SELECT T1.*
INTO Temp3
from
( SELECT B.Name
FROM [Temp2] as B
WHERE B.Name
Not IN (
SELECT E.WorkerName
FROM WorkerDetail as E ) ) AS T1
I'm not sure if you actually need to use a derived table, however.
This should also work:
SELECT B.Name
INTO Temp3
FROM [Temp2] as B
WHERE B.Name
Not IN (
SELECT E.WorkerName
FROM WorkerDetail as E )

Maybe Temp3 already exists?
In MSSQL SELECT..INTO used to populate new table with data.
If this table exist, you can use INSERT INTO .. SELECT FROM statement.

Related

How to create a select clause using a subquery

I have the following sql statement:
WITH
subquery AS (
select distinct id from a_table where some_field in (1,2,)
)
select id from another_table where id in subquery;
Edit
JOIN is not an option (this is just a reduced example of a bigger query)
But that obviously does not work. The id field exists in both tables (with a different name, but values are the same: numeric ids). Basically what I want to do is filter by the result of the subquery, like a kind of intersection.
Any idea how to write that query in a correct way?
You need a subquery for the second operand of IN that SELECTs from the CTE.
... IN (SELECT id FROM subquery) ...
But I would recommend to rewrite it as a JOIN.
Are you able to join on ID and then filter on the Where clause?
select a.id
from a.table
inner join b.table on a.id = b.id
where b.column in (1,2)
Since you only want the id from another_table you can use exists
with s as (
select id
from a_table
where some_field in (1,2)
)
select id
from another_table t
where exists ( select * from s where s.id=t.id )
But the CTE is really redundant since all you are doing is
select id
from another_table t
where exists (
select * from a_table a where a.id=t.id and a.some_field in (1,2)
)

Converting sub query

How can I convert this subquery to join query or something more simple like with?
SELECT *
FROM ViewBuy99
WHERE Id NOT IN (
SELECT Id
FROM Buy99
)
INSERT INTO Buy99
SELECT *
FROM ViewBuy99
WHERE Id NOT IN (
SELECT Id
FROM Buy99
)
You can express this using JOIN as:
insert into Buy99
select vb.*
from ViewBuy99 vb left join
Buy99 b
on b.id = vb.id
where b.id is null;
However, I would recommend NOT EXISTS:
insert into Buy99
select vb.*
from ViewBuy99 vb
where not exists (select 1
from Buy99 b
where b.id = vb.id
);

SQL-TABLE CREATION

Please explain to me the use of the comma after 'FROM TABLE_ABC A'. how does it work in the execution of the sql query.
CREATE TABLE ABCD AS
( SELECT A.*
FROM TABLE_ABC A,
(SELECT COL_1,COL_2 FROM
(SELECT B.*,C.* FROM
TABLE_XYZ B, TABLE_MNO C
WHERE B.COL_X=C.COL_Y
)D
)A.COL_C=D.COL_D
)
WITH DATA PRIMARY INDEX(SASAJS)
It is similar to join
select * from #tempA ta join #tempB tb
on ta.ID = tb.ID
same as
select * from #tempA ta, #tempB tb
where ta.ID = tb.ID
Using JOINS makes the code easier to read
You're using a select to create a 2nd table that is also created from another subselect. See it like this and you'll understand it better:
CREATE TABLE ABCD AS(
SELECT
A.*
FROM
TABLE_ABC A,
(
SELECT
COL_1,
COL_2
FROM
(
SELECT
B.*,
C.*
FROM
TABLE_XYZ B,
TABLE_MNO C
WHERE
B.COL_X = C.COL_Y
) D
)
WHERE
A.COL_C = D.COL_D
) WITH DATA PRIMARY INDEX(SASAJS)
but your original code is lacking a WHERE CLAUSE before A.COL_C = D.COL_D. I included it.
I'm assuming that B.* or C.* has a column named COL_D AND A also has it. It would also be better if the D as after the ) before the last WHERE

Written a subquery that can return more than one field without using the Exists

The query below is supposed to pull records for fields with the max date.
I am getting an error
You have written a subquery that can return more than one field without using EXISTS reserved word in the Main query's FROM clause. Revise the SELECT statement of the subquery to request only one column.
Code:
SELECT *
FROM TableName
WHERE (((([Project_Name], [Date])) IN (SELECT Project_Name, MAX(Date)
FROM TableName
GROUP BY Project)));
Your probably thinking of a nested subquery used as a table, like the below:
select a.*, b.1, b.2
from FirstTable A
join (Select Id, firstcolumn as 1, secondcolumn as 2
from SecondTable) B on b.ID = a.ID
Works pretty much like a regular join except you are using a subquery. Hope that helps,
SELECT A.*
FROM TableName A
INNER JOIN (select Project_Name, max(Date) MaxDate
from TableName
group by Project) B
ON A.[Project_Name] = B.[Project_Name]
AND A.[Date] = B.MaxDate
A version using EXISTS() looks like this:
SELECT *
FROM TableName AS A
WHERE EXISTS(
SELECT * FROM (
SELECT B.Project_Name, MAX( B.Date ) AS MaxDate
FROM TableName AS B
GROUP BY B.Project_Name ) AS C
WHERE C.Project_Name = A.Project_Name AND C.MaxDate = A.Date
);
Although I have the feeling this will have poorer performance than a JOIN because the GROUP BY statement might have to be executed for each record and each call to the EXISTS() function...

SQL: how to find unused primary key

I've got a table with > 1'000'000 entries; this table is referenced from about 130 other tables. My problem is that a lot of those 1-mio-entries is old and unused.
What's the fastet way to find the entries not referenced by any of the other tables? I don't like to do a
select * from (
select * from table-a TA
minus
select * from table-a TA where TA.id in (
select "ID" from (
(select distinct FK-ID "ID" from table-b)
union all
(select distinct FK-ID "ID" from table-c)
...
Is there an easier, more general way?
Thank you all!
You could do this:
select * from table_a a
where not exists (select * from table_b where fk_id = a.id)
and not exists (select * from table_c where fk_id = a.id)
and not exists (select * from table_d where fk_id = a.id)
...
try :
select a.*
from table_a a
left join table_b b on a.id=b.fk_id
left join table_c c on a.id=c.fk_id
left join table_d d on a.id=d.fk_id
left join table_e e on a.id=e.fk_id
......
where b.fk_id is null
and c.fk_id is null
and d.fk_id is null
and e.fk_id is null
.....
you might also try:
select a.*
from table_a a
left join
(select b.fk_id from table_b b union
select c.fk_id from table_c c union
...) table_union on a.id=table_union.fk_id
where table_union.fk_id is null
This is more SQL oriented and it will not take forever like the above solution.
Not sure about efficiency but:
select * from table_a
where id not in (
select id from table_b
union
select id from table_c )
If your concern is allowing the database to continue normal operations while you do the house keeping you could split it into multiple stages:
insert into tblIds
select id from table_a
union
select id from table_b
as may times as you need and then:
delete * from table_a where id not in ( select id from tableIds )
Of course sometimes doing a lot of processing takes a lot of time.
I like #Patrick's answer above, but I would like to add to that.
Rather than building the 130-step query by hand, you could build these INSERT statements by scanning sysObjects, finding key relations and generating your INSERT statements.
That would not only save you time, but should also help you to know for sure whether you've covered all the tables - maybe there are 131, or only 129.
I'm inclined to Marcelo Cantos' answer (and have upvoted it), but here is an alternative in an attempt to circumvent the problem of not having indexes on the foreign keys...
WITH
ids_a AS
(
SELECT id FROM myTable
)
,
ids_b AS
(
SELECT id FROM ids_a WHERE NOT EXISTS (SELECT * FROM table_a WHERE fk_id = ids_a.id)
)
,
ids_c AS
(
SELECT id FROM ids_b WHERE NOT EXISTS (SELECT * FROM table_b WHERE fk_id = ids_b.id)
)
,
...
,
ids_z AS
(
SELECT id FROM ids_y WHERE NOT EXISTS (SELECT * FROM table_y WHERE fk_id = ids_y.id)
)
SELECT * FROM ids_z
All I'm trying to do is to suggest an order to Oracle to minimise its efforts. Unfortunately Oracle will compile this to comething very similar to Marcelo Cantos' answer and it may not performa any differently.