Convert SQL to Linq - Where, Groupby and Having - sql

The proper syntax in Linq for this SQL query is eluding me. I'd appreciate any hand with this.
SQL:
SELECT TOP 1 posdate
FROM dailypos
GROUP BY posdate
HAVING Count(DISTINCT customernumber) = 3
ORDER BY posdate DESC)
Essentially, I get files from 3 different customers and I need a quick way to determine the most recent date for which I have data from all 3. I'm open to a different approach, but this SQL works for what I need.
I can do the group, but I don't know how to handle the 4th line (HAVING Count(Distinct...))
Thanks in advance.

Try something like this:
var result = context.dailypos
.GroupBy(x => x.posdate)
.Where(g => g.Select(x => x.customernumber).Distinct().Count() == 3)
.Select(g => g.Key)
.OrderByDescending(x => x)
.Take(1);

Related

I need to get Subject name with total number of Book

I have a SQL Query which I want to convert in Linq or want to show data
as pictured here
Here is the query:
select sae_subcategorymaster.subject, count(sae_tblbookdetail.title)
from sae_tblbookdetail inner join sae_subcategorymaster
on sae_subcategorymaster.subject=sae_tblbookdetail.subject
group by sae_subcategorymaster.subject
What is a simple way to do this?
Grouping is supported in LinqEF. Provided you have your entities related. (Books have a reference to their subcategory.)
var totals = context.Books
.GroupBy(book => book.SubCategory.Subject)
.Select(group => new
{
Subject = group.Key,
BookCount = group.Count()
}).ToList();

Get multiple distinct values from query in Entity Framework

I'm running a query that will get results based on a location search and date. I have a geography column with location points (lat/long) that's indexed. When I search for an event on a date it searches for all events within a distance (radius) on that date.
The problem is that if there are, say 10 events, all at the same location on the same date, all 10 results will come back in the first page. I'd like to mix this up and only show 2-3 from each location to give the result set some variety, so the user doesn't just see all events from one location.
I know I can use distinct to only fetch one event from each location, but how would I use it to get me 2-3 distinct values?
Here is my query so far.
viewModel.Events = dbContext.YogaSpaceEvents
.Where(i => i.EventDateTime >= adjustedSearchDate &&
i.LocationPoints.Distance(location) <= radius)
.Distinct()
.OrderBy(i => i.EventDateTime)
.Select(i => new EventResult
{
//fill view model here
})
.ToPagedList(Page, 10);
I don't think there's a way to get EF to generate such a query, which for SQL Server would be something like this:
with q as
(
select *,
( row_number() over (partition by StateProvinceId order by CityID) -1 ) / 3 MyRanking
from Application.Cities
)
select CityId, CityName,StateProvinceID
from q
order by MyRanking, StateProvinceID, CityID
offset 10 rows
fetch next 20 rows only
Note that this example doesn't use distance. But the idea is identical: the first 3 cities in each state are returned first, then the next 3, etc.
Or you could fetch all the matching events and sort them in memory.
I think you should be able to do something like:
dbContext.YogaSpaceEvents
.Where(i => i.EventDateTime >= adjustedSearchDate &&
i.LocationPoints.Distance(location) <= radius)
.GroupBy(i => i.Geography)
.SelectMany(g => g.OrderBy(x => x.EventDateTime).Take(3))
.Select(i => new EventResult { //fill view model here })
.ToPagedList(Page, 10);

CakePHP SQL-query count

I have a problem concerning CakePHP SQL-queries. I need to fetch products from the database where shop_id is given and then count the products. All I need is Product.url and its count.
This will do the trick in plain SQL:
SELECT url,COUNT(*) as count FROM products GROUP BY url ORDER BY count DESC;
This one I used to get all products relating to shops:
$this->Product->find('all', array('conditions' => array('Product.shop_id'=>$id)));
That works correctly, but I need to convert that SQL-query above to CakePHP.
I tried something like this:
$this->Product->find('count', array('conditions' => array('Product.shop_id'=>$id),
'fields'=>array('Product.url','Product.id'),
'order'=>'count DESC',
'group'=>'Product.url'));
That returns only an int. But if I run that SLQ-query presented above in mysql server, I get two columns: url and count. How do I get the same results with CakePHP?
You can try this:
$data = $this->Post->query(
"SELECT COUNT(id),MONTH(created) FROM posts GROUP BY YEAR(created), MONTH(created);"
);
The most easiest way to do this:
$this->Product->query("SELECT url,COUNT(*) as count FROM products GROUP BY url ORDER BY count DESC;");
...at least for me.
Try the following code:
$this->Product->virtualFields['CNT_PRODUCTS'] = 0;
$this->Product->find('count', array('conditions' => array('Product.shop_id' => $id),
'fields' => array('Product.id', 'Product.url', 'count(*) as CNT_PRODUCTS'),
'order' => array('CNT_PRODUCTS' => 'DESC'),
'group' => 'Product.url'));

Need help creating a linq select

I need some help creating an LINQ select, i have a table with some columns in it, but only 2 of interest to this problem.
userid, type
Now this table have many thousands entries, and I only want the top, let’s say 50. So far so good, but the hard part is that there a lot of rows in success that should only be counted as 1.
Example
Type UserId
============
Add 1
Add 1
Add 1
Add 2
I would like this to only be counted as 2 in the limit of rows I am taking out, but I would like all the rows to be outputted still.
Is this possible with a single SQL request, or should I find another way to do this?
Edit: I can add columns to the table, with values if this would solve the problem.
Edit2: Sotred procedures are also an solution
Example 2: This should be counted as 3 rows
Type UserId
============
Add 1
Add 1
Add 2
Add 1
Are you stuck on LINQ?
Add a PK identity.
Order by PK.
Use a DataReader and just count the changes.
Then just stop when the changes count is at your max.
If you are not in a .NET environment then same thing with a cursor.
Since LINQ is deferred you might be able to just order in LINQ and then on a ForEach just exit.
I'm not close to a computer right now so I'm not sure is 100% correct syntax wise, but I believe you're looking for something like this:
data.Select(x => new {x.Type, x.UserId})
.GroupBy(x => x.UserId)
.Take(50);
You could do it with Linq, but it may be a LOT slower than a traditional for loop. One way would be:
data.Where((s, i) => i == 0 ||
!(s.Type == data[i-1].Type && s.UserId == data[i-1].UserId))
That would skip any "duplicate" items that have the same Type and UserID as the "previous" item.
However this ONLY works if data has an indexer (an array or something that implements IList). An IEnumerable or IQueryable would not work. Also, it is almost certainly not translatable to SQL so you'd have to pull ALL of the results and filter in-memory.
If you want to do it in SQL I would try either scanning a cursor and filling a temp table if one of the values change or using a common table expression that included a ROW_NUMBER column, then doing a look-back sub-query similar to the Linq method above:
WITH base AS
(
SELECT
Type,
UserId,
ROW_NUMBER() OVER (ORDER BY ??? ) AS RowNum
FROM Table
)
SELECT b1.Type, b1.UserId
FROM base b1
LEFT JOIN base b2 ON b1.RowNum = b2.RowNum - 1
WHERE (b1.Type <> b2.Type OR b1.UserId <> b2.UserId)
ORDER BY b1.RowNum
You can do this with LINQ, but I think it might be easier to go the "for(each) loop" route...
data.Select((x, i) => new { x.Type, x.UserId, i })
.GroupBy(x => x.Type)
.Select(g => new
{
Type = g.Key,
Items = g
.Select((x, j) => new { x.UserId, i = x.i - j })
})
.SelectMany(g => g.Select(x => new { g.Type, x.UserId, x.i }))
.GroupBy(x => new { x.Type, x.i })
.Take(50);
.SelectMany(g => g.Select(x => new { x.Type, x.UserId }));

Using a subquery for a column with QueryOver

I'm trying to get something similar to the SQL below via QueryOver:
SELECT
docs.*,
(SELECT TOP 1 eventDate from events WHERE id=docs.id
AND type=4 ORDER BY eventDate DESC) as eventDate
FROM documents as docs
WHERE doc.accountId = ...
I've got close with a projection, however I'm not sure how to get the entire documents table back. Documents has a one-to-many relationship with Events, I don't want to outer join as it will bring multiple results, and an inner join may not bring back a row:
var query = QueryOver<Document>
.Where(d => d.Account == account)
.SelectList(list => list
.Select(d => d)
.Select(d => d.Events.OrderByDescending(e => e.EventDate).FirstOrDefault(e => e.Type == 4))
)
.List<object[]>()
.Select(d => return new DocumentSummary(d[0],d[1]) etc.);
Is there an easier way of performing subqueries for columns? I'm reluctant to replace this with the property performing a query in its get.
After some research it looks like HQL (which QueryOver is converted into) does not support TOP inside subqueries.
My solution: create a view which includes the computed properties, and then mark these properties in the mappings files as insert="false" and update="false"