Excluding a column from an inner join in PostgreSQL - sql

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));

Related

Set table name in a SELECT statement from a nested select

I have a table in MSSQL which has been called tables_list, it has a list of tables.
How can I use this list in another SELECT to count number of rows in each table and reach a list like the following picture:
SELECT T.[TABLE_NAME], count(*) AS NUMBERS
FROM [db_name].[schema_name].[T.[TABLE_NAME]]
(SELECT [TABLE_NAME],
FROM [db_name].[schema_name].[tables_list]) T
in sql server you can use system partition table to get the row counts in a database :
SELECT * FROM
(SELECT
QUOTENAME(SCHEMA_NAME(sOBJ.schema_id)) + '.' + QUOTENAME(sOBJ.name) AS [TableName],
SUM(sPTN.Rows) AS [RowCount]
FROM
sys.objects AS sOBJ
INNER JOIN sys.partitions AS sPTN ON sOBJ.object_id = sPTN.object_id
WHERE
sOBJ.type = 'U'
AND sOBJ.is_ms_shipped = 0x0
AND index_id < 2 -- 0:Heap, 1:Clustered
GROUP BY
sOBJ.schema_id,
sOBJ.name
ORDER BY [TableName]) X
WHERE X.[TableName] IN
(SELECT
CONCAT('[schema_name].[prefix', [TABLE_NAME], ']')
FROM [db_name].[schema_name].[tables_list])

Is there a function to return which phrase in array the row matched when using LIKE ANY (array['%term1%', '%term2%', etc.])?

I want all tables from my schema that match a list of other tables with similar names from a different schema. I am using the following query:
SELECT TABLE_NAME, COUNT(COLUMN_NAME) ColCount
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'schema_2' and TABLE_NAME LIKE ANY (array[
'%table1%',
'%table2%',
'%table3%',
'%table4%',
'%table5%'])
I want another column added to the output which specifies which name in the array the table from schema_2 matched, i.e.
TABLE_NAME COL_COUNT SCHEMA_1_TABLE
table1a 15 table1
Is there a way to do this?
You could try using a join. Kind of like:
WITH a AS (SELECT unnest(array['%table1%','%table2%','%table3%','%table4%','%table5%'])
AS table)
SELECT
isc.TABLE_NAME,
COUNT(isc.COLUMN_NAME),
REPLACE(a.table,'%','')
FROM
INFORMATION_SCHEMA.COLUMNS isc
LEFT JOIN a ON (isc.TABLE_NAME LIKE a.table)
WHERE
isc.TABLE_SCHEMA = 'schema_2'
AND a.table IS NOT NULL
GROUP BY
isc.TABLE_NAME,
a.table
You could also join INFORMATION_SCHEMA.COLUMNS with INFORMATION_SCHEMA.TABLES in order to just detect any duplication, not just the ones specified.

Why is ORACLE throwing ORA-01790?

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!!

Append Table Name to Field Name with Select *

Sorry if this is a duplicate. I have searched but only find aliasing fields and tables.
I have a query:
SELECT *
FROM MyTable1 ca LEFT OUTER JOIN MyTable2 dcn ON dcn.dstrct_code = ca.dstrct_code
LEFT OUTER JOIN MyTable2 cdn ON cdn.dstrct_code = ca.cost_dstrct_cde
LEFT OUTER JOIN MyTable3 bb ON bb.supplier_code = ca.supplier_code
WHERE ca.dstrct_code = '0001'
AND ca.req_232_type = 'P'
AND ca.requisition_no = '264982 000'
AND ca.alloc_count = '01'
ORDER BY ca.alloc_count ASC
Please dont shoot me down for using * im not done with the query yet. If I execute this query I get a row of data however the tables I am selecting from all have a good number of fields and many are simularly named. So my question is... Is there anyway to select * from and append the table name to the field name so it is more obvious which field belongs to which table?
I don't think there's a way to do that directly but you can do this instead. Run a query like this:
SELECT
(case t.name when 'MyTable1' then 'ca' when 'MyTable2' then 'dcn' when 'MyTable3' then 'cdn' when 'MyTable4' then 'bb' end)
+ '.' + c.name
+ ' AS "' + t.name + '.' + c.name + '",'
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE t.name in ('MyTable1', 'MyTable2', 'MyTable3', 'MyTable4')
ORDER BY t.name
Run it, preferably with results to Text (Ctrl+T), and use the results instead of the * in your original query. You have to manually remove the comma from the last line.
If you like the approach, you could streamline the process with some dynamic SQL.

select column which is another table column value

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?