An object or column name is missing or empty GEOMETRY - sql

I'm trying to do a geometric union but it's not working
select b.[Region],geometry::UnionAggregate(geom.MakeValid()) into dbo.DRShape
from [dbo].[departements2] a join [dbo].[BM_REGIONFR] b
on a.[code_insee] = b.[dep_2]
group by b.Region
Msg 1038, Niveau 15, État 5, Ligne 1
An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.

You are trying to create a table using SELECT INTO, the table needs a column name for each column. Your second column result is missing an alias (that will turn into a column name).
select
b.[Region],
geometry::UnionAggregate(geom.MakeValid()) AS Geo -- Add Alias!!
into
dbo.DRShape
from [dbo].[departements2] a join [dbo].[BM_REGIONFR] b
on a.[code_insee] = b.[dep_2]
group by b.Region
If you comment the INTO dbo.DRShape and you see that your query runs OK, means that the problem is the creation of your new table.

Related

Why am I getting the error: "Ambiguous column" in my query?

In this query I inserting records into a new empty table I created. These records are derived from another table where I am left joining that table to itself, in order to output records that are not included in the recent table that is appended on top of an older table. So basically it outputs records that were deleted.
CREATE DEFINER=`definer` PROCEDURE `stored_procedure_name`()
MODIFIES SQL DATA
SQL SECURITY INVOKER
BEGIN
START TRANSACTION;
INSERT INTO exceptions_table (
`insert_date`,
`updated`,
`account_number`,
`id_number`)
SELECT
`insert_date`,
`updated`,
`account_number`,
`id_number`
FROM original_table ot1
LEFT JOIN original_table ot2
ON ot1.`account_number` = vdcaas2.`account_number`
AND ot2.`insert_date` = '2022-12-20'
WHERE ot1.`insert_date` = '2022-12-10'
AND ot2.`account_number` IS NULL;
COMMIT;
END
I get an error stating: "SQL Error: Column "insert_date" in field list is ambiguous.
I'm not sure why because I have specified which table I am grabbing "insert_date" from when INSERTING and when SELECTING and JOINING..
Every row in your query has two columns called insert_date: one from the table you've aliased as "ot1", and one from the table (as it happens, the same table) you've aliased as "ot2".
The database system doesn't know which one you want, so you have to tell it by writing either "ot1.insert_date" or "ot2.insert_date", just as you do elsewhere in the query:
... ot2.`insert_date` = '2022-12-20'
...
... ot1.`insert_date` = '2022-12-10'
The same is true of the other columns you've listed to select.
You need to change this
SELECT
`insert_date`,
`updated`,
`account_number`,
`id_number`
to this
SELECT
ot1.`insert_date`,
ot1.`updated`,
ot1.`account_number`,
ot1.`id_number`
or this
SELECT
ot2.`insert_date`,
ot2.`updated`,
ot2.`account_number`,
ot2.`id_number`
or some combination
Issue
SQL Error: Column "insert_date" in field list is ambiguous error means that the query is trying to reference the "insert_date" column from both tables, ot1 and ot2.
Try the following:
SELECT
ot1.`insert_date`,
ot1.`updated`,
ot1.`account_number`,
ot1.`id_number`
Also, you have a typo in your query:
ON ot1.`account_number` = vdcaas2.`account_number` -> ON ot1.`account_number` = ot2.`account_number`

VIEW Duplicate column name 'ISLAND'

CREATE VIEW ALL_TABLES AS SELECT * FROM employee_view, av_pay;
I keep getting error message how do I overcome this
VIEW Duplicate column name 'ISLAND'
av_pay:
employee_view:
You are doing a select *, which will output the same column names as defined in the tables you are querying. As you have both columns defined with the same name in both, there you have the error.
So either rename one of the columns or change the query to something like:
select employee_view.ISLAND ISLAND_V, av_pay.ISLAND ISLAND_P, ... FROM ...
The db engine complaints because your select clause is "*" and both the source tables contain the column "island". As a result, the dbms does not know which column should be returned - from employee_view or av_pay?
BTW, a select from 2 tables without a join will result in a cartesian product...

CREATE TABLE failed ORA 00957 Duplicate column name

As I tried to create new table from existing 2 table with specific column name in oracle.
I tried below code
CREATE TABLE D_T1
AS
SELECT a.col1, a.col2, a.col3, a.col4, a.col5, b.col6, b.col7, b.col8
FROM db1.table1 a INNER JOIN db1.table2 b
ON (a.col1 = b.colNum AND a.col2 = b.colnum1)
But I get error
CREATE TABLE failed ORA 00957 Duplicate column name
Can anyone help?
Ignoring the other errors you seem to have introduced by retyping the code, you've shown that you do have a duplicate column, which is what the error is telling you:
a.VALIDFLAG, b.VALIDFLAG
You seem to be under the impression that the table (alias) prefix makes the column names in the projection unique. They do not. The table prefix tells Oracle which table to get the column value from (unless you're using the using join syntax, which you are not). If the column appears in two tables you have to prefix the column name with the table. If you want the value from both tables you have to prefix both of them.
With a simple query then referring to both table columns without column aliases is OK, but something trying to consume the result set might struggle. This is fine:
select a.dummy, b.dummy
from dual a
join dual b on b.dummy = a.dummy;
DUMMY DUMMY
------- -------
X X
But notice that both columns have the same heading. If you tried to create a table using that query:
create table x as
select a.dummy, b.dummy
from dual a
join dual b on b.dummy = a.dummy;
You'd get the error you see, ORA-00957: duplicate column name.
If you alias the duplicated columns then the problem goes away:
create table x as
select a.dummy as dummy_a, b.dummy as dummy_b
from dual a
join dual b on b.dummy = a.dummy;
So in your case you can alias those columns, if you need both:
..., a.VALIDFLAG AS validflag_a, b.VALIDFLAG AS validflag_b, ...
To be completely honest, that query is a mess. You've got several errors in your SQL statement:
CREATE TABLE AS SELECT
The table name is missing - this should be
CREATE TABLE my_new_table AS SELECT
to create a new table named my_new_table.
a.ALIDFLAG,b,VALIDFLAG,
I've got a suspicion that this should really be a.VALIDFLAG instead of a.ALIDFLAG. Also, you need to replace b,VALIDFLAG with b.VALIDFLAG.
SELECT a.BILLFREQ a.CDHRNUM,
You're missing a comma after a.BILLFREQ - this is a syntax error.
a.A‌​GNYCOY,a.AGNTCOY
There's the culprit - you're selecting the same column twice. Get rid of the second one.
EDIT Actually, the names are different, so this isn't the cause of the error (unless you've mistyped your query in the comment instead of copy& paste).
To debug this kind of errors, try to
format your SQL statement in a readable way
comment out everything but one column, run the statement and ensure it works
add one column
repeat until you find the error or you've added all columns
2ND UPDATE
With the updated query, the error is here:
a.VALIDFLAG,
b,
VALIDFLAG,
You have two columns named VALIDFLAG - use an alias for one of these, and it should work.
ORA-00957: duplicate column name
The only reason for that error in your CTAS statement is that you have similar column name in the SELECT statement. Though you might be referring to different table columns, but you did not use a column alias
Error reproduce:
Using the standard EMP and DEPT table.
SQL> CREATE TABLE D_T1 AS
2 SELECT a.deptno,
3 b.deptno
4 FROM emp A
5 INNER JOIN dept b
6 ON (a.deptno = b.deptno);
b.deptno
*
ERROR at line 3:
ORA-00957: duplicate column name
Workaround:
Use proper alias:
SQL> CREATE TABLE D_T1 AS
2 SELECT a.deptno e_deptno, --add column alias
3 b.deptno d_deptno --add column alias
4 FROM emp a
5 INNER JOIN dept b
6 ON (a.deptno = b.deptno);
Table created.
Edit as per new input from OP.
You have some syntax error in your query. Corrected those. I also checked for duplicate columns. There are none. Now run this and let me know if you still get same error.
CREATE TABLE D_T1 AS SELECT
a.B, a.C,a.C, a.C, a.C,a.J, a.O,
a.P,a.P,a.S,a.S,a.R,a.B,
a.S,a.S,b.A,b.C,b.C,b.I, b.M,
b.P,b.P,b.S,b.S,b.S,b.Z,b.Z,a.C,
a.CH‌​,a.G,b,V,b.T,a.C,a.C,a.C,
a.A,a.A‌​Ga.AG
FROM tbl1 a JOIN tbl2 b
ON (a.C= b.C AND a.C1= b.C1)

Compare comma separated list with individual row in table

I have to compare comma separated values with a column in the table and find out which values are not in database. [kind of master data validation]. Please have a look at the sample data below:
table data in database:
id name
1 abc
2 def
3 ghi
SQL part :
Here i am getting comma separated list like ('abc','def','ghi','xyz').
now xyz is invalid value, so i want to take that value and return it as output saying "invalid value".
It is possible if i split those value, take it in temp table, loop through each value and compare one by one.
but is there any other optimal way to do this ??
I'm sure if I got the question right, however, I would personally be trying to get to something like this:
SELECT
D.id,
CASE
WHEN B.Name IS NULL THEN D.name
ELSE "invalid value"
END
FROM
data AS D
INNER JOIN badNames B ON b.Name = d.Name
--as SQL is case insensitive, equal sign should work
There is one table with bad names or invalid values if You prefer. This can a temporary table as well - depending on usage (a black-listed words should be a table, ad hoc invalid values provided by a service should be temp table, etc.).
NOTE: The select above can be nested in a view, so the data remain as they were, yet you gain the correctness information. Otherwise I would create a cursor inside a function that would go through the select like the one above and alter the original data, if that is the goal...
It sounds like you just need a NOT EXISTS / LEFT JOIN, as in:
SELECT tmp.InvalidValue
FROM dbo.HopeThisIsNotAWhileBasedSplit(#CSVlist) tmp
WHERE NOT EXISTS (
SELECT *
FROM dbo.Table tbl
WHERE tbl.Field = tmp.InvalidValue
);
Of course, depending on the size of the CSV list coming in, the number of rows in the table you are checking, and the style of splitter you are using, it might be better to dump the CSV to a temp table first (as you mentioned doing in the question).
Try following query:
SELECT SplitedValues.name,
CASE WHEN YourTable.Id IS NULL THEN 'invalid value' ELSE NULL END AS Result
FROM SplitedValues
LEFT JOIN yourTable ON SplitedValues.name = YourTable.name

SQL statement to return data from a table in an other sight

How would the SQL statement look like to return the bottom result from the upper table?
The last letter from the key should be removed. It stands for the language. EXP column should be split into 5 columns with the language prefix and the right value.
I'm weak at writing more or less difficult SQL statements so any help would be appreciated!
The Microsoft Access equivalent of a PIVOT in SQL Server is known as a CROSSTAB. The following query will work for Microsoft Access 2010.
TRANSFORM First(table1.Exp) AS FirstOfEXP
SELECT Left([KEY],Len([KEY])-2) AS [XKEY]
FROM table1
GROUP BY Left([KEY],Len([KEY])-2)
PIVOT Right([KEY],1);
Access will throw a circular field reference error if you try to name the row heading with KEY since that is also the name of the original table field that you are deriving it from. If you do not want XKEY as the field name, then you would need to break apart the above query into two separate queries as shown below:
qsel_table1:
SELECT Left([KEY],Len([KEY])-2) AS XKEY
, Right([KEY],1) AS [Language]
, Table1.Exp
FROM Table1
ORDER BY Left([KEY],Len([KEY])-2), Right([KEY],1);
qsel_table1_Crosstab:
TRANSFORM First(qsel_table1.Exp) AS FirstOfEXP
SELECT qsel_table1.XKEY AS [KEY]
FROM qsel_table1
GROUP BY qsel_table1.XKEY
PIVOT qsel_table1.Language;
In order to always output all language columns regardless of whether there is a value or not, you need to spike of those values into a separate table. That table will then supply the row and column values for the crosstab and the original table will supply the value expression. Using the two query solution above we would instead need to do the following:
table2:
This is a new table with a BASE_KEY TEXT*255 column and a LANG TEXT*1 column. Together these two columns will define the primary key. Populate this table with the following rows:
"AbstractItemNumberReportController.SelectPositionen", "D"
"AbstractItemNumberReportController.SelectPositionen", "E"
"AbstractItemNumberReportController.SelectPositionen", "F"
"AbstractItemNumberReportController.SelectPositionen", "I"
"AbstractItemNumberReportController.SelectPositionen", "X"
qsel_table1:
This query remains unchanged.
qsel_table1_crosstab:
The new table2 is added to this query with an outer join with the original table1. The outer join will allow all rows to be returned from table2 regardless of whether there is a matching row in the table1. Table2 now supplies the values for the row and column headings.
TRANSFORM First(qsel_table1.Exp) AS FirstOfEXP
SELECT Table2.Base_KEY AS [KEY]
FROM Table2 LEFT JOIN qsel_table1 ON (Table2.BASE_KEY = qsel_table1.XKEY)
AND (Table2.LANG = qsel_table1.Language)
GROUP BY Table2.Base_KEY
PIVOT Table2.LANG;
Try something like this:
select *
from
(
select 'abcd' as [key], right([key], 1) as id, expression
from table1
) x
pivot
(
max(expression)
for id in ([D], [E])
) p
Demo Fiddle