How to translate this SQL Query to DQL? - sql

I have a lot of difficulties in trying to translate this SQL query to a DQL one (I am using Symfony2).
SELECT d.* FROM Document d JOIN DocumentType dt ON dt.id = d.document_type_id WHERE (d.dateFinal - INTERVAL dt.renewal SECOND) > NOW();"
Thanks a lot !

Instead of database fields, you need to use the fields of you Document and DocumentType classes.
The join is done by naming the association field of your document (something like d.documenttype)
For those MySQL date functions: At least as far as I know, you would need to write custom DQL functions. Behind that link there is an example of an INTERVAL function. This might be the point where you want to execute the native SQL query instead.
SELECT d
FROM MyProject\Model\Document d
JOIN d.documenttype
WHERE [...your own DQL functions here...]

Related

Converting Legacy SQL Query to Standard SQL

I am looking to convert the below Legacy SQL Query to Standard SQL. The issue I am having is that I need to unnest two tables (labels and credits). How can I convert this query? Thanks!
I run into a "Scalar subquery produced more than one element" whenever I try to rewrite this query (see below).
Legacy SQL Query I am trying to rewrite:
SELECT
service.description,
sku.description,
usage_start_time,
usage_end_time,
labels.key,
labels.value,
cost,
usage.amount,
project.name,
credits.name,
credits.amount
FROM
flatten([gcp_billing_data.gcp_billing_export],
credits)
What I have tried so far in Standard SQL:
SELECT
service.description AS service,
sku.description AS sku,
usage_start_time,
usage_end_time,
l.key,
l.value,
cost,
usage.amount AS usage,
project.name AS project,
c.name AS credit,
c.amount
FROM
`gcp_billing_data.gcp_billing_export`,
UNNEST(labels) AS l,
UNNEST(credits) AS c
Group by 1,2,3,4,5,7,8,9,10,11
This query runs, but the number of rows is significantly less than I would expect.
quick and formal fix for your query in Standard SQL is something like to replace below
(select l.value from unnest(labels) as l)
with
(select string_agg(l.value) from unnest(labels) as l)
But it is still not exactly the same as what initial version of your Legacy SQL version of query is doing

Group By Using Wildcards in Big Query

I have this query:
SELECT SomeTableA.*
FROM SomeTableB
LEFT JOIN SomeTableA USING (XYZ)
GROUP BY SomeTableA.*
I know that I cannot do the GROUP BY part with wildcards. At the same time, I don't really like listing all the columns (can be up to 20) manually.
Could this be added as new feature? Or is there any way how to easily get the list of all 20 columns from SomeTableA for the GROUP BY part?
If you really have the exact query shown in your question - then try below instead - no grouping required
#standardSQL
SELECT DISTINCT *
FROM `project.dataset.tableA`
WHERE xyz IN (SELECT xyz FROM `project.dataset.tableB`)
As of Group By Using Wildcards in Big Query this sounds more like grouping by struct which is not supported so you can submit feature request if you want - https://issuetracker.google.com/issues/new?component=187149&template=0

LinqPad - Convert SQL to Linq command

I recently purchased LINQPad in hopes that it would allow me to convert SQL statements into LINQ statements.
Using LINQPad, I am able to attach a DB and run the SQL statement which returns the results I need.
But I can not find a 'command' to convert that SQL statement into LINQ.
Can you please let me know how to convert SQL to LINQ by using LINQPad OR another tool?
There is a tool called Linqer, but be careful: transliterating from SQL to LINQ can give you the worst of both worlds.
For instance, suppose you want all purchases of $1000 or greater paid for in cash or by customers who live in Washington. Here's the query in SQL:
SELECT p.*
FROM Purchase p
LEFT OUTER JOIN
Customer c INNER JOIN Address a ON c.AddressID = a.ID
ON p.CustomerID = c.ID
WHERE
(a.State = 'WA' || p.CustomerID IS NULL)
AND p.ID in
(
SELECT PurchaseID FROM PurchaseItem
GROUP BY PurchaseID HAVING SUM (SaleAmount) > 1000
)
How would translate this to LINQ? The wrong way is to transliterate the query into LINQ, trying to reproduce the outer and inner joins, subquery and group clause. The right way is to map your original query (in English) directly into LINQ, leveraging LINQ's linear flow of data and association properties:
I want all purchases...
from p in db.Purchases
...of $1000 or greater...
where p.PurchaseItems.Sum (pi => pi.SaleAmount) > 1000
...paid for in cash...
where p.Customer == null
...or by customers who live in Washington
|| p.Customer.Address.State == "WA"
Here's the final query:
from p in db.Purchases
where p.PurchaseItems.Sum (pi => pi.SaleAmount) > 1000
where p.Customer == null || p.Customer.Address.State == "WA"
select p
More info here.
In general there are no tools to covert SQL to Linq as #andres-abel mention before, but sometimes you have to write Linq that will execute exactly as specified SQL (for example because of performance issues, backward compatability or some other reasons).
In this case I'll advice you to do reverse engineering by yourself:
configure logging of dump SQL statements generated by Linq to stdout using
ObjectQuery.ToTraceString,
DbCommand.CommandText,
logger availabe to your data source
manually rewrite Linq statement until you'll get what you need
LinqPad contains no SQL->LINQ translator. LinqPad does actually not contain any LINQ->SQL translator either. It relies on the .Net Linq-to-Sql library or Entity framework for the translation.
I don't know of any other tool with that capability either. In simple cases it would be possible to make one, but for more complex scenarios it would be impossible as there is no LINQ expression that matches some SQL constructs.

How do I convert this SQL query to LINQ-to-SQL?

How do I convert this SQL query to LINQ-to-SQL?:
select
COUNT(ua.UserAlertID) as Alerts,
ph.LastName +' '+ ph.MiddleName as Name,
ph.Email,us.UserSystemID,
ur.UserStatus from PHUser ph
inner join UserSystem us on us.UserID=ph.UserID
inner join UserRole ur on ur.UserID=ph.UserID
inner join Role rr on rr.RoleID=ur.RoleID
inner join UserAlerts ua on ua.SeniorID=ph.UserID
group by ph.LastName,ph.MiddleName,ph.Email,us.UserSystemID,ur.UserStatus
I have converted most of the above query to LINQ but I got stuck to counting the number of values on the ua.UserAlertID column: COUNT(ua.UserAlertID) as Alerts.
How do I convert that to LINQ?
Kindly suggest How to convert COUNT(ua.UserAlertID) as Alerts in Linq??
Thanks
Can I see your converted LinQ?
If your Query is ok, you got a line like
group s by new {ua.UserAlertId,...,...} into temp.
as I remember in select new {} part use
select new
{
Total Alerts = temp.Key.UserAlertdId.Count(), ...,...,...,...
}
can you please try this and notify me?
A number of people can help you with this query. It's pretty straightforward. There is a product (very inexpensive) that can help you with these sorts of things. The product is Linqer (http://www.sqltolinq.com/). This tool will convert most SQL statements to Linq. There is a 30 day trial period and I think the cost is < $40. I have used it many times to get up to speed on converting SQL queries to Linq.
Then I might suggest you get a free product called LinqPad. This will allow you to prototype your Linq queries before pasting them into production code. It is a phenomenal tool.

NHibernate: help translating an hql query to use criteria api instead

I have the following hql query which I'd like to switch over to the criteria API
select a.Id as Id, a.Name as Name, a.ActiveStatus as ActiveStatus,
dbo.GetActivityStartDate(a.Id) as StartDate,
dbo.GetActivityEndDate(a.Id) as EndDate,
coalesce(ac.Id,0) As CategoryId,
coalesce(ac.Name,'') As CategoryName
from Activity as a
left outer join a.Category as ac
Obviously the initial properties on the select line are trivial (Projections.Property); my question is..how do I map the remaining 4 properties?
I have a custom dialect that registers dbo.GetActivityStartDate and dbo.GetActivityEndDate as standard SQL functions - so that much is already taken care of.
So...it turned out that I needed to register the "ISNULL" function with my custom dialect; once I did that, it was a simple matter of using the Projections.SqlFunction to extract the data in the format I required.