I have two views :
ZC_PurRequisitionFs and ZMM_ONAYT005.
First view extended from C_PurRequisitionFs.
Second view gets data from Z* table.
How can I get only first view data that does not exist on second view ?
#AbapCatalog.sqlViewAppendName: 'ZCPURREQUISFS'
#EndUserText.label: 'Sat belgeleri'
extend view C_PurRequisitionFs with ZC_PurRequisitionFs {
*
} where ZC_PurRequisitionFs.object_id not in( SELECT * FROM ZMM_ONAYT005 ).
While CDS views do not support subqueries, they do support JOINs.
Usually you would use a JOIN to get only those entries which exist in both tables. But when you want all entries from table A which don't exist in table B, you can do a left outer join and then add a where-condition for only entries where the right table is null.
define view Z_TEST as select
from table_a
left outer join table_b on
table_a.object_id = table_b.object_id
{
... fields....
}
where table_b.object_id is null;
As far as I know, ABAP CDS view does not support subqueries, so you would need to include it in the main query.
Please check this guide.
Related
I am working on my sql assignment and there is this question about sorting a view using a column that is outside the view. So I had the idea of joining the view with the column itself, without showing the column the in the query, and this is what I found online
SELECT *
FROM (SELECT *
FROM vwCustomerOrder
INNER JOIN ORDERDETAILS.ORDERLINENUMBER
ON vwCustomerOrder.QUANTITYORDERED = ORDERDETAILS.QUANTITYORDERED
ORDER BY ORDERDETAILS.ORDERLINENUMBER)
This shows a ORA-00942 error, which said that the view is not reaching the column, anyone know how to do this ?
if the missing column is coming from the same table , then select from origin table instead of view , the join would be redundant .
if not if the relationship is one-one then join can work :
SELECT *
FROM vwCustomerOrder
INNER JOIN ORDERDETAILS
ON vwCustomerOrder.QUANTITYORDERED = ORDERDETAILS.QUANTITYORDERED
ORDER BY ORDERDETAILS.ORDERLINENUMBER
So we have a db that receives constant input and then we have it moved after x days to a different db as a type of archive/reporting db. So the question is when I try to create a view that combines two of the tables (one from each db):
CREATE VIEW Table_Full AS
SELECT *
FROM [Front].dbo.[DATA] AS A
Inner Join Back.dbo.DATA AS B
ON A.DATETIME=B.DATETIME
I get the following error: "Column names in each view or function must be unique. Column name 'PARTNO' in view or function 'Table_Full' is specified more than once."
So, is there a better way to accomplish what I am trying to do or ?
Given your use case, I think you want a union. That is
select * from table_a
Union all
Select * from table_b
Instead of an asterisk, you have to specify each field and provide aliases for the duplicate field names:
CREATE VIEW Table_Full AS
SELECT
a.ID as A_ID,
b.ID as B_ID,
a.Thingy as A_Thingy,
a.Etcetera as Atcetera
FROM [Front].dbo.[DATA] AS A
Inner Join Back.dbo.DATA AS B
ON A.DATETIME=B.DATETIME
I have a very large view containing 5 million records containing repeated names with each row having unique transaction number. Another view of 9000 records containing unique names is also present. Now I want to retrieve records in first view whose names are present in second view
select * from v1 where name in (select name from v2)
But the query is taking very long to run. Is there any short cut method?
Did you try just using a INNER JOIN. This will return all rows that exist in both tables:
select v1.*
from v1
INNER JOIN v2
on v1.name = v2.name
If you need help learning JOIN syntax, here is a great visual explanation.
You can add the DISTINCT keyword which will remove any duplicate values that the query returns.
use JOIN.
The DISTINCT will allow you to return only unique records from the list since you are joining from the other table and there could be possibilities that a record may have more than one matches on the other table.
SELECT DISTINCT a.*
FROM v1 a
INNER JOIN v2 b
ON a.name = b.name
For faster performance, add an index on column NAME on both tables since you are joining through it.
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
I'm currently converting MS access queries to SQL queries and noticed that in the access query it appears to be joining another query to other tables. So I looked around and it seems like that query pretty much makes the query look cleaner without needing to have all sorts of subqueries in the same script
Something like
FROM [query name] INNER JOIN [some other table]
Is there something like this in SQL?
You are probably looking for VIEWS.
A view is basically a stored version of a SELECT query. It allows you to reference the result set without rewriting the query every time.
You can create a VIEW as a query, then reference the view in another query.
CREATE VIEW <viewname> AS <SELECT STATEMENT>
then
SELECT * FROM <viewname> INNER JOIN <other table>
Yes. They are called views.
You can create a view like
CREATE VIEW vw_some_query AS
SELECT * FROM
table_a LEFT INNER JOIN table_b ON table_a.id = table_b.id2
then you can write a select like:
SELECT * FROM vw_some_query LEFT INNER JOIN table_c ON vw_some_query.id = table_c.id3
Is there something like this in SQL?
Yes. In SQL you would probably use the WITH clause:
WITH someData AS
(
select a.col1, b.col2
from tableA a join tableB b
on (a.someKey = b.someKey)
),
...
select data1.col1, data1.col2, data2.col3
from someData data1 join tableC data2
on (data1.col1 = data2.anotherKey)
where ...
Views are ok too, but another db object to keep track of, and if using a materialized view, need to worry about refreshing snapshot table, etc. My suggestion is to use WITH along with plenty of comments where possible.
EDIT: If you find yourself asking the same question of the db over and over, then a view (or mat view) would be more appropriate. But otherwise, keep logic in the query.
I want to create an indexed view that integrates data from several tables, however SQL Server complains with the error message:
Cannot create index on view "MyView". The view contains a self join on "dbo.Companies".
The view definition is something like this (simplified version):
SELECT T.Field1
, T.Field2
, P.CompanyName AS ProviderName
, C.CompanyName AS CustomerName
, T3.Field1
FROM dbo.Table1 T
INNER JOIN dbo.Companies P ON T.ProviderId = T2.Id
INNER JOIN dbo.Companies C ON T.CustomerId = T2.Id
INNER JOIN dbo.Table3 ON T.Id = T3.Id
Is there any workaround for this case? I don't want to split the Companies table in two tables.
Thanks in advance.
You won't be able to work around this, the indexed views must conform to certain restrictions enumerated in Creating Indexed Views. Among other things, outer and self joins are not supported (10th restriction from top to bottom). It boils down to the engine ability to be able to update the view index when the base table is updated.
Not knowing exact all the details of your data model, are you sure that an indexed view is necessary and won't the base table indexes suffice?
One workaround is described here.
You create two views:
an indexed view that has a set of rows for your first join and another set of rows for your second join, i.e. it's un-pivoted the data. This view can be indexed.
a not-indexed view that takes your indexed view and PIVOTs it back to separate columns
In this workaround you do get an indexed view that does the joins, but SQL needs to do an extra PIVOT operation to get the data how you want to use it. Whether this will improve performance vs just doing the joins without an indexed view I don't know. (But if you do the analysis please let me know).