How do I get the next record in table with linq? - linq

I have a table Orders like this:
ID
CustomerID
name
ID CustomerID name
1 4 aa
5 6 bbb
4 9 ccc
8 10 ddd
first ordering the table,then get the next row..... How to do?
if current row id is 4 ,i want to get the row where id==5

I think you want this:
Orders.OrderBy(x=>x.ID).Skip(1).Take(1)
Edit: If I understand your question now:
Orders.OrderBy(x=>x.ID).Where(x=>x.ID>4).FirstOrDefault();

Related

How to get count from pivot table laravel?

I have two tables,
User cities user_cities
id name id name user_id city_id
1 aaa 1 cityA 2 1
2 bbb 2 cityB 2 3
3 ccc 3 cityC 1 2
3 3
3 2
Now i am filtering the user by cities. I am having the city_id = 2. So that i have to get the count of users belongs to the city_id 2.
How can i get that using laravel eloquent
As you describe in your answer you have the third table user_cities.
If you have table user_cities then count user like
DB::table('user_cities')->where('city_id', '2')->count();
There are different ways to do this, check https://laravel.com/docs/5.8/queries
But a simple way of doing it could be:
UserCity::whereCityId(2)->count();
Or:
UserCity::where('city_id',2)->count();
If you have set correctly the relationship between them, you can do something like that:
City::where('id', '=', 2)->withCount('users')->get();
It will place a {relation}_count column on your resulting model, and thus you can call it like a property: $city->users_count
Edit: For further information, you can check the official documentation.
Try this query :
$count = City::where('id', 2)->whereHas('users')->count();

Insert empty row which shows total when Item number get change

In my report, there is a table which shows Item number, Employee name, and Hours.
Item Number Employee Hrs
1 ABC 2
1 DEF 1
2 ABC 3
2 XYZ 4
I want to show one empty row below as a separator when Item number get change. In that row want to show the total number of hours. Like below.
Item Number Employee Hrs
1 ABC 2
1 DEF 1
Total 3
2 ABC 3
2 XYZ 4
Total 7
Thanks, Bhushan the link was helpful. Link: https://learn.microsoft.com/en-us/sql/reporting-services/lesson-6-adding-grouping-and-totals-reporting-services?view=sql-server-2017.
I changed the report design by adding grouping. Also showing sum after each group.

PIG: Filter hive table by previous table result

I need to query one HIVE table and filter the other table with one column of the previous one.
Example:
A = LOAD 'db.table1' USING org.apache.hive.hcatalog.pig.HCatLoader();
filterA = filter A by (id=='123');
B = LOAD 'db.table2' USING org.apache.hive.hcatalog.pig.HCatLoader();
//the problem is here. filterA has many rows. I need to apply filter for each of the row.
filterB = filter B by (id==filterA.id);
Data in A:
tabid id dept location
1 1 IS SJ
2 4 CS SF
3 5 EC MD
Data in B:
tabid id name address
1 4 john 123 S AVE
2 5 jane 456 N BLVD
3 9 nick 789 GREAT LAKE DR
Expected Result:
tabid id name address
1 4 john 123 S AVE
2 5 jane 456 N BLVD
As asked in the comment, it sounds like what you're looking for is a join. Sorry if I misunderstood your question.
A = LOAD 'db.table1' USING org.apache.hive.hcatalog.pig.HCatLoader();
B = LOAD 'db.table2' USING org.apache.hive.hcatalog.pig.HCatLoader();
C = JOIN A by id, B by id;

Oracle reduce result set on field duplication

I have a result set of a select in Oracle (12c) as the following:
GROUP_ID NAME ORDERING
1 AA 0
1 AA 1
1 AB 2
1 AC 3
2 BA 1
2 BA 2
2 BB 3
2 BC 4
I do not know how I could reduce the result set to remove rows based on one column while keeping the other fields. The expected outcome looks like the following:
GROUP_ID NAME ORDERING
1 AA 1
1 AB 2
1 AC 3
2 BA 2
2 BB 3
2 BC 4
I tried to solve it using group by but it got rid of the required field ordering. I am not an expert on window functions but I think it could be a valid attempt to use one.
From your data, it seems that you only need:
select group_id, name, max(ordering)
from yourTable
group by group_id, name

How do i resolve conflict when selecting productid whose product visit have same occurences

My Database Table Contain Records Like this:
ID ProductId Occurences
1 1 1
2 2 5
3 3 3
4 4 3
5 5 5
6 6 8
7 7 9
Now i want to get top 4 ProductId with the highest Occurences.
This is my Query:
var data = (from temp in context.Product
orderby temp.Occurences descending
select temp).Take(4).ToList();
Now here as because ProductId 2 and 5 have same occurences and ProductId 3 and 4 also have same occurences then here I am not getting that how to resolve this means which product id should i take as because they are having same occurences.
Basically i am selecting this productid to display this products on my website.i will display those products which are return by this query.
So can anyone please give me some idea like how to resolve this ???
Expected Output:
ID ProductId Occurences
1 7 9
2 6 8
so Now for 3rd position Which ProductId i should select as because both ProductId 2 and 5 have same Occurences and for 4th position which ProductId i should select among 3 and 4 as because they both too have same occurences.
can I suggest you to use group by technique and pick the first one
context.Product.GroupBy(p => p.Occurance)
.Select(grp => grp.FirstOrDefault())
.Take(4)ToList();
Sorry, I have not tested this but it should do the job. You just need to add OrderBy along with it.

Resources