I have written a lambda expression and produce the results below.
Result
GroupId GroupName
1 network.it
1 network.it
1 network.it
2 software.it
2 software.it
2 software.it
2 software.it
After getting the above result I want to convert above data by using Count and GroupBy through lambda expression like following.
Desired Result
Group Id, GroupName, Count
1 network.it 3
2 software.it 4
I hope anyone can answer as soon as possible.
Thanks in advance
You have to use GroupBy clause
var result = from d in data
group d by d.GroupId into g
select new {g.Key, g.First().GroupName, g.Count()}
A little late, but in case someone else comes across this looking for the same information, the lambda way would be
var result = Data
.GroupBy(d => d.GroupId)
.Select(d =>
new
{
GroupId = d.Key,
GroupName = d.FirstOrDefault().GroupName,
Count = d.Count()
});
Related
My postgres DB has a Price table where I store price data for a bunch of products. For each Price object I store when it was created (Price.timestamp), and whenever there is a new price for the product, I create a new Price object, and for the old Price object I store when it ended (Price.end_time). Both times are datetime objects.
Now, I want to count how many Prices there are at over a time period. Easy I thought, so I did the query below:
trunc_date = db.func.date_trunc('day', Price.timestamp)
query = db.session.query(trunc_date, db.func.count(Price.id))
query = query.order_by(trunc_date.desc())
query = query.group_by(trunc_date)
prices_count = query.all()
Which is great, but only counts how many prices were new/created for each day. So what I thought I could do, was to filter so that I would get prices where the trunc_date is between the beginning and the end for the Price, like below:
query = query.filter(Price.timestamp < trunc_date < Price.time_ended)
But apparently you are not allowed to use trunc_date this way. Can anyone help me with how I am supposed to write my query?
Data example:
Price.id Price.timestamp Price.time_ended
1 2022-18-09 2022-26-09
2 2022-13-09 2022-20-09
The query result i would like to get is:
2022-27-09; 0
2022-26-09; 1
2022-25-09; 1
...
2022-20-09; 2
2022-19-09; 2
2022-18-09; 2
2022-17-09; 1
...
2022-12-09; 0
Have you tried separating the conditions inside the filter?
query = db.session.\
query(trunc_date, db.func.count(Price.id)).\
filter(
(Price.timestamp < trunc_date),
(trunc_date < Price.time_ended)
).\
group_by(trunc_date).\
order_by(trunc_date.desc()).\
all()
you can use
trunc_date.between(Price.timestamp, Price.time_ended)
I figured it out.
First I created a date range by using a subquery.
todays_date = datetime.today() - timedelta(days = 1)
numdays = 360
min_date = todays_date - timedelta(days = numdays)
date_series = db.func.generate_series(min_date , todays_date, timedelta(days=1))
trunc_date = db.func.date_trunc('days', date_series)
subquery = db.session.query(trunc_date.label('day')).subquery()
Then I used the subquery as input in my original query, and I was finally able to filter on the dates from the subquery.
query = db.session.query(subquery.c.day, db.func.count(Price.id))
query = query.order_by(subquery.c.day.desc())
query = query.group_by(subquery.c.day)
query = query.filter(Price.timestamp < subquery.c.day)
query = query.filter(Price.time_ended > subquery.c.day)
Now, query.all() will give you a nice list that counts the prices for each day specified in the date_series.
I'm looking to union two values from different tables into one result table. something like this. Thanks
if [LossRsvAmt] > 0 OR [ClaimStatus] = 'Subrogation' then [ClaimStatus]
union
if [Subrogation inventory] = 1 then 'Subrogation Inventory'
end
I'd like to concatenate two dataframes A, B to a new one without duplicate rows (if rows in B already exist in A, don't add):
Dataframe A:
A B
0 1 2
1 3 1
Dataframe B:
A B
0 5 6
1 3 1
I wish to merge them such that the final DataFrame is of the following shape:
Final Dataframe:
A B
0 1 2
1 3 1
2 5 6
How can I do this?
pyspark.sql.DataFrame.union and pyspark.sql.DataFrame.unionAll seem to yield the same result with duplicates.
Instead, you can get the desired output by using direct SQL:
dfA.createTempView('dataframea')
dfB.createTempView('dataframeb')
aunionb = spark.sql('select * from dataframea union select * from dataframeb')
Using SQL produces the expected/correct result.
In order to remove any duplicate rows, just use union() followed by a distinct().
Mentioned in the documentation
http://spark.apache.org/docs/2.1.0/api/python/pyspark.sql.html
"union(other)
Return a new DataFrame containing union of rows in this frame and another frame.
This is equivalent to UNION ALL in SQL. To do a SQL-style set union (that does deduplication of elements), use this function followed by a distinct."
You have just to drop duplicates after union.
df = dfA.union(dfB).dropDuplicates()
I have a table with one to many to many relation ie
ManyToMany table:
MenuGroup
menuid groupid
1 4
1 5
Menu
menuid name
1 One
2 Two
Group
groupid name
4 group4
5 group5
Groupuser
groupid userid
4 101
4 103
5 102
i would like to get all menus of the user 101
ie
Menuid groupid name
1 4 group4
But i am getting the wrong out put eventhough writing the correct join queries. Can anyone help what am i doing wrong here?
Menuid groupid name
1 4 group4
1 5 group5
(from m in context.Menus
join mg in context.MenuGroup on m.MenuId equals mg.MenuId
join gu in context.Groupuser on mg.GroupId equals gu.GroupId
where gu.UserId == 101
select m);
i would like to include only this particular user's group details though this menu is in other group as well.
my expected output in json would be
{
"menuid": 1,
"name": "One",
"groups":[
{
"groupid":4,
"name":"group4"
}
]
}
Your linq query looks good, I suspect data issue, but would you like to try the following query to see what you get back. This following query requires navigation properties declared.
var userMenus = context.GroupUser.Where(u=>u.UserId = 101).SelectMany(g=>g.Group.Menus
.Select(m=> new {Menu=m.MenuId, GroupId=g.GroupId,GroupName=g.Group.name))
.ToList();
In case you want the complete Menu object
var userMenus = context.GroupUser.Where(u=>u.UserId = 101).SelectMany(g=>g.Group.Menus
.Select(m=> new {Menu=m.Menu, GroupId=g.GroupId,GroupName=g.Group.name))
.ToList();
In case you don't care about Group columns and just want Menu then
var userMenus = context.GroupUsers.Where(u => u.UserId == 101)
.SelectMany(g => g.Group.Menus.Select(m=>m.Menu));
I want to have a output count as 2 for the user_mail logged as test1#gmail.com for a query like this,
SELECT Count(user_refemai) from Table_users where userref_mail = user_mail
but, I'm getting the output as 0! What am I doing wrong?
My table_users looks like:
user_id user_mail user_refemail
1 test1#gmail.com NULL
2 test2#gmail.com test1#gmail.com
3 test3#gmail.com test1#gmail.com
you are getting 0 as in your query the condition is false all the time
for this purpose you have to use the cursor or inner queries and then get the count
or pass the parameter to the query for which you want to get the counts.
Shafqat is correct. To build on that, you'd pass in the query parameter like this:
SELECT count(*)
FROM
table_users
WHERE
userref_email = ?
If you need a reporting query instead of for a particular email address, you could use a self join.