C# Sql CTE Query - Select out extra information - c#

I am trying to write a CTE Recursive Query to build a tree relationship of a flat table with a 'marketGroupID' (that element's ID) and a 'parentGroupID' (the element's parent ID). Where each 'marketGroup' can have any number of children 'marketGroups' and so on.
Here is my working query (tested in Sql Server Management):
With cte As
(SELECT [marketGroupID]
,[parentGroupID]
,[marketGroupName]
,[description]
,[iconID]
,[hasTypes]
, 0 As Level
FROM [Eve_Retribution_1.0.7].[dbo].[invMarketGroups]
WHERE [parentGroupID] IS NULL
UNION All
Select mg.marketGroupID
,mg.parentGroupID
,mg.marketGroupName
,mg.description
,mg.iconID
,mg.hasTypes
,c.Level + 1 As Level
FROM [Eve_Retribution_1.0.7].dbo.invMarketGroups mg
Inner Join cte c On mg.parentGroupID = c.marketGroupID
WHERE mg.marketGroupID <> mg.parentGroupID
)
SELECT marketGroupID
,parentGroupID
,marketGroupName
,description
,iconID
,hasTypes
, Level
FROM cte
This Query correctly lists the Elements in the correct order and the Level parameter is meant to be used to build the tree from the elements.
Translating this into C# is where I have a problem. I have integrated this database and all the corresponding tables have been built from my database into my code automatically. I try to call this query with C# as follows:
EveOnlineClassesDataContext context = new EveOnlineClassesDataContext();
IEnumerable<invMarketGroup> results = context.ExecuteQuery<invMarketGroup>
(#"**ABOVE QUERY**");
Where the 'invMarketGroup' class is the automatically created class built by the O/R Designer. My problem is that I lose access to the Level parameter for each 'marketGroup' as it was not part of the table itself and has no element in the provided class.
I want to retrieve from the query the actual 'invMarketGroup' class objects and the level corresponding to each so I can build a tree from this in memory representing this structure. How would I go about doing this?
Thanks

Might be easier to create a View vwInvMarketGroup inside your database using that query:
CREATE VIEW vwInvMarketGroup AS
With cte As
(SELECT [marketGroupID]
,[parentGroupID]
,[marketGroupName]
,[description]
,[iconID]
,[hasTypes]
, 0 As Level
FROM [Eve_Retribution_1.0.7].[dbo].[invMarketGroups]
WHERE [parentGroupID] IS NULL
UNION All
Select mg.marketGroupID
,mg.parentGroupID
,mg.marketGroupName
,mg.description
,mg.iconID
,mg.hasTypes
,c.Level + 1 As Level
FROM [Eve_Retribution_1.0.7].dbo.invMarketGroups mg
Inner Join cte c On mg.parentGroupID = c.marketGroupID
WHERE mg.marketGroupID <> mg.parentGroupID
)
SELECT marketGroupID
,parentGroupID
,marketGroupName
,description
,iconID
,hasTypes
, Level
FROM cte
GO
Then you can use this:
IEnumerable<invMarketGroup> results = context.ExecuteQuery<invMarketGroup>(#"SELECT * FROM vwInvMarketGroup");

Related

Create a nested grid with a self referencing table

I have a table with following schema:
SELECT [ParkingCardId],
[ParentParkingCardId],
[CompanyId],
[DateRequested],
[StaffNo],
[Name],
[Section],
[JobTitle],
[Position],
[Telephone],
[Mobile],
[Fax],
[POBox],
[Email],
[Nationality],
[Gender],
[ShiftType],
[Amount],
[PassIssueDate],
[PassExpiryDate]
FROM [DCAServices].[dbo].[ParkingCards];
The first two columns are key here: ParkingCardId is the PK and ParentParkingCardId, if not NULL, is pointing to another ParkingCardId in the same table and is, itself, a renew of a lost card case.
I want to display this information as hierarchical in Grid on MVC (Kendo) but my DAL is pure Entity Framework based and I have not used any Stored Procedures so far. I know it may not be easy to have a LINQ query to transform this data.
There is also a possibility that there may be generations of parents. A staff might be on his eights renewal. So the parent row on the grid may expand to show one child that may be a parent itself and so on.
I'm currently looking at search result that comes up against query like "Parent-Child relation in the same table"
This SQL will return hierarchical result with Level of current item
WITH cte as
(
SELECT i.[ParkingCardId], i.[ParentParkingCardId], i.[CompanyId], i.[DateRequested], i.[StaffNo], i.[Name], i.[Section], i.[JobTitle], i.[Position],
i.[Telephone], i.[Mobile], i.[Fax], i.[POBox], i.[Email], i.[Nationality], i.[Gender], i.[ShiftType], i.[Amount], i.[PassIssueDate], i.[PassExpiryDate], 0 AS [Level]
FROM [DCAServices].[dbo].[ParkingCards] i
WHERE i.[ParentParkingCardId] is null
UNION ALL
SELECT i1.[ParkingCardId], i1.[ParentParkingCardId], i1.[CompanyId], i1.[DateRequested], i1.[StaffNo], i1.[Name], i1.[Section], i1.[JobTitle], i1.[Position],
i1.[Telephone], i1.[Mobile], i1.[Fax], i1.[POBox], i1.[Email], i1.[Nationality], i1.[Gender], i1.[ShiftType], i1.[Amount], i1.[PassIssueDate], i1.[PassExpiryDate], [Level] + 1
FROM [DCAServices].[dbo].[ParkingCards] i1
INNER JOIN cte
ON cte.[ParkingCardId] = i1.[ParentParkingCardId]
)
SELECT * From cte
ORDER BY [Level]
Check this link Common Table Expressions

Execute SELECT for all returned rows from another SELECT within the same query

With this query:
SELECT id FROM org.employees WHERE {some_condition}
For every row from the above query, I need to call:
SELECT * FROM org.work_schedule(#employeeId, #fromDate, #toDate)
where org.work_schedule is table-valued function that process all of the employee's available work schedules and constraints and return two DATETIME (start, end) columns representing the availabilities of the given employee for the provided date range.
I am thinking using a cursor on the first query and feed a temporary table that would be returned. Is this the only solution?
The project is in C# and I could also accomplish this in C# directly, but I suspect it would be more optimal to do this entirely in SQL (SQL Server 2008).
This seems localized, and I would generalize the question with :
How can I execute a query (SELECT) for every row returned by another query (SELECT) and return the entire results in one call (dynamically do SELECT UNION SELECT UNION ...)?
Thanks
You should use OUTER APPLY or CROSS APPLY instead of a cursor:
SELECT *
FROM ( SELECT id
FROM org.employees
WHERE {some_condition}) A
OUTER APPLY org.work_schedule(A.id, #fromDate, #toDate) B

sp_executesql runs in milliseconds in SSMS but takes 3 seconds from ado.net [duplicate]

This question already has an answer here:
Stored Proc slower from application than Management Studio
(1 answer)
Closed 9 years ago.
This is my dynamic query used on search form which runs in milliseconds in SSMS roughly between 300 to 400 ms:
exec sp_executesql N'set arithabort off;
set transaction isolation level read uncommitted;
With cte as
(Select ROW_NUMBER() OVER
(Order By Case When d.OldInstrumentID IS NULL
THEN d.LastStatusChangedDateTime Else d.RecordingDateTime End
desc) peta_rn,
d.DocumentID
From Documents d
Inner Join Users u on d.UserID = u.UserID
Inner Join IGroupes ig on ig.IGroupID = d.IGroupID
Inner Join ITypes it on it.ITypeID = d.ITypeID
Where 1=1
And (CreatedByAccountID = #0 Or DocumentStatusID = #1 Or DocumentStatusID = #2 )
And (d.JurisdictionID = #3 Or DocumentStatusID = #4 Or DocumentStatusID = #5)
AND ( d.DocumentStatusID = 9 )
)
Select d.DocumentID, d.IsReEfiled, d.IGroupID, d.ITypeID, d.RecordingDateTime,
d.CreatedByAccountID, d.JurisdictionID,
Case When d.OldInstrumentID IS NULL THEN d.LastStatusChangedDateTime
Else d.RecordingDateTime End as LastStatusChangedDateTime,
dbo.FnCanChangeDocumentStatus(d.DocumentStatusID,d.DocumentID) as CanChangeStatus,
d.IDate, d.InstrumentID, d.DocumentStatusID,ig.Abbreviation as IGroupAbbreviation,
u.Username, j.JDAbbreviation, inf.DocumentName,
it.Abbreviation as ITypeAbbreviation, d.DocumentDate,
ds.Abbreviation as DocumentStatusAbbreviation,
Upper(dbo.GetFlatDocumentName(d.DocumentID)) as FlatDocumentName
From Documents d
Left Join IGroupes ig On d.IGroupID = ig.IGroupID
Left Join ITypes it On d.ITypeID = it.ITypeID
Left Join Users u On u.UserID = d.UserID
Left Join DocumentStatuses ds On d.DocumentStatusID = ds.DocumentStatusID
Left Join InstrumentFiles inf On d.DocumentID = inf.DocumentID
Left Join Jurisdictions j on j.JurisdictionID = d.JurisdictionID
Inner Join cte on cte.DocumentID = d.DocumentID
Where 1=1
And peta_rn>=#6 AND peta_rn<=#7
Order by peta_rn',
N'#0 int,#1 int,#2 int,#3 int,#4 int,#5 int,#6 bigint,#7 bigint',
#0=44,#1=5,#2=9,#3=1,#4=5,#5=9,#6=94200,#7=94250
This sql is formed in C# code and the where clauses are added dynamically based on the value the user has searched in search form. It takes roughly 3 seconds to move from one page to 2nd. I already have necessary indexes on most of the columns where I search.
Any idea why would my Ado.Net code be slow?
Update: Not sure if execution plans would help but here they are:
It is possible that SQL server has created inappropriate query plan for ADO.NET connections. We have seen similar issues with ADO, usual solution is to clear any query plans and run slow query again - this may create better plan.
To clear query plans most general solution is to update statistics for involved tables. Like next for you:
update statistics documents with fullscan
Do same for other tables involved and then run your slow query from ADO.NET (do not run SSMS before).
Note that such timing inconsistencies may hint of bad query or database design - at least for us that is usually so :)
If you run a query repeatedly in SSMS, the database may re-use a previously created execution plan, and the required data may already be cached in memory.
There are a couple of things I notice in your query:
the CTE joins Users, IGroupes and ITypes, but the joined records are not used in the SELECT
the CTE performs an ORDER BY on a calculated expression (notice the 85% cost in (unindexed) Sort)
probably replacing the CASE expression with a computed persisted column which can be indexed speeds up execution.
note that the ORDER BY is executed on data resulting from joining 4 tables
the WHERE condition of the CTE states AND d.DocumentStatusID = 9, but AND's other DocumentStatusIDs
paging is performed on the result of 8 JOINed tables.
most likely creating an intermediate CTE which filters the first CTE based on peta_rn improves performance
.net by default uses UTF strings, which equates to NVARCHAR as opposed to VARCHAR.
When you are doing a WHERE ID = #foo in dot net, you are likely to be implicitly doing
WHERE CONVERT(ID, NVARCHAR) = #foo
The result is that this where clause can't be indexed, and must be table scanned. The solution is to actually pass each parameter into the SqlCommand as a DbParameter with the DbType set to VARCHAR (in the case of string).
A similar situation could of course occur with Int types if the .net parameter is "wider" than the SQL column equivalent.
PS The easiest way to "prove" this issue is to run your query in SSMS with the following above
DECLARE #p0 INT = 123
DECLARE #p1 NVARCHAR = "foobar" //etc etc
and compare with
DECLARE #p0 INT = 123
DECLARE #p1 VARCHAR = "foobar" //etc etc

querying flags recursively in database

I have a hierarchy of Article, and each Article has a property IsCommentable. This can take a value of true, false, or NULL. If it is NULL, it means that it inherits the value based on it's parents. Articles can be nested recursively, and there is no limit to the 'depth'.
Now, I need to make a query where I get all the articles from a database which are commentable. Is there any way these can be loaded via an SQL query?
Assuming you are using SQL Server, You can do it with a recursive CTE.
WITH cte (id, iscommentable) AS (
SELECT id, iscommentable FROM Article WHERE iscommentable IS NOT NULL
UNION ALL
SELECT a1.id, a2.iscommentable FROM Article a1
INNER JOIN cte a2 ON a1.parent=a2.id
WHERE a1.iscommentable IS NULL
)
SELECT * FROM cte
SQL fiddle example.

Turning a table "on it's side" in asp.net - how?

How do I turn this table:
+------------+-----------------+
| Category + Subcategory |
+------------+-----------------+
|Cat..........+ Persian.........|
|Cat..........+ Siamese........|
|Cat..........+ Tabby...........|
|Dog.........+ Poodle..........|
|Dog.........+ Boxer............|
+------------+----------------+
on it's side to get the following:
+------------+-----------------+
| Cat......... + Dog............. |
+------------+-----------------+
+ Persian..+ Poodle.........+
+ Siamese + Boxer...........+
+ Burmese + ...................+
+------------+-----------------+
The initial table is from the following MySQL query:
select c.CATEGORYNAME, sc.NAME from subcategorydefinition sc
join categorydefinition c on sc.CATEGORYID = c.CATEGORYID
where c.ISDELETED = 0
order by CATEGORYNAME, NAME ASC
And I want to display it in (probably) a Gridview.
Cheers!
Pivot is static in SQL. You need to know in advance the columns you want in output, so if the list of categories is not fixed, you can't use pivot directly.
If you were using Microsoft SQL Server (which I know you're not, but it's for the sake of example), you could use a dynamic query in a stored procedure, as described here:
http://www.simple-talk.com/community/blogs/andras/archive/2007/09/14/37265.aspx
Now, in MySql, there is no way to execute dynamic SQL on the sql side (no equivalent of EXECUTE or sp_executeqsl), so your best choice would be to generate a similar SQL query server-side (aspnet server-side).
Another simpler idea IMHO would be to forget about doing it in SQL, but to do the aggregation in your C# code.
You should use pivot
To do this in SQL, you'd need to dynamically generate your query based on the available set of values in the "Category" column. This is usually fairly painful and error prone, regardless of whether you do it in pure SQL (in a sproc) or in code (dynamic SQL).
I'd recommend reading your values from the database in the way that they are stored, then dynamically creating a DataTable or similar structure to use as the datasource for your UI.
I don't have a working version of MySql handy but this will work as long as there is always more cats than dogs because of the left join at the end of the script. I forgot that there isn't a full outer join in MySql but you could use this logic to try it out.
But the point of this is that if you have two tables with arbitrary keys you can join on the keys to get the results lined up like you want.
-- drop tables
DROP TABLE dbo.cat
DROP TABLE dbo.dog
--create dog table
create table dog (
dog_id int IDENTITY(1,1) NOT NULL
,dog varchar(50)
)
--add dogs only
insert into dog (dog)
select subcategory
FROM play.dbo.test
where category = 'Dog'
--create cat table
create table cat (
cat_id int IDENTITY(1,1) NOT NULL
,cat varchar(50)
)
--add cats only
insert into cat (cat)
select subcategory
FROM play.dbo.test
where category = 'cat'
-- disply everything
SELECT cat
, dog
from dog d
--full outer join cat c
left join dog d
on d.dog_id = c.cat_id

Categories