Denodo VQL, use union to add a record to view - union

I'm trying to add a specific record to a view in VQL using UNION but it gives me an error saying com.denodo.vdb.admin.model.vdbserverproxy.VDBServerProxyException: The field properties can only be specified for derived fields. Does anyone know how to fix this?
(SELECT x,x,x
FROM a
INNER JOIN b ON a.id = b.id
LEFT OUTER JOIN c ON c.id = a.id
WHERE .....)
UNION
(SELECT 'y' 'y', 'y');

In order to avoid this behavior, you could build a SELECTION view on top of this UNION view and add there your descriptions and properties.

Related

Check constraints not working on a query/view with join

We have a view described as the following :
CREATE view [dbo].[PriceHourlyView]
AS
select NodeId, TimeStamp,RtDa,MarketId,Lmp,Mlc,Mcc,Issettledprice,datecreated,IsCalculated from dbo.PriceHourly WITH (NOLOCK)
union all
select NodeId, TimeStamp,RtDa,MarketId,Lmp,Mlc,Mcc,Issettledprice,datecreated,IsCalculated from dbo.PriceHourly2018 WITH (NOLOCK)
union all
select NodeId, TimeStamp,RtDa,MarketId,Lmp,Mlc,Mcc,Issettledprice,datecreated,IsCalculated from dbo.PriceHourly2017 WITH (NOLOCK)
union all
select NodeId, TimeStamp,RtDa,MarketId,Lmp,Mlc,Mcc,Issettledprice,datecreated,IsCalculated from dbo.PriceHourly2016 WITH (NOLOCK)
union all
select NodeId, TimeStamp,RtDa,MarketId,Lmp,Mlc,Mcc,Issettledprice,datecreated,IsCalculated from dbo.PriceHourly2015 WITH (NOLOCK)
union all
select NodeId, TimeStamp,RtDa,MarketId,Lmp,Mlc,Mcc,Issettledprice,datecreated,IsCalculated from dbo.PriceHourly2014 WITH (NOLOCK)
union all
select NodeId, TimeStamp,RtDa,MarketId,Lmp,Mlc,Mcc,Issettledprice,datecreated,IsCalculated from dbo.PriceHourly2013 WITH (NOLOCK)
union all
select NodeId, TimeStamp,RtDa,MarketId,Lmp,Mlc,Mcc,Issettledprice,datecreated,IsCalculated from dbo.PriceHourly2012 WITH (NOLOCK)
Each of the tables has a check constraint as follows for each year except for the current table without year specified :
ALTER TABLE [dbo].[PriceHourly2017] WITH CHECK ADD CONSTRAINT [CK_PriceHourly2017_Timestamp] CHECK (([timestamp]>='2017-01-01' AND [timestamp]<='2017-12-31 23:59'))
When this view is queried by itself the check constraints limit the tables being searched. The execution plan looks like this :
SELECT
*
FROM PriceHourlyview
WHERE nodeid = 24511
AND TimeStamp BETWEEN '2017-05-17' AND '2017-05-24'
Now when I join on this table on the timestamp field the query no longer uses the check constraints and uses every table to check for the data.
SELECT
*
FROM ShapeProfileDetails s WITH (NOLOCK)
LEFT JOIN PriceHourlyView p WITH (NOLOCK)
ON s.TimeStamp = p.Timestamp
AND s.EffectiveDate BETWEEN '2017-05-17' AND '2017-05-24'
WHERE NodeId = 24512
--AND s.EffectiveDate BETWEEN '2017-05-17' AND '2017-05-24'
I know I'm not querying the same field in the joined example and assuming that's the issue but that is the field I need to query for the correct results. I'm wondering if there is anyway to hint or force the query to use the correct check constraints. Or what is the best practice on joining to try and utilize these check constraints.
Okay, as discussed we know that EffectiveDate and TimeStamp are nearly the same. I would try to do something like this. Technically it's the same query, but we will let know SQL Server that it can use constraints (just subtract and add one day on the edges of BETWEEN).
SELECT * FROM ShapeProfileDetails s WITH (NOLOCK)
JOIN PriceHourlyView p WITH (NOLOCK)
ON s.TimeStamp = p.Timestamp
AND s.EffectiveDate BETWEEN '2017-05-17' AND '2017-05-24'
AND s.TimeStamp BETWEEN '2017-05-16' AND '2017-05-25'
WHERE NodeId = 24512

SQL Server : can't find the right 'join' formula for what I want

I'm trying to find the right way to achieve this. Suppose I have 3 tables A, B and C.
I want my request to show some info from all 3 tables, but I want to show only one line by records that are in A.
The problem, if I join tables, is that there is most of the time a lot of B records linked to one A record, even worse, there is a lot of C linked to one B, so sometimes, the same A record is shown over a hundred times...
I tried select top(1) for B and top(1) again for C but still, it returns top(1) written on every 100 row of the same A, tried left join... inner join...
I'm trying to figure out how to group by but still can't find the right grouping. I ended up making A LOT of nested select, in fact, my query contains more nested select then anything else... it works but it takes forever...
Would it be faster if I find a way to remove most of my nested select ?
Is that even possible? I mean, did someone ever manage to accomplish this one line for all 'A' records query?
Try this:
Select * FROM A
OUTER APPLY (Select TOP 1 * FROM B Where A.colX = B.ColY) as New_B
OUTER APPLY (Select TOP 1 * FROM C Where A.colX = C.ColY) as New_C
You may need to modify the New_B and New_C Select statement to match your requirement.
You can use common table expression (cte) with row_number. Something like this.
;with cte as (
select a.id,b.name,c.price,
row_number() over(partition by a.id order by b.name, c.price) rn
from a inner join b on a.id = b.a_id
inner join c on b.id = c.b_id
)
select * from cte
where rn=1

Joining view and tables in sql server

I have the following tables.
Customer Customer_id, Customer_name
Customer_details Customer_id,Phone_no,Address
Order_details Order_id,Customer_id,Order_type
I have created a view as following
Create view Orders_Analysis
Select c.Customer_id,cd.phone_no,od.order_id
From customer c
inner join order_details od
on c. Customer_id=od. Customer_id
Inner join Where c. Customer_id=cd. Customer_id
cd. Customer_id=c.Customer_id
Now using the above view and pre mentioned tables I have to extract only those records in the view which are of a particular order_type.
Can you guys suggest me a method.
First of all, You have some mistake in View, This is the correct View after fix mistakes
Create view Orders_Analysis
Select c.Customer_id,cd.phone_no,od.order_id
From customer c
INNER JOIN order_details od
ON c.Customer_id = od.Customer_id
INNER JOIN Customer_details CD
ON c.Customer_id = cd.Customer_id
Now You want to extract only those records in the view which are of a particular order_type.
Solution One:- Because you dont have column order_type in View then use INNER JOIN
SELECT *
FROM Orders_Analysis OA
INNER JOIN Order_details OD
ON OA.order_id = OD.order_id
WHERE OD.Order_type = your value here
Solution Two:-
Otherwise Add Order-Type column in View
Create view Orders_Analysis
Select c.Customer_id,cd.phone_no,od.order_id,od.order_type
From customer c
INNER JOIN order_details od
ON c.Customer_id = od.Customer_id
INNER JOIN Customer_details CD
ON c.Customer_id = cd.Customer_id
and use
Select * from Orders_Analysis where Order_Type = your value here
You need to add the field, od.order_type, to the Select statement results set in your view definition:
SELECT c.Customer_id,cd.phone_no,od.order_id,od.Order_type
Then, if you run a select against the view, specify a WHERE clause on the order_type field for the value you are looking for.
Update your view and select also order type
Select c.Customer_id,cd.phone_no,od.order_id, od.Order_type
You can now execute select query using the view you created
Select * from Orders_Analysis where Order_Type = "any value"

Grouping data based on expression using CountDistinct aggregate function

I am newbie to Stack overflow and also SQL server reporting services. So please excuse me for the format of the question.
So here is the situation:
I am developing a SSRS report which needs to be grouped by an Count of Distinct product names as shown below.
I created a text box called ProdCount with an expression
COUNTDISTNCT(Fields!Product.value,"DataSet1")
which gives me the count 63 within the scope of DataSet1.
Now i need to group the data by taking product names where the above formula is >1 .
=IIF(ProdCount>1,Fields!Product.value,Nothing)
My Problem:
I tried to call the ProdCount from the calculated field since i
cant use the aggregate functions in Calculated Fields and use
the second expression by using
= ReportItems!ProdCount.value
which gives me an error FieldValue Denying ReportItems
I tried to combine the above two expressions by creating a calculated field by
IIF(CountDistinct(Fields!Product.Value,"DataSet1")>1,Fields!Product.Value,Nothing)
which gives me an error Calculated fields cannot have expressions
I tried to use Report Variables in the same way as above(1) which was not working either.
I also tried to use CROSS JOIN in the query
Select Count(Distinct(Product Name)
from Query1
Cross join
My Main Query which give me the data
which is taking more time to execute.
So Can anyone help me with solution where i can group the data by combining the above two expressions.
Please excuse me for the format. I was confused with framing question. I accept all your edits , so that i can learn in future.
Here is my code:
SELECT * FROM
--Query1 which counts the number of distinct products)
(SELECT DISTINCT COUNT(gproduct.ProductName) AS ProdCount
FROM Table1
LEFT JOIN Table4
ON Table1.column=Table1.column
LEFT JOIN Table2
ON Table3.Column = TTable1.Column
LEFT JOIN
(
SELECT Distinct Table6.Name AS ProductName,Table9.ColumnId
FROM Table6
INNER JOIN Table7
ON Table6.Column=Table7.Column
INNER JOIN Table8
ON Table7.Column=Table8.Column
INNER JOIN Table9
ON Table9.Column=Table8.Column
)gproduct
ON Table1.ColumnId=gproduct.ColumnId
GROUP BY gproduct.ColumnId,
)qProduct
CROSS JOIN
--My main Query which get data from different table including Product name
(SELECT
Upper(CASE WHEN (CASE WHEN Table4.Column =1 THEN 'Yes' ELSE 'NO' END)='YES'
THEN gab.ProductName
ELSE
Table2.productName
END) AS Product,
FROM Table1 AS ec
LEFT JOIN Table2 AS ep
ON --
LEFT JOIN Table3 AS ebrd
ON --
Left JOIN Table4 AS etpc
ON --
LEFT JOIN Table5 AS gst
ON --
LEFT JOIN
(
SELECT Distinct Table6.Name AS ProductName,Table9.ColumnId
FROM Table6
INNER JOIN Table7
ON Table6.Column=Table7.Column
INNER JOIN Table8
ON Table7.Column=Table8.Column
INNER JOIN Table9
ON Table9.Column=Table8.Column
) gab
ON Table1.ColumnId=gab.ColumnId
)QMain
Personally I would try to solve the problem in query itself instead of SSRS report. According the data you provided it would be something like:
SELECT
ProductName,
count(distinct Product)
from
YourTable
group by
ProductName
having count(distinct product) > 1
Later on creating SSRS report should be quite easy.

remove subquery from view to make it Indexed view

i want to create a Index View for full text search.
the only problem i,m facing with subquery, because index views does not allow subquery.
below is my query
ALTER VIEW [dbo].[Demo] with SCHEMABINDING AS
select distinct a.ID,a.Title, a.Description ,b.Name as Recipe, c.Name as Taste , d.Name as CuisineType,
STUFF((SELECT ',' + Name FROM dbo.Ingredients where ID in (select IngredientID from dbo.listingIngredients
where listingid = a.ID ) FOR XML PATH('')), 1, 1, '') as Ingredients
from dbo.Listing as a
inner join dbo.RecipeType b on a.RecipeTypeID = b.ID
inner join dbo.taste c on a.tasteID = c.ID
inner join dbo.CuisineType d on a.CuisineTypeID = d.ID
inner join dbo.listingIngredients e on a.ID = e.listingID
GO
I,m using subquery to get ingredients as concatenate string from Ingredients table using STUFF.
can some one please let me know how can i remove this subquery and have ingredients as contented string.
please let me know
regards
manish
The XML part of the query will cause problems, even if you did manage to remove the sub-selected.
However, all is not lost. You could rewrite the view into a part that can be indexed and another part that is cheaper, but can't. For example, you could write:
ALTER VIEW [dbo].[Demo_Part] with SCHEMABINDING AS
select a.ID,a.Title
, a.Description
, b.Name as Recipe
, c.Name as Taste
, d.Name as CuisineType
, e.name
from dbo.Listing as a
inner join dbo.RecipeType b on a.RecipeTypeID = b.ID
inner join dbo.taste c on a.tasteID = c.ID
inner join dbo.CuisineType d on a.CuisineTypeID = d.ID
inner join dbo.listingIngredients e on a.ID = e.listingID
GROUP BY a.ID,a.Title
, a.Description
, b.Name as Recipe
, c.Name as Taste
, d.Name as CuisineType
, e.name
Depending on your data model, you may not even need the group by. This view can be indexed
And then write another view that is not indexed, but which replaces your original view
CREATE VIEW [dbo].[Demo]
SELECT ...
STUFF (...)
FROM [dbo].[Demo_Part]
As a meta-answer I would add that if you need to index a view like this (and use the DISTINCT), chances are that your data modeller made a pretty big mistake with the data model or that your data access code is very inefficient. Everything about this smells like you are trying to work around poor coding and modelling practices.

Resources