Simple.Data coalesce operation in select statement - simple.data

can any one help on translating below T-SQL statement to
Simple.Data query
select isnull(A.SomeValue,B.SomeValue),T.Id
from Table1 T1
left outer join ATable A on T1.AID = A.Id
left outer join BTable B on T1.BId = B.Id

Simple.Data doesn't support coalescing in T-SQL. The standard way to get around this is to put the query in a stored procedure and then use Simple.Data to call the sproc. Details for that can be found enter link description here.

Related

T-SQL: Select foreign key from one table and find corresponding rows in another

I have two tables: tblA contains a foreign key which I want to use to pull corresponding rows from tblB. The following queries will not work, but they explain what I want to do:
SELECT [MyID]
FROM [tblA]
SELECT [MyColumn]
FROM [tblB]
WHERE [ID] = [tblA].[MyID]
This should be a fairly simple query but I am a noob with T-SQL.
Use a JOIN:
SELECT A.MyID, B.MyColumn
FROM tblA A INNER JOIN tblB B
ON A.MyID= B.ID
There are different types of joins, the INNER JOIN returns only rows from tblA with a matching key in tblB.

EF Function Import does not recognise columns returned by StoredProc [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
EF4 - The selected stored procedure returns no columns
I have a stored procedure which populates a #temp table, does things to its records and then SELECTs the records.
Problem is, when I try to create a function import and click on [Get Column Information], the results pane shows the message "The selected stored procedure or function returns no columns".
Now, I KNOW for a fact that it does indeed return columns because if I run it directly from the db, I get the expected resultset back.
The stored procedure can be summarised as follows:
SELECT P.PersonID, P.Surname, P.NickName, P.DateofBirth
INTO #SeriesCompleted
FROM
Table1 T (NOLOCK)
INNER JOIN
Table2 P (NOLOCK) ON T.PID = P.PID
;
Select r.PID, SUM(rt.Distance) 'Distance'
INTO #Distance
FROM
#SeriesCompleted sc
inner join table3 rsr (NOLOCK) on rsr.SeriesId = sc.SeriesId
inner join table4 r (NOLOCK) on r.PID = sc.PID
inner join table5 rt (NOLOCK) on rt.RouteID = r.RouteID
GROUP BY r.PID;
UPDATE #SeriesCompleted
SET Distance = d.Distance
FROM #SeriesCompleted sc
INNER JOIN #Distance d on d.PID = sc.PPID;
--Here is where the result is returned.
SELECT distinct sc.PersonID, sc.NickName, sc.Surname, sc.DateofBirth, sc.NumberFinished, sc.Distance
FROM #SeriesCompleted SC
After googling furiously, I came across the answer: EF4 - The selected stored procedure returns no columns
EF cannot get metadata from a stored proc that uses dynamic queries or temp tables. The solution was to either manually create the complex returned type OR put
SET FMTONLY OFF
in my stored proc definition. The danger with the second option, of course is that the stored proc will be executed when Visual Studio executes the metadata call, so this would ideally only be used if the stored procedure doesn't change anything.
UPDATE: An alternative is to make sure that the stored procedure actually works. Another thing you could do is to create a dummy stored procedure that returns the columns you want, bind to it and then do the actual logic.

Combining data from two SQL queries

I'm porting my app from Django to ASP.NET Webforms (against my will, but what can we do with the corporate world..), and I'm used to Django generating all my SQL queries so now I need help.
I have 3 tables: proceso,marcador,marcador_progreso
Every proceso has many marcador_progreso, which in turn is the foreign key table to marcador.
So basically the tables look like:
proceso
id
marcador
id
text
marcador_progreso
id
marcador_id
proceso_id
state
For all the marcador_progreso where its proceso_id is the current proceso (from a QueryField in the URL), I need to list its state and it's respective marcador.text.
I've been working with EntityFramework but this is like a double query so I'm not sure how to do it.
I guess it is something that combines the following two statements, but I'm not sure how to do it.
SELECT [state] FROM [marcador_progreso]
SELECT [text] FROM [marcador] WHERE ([id] = marcador_id)
You want to do a JOIN:
SELECT mp.state, m.text
FROM marcador_progreso as mp
INNER JOIN marcador as m
ON mp.marcador_id = m.id
This is an excellent post that goes over the various join types.
You'll want to know about JOINs to call more than one table in your FROM clause. JOIN combines records from two or more tables in a database by using values common to each. There are different types - the SQL example below is an INNER join, which gets only records where both of the tables have a match on the common value. You may want to consider a LEFT join which would get any records that exist for the LEFT table (in this case marcador), even if there are not any matching record in the RIGHT(marcador_progreso ) table.
Pop the below in Management Studio, Play with different joins. Replace the INNER with LEFT, run it without the WHERE.
Read about `JOIN's.
In general, for your new venture of writing your own queries, they all start with the same basic structure:
SELECT (UPDATE,WHATEVER DML statement, etc) (COLUMNS) what you want to display (update,etc)
FROM (TABLE) where those records live
WHERE (FILTER/LIMIT) conditions that must be met by the data
Happy fetching!
SQL:
DECLARE #ProcessoId int
SET #ProcessoId = --1
SELECT m.[STATE],mp.[TEXT]
FROM marcador M
INNER JOIN marcador_progreso MP ON MP.marcador_id = m.id
WHERE proceso_id = #ProcessoId
EF INNER example
var marc = from m in yourcontext.marcador
join mp in yourcontext.marcador_progreso on m.id equals mp.marcador_id
where proceso_id == processoIdvariable
EF LEFT example
var marc = from m in yourcontext.marcador
join mp in yourcontext.marcador_progreso on m.id equals mp.marcador_id into details
from d in details.DefaultIfEmpty()
where proceso_id == processoIdvariable

SQLite "in" expression on multiple columns

I have such database query, which works well in MySQL:
select authors.name, authors.surname, books.name, publishers.name, copies.comment
from authors, books, publishers, copies
where (authors.id, books.id) in (select authorship.author_id, authorship.book_id from authorship)
and (books.id, publishers.id, copies.publish_id) in (select publishment.book_id, publishment.publisher_id, publishment.id from publishment);
In SQLite database I've got such error on part "where (authors.id, books.id) in":
Error: near ",": syntax error
Is it possible to make such type of query in SQLite (maybe - with different syntax)?
PS: I know that using joins is better for those case, but I want to know if it is possible for general knowledge.
I know that using joins is better for those case, but I want to know if it is possible for general knowledge.
No, this is not possible. Use explicit joins.
SELECT
a.name, a.surname, b.name, p.name, c.comment
FROM
authors a
INNER JOIN authorship ab ON ab.author_id = a.id
INNER JOIN books b ON b.id = ab.book_id
INNER JOIN publishment p ON p.book_id = b.book_id
INNER JOIN copies c ON c.publish_id = p.id
(And by the way, you should do the same for mySQL)

SQLAlchemy+SQLite Left Join Performance Issue

I have two identical queries save for the position of the left join in the from clause. One runs very slow, the other runs very fast (abbreviated for clarity):
--SLOW
SELECT DISTINCT b.id
FROM a LEFT OUTER JOIN b ON a.id = b.id
JOIN c ON a.id = c.a_id
JOIN d ON c.id = d.c_id
WHERE d.value = 9;
--FAST
SELECT DISTINCT b.id
FROM a JOIN c ON a.id = c.a_id
JOIN d ON c.id = d.c_id
LEFT OUTER JOIN b ON a.id = b.id
WHERE d.value = 9;
My problem is that, using SQLAlchemy, I seem to only be able to create the slow version of the query. Specifically, I am using table inheritance and am trying to run the following code:
return session.query(A).\
with_polymorphic(B).\
join(C).\
join(D).\
filter(D.value_id == 9).\
distinct()
In other words, I don't have control over where the LEFT JOIN is being created.
How do I make SQLite and/or SQLAlchemy smarter about this?
I am not aware of the ways to force SA into changing the query. But I would to identify why there is a difference in execution plan for the query. I would assume that if you have indices on the join columns, the order of the JOINs in the query should not matter.
You can use the EXPLAIN statement to check the execution plan. Although you need to get familiar with The Virtual Database Engine of SQLite. Still it might be possible you spot the difference between two execution plans immediatelly and will be able to improve the database performance instead of tricking the SA.
Try to also remove C or D from the query for even easier comparison of execution plans.

Resources