Fetch a value from another table - dynamics-crm

I am currently learning KQL and have only a beginner level of knowledge with SQL.
I have a table called Dynamics365Activities which has the following columns:
UserID (username of the person performing an action)
SystemUserID (Object ID of the username performing an action)
Operation (Type of operation performed by the user)
EntityID (SystemUserID [or object id] of the person on which the operation was performed)
I would like to get a username of the person on which the operation was performed, rather than the object ID. Below is the KQL I have constructed:
Dynamics365Activity
| project TimeGenerated, UserId, SystemUserId, Message, EntityId
| join (
Dynamics365Activity
| project UserId, EntityId, SystemUserId
) on $left.EntityId == $right.SystemUserId
| project TimeGenerated, UserId, Message, UserId1
| order by TimeGenerated desc
This KQL is returning redundant results. Any help would be highly appreciated

Related

Find the maximum of hits within a session in BigQuery (legacy SQL)

In BigQuery using data from GA, I am trying to find the pagetype (based on pagepath) with the maximum number of hits within a session for each session for a user. This will be used to determine which pagetype had the most activity for a session (I want only one, hence max).
Using the row number to assign rank for each pagetype within a session and filtering for rank 1 works for one user. When I try to replicate that for the bigger dataset (~400GB), I get the 'Resources exceeded....' error.
I'm new to BigQuery and would appreciate any tips to optimize this code.
SELECT
userid,
sessionid,
pagetype,
hits
FROM (
SELECT
*,
ROW_NUMBER() OVER (PARTITION BY userid, sessionid ORDER BY sessionid ASC) rnk
FROM (
SELECT
userid,
sessionid,
pagetype,
COUNT(1) AS hits
FROM
[xxxxxxx] WHERE
GROUP BY
userid,
sessionid,
pagetype
ORDER BY
sessionid,
hits DESC ) )
WHERE
rnk = 1
Using standard SQL, you can write a query such as:
#standardSQL
SELECT
first_session.*
FROM (
SELECT
ARRAY_AGG(
STRUCT(userid, sessionid, pagetype, hits)
ORDER BY sessionid ASC LIMIT 1
)[OFFSET(0)] AS first_session
FROM (
SELECT
userid,
sessionid,
pagetype,
COUNT(*) AS hits
FROM `xxxxxxx`
GROUP BY
userid,
sessionid,
pagetype
)
GROUP BY userid, sessionid
);
This builds a struct with the relevant columns for each group and selects only the first one, as determined by sessionid.

Laravel how to get values from a table with an id from another table

i am having some trouble getting results from a query. it's the following. I have two tables, tasks and task_interactions. the table tasks stores the tasks, and the table task_interactions stores the history of the tasks, like a change of status, comments... The column task_id in the task_interactions table is the id in the table tasks.
The table tasks has the following columns: id | project_id | task_type_id | related_block | created_by | assigned_to | start_date | due_date | task_priority | comments | status | created_at |updated_at The status in this table will be deleted.
The table taskinteractions has the following columns:
id,task_id,status,comments,created_at
In the models i have created the following relationships:
Task Model:
public function taskinteractions()
{
return $this->hasMany('App\Taskinteraction');
}
Taskinteraction Model:
public function tasks()
{
return $this->belongsTo('App\Task');
}
And now the question:
I want to get all the tasks assigned to the authenticated user with status (in the table taskinteractions) equal to "In Progress".
I have tried a lot of different approaches with no good results.
I am a newbie and so i believe that the solution might be simple, but i no idea how to do it.
Assuming assigned_to has the id of the user and a typical user table, try the following,$tasks is the array of tasks you want to get
$interactions = TaskInteractions::where('status','In Progress')->get();
$tasks = array();
foreach($interactions as $interaction){
$task = $interaction->task;
if((!(in_array($task, $tasks))) && (Auth::user()->id == $task->assigned_to) ){
array_push($tasks,$task);
}

Hive query to find the ip address and country which is not present

I have a Hive table named 'Login'. It contains the following columns :-
UserID | UserName | UserIP | UserCountry | Date
On a particular day (all that logins of that day), I want to find out the UserIDs, which has been accessed from a country (UserCountry) from where the user has never accessed his account from OR the IPs (UserIP) from which the account has never been accessed before.
I would start with an except where I remove prior countries and IPs
select userid, usercountry, userip
from table
where date=xx
except
select userid, usercountry, userip
from table
where date<xx
I think that the best way is the GROUP clause!
You say "has never been accessed before", means COUNT = 1.
To find the IP use only once :
select UserId, UserIP, COUNT(UserIP) FROM Login WHERE Date = yourdate GROUP BY UserIP, UserId HAVING COUNT(UserIP) = 1
To find the country use only once :
select UserId, UserCountry, COUNT(UserCountry) FROM Login WHERE Date = yourdate GROUP BY UserCountry, UserId HAVING COUNT(UserCountry) = 1
Left Outer Join will be able to satisfy your requirement in HIVE.
select t1.userid, t1.usercountry, t1.userip
from table t1
LEFT OUTER JOIN
from table t2
ON (t1.userid=t2.userid)
WHERE t1.date=xx and
t2.data < xx and
(t2.usercountry IS NULL or
t2.userip IS NULL);
Hope this helps...

System.Linq.Dynamic to select a collection inside of a collection from Entity Framework IQueryable

I have an EF model with the relationship User->Roles. It's a one to many relationship in our database. I would like to select the ID from role's record and the name from the user's records.
What I have come up with works for one-to-one relationships. I'm not sure how to generate the a list inside of dynamic linq. Maybe a SelectMany?
User table: Users
Join Table: User_Role
Role Table: Roles
//does not select inside a collection of USER_ROLES
q= query.Select(" new ( ID, (new (USER_ROLES.ID as ID) as USER)")
//not valid
//q = query.Select(" new ( ID, (new List<Object> (USER_ROLES.ID as ID) as USER)") something I figure this would give me a list of User_Role's ID
//not valid
//q = query.Select(" new ( ID, (new List<Object> (USER_ROLES.ROLE.ID as ID) as USER)") something I figure this would give me a list of Role's ID
Update I'm getting closer. I used a selectmany, but the results are duplicated
q = query.SelectMany("USER_ROLES","new (inner as myUSER,outer as myROLE) ").SelectD("new (myROLE.ID as ROLE_ID, new( myROLE.NAME, myUSER.USER.FIRSTNAME,myUSER.USER.ID)as user)")
The results look like this:
Role->User_Role A-> User A
User_Role A-> User B ..notice the repeat of "User_Role A"
User_Role A-> User C
it should be
Role->User Role a -> User A
+ User B
+ User C

Linq query accross joined tables in EF

hello, can you help me how to write Linq query (using Entity Framework) for the following requirements:
I have tables:
User(ID, Name),
Checklist(ID, Name, UserID(NOT NULL))
Job(ID, Name, UserID(NOT NULL))
Task(ID, Name, JobID(NULLABLE), UserID(NULLABLE), ChecklistID(NULLABLE)),
In the Task table there is always only one of foreign keys filled (others are NULL):
Task data example:
ID Name JobID UserID ChecklistID
1 T1 1 NULL NULL
2 T2 NULL 1 NULL
3 T3 NULL NULL 1
...
I want to select all tasks for the user based on the following rules:
1. all tasks belonging to user (UserID is filled)
2. all tasks which are related to job (JobID is filled) and Job is owned by the user
3. all tasks which are related to Checklist (ChecklistID is filled) and Checklist is owned by the user
4. ID of the user is input parameter
I want to get result in one Linq query. I'm beginner in Linq and I have no idea how to do it correctly.
thanks a lot for your help.
This appears to be what you're trying to get. You may have to add in "DataContext." before the table names.
var stuff = (from t in ScheduledTasks
where t.UserID == userID
|| t.CheckLists.UserID == userID
|| t.Jobs.UserID == userID
select t);

Resources