selecting column which is another table column value.what's wrong with this query
select
geoName
from
hgeo h
where
geoName not in (select (select column_name
from information_schema.columns
where ((column_name = h.levelName) and (table_Name = 'flatgeo')))
from flatgeo)
I think there is some problem
(select
(select column_name
from information_schema.columns
where ((column_name = h.levelName) and (table_Name = 'flatgeo')))
How to write it?
You can't use meta-information to change the shape of your query. Unless you want to go down the dynamic sql route (which can quickly get complex), I'd go for the following:
SELECT
geoName
FROM hgeo h
WHERE
NOT EXISTS (SELECT * FROM flatgeo f
WHERE
(h.levelName = 'value1' and f.value1 = h.geoName) OR
(h.levelName = 'value2' and f.value2 = h.geoName)
//Repeat for each possible levelName value.
)
you can try this
select GeoName from HGeo where HGeo.GeoName not in
(select FlatGeo.value1 from FlatGeo union
select FlatGeo.value2 from FlatGeo union
select FlatGeo.value3 from FlatGeo)
You should use tablename before your column geoname. Like hgeo.geoname.
do you want to find out the geoname of hgeo that are not in flatgeo?
Related
I am trying to exclude a column (county) that is shared by both table so I can do a LEFT JOIN in a query. This is what I thought would work but there seems to be some issues (below produces the ERROR: syntax error at or near "LEFT"). What can I do to get this work?
CREATE TABLE levee_prioritization.svi_justice_communities_joined AS
SELECT * FROM ((SELECT COLUMN_NAME FROM information_schema.columns
WHERE table_schema = 'levee_prioritization'
AND table_name = 'svi2018_us_tract') AS allColumns
WHERE allColumns.COLUMN_NAME NOT IN ('county'))
LEFT JOIN levee_prioritization.justice40_communities b ON (a.fips =
LPAD(round(b.geoid_tract)::text, 11));
You didn't specify the view name for inner select:
CREATE TABLE levee_prioritization.svi_justice_communities_joined AS
SELECT * FROM (SELECT * FROM (SELECT COLUMN_NAME FROM information_schema.columns
WHERE table_schema = 'levee_prioritization'
AND table_name = 'svi2018_us_tract') AS allColumns
WHERE allColumns.COLUMN_NAME NOT IN ('county')) a
LEFT JOIN levee_prioritization.justice40_communities b
ON (a.fips = LPAD(round(b.geoid_tract)::text, 11));
I have a query with UNION ALL statements.
SELECT * FROM HULL_A
UNION ALL
SELECT * FROM HULL_B;
It throws the error
ORA-01790 "expression must have same datatype as corresponding
expression"
However, I checked that, and I think it is not the case. I used the following to check:
select db1.data_type, db2.data_type, db1.data_length, db2.data_length, db1.data_precision, db2.data_precision, db1.data_scale, db2.data_scale
from all_tab_columns db1
inner join all_tab_columns db2
on (db1.owner = db2.owner
and db1.column_name = db2.column_name)
where db1.table_name = 'HULL_A'
and db2.table_name = 'HULL_B'
and (
db1.data_type = db2.data_type
OR
db1.data_length = db2.data_length
)
The result is:
I was able to link HULL_A to HULL_C and HULL_D using UNION ALL.
So why is ORACLE throwing the error? What other test could I perform in order to be able to perform the UNION ALL?
I am working on WINDOWS 10 ORACLE 11g
Columns in both the table must have the same data type and order of the columns must be the same.
You can try the following query to identify the mismatch in columns of both the tables:
SELECT
A.COLUMN_ID COLUMN_HULL_A,
A.DATA_TYPE DATA_TYPE_HULL_A,
B.COLUMN_ID COLUMN_HULL_B,
B.DATA_TYPE DATA_TYPE_HULL_B
FROM
(
SELECT
COLUMN_ID,
DATA_TYPE
FROM
USER_TAB_COLUMNS
WHERE
TABLE_NAME = 'HULL_A'
) A
FULL OUTER JOIN (
SELECT
COLUMN_ID,
DATA_TYPE
FROM
USER_TAB_COLUMNS
WHERE
TABLE_NAME = 'HULL_B'
)B ON (A.COLUMN_ID = B.COLUMN_ID AND A.DATA_TYPE = B.DATA_TYPE)
WHERE
A.COLUMN_ID IS NULL
OR B.COLUMN_ID IS NULL;
Cheers!!
I have IBM DB2 database. I would like to get all column names, types length and scale grouped by table names.
To get all tables from schema XYZ:
select name
from SYSIBM.SYSTABLES
where creator = 'XYZ';
Now I can get colum descriptions for given table:
SELECT NAME, COLTYPE, LENGTH, SCALE
FROM SYSIBM.SYSCOLUMNS
WHERE TBNAME = 'sometablename'
I would like to group it:
SELECT NAME, COLTYPE, LENGTH, SCALE
FROM SYSIBM.SYSCOLUMNS
WHERE TBNAME in (select name from SYSIBM.systables where creator = 'XYZ')
GROUP BY table_names_from_schema_xyz;
How to do it?
Grouping (in the SQL sense) only makes sense in the context of aggregation functions.
I suspect what you are looking for is the output ordered by table name, then column name, so all columns of the same table are "grouped" together.
This query might work for you.
SELECT T.NAME AS TABNAME, C.NAME AS COLNAME, COLTYPE, LENGTH, SCALE
FROM SYSIBM.SYSTABLES T, SYSIBM.SYSCOLUMNS C
WHERE T.NAME = C.TBNAME
AND CREATOR = 'XYZ'
ORDER BY T.NAME, C.NAME;
Your question can be answered only from SYSCAT.COLUMNS
select tabname, colname, typename, length, scale
from syscat.columns
where tabschema = 'XYZ'
order by tabname, colno
Try inner join with SYSIBM.systables,probably the below example should work
select c.colname,
t.tabname as tables, COLTYPE, LENGTH, SCALE
from SYSIBM.SYSCOLUMNS c
inner join SYSIBM.systables t on
t.tabschema = c.tabschema and t.tabname = c.tabname
where t.type = 'T'
and t.tabschema = 'XYZ'
order by c.colname;
I have a select query where I am doing inner joins and in AND clause I am checking like this
AND UPPER (b.a1||b.a2) IN
(
select a.a1||a.a2
from a
where a.a3 =
(
select UPPER(decode('609',null,c.c1,'609'))
from dual
)
)
but so because of || opertaor it is taking more than 2 minutes. Can we have any other solution to this?
How about using EXISTS clause?
AND EXISTS (
SELECT
1
FROM
a
WHERE
a.a3 = (SELECT UPPER(DECODE('609',c.c1,'609')) FROM dual) -- this condition is pretty odd
AND a.a1 = UPPER(b.a1)
AND a.a2 = UPPER(b.a2)
)
Adding Function Based Indexes on UPPER(b.a1) AND UPPER(b.a2) may help as well.
Speaking of that odd condition: (SELECT UPPER(DECODE('609',c.c1,'609')) FROM dual):
Why do you perform a SELECT from dual there?
What you check is - if '609' equals c.c1 ('609') then a.a3 must equal '609', in any other case your SELECT returns NULL, thus no value from table a is returned. So you can just change the entire condition to a.a3 = '609'.
Try with a JOIN
SELECT *
FROM b
JOIN ( select UPPER(a.a1), UPPER(a.a2)
from a
where a.a3 = (select UPPER(decode('609',c.c1,'609')) from dual)
) a
ON UPPER(b.b1) = a.a1
AND UPPER(b.b2) = a.a2
But the problem is when you do UPPER(b.b1) or b1||b2 you cant use the index anymore.
You may need a function index
You don't need to concatenate. In fact: concatenating the values is a bug waiting to happen: consider the the values foob and ar in table b and foo and bar in a - the concatenation treats those as the same tuple although they are not.
You also don't need the select from dual for a constant:
AND (UPPER (b.a1), upper(b.a2)) IN
(
select a.a1. a.a2
from a
where a.a3 = UPPER(decode('609',null,c.c1,'609'))
)
An index on b(a1,a2) can't be used for this, but you can create an index on b (UPPER (b.a1), upper(b.a2)) which would be used.
I have a variable called c_kilometers. I have a cursor that grabs a bunch of records that have those kilometers. I need to run two separate SELECT statements in the cursor that simply grab a kilometer from one table based on values in the cursor, and run another SELECT doing the same thing on another table.
SELECT t.kilometers INTO c_kilometers
FROM table_name WHERE WHERE l.code = cursor_t.code_att
SELECT g.kilometers INTO c_kilometers
FROM table_name WHERE l.code = cursor_t.code_aff
My question is can I add the c_kilometers together without creating a temporary variable to hold on of the values? I haven't used PL/SQL in awhile, and I don't remember having to do this ever, so this is more of a learning question than anything.
Provided that both your queries always return exactly one row, you can do either of the following:
/* Variant 1 */
SELECT t.kilometers + g.kilometers
INTO c_kilometers
FROM table_name t, table_name2 g
WHERE etc1
AND etc2
/* Variant 2 */
SELECT t.kilometers
INTO c_kilometers
FROM table_name
WHERE etc;
SELECT c_kilometers + g.kilometers
INTO c_kilometers
FROM table_name2
WHERE etc;
If they're in the same table as you posted, you can use:
SELECT COALESCE(SUM(kilometers), 0)
INTO c_kilometers
FROM table_name
WHERE l.code IN (cursor_t.code_aff, cursor_t.code_att)
It seems that it will be more efficient to put table_name into your SELECT query that produces the cursor.
If you post this query, I'll probably can help to do this.
Join the SELECTs like this:
SELECT a.kilometers + b.kilometers
FROM (SELECT code, kilometers FROM table_name WHERE ...) a JOIN
(SELECT code, kilometers FROM table_name WHERE ...) b ON a.code = b.code