Column Ambiguously Defined - No Line / Column Number

I've been trying to create a query that joins two sets of tables together using several columns. I have tried assigning aliases to each column but I am still receiving a&nbsp;<strong>(ORA-00918: column ambiguously defined)</strong>&nbsp;error with no line number / direction on which column oracle has an issue with. Can someone please help me? Been stuck on this issue for hours now. My code is below, thanks in advance!

I've been trying to create a query that joins two sets of tables together using several columns. I have tried assigning aliases to each column but I am still receiving a (ORA-00918: column ambiguously defined) error with no line number / direction on which column oracle has an issue with. Can someone please help me? Been stuck on this issue for hours now. My code is below, thanks in advance!

 SELECT *
 FROM
 (select v.value_id as i_value_id
 , v.value_nb as i_value_nb
 , v.utc_offset as i_utc_offset
 , v.data_Date as i_data_date
 , v.hr_utc as i_hr_utc
 , v.utc_offset as i_utc_offset
 , v.hr as i_hr
 , v.hr_num as i_hr_num
 , v.data_Code as i_data_code
 , v.Code as i_code
 , ff.form_Field_tx as i_form_field_tx
 , sv.submission_value_id as i_submission_value_id
 , s.submission_name_tx as i_submission_name_tx
 , s.submission_id as i_submission_id
 , fl.form_line_tx as i_form_line_tx
 , fs.form_section_tx as i_form_section_tx
 , sf.form_name_tx as i_form_name_tx
 , sf.form_label_tx as i_form_label_tx
 , sf.form_number_tx as i_form_number_tx
 , sf.survey_id as i_survey_id
   from value v
   join sub_value sv on v.value_id = sv.value_id
   join form_Field ff on sv.form_Field_id = ff.form_Field_id
   join sub s on sv.submission_id = s.submission_id
   LEFT OUTER JOIN form_line fl ON ff.form_line_id = fl.form_line_id
   LEFT OUTER JOIN form_section fs ON fl.form_section_id = fs.form_section_id
   LEFT OUTER JOIN survey_form sf ON fs.survey_form_id = sf.survey_form_id) subq1
   JOIN
   (select va.value_id as o_value_Id
   , va.value_nb as o_value_nb
   , va.utc_offset as o_utc_offset
   , va.data_Date as o_data_date
   , va.hr_utc as o_hr_utc
   , va.hr as o_hr
   , va.hr_num as o_hr_num


An Introduction to Oracle SQL

An Introduction to Oracle SQL

An Introduction to Oracle SQL: A quick tour of the six principal clauses of the SELECT statement.

This course is a brief presentation of Oracle SQL.
We will install a version of Oracle Express Edition and Oracle SQL Developer for free.
We'll then look at the SELECT statement with its 6 main clauses (SELECT, FROM, WHERE, GROUP BY, HAVING and ORDER BY).
Following this, we'll look at what else you need to know, and resources (which range from FREE to very expensive) which will help you to learn it..

What you'll learn

  • Either Download and Install Oracle Express Edition and Oracle SQL Developer on your computer - for free
  • Write your own SELECT statements.
  • Learn the SELECT, FROM, WHERE, GROUP BY, HAVING and ORDER BY clauses.
  • Get an easy way to remember the order of these clauses.
  • Find out what more you need to know, and resources in how to learn it.

What are the differences between Standard SQL and Transact-SQL?

What are the differences between Standard SQL and Transact-SQL?

In this article, we'll explain syntax differences between standard SQL and the Transact-SQL language dedicated to interacting with the SQL

#1 Names of Database Objects

In relational database systems, we name tables, views, and columns, but sometimes we need to use the same name as a keyword or use special characters. In standard SQL, you can place this kind of name in quotation marks (""), but in T-SQL, you can also place it in brackets ([]). Look at these examples for the name of a table in T-SQL:

CREATE TABLE dbo.test.“first name” ( Id INT, Name VARCHAR(100));
CREATE TABLE dbo.test.[first name]  ( Id INT, Name VARCHAR(100));

Only the first delimiter (the quotation marks) for the special name is also part of the SQL standard.

What Is Different in a SELECT Statement?#2 Returning Values

The SQL standard does not have a syntax for a query returning values or values coming from expressions without referring to any columns of a table, but MS SQL Server does allow for this type of expression. How? You can use a SELECT statement alone with an expression or with other values not coming from columns of the table. In T-SQL, it looks like the example below:

SELECT 12/6 ;

In this expression, we don’t need a table to evaluate 12 divided by 6, therefore, the FROM statement and the name of the table can be omitted.

#3 Limiting Records in a Result Set

In the SQL standard, you can limit the number of records in the results by using the syntax illustrated below:

SELECT * FROM tab FETCH FIRST 10 ROWS ONLY

T-SQL implements this syntax in a different way. The example below shows the MS SQL Server syntax:

SELECT * FROM tab ORDER BY col1 DESC OFFSET 0 ROWS FETCH FIRST 10 ROWS ONLY;

As you notice, this uses an ORDER BY clause. Another way to select rows, but without ORDER BY, is by using the TOP clause in T-SQL:

SELECT TOP 10 * FROM tab;
#4 Automatically Generating Values

The SQL standard enables you to create columns with automatically generated values. The syntax to do this is shown below:

CREATE TABLE tab (id DECIMAL GENERATED ALWAYS AS IDENTITY);

In T-SQL we can also automatically generate values, but in this way:

CREATE TABLE tab (id INTEGER IDENTITY);
#5 Math Functions

Several common mathematical functions are part of the SQL standard. One of these math functions is CEIL(x), which we don’t find in T-SQL. Instead, T-SQL provides the following non-standard functions: SIGN(x), ROUND(x,[,d]) to round decimal value x to the number of decimal positions, TRUNC(x) for truncating to given number of decimal places, LOG(x) to return the natural logarithm for a value x, and RANDOM() to generate random numbers. The highest or lowest number in a list in the SQL standard is returned by MAX(list) and MIN(list) functions, but in Transact-SQL, you use the GREATEST(list) and LEAST(list) functions.

T-SQL function ROUND:

SELECT ROUND(col) FROM tab;

#6 Aggregate Functions

We find another syntax difference with the aggregate functions. The functions COUNT, SUM, and AVG all take an argument related to a count. T-SQL allows the use of DISTINCT before these argument values so that rows are counted only if the values are different from other rows. The SQL standard doesn't allow for the use of DISTINCT in these functions.

Standard SQL:
SELECT COUNT(col) FROM tab;

T-SQL:
SELECT COUNT(col) FROM tab;

SELECT COUNT(DISTINCT col) FROM tab;

But in T-SQL we don’t find a population covariance function: COVAR_POP(x,y), which is defined in the SQL standard.

#7 Retrieving Parts of Dates and Times

Most relational database systems deliver many functions to operate on dates and times.

In standard SQL, the EXTRACT(YEAR FROM x) function and similar functions to select parts of dates are different from the T-SQL functions like YEAR(x) or DATEPART(year, x).

There is also a difference in getting the current date and time. Standard SQL allows you to get the current date with the CURRENT_DATE function, but in MS SQL Server, there is not a similar function, so we have to use the GETDATE function as an argument in the CAST function to convert to a DATE data type.

#8 Operating on Strings

Using functions to operate on strings is also different between the SQL standard and T-SQL. The main difference is found in removing trailing and leading spaces from a string. In standard SQL, there is the TRIM function, but in T-SQL, there are several related functions: TRIM (removing trailing and leading spaces), LTRIM (removing leading spaces), and RTRIM (removing trailing spaces).

Another very-often-used string function is SUBSTRING.

The standard SQL syntax for the SUBSTRING function looks like:

SUBSTRING(str FROM start [FOR len])

but in T-SQL, the syntax of this function looks like:

SUBSTRING(str, start, length)

There are reasons sometimes to add values coming from other columns and/or additional strings. Standard SQL enables the following syntax to do this:

As you can see, this syntax makes use of the || operator to add one string to another.

But the equivalent operator in T-SQL is the plus sign character. Look at this example:

SELECT col1 + col2  FROM tab;

In SQL Server, we also have the possibility to use the CONCAT function concatenates a list of strings:

SELECT CONCAT(col1, str1, col2, ...)  FROM tab;

We can also repeat one character several times. Standard SQL defines the function REPEAT(str, n) to do this. Transact-SQL provides the REPLICATE function. For example:

SELECT  REPLICATE(str, x);

where x indicates how many times to repeat the string or character.

#9 Inequality Operator

During filtering records in a SELECT statement, sometimes we have to use an inequality operator. Standard SQL defines <> as this operator, while T-SQL allows for both the standard operator and the != operator:

SELECT col3 FROM tab WHERE col1 != col2;
#10 ISNULL Function

In T-SQL, we have the ability to replace NULL values coming from a column using the ISNULL function. This is a function that is specific to T-SQL and is not in the SQL standard.

SELECT ISNULL(col1) FROM tab;
Which Parts of DML Syntax Are Different?

In T-SQL, the basic syntax of DELETE, UPDATE, and INSERT queries is the same as the SQL standard, but differences appear in more advanced queries. Let’s look at them.

#11 OUTPUT Keyword

The OUTPUT keyword occurs in DELETE, UPDATE, and INSERT statements. It is not defined in standard SQL.

Using T-SQL, we can see extra information returned by a query. It returns both old and new values in UPDATE or the values added using INSERT or deleted using DELETE. To see this information, we have to use prefixes in INSERT, UPDATE, and DELETE.

UPDATE tab SET col='new value'
OUTPUT Deleted.col, Inserted.col;

We see the result of changing records with the previous and new values in an updated column. The SQL standard does not support this feature.

#12 Syntax for INSERT INTO ... SELECT

Another structure of an INSERT query is INSERT INTO … SELECT. T-SQL allows you to insert data from another table into a destination table. Look at this query:

INSERT INTO tab SELECT col1,col2,... FROM tab_source;

It is not a standard feature but a feature characteristic of SQL Server.

#13 FROM Clause in DELETE and UPDATE

SQL Server provides extended syntax of the UPDATE and DELETE with FROM clauses. You can use DELETE with FROM to use the rows from one table to remove corresponding rows in another table by referring to a primary key and a foreign key. Similarly, you can use UPDATE with FROM update rows from one table by referring to the rows of another table using common values (primary key in one table and foreign key in second, e.g. the same city name). Here is an example:

DELETE FROM Book
FROM Author
WHERE Author.Id=Book.AuthorId AND Author.Name IS NULL;

UPDATE Book
SET Book.Price=Book.Price*0.2
FROM Author
WHERE Book.AuthorId=Author.Id AND Author.Id=12;

The SQL standard doesn’t provide this syntax.

#14 INSERT, UPDATE, and DELETE With JOIN

You can also use INSERT, UPDATE, and DELETE using JOIN to connect to another table. An example of this is:

DELETE ItemOrder FROM ItemOrder
JOIN Item ON ItemOrder.ItemId=Item.Id
WHERE YEAR(Item.DeliveredDate) <= 2017;

This feature is not in the SQL standard.

Summary

This article does not cover all the issues about syntax differences between the SQL standard and T-SQL using the MS SQL Server system. However, this guide helps point out some basic features characteristic only of Transact-SQL and what SQL standard syntax isn’t implemented by MS SQL Server.

Thanks for reading. If you liked this post, share it with all of your programming buddies!

Originally published on https://dzone.com


Sum Time columns in SQL

I'm writing a query to calculate when the students check-in and check-out and how much time they spend at the GYM. I have a Checking Field that contains the check-in time and a check-out field that contains the check-out, and a Duration.

I'm writing a query to calculate when the students check-in and check-out and how much time they spend at the GYM. I have a Checking Field that contains the check-in time and a check-out field that contains the check-out, and a Duration.

Check In        Check Out      Duration

12:30:00 1:30:00 1:00:00

How can I calculate check-in and check-out to obtain a duration like in the example?