I was unable to cope with converting this Sql Query to Linq Expression
SELECT IdVehicle,
AVG(Kilometers)
FROM [Fuel]
GROUP BY IdVehicle, CONVERT(NVARCHAR, Fuel.Time, 102)
Rows are inserted into this table when someone fills fuel into vehicle. Kilometers are read from tachograph. It's possible to fill fuel twice a day so I need average value.
Ideally, LINQ result would be Dictionary.
I would really appreciate any suggestion.
For cases like these you might wanna try LinqPad (www.linqpad.net). This great tool lets you test and analyze both SQL statements and Linq expressions.
Related
I want to count the number of records inserted in a kdb+ database using a q query.
Currently, using below query:
count select from executionTable where ingestTimeStamp within 2019.09.07D00:00:00.000000000 2019.09.08D00:00:00.000000000
It works but not highly performant. Any recommendations to make it efficient is highly appreciated.
Thank you for your help.
If you only want count then use 'count i' inside select like below:
q) select count i from executionTable where ingestTimeStamp within 2019.09.07D00:00:00.000000000 2019.09.08D00:00:00.000000000
This will only get the count instead of fetching full data which is what your query is doing and that's one of the reasons for taking more time.
And if it is a partitioned database, then add 'date' in the filter as #Callum Biggs mentioned.
Given the information you have provided I'm assuming you're querying on-disk data, likely saved in a standard date partitioned structure. In this case, you should be specifying a date clause before you specify a time clause, this will prevent searching all the date directories.
select from executionTable where date=2019.09.07, ingestTimeStamp within 2019.09.07D00:00:00.000000000 2019.09.08D00:00:00.000000000
I'd suggest reading through the whitepaper on query optimization, it will give some guidance in good query structure, and how to take advantage of map reduction in kdb.
I am using JOOQ for writing SQL in my java code.I have following query written into the PostgreSQL database:'
Query: Fetches the total number of checked task and the total time taken to complete the tasks.
Total time for a task is calculated from table "workevents" by doing (endtime-starttime).But here I am fetching the total time spent on all the tasks.
with taskdata as (
select taskid from unittest.tasks
where projectname='test'and status='checked'
),
workevents as(
select (endtime-starttime) diff ,unittest.workevents.taskid as
workeventtaskid from unittest.workevents ,taskdata
where taskdata.taskid=unittest.workevents.taskid
)
select sum(workevents.diff),count(distinct workeventtaskid) from
workevents;
I have converted it into the jooQ AS below:
final String sql =
with(TASK_INFO_WRAPPER)
.as(select(TASK_ID).from(TASK_TABLE)
.where(PROJECT_NAME.eq(param()).and(TASK_STATUS.eq("checked"))))
.with(WORKEVENT_INFO_WRAPPER)
.as(select(TASK_END_TIME.sub(TASK_START_TIME).as("diff"),
WORKEVENT_TASK_ID.as("workeventtaskid"))
.from(WORKEVENT_TABLE, table(name(TASK_INFO_WRAPPER)))
.where("workeventinfo.taskid=taskinfo.taskid"))
.select().getSQL(ParamType.INDEXED);
But I am not able to get the aggregate sum of the "diff"(difference of the dates).Is there any function in JOOQ that can convert sql statement "select sum(workevents.diff)" into JOOQ.
I have tried sum(field) function but its giving compile time error because sum is used for numbers.
and Here I am calculating the accumulative sum of the difference of the two dates(diff).
All RDBMS behave subtly differently when implementing a date difference using the - operator, which is why it is generally recommended to use jOOQ's DSL.dateDiff() or DSL.timestampDiff() instead.
A side note on using WITH
WITH is often used to decompose a problem into smaller problems in SQL. But at some point, that decomposition leads to more complicated queries than necessary, as in your case. Especially when using jOOQ, it is often recommended to avoid common table expressions (WITH) or derived tables, not only because they're a bit more difficult to express in jOOQ, but also because they don't really add value to your query.
Your query could be written like this instead:
select
sum(e.endtime - e.starttime),
count(distinct e.taskid)
from unittest.tasks t
join unittest.workevents e on t.taskid = e.taskid
where t.projectname = 'test' and t.status = 'checked'
And that would obviously be quite easier to translate to jOOQ.
I am querying the Nhibernate criteria query with more then 2100 values for In clause.
I do something like Session.CreateCriteria(typeof()).Add(Expression.In("fieldName",arrayValue))
Where arrayValue contains more then 2100 values. I face error
Exception occurred:
UnknownError
NHibernate.ADOException: could not execute query ..then the query with more then 3000 values in array.
with some google help we found out that IN clause in Sql supports only till 2100 values.
Does anyone has faced similar issue earlier? We do not want to change the query as it is written in some generic way and not customized one.
This is a limitation of SQL Server. I wouldn't suggest doing this, but if you insist, you could work around it by creating a table-value sql function (see http://www.dzone.com/snippets/function-getting-comma) that splits up a string by commas (or whatever delimiter you want) and returns the values as a table, and then pass in all your ID's as (say) a comma separated list in 1 parameter and use a SQLCriterion in your criteria query.
eg:
criteria.Add(
new SQLCriterion("{alias}.ID IN (SELECT element FROM dbo.GetCSVValues(?))",
new[]{csvListOfIds},
new[]{NHibernateUtil.String}))
You could split the array into multiple batches, query multiple times, and then combine the result.
I know how to use "DISTINCT" in Doctrine 2, but I really need to use "DISTINCT ON (field)" and I don't know how to do this with the QueryBuilder.
My SQL query looks like:
SELECT DISTINCT ON (currency) currency, amount FROM payments ORDER BY currency
And this query works perfect, but I can't use it with the QueryBuilder. Maybe I could write this query on some other way?
I would suggest that the SELECT DISTINCT ON (..) construct that PostgreSQL supports is outside the Object Relational Model (ORM) that is central to Doctrine. Or, perhaps put another way, because SELECT DISTINCT ON (..) is rare in SQL implementations Doctrine haven't coded for it.
Regardless of the actual logic for it not working, I would suggest you try Doctrine's "Native SQL". You need to map the results of your query to the ORM.
With NativeQuery you can execute native SELECT SQL statements and map
the results to Doctrine entities or any other result format supported
by Doctrine.
In order to make this mapping possible, you need to describe to
Doctrine what columns in the result map to which entity property. This
description is represented by a ResultSetMapping object.
With this feature you can map arbitrary SQL code to objects, such as
highly vendor-optimized SQL or stored-procedures.
SELECT DISTINCT ON (..) falls into vendor-optimized SQL I think, so using NativeQuery should allow you to access it.
Doctrine QueryBuilder has some limitations. Even if I didn't check if it's was possible with query builder, I do not hesitate to use DQL when I do not know how to write the query with query builder.
Check theses examples at
http://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html#dql-select-examples
Hope this help.
INDEX BY can be used in DQL, allowing first result rows indexed by the defined string/int field to be overwritten by following ones with the same index:
SELECT
p.currency,
p.amount
FROM Namespace\To\Payments p INDEX BY p.currency
ORDER BY p.currency ASC
DQL - EBNF - INDEX BY
Let's say I have a SQL 2005 database with a table called Restaurants. The Restaurants table has the following columns:
RestaurantId
Name
Latitude
Longitude
I want to let users search for restaurants by name and/or address. How do I write the LINQ query to support this? I need to be able to support the possibility that the user doesn't enter in a name or address, just the name, just the address, or both name and address.
My initial idea was to write a stored procedure to calculate the distance between two lat/long pairs and a table value function for calling FREETEXTTABLE and using some conditional Join calls on my query. However, it appears that Entity Framework 4 doesn't support table value functions.
You certainly can write a proc which returns entity types. Indeed, in EF 1 that was the only option for procs. The proc returns a set of values, not a table, but I can't see that you actually need this.
You can also do free-form T-SQL in EF 4 using Context.ExecuteStoreQuery.
You cannot write any LINQ that supports geospatial queries at this point of time - be it EF or LinqToSql. This is because there is no LINQ syntax which can handle the special ST<whatever> spatial syntax that exists in SQL Server 2008. (eg. STIntersects(..))
You will need to write a Stored Procedure which you can then get access to via EF.
If you wish to return a Sql GEOGRAPHY field in a result, you will need to return a VARBINARY(MAX) i think as the equivalent field type for the C# code.
Hope This Helps.