select from two sql views not using one of them - sql

I have this sql view, this is not my code:
SELECT
`view_combined_rev_data`.`date` AS `date`,
`view_combined_rev_data`.`book_title` AS `book_title`,
`view_combined_rev_data`.`marketplace` AS `marketplace`,
`view_combined_rev_data`.`amazon_kdp_avg_list_price` AS `amazon_kdp_avg_list_price`,
`view_combined_rev_data`.`amazon_kdp_royalty_type` AS `amazon_kdp_royalty_type`,
`view_combined_rev_data`.`amazon_kdp_revenue_in_usd` AS `amazon_kdp_revenue_in_usd`,
`view_combined_rev_data`.`amazon_kdp_royalty_in_usd` AS `amazon_kdp_royalty_in_usd`,
`view_combined_rev_data`.`amazon_kdp_paid_downloads` AS `amazon_kdp_paid_downloads`,
`view_combined_rev_data`.`amazon_kdp_free_downloads` AS `amazon_kdp_free_downloads`,
`view_combined_rev_data`.`amazon_ku_pages_read` AS `amazon_ku_pages_read`,
`view_combined_rev_data`.`amazon_ku_revenue_in_usd` AS `amazon_ku_revenue_in_usd`,
`view_combined_rev_data`.`create_space_revenue_in_usd` AS `create_space_revenue_in_usd`,
`view_combined_rev_data`.`create_space_royalty_in_usd` AS `create_space_royalty_in_usd`,
`view_combined_rev_data`.`create_space_paperbacks_sold` AS `create_space_paperbacks_sold`,
(
(
`view_combined_rev_data`.`amazon_kdp_revenue_in_usd` + `view_combined_rev_data`.`amazon_ku_revenue_in_usd`
) + `view_combined_rev_data`.`create_space_revenue_in_usd`
) AS `daily_total_revenue`,
(
(
`view_combined_rev_data`.`amazon_kdp_royalty_in_usd` + `view_combined_rev_data`.`create_space_royalty_in_usd`
) + `view_combined_rev_data`.`amazon_ku_revenue_in_usd`
) AS `daily_total_royalty`
FROM
`view_combined_rev_marketplace_data` `view_combined_rev_data`
My question is simple:
Why view_combined_rev_marketplace_data is used in this line. I can't find the code using it anywhere else, so can I simply remove it?
FROM
`view_combined_rev_marketplace_data` `view_combined_rev_data`

This is your FROM clause:
FROM `view_combined_rev_marketplace_data` `view_combined_rev_data`
The first name, view_combined_rev_marketplace_data is the name of a table or view (presumably a view) that exists in the database.
The second name, view_combined_rev_data, is a table alias. This is how the table/view is referred to in the query.
I recommend that you use table/view aliases that are abbreviations for the table/view name, something like this:
FROM `view_combined_rev_marketplace_data` vcrmd
Then the references to columns would look like:
SELECT vcrmd.`date` AS `date`,
vcrmd.`book_title` AS `book_title`,
. . .
And this would further simplify to:
SELECT vcrmd.`date`,
vcrmd.`book_title`,
. . .
The column alias (name given after the as) is unnecessary in this case, because it defaults to the column name. Note, though, that local coding styles may recommend having explicit column aliases.

FROM
`view_combined_rev_marketplace_data` `view_combined_rev_data`
These are not two views, but one view view_combined_rev_marketplace_data with an alias view_combined_rev_data.
When an alias is used to reference a table/view/function, then it must be used in the statement instead of the object's name. Usually aliases are meant to provide a shorter or more readable reference to the SQL object. In this case it is relatively long.

Related

What is the "DATA" keyword in T-SQL for?

I'm working with a database in T-SQL/SQL Server 2016 at the moment which has some stored procedures containing a keyword I'm not familiar with, namely the "DATA" suffix after a query:
SELECT * FROM dbo.TableName DATA
I'm struggling to find any documentation on what the purpose of this "DATA" keyword is. Could someone shed some light please?
It is not some specific keyword. It is just a table alias. Note that if you changed your select to
SELECT DATA.* FROM dbo.TableName DATA
it will work, as the table now has the "DATA" alias. For the same reason, this:
SELECT dbo.TableName.* FROM dbo.TableName DATA
will throw an error.
This is an alias for the table name, usually it is used if we are inner joining the same table more than one time, or when we need to call the table with a shortcut name.
For example if the table has a key named ID, then:
SELECT DATA.* FROM dbo.TableName DATA
where DATA.ID = "1"
is like
SELECT dbo.TableName.* FROM dbo.TableName
where TableName .ID = "1"

Use of dynamically created columns prompts "Invalid Column"Error

I am trying to create new column by performing some addition, multiplication and subtraction with the values of the columns that is already created with the MS SQL table . In the below code, I created [PremiumByCodes] col. by doing the calculation. Likewise, when I try to create col. [Commission] , it shows me, invalid column name "PremiumBYCodes". I also wanted to do the same kind of multiplication to Deduction column and create a new column. Any suggestions, on how to proceed by doing this ?
select
[PremiumByCodes] = a.[NWP]/(1-c.[Commission%]-c.[Deduction%]),
[Commission] = [PremiumByCodes] /c.[Commission%],
[Deduction] = [PremiumByCodes] /c.[Deduction%],
This sure looks like SQL Server code.
First, databases just do not allow re-using column aliases in the same SELECT where they are defined. The normal approach is to use a subquery or CTE. Another approach uses lateral joins, which is implemented in SQL Server using APPLY:
select v.PremiumByCodes, v.PremiumByCodes / c.[Commission%] as Commission,
v.PremiumByCodes /c.[Deduction%] as Deduction,
. . .
from . . . outer apply
(values (a.[BHSINWP]/(1-c.[Commission%]-c.[Deduction%]) )) v(PremiumByCodes)
. . .
In sql-server, you can not reference a column by its alias except for in the order by of your query.
select
[PremiumByCodes] = a.[BHSINWP]/(1-c.[Commission%]-c.[Deduction%])
, [Commission] = (a.[BHSINWP]/(1-c.[Commission%]-c.[Deduction%]))/c.[Commission%]
, [Deduction] = (a.[BHSINWP]/(1-c.[Commission%]-c.[Deduction%]))/c.[Deduction%]

ORA-00998: must name this expression with a column alias

I get that I should add alias with all the columns and I'm doing so but I'm still getting error.
CREATE TABLE MENTIONS AS SELECT
UM.USER_ID AS U_ID,
UM.SCREEN_NAME AS USER_SCREEN_NAME,
UM.MENTION_ID AS M_USER_ID,
(
SELECT
UI.USER_SCREEN_NAME AS MENTIONED_USER
FROM
USER_INFO UI
WHERE
UI.USER_ID = UM.MENTION_ID
AND ROWNUM = 1
)
FROM
USER_MENTION UM
USER_MENTION table
USER_ID SCREEN_NAME MENTION_ID
135846337 irisschrijft 774759032636727300
50117969 Chjulian 13769472
14411827 thenriques45 13769472
26681613 ahenotri 252074645
26681613 ahenotri 9796472
158378782 SpringerOpen 9796472
144241914 Kumarappan 252074645
User_INFO table:
USER_ID USER_SCREEN_NAME
22553325 jasonesummers
23435691 QRJAM false
67421923 inTELEgentMSP
97393397 knauer0x
85303739 MarriageTheorem
3842711 seki
3036414608 Bayes_Rule
838677852 BOLIGATOR
I'm still getting the above mentioned error, what am I doing wrong?
Lookup the Oracle Error Message Manual of the current Oracle version. Here the error is mentioned but without additional information.
In such a case look up the
Oracle Error Message Manual of version 9i
For reasons I don't know a lot of error messages have a description in the 9i manual but not in the manuals of higher versions. 9i is a rather old version so the description may be out of date. But it may contain valuable hints.
ORA-00998 must name this expression with a column alias
Cause: An expression or function was used in a CREATE VIEW statement, but no corresponding column name was specified. When expressions or functions are used in a view, all column names for the view must be explicitly specified in the CREATE VIEW statement.
Action: Enter a column name for each column in the view in parentheses after the view name.
We don't have a view but a a table that was created by a select. And actually the last expression of the select list is an expression without an alias. So try your statement using an alias for the last expression. So try
CREATE TABLE MENTIONS AS SELECT
UM.USER_ID AS U_ID,
UM.SCREEN_NAME AS USER_SCREEN_NAME,
UM.MENTION_ID AS M_USER_ID,
(
SELECT
UI.USER_SCREEN_NAME
FROM
USER_INFO UI
WHERE
UI.USER_ID = UM.MENTION_ID
AND ROWNUM = 1
) AS MENTIONED_USER
FROM
USER_MENTION UM
The column alias in the inner select list is useless and can be removed.
The problem with your query is that each column in the create table needs to have a name. You think you are assigning a name in the sub-select. However, you are not.
The subquery is just returning a value -- not a value with a name. So, the AS MENTIONED_USER in your version does nothing. This is a bit tricky, I guess. One way to think of the scalar subquery is that it is just another expression or function call. Things that happen inside it don't affect the outer query -- except for the value being returned.
The correct syntax is to put the column alias outside the subselect, not inside it:
CREATE TABLE MENTIONS AS
SELECT UM.USER_ID AS U_ID, UM.SCREEN_NAME AS USER_SCREEN_NAME, UM.MENTION_ID AS M_USER_ID,
(SELECT UI.USER_SCREEN_NAME
FROM USER_INFO UI
WHERE UI.USER_ID = UM.MENTION_ID AND ROWNUM = 1
) AS MENTIONED_USER
FROM USER_MENTION UM;

Table name after parenthesis notation

I was going through some old-ish SQL code someone else had written that I couldn't quite understand. I have simplified its structure here, but if anyone can walk me through what exactly is going on, that would be appreciated! You may ignore the specific column operations as they were just examples.
SELECT table.*,
column1 - column2
AS 'col1 - col2',
...
columnn
AS 'coln'
FROM
(SELECT
...
) table
What I don't understand is the final line. I am assuming it is the definition of "table" in the FROM (SELECT ...) part, and the ) table part indicates the name of the defined table.
Thanks in advance!
An inner select needs an alias name
select alias_name.* from
(
select * from some_table ...
) alias_name
The 'table' in that final line is an alias for the subquery.
In T-SQL it is AFAIK mandatory to specify an alias for a subquery if you're selecting from a subquery.
You can name the alias whathever you want. It is perfectly ok to use it like this
select * from
( select * from ... ) as X
(The as keyword is not mandatory, but I always specify the alias-name using 'as').

ORA-01731: circular view definition encountered

we are migrating over to oracle from sql server side.
on sqlserver we used to have a view like the following
create view blah
AS
Select column1,
column2
FROM blah;
but doing this on oracle produces circular view error.
is this not allowed on oracle side?
You cannot have a view reference itself. It logically does not make sense. A view is essentially a cached query whose results are displayed as a table. How can a query refer to itself?
Indeed, circular view definitions are not allowed in Oracle. If you have a circular view definition, then you likely have a bug in your database code that should be addressed. Perhaps the translation from SQL server to Oracle was flawed and accidentally introduced this circular definition?
You can actually do this in ORACLE, but it is more fragile, as you need to explicitly list the output columns of your CTE. So if you change the tables, you need to manually update the CTE.
Here is an example from our db, showing how to calculate the hierarchical depth of the a record...
CREATE OR REPLACE VIEW deploy.PHARMACYDISPENSE_EX
AS
WITH SRC (
PDID, WAREID, GCN_SEQNO, QTY, UOFM, XACTDTTM, CREATEDON, PROCESSEDON,
XACTTYPE, OPDID, CLOSEDON, BYPASSEDON, BYPASSEDBY, ITEMNO, LOTNO,
EXP_DATE, VOLUMETYPE, POTYPE, DEPTH
) AS (
SELECT D.PDID, D.WAREID, D.GCN_SEQNO, D.QTY, D.UOFM, D.XACTDTTM,
D.CREATEDON, D.PROCESSEDON, D.XACTTYPE, D.OPDID, D.CLOSEDON,
D.BYPASSEDON, D.BYPASSEDBY, D.ITEMNO, D.LOTNO, D.EXP_DATE,
D.VOLUMETYPE, D.POTYPE, 0 FROM deploy.PHARMACYDISPENSE D
WHERE OPDID IS NULL
UNION ALL
SELECT D.PDID, D.WAREID, D.GCN_SEQNO, D.QTY, D.UOFM, D.XACTDTTM,
D.CREATEDON, D.PROCESSEDON, D.XACTTYPE, D.OPDID, D.CLOSEDON,
D.BYPASSEDON, D.BYPASSEDBY, D.ITEMNO, D.LOTNO, D.EXP_DATE,
D.VOLUMETYPE, D.POTYPE, (S.DEPTH + 1)
FROM deploy.PHARMACYDISPENSE D JOIN SRC S ON S.PDID = D.OPDID
)
SELECT PD.*
FROM SRC PD;
The important part here is the WITH SRC (<output column list>) AS .... You need that output column list. So it is possible, and does work, it just takes a bit more code than in SQL Server.
Your example is incomplete - well at least doesn't show the pertinent parts.:
-- create a table
CREATE TABLE Scrap
(fieldName VARCHAR2(20));
-- create a view
CREATE VIEW ScrapVW1
AS
SELECT * FROM Scrap;
-- create a second view that uses the first view
CREATE VIEW ScrapVW2
AS
SELECT * FROM Scrap
UNION ALL
SELECT * FROM ScrapVW1;
-- recreate the first view that references the 2nd view which contains a reference to itself
CREATE OR REPLACE VIEW SCRAP_VW1
AS
SELECT * FROM ScrapVW2;
Gives a circular reference error when you try to recreate ScrapVW1. I would guess you have some unintentional name collision going on in your conversion. If it's quite complex I'd get rid of the 'CREATE OR REPLACE VIEW' syntax and just use CREATE VIEW which would then give you 'ORA-00955 Name already used' error.
Oracle deals with Hierarchical problems different than SQL apparently. Instead of self referring view, you can use connect by clause
SELECT employee_id, last_name, manager_id
FROM employees
CONNECT BY PRIOR employee_id = manager_id;