With this formula, it shows the number of log times perfectly, since day 1:
SELECT username, COUNT(time) AS session_count
FROM login_history
GROUP BY username;
But with this one, which is my (almost) full formula to gather a lot of different data, it doesn't give me the same results regarding the logs count:
SELECT
user.username AS 'Contact ID',
user.firstname AS 'First name',
user.lastname AS 'Last name',
user.email AS Email,
user.orgname AS 'Company ID',
expiry_date AS 'Expiry date',
COUNT(time) AS 'Login activity since day 1',
MAX(time) AS 'Last login date',
login_history.browser_family AS 'Web browser',
login_history.browser_version AS 'Web browser version',
login_history.os_family AS 'Device OS',
login_history.os_version AS 'OS version',
login_history.device_family AS 'Device family',
login_history.device_brand AS 'Device brand',
login_history.device_model AS 'Device model',
login_history.ip_address AS 'IP address',
RIGHT(user.email, length(user.email)-INSTR(user.email, '#')) AS 'Company domain'
FROM
`user`
INNER JOIN
`login_history` ON login_history.username = user.username
WHERE
orgname != 'XXX'
AND is_active = 1
GROUP BY
user.username, user.firstname, user.lastname, user.email,
user.orgname, login_history.browser_family,
login_history.browser_version, login_history.os_family,
login_history.os_version, login_history.device_family,
login_history.device_brand, login_history.device_model,
login_history.ip_address
ORDER BY
orgname ASC;
I'm also trying to complete this formula to gather session logs count from the past 30 days and also from the past 3 days...
Thanks a lot for your help! :D
Without data is not easy to try to guide you, but I would do as follows. Let's call "user_stuff" the whole user.username, user.firstname, user.lastname, user.email, user.orgname and login_stuff the whole login_history.browser_family, login_history.browser_version, login_history.os_family, login_history.os_version, login_history.device_family, login_history.device_brand, login_history.device_model, login_history.ip_address.
Does the following work as intended?
SELECT "login_stuff", COUNT(time) AS session_count
FROM login_history
GROUP BY "login_stuff";
If so, does the following work as intended?
SELECT "login_stuff", COUNT(time) AS session_count
FROM login_history
WHERE orgname != 'XXX' AND is_active = 1
GROUP BY "login_stuff";
If so, does the full query work as intended? Knowing where you introduced the "bug" (maybe just assumptions about the data) is of vital importance to try to understand how to fix this problem.
Related
By checking the Bigquery Export Schema, the first thought is:
SELECT COUNT(*)
FROM bigquery.query
WHERE hits.hitNumber = 1
AND hits.contentGroup.contentGroup1 IN ('product list page', 'product detail page')
Should there be any other rule for it or is this one enough?
Good day!
I am having a problem trying a query in my laravel 8 project. In my database, I have 2 tables: users and roles. In users table it has an roles column which is the data inserting in that column is the roles table id.
Or can any one help me to convert this sql command into laravel.
----SQL----
SELECT roles.id, roles.role_name,
COUNT(users.roles) AS users,
roles.permission,
case when roles.`status` = 1 then 'Active'
ELSE 'Inactive'
END AS `status`
FROM roles
INNER JOIN users ON roles.id = users.roles
GROUP BY roles.id
--My laravel code that is not working--
$data = Roles::select(
"roles.id",
"roles.role_name",
DB::raw("count(users.roles) as users, roles.permission, (CASE WHEN roles.status = 1 THEN 'Active' ELSE 'Inactive' END) as status")
)
->join("users", "users.roles","=","roles.id")
->groupBy("roles.id")
->get();
Somebody help me. Thanks
DB::table("roles")
->innerJoin("users", function($join){
$join->on("roles.id", "=", "users.roles");
})
->select("roles.id", "roles.role_name", "count (users.roles) as users", "roles.permission")
->addSelect(DB::raw("case when roles.`status` = 1 then 'active' else 'inactive' end as `status`"))
->groupBy("roles")
->get();
Change the model name i.e Role as per whatever your model name is ex: Roles
$data = Role::select([
"roles.id",
"roles.name",
DB::raw("count(users.roles) as users, roles.permission, (CASE WHEN roles.status = 1 THEN 'Active' ELSE 'Inactive' END) as status")
])
->innerJoin("users", function($join){
$join->on("roles.id", "=", "users.roles");
})
->groupBy("roles.id")
->get();
I already know what is cause. First, I can't identify the error because I am using dataTable so the error is from dataTable. Then I tried to fetch the result of my query using view and the error says SQLSTATE[42000]: Syntax error or access violation: 1055
This the solution I deed. Kindly see below:
In config\database.php
Find mysql array and change the 'strict' => true to 'strict' => false
Thank you for help guys.
Sorry if the question is confusing, because I'm not sure on how to ask the question, but here I go:
I'm inside the rails console trying to find all users with orders where all orders has the state "errored", and ONLY errored. A user has many orders, and the order state on each of them can differ from "completed", "refunded" or "errored".
User.includes(:orders).where(orders: { state: "errored" })
This is returning all users who has one or more errored orders as it's suppose to, whether or not the user have orders with other states as well. But I'm trying to fetch the users who ONLY has errored orders.
I've tried a lot of things, from iterating through every order in every user, to trying to manually find them. But it got to be a better way.
My SQL isn't what it used to be but I believe that for a pure SQL solution it would need to look something like:
SELECT "users".*
FROM "users"
LEFT JOIN orders on orders.user_id = users.id
LEFT JOIN orders non_errored_orders on non_errored_orders.user_id = users.id and non_errored_orders.state <> 'errored'
WHERE "non_errored_orders"."id" IS NULL AND "orders"."id" IS NOT NULL
So, we left join the orders table with the alias non_errored_orders and that will make it so that if there is an order where the user id matches and the state is not equal to errored, a row will appear (and non_errored_orders.id will end up as NOT NULL). In the where clause we then filter down to only users whose non_errored_orders.id IS NULL, filtering out all users who matched an order that was not errored.
We then left join the orders table again with no alias only matching on users.id = orders.user_id. If there is no orders.id, that means the user does not have any orders in the table at all, so we want to filter down to only users where orders.user_id IS NOT NULL, meaning they had an order.
You can do a query like this in rails by doing something like:
User.
joins(:orders).
joins("LEFT JOIN orders non_errored_orders on non_errored_orders.user_id = users.id and non_errored_orders.state <> 'errored'").
where(non_errored_orders: { id: nil }).
where.not(orders: { id: nil }).distinct
# User Load (0.3ms) SELECT DISTINCT "users".* FROM "users" INNER JOIN "orders" ON "orders"."user_id" = "users"."id" LEFT JOIN orders non_errored_orders on non_errored_orders.user_id = users.id and non_errored_orders.state <> 'errored' WHERE "non_errored_orders"."id" IS NULL AND ("orders"."id" IS NOT NULL) LIMIT ? [["LIMIT", 11]]
# => #<ActiveRecord::Relation [#<User id: 1, ...>]>
In my very limited test set this seems to be working.
test data was
User.find(1).orders.create([ { state: 'errored' }, { state: 'errored' } ])
User.find(2).orders.create([ { state: 'errored' }, { state: 'completed' }])
User.find(3).orders.create([ { state: 'refunded' } ])
How about little long but simplified path?
users_with_other_order_states = User.join(:orders).where.not(orders: { state: "errored" }).pluck(:id)
User.joins(:orders).where('orders.state = "errored" AND users.id NOT IN (?)', users_with_other_order_states)
I think you can try define an errored_orders relation:
class User < ApplicationRecord
has_many errored_orders, ->{where state: 'errored'}
end
User.includes(:errored_orders).where(...)
Hope it helps!
I have a scenario as shown below ,
I want to query the database so I get the following result,
User Resource Permissions
Edi Plan A [view]
Where
resource.name = 'Plan A' and user.name = 'Edi'
my query for above is
SELECT name,
out('hasARole').out('ofType').in('isOfType')[name = 'Plan A'].name,
set(out('hasARole').out('hasA').name) as permission
FROM user
WHERE name = 'Edi'
It should display
User Resource Permissions
Adrian Plan A [view,edit, delete]
if I change it to,
Where
resource.name = 'Plan A' and user.name = 'Adrian'
my query for above is
SELECT name,
out('hasARole').out('ofType').in('isOfType')[name = 'Plan A'].name,
set(out('hasARole').out('hasA').name) as permission
FROM user
WHERE name = 'Adrian'
Now above queries work as long as the users don't have another role on another type of resource. e.g. if Edi had Admin role on let's say a resource type of Workspace then the query gives me back all the permissions that an Admin would have , instead of just view as he only has view permission on Plan A
I have used the following graph for my answer. Note that I have corrected some incositencies with your original edges.
I see a number of possible queries for this problem. I am a bit confused why you would want to return the User and Resource in the query, as you probably already have these records due to the fact you use them to create the query. You can't 'nest' the full records in the results either (unless you JSON them). Further to this, querying on the name field, and returning only the name field seem a little nonsensical to me - but maybe you have done so to simplify the question. Regardless, the following queries will get you on your way to your desired results.
My first idea is to run a query to get all of the Roles related to a Resource. We then run a query over these results to filter for the Roles that include the User. This looks like the following;
select from (
select expand(out('isOfType').in('ofType')) from Resource where name = "Plan A"
) where in('hasARole') contains first((select from User where name = "Edi"))
This query correctly returns just the Viewer record for both Edi and Adrian.
My second idea is to run 1 query for the Roles related to a Resource (similar to above), and another for the Roles related to a User, and then find the intersect. This looks like the following, and gives the same results as the query above;
select expand(intersect) from (
select intersect($resource_roles, $user_roles)
let
$resource_roles = (select expand(out('isOfType').in('ofType')) from Resource where name = "Plan A"),
$user_roles = (select expand(out('hasARole')) from User where name = "Edi")
)
Now if you really do want the User, Resource and Permissions all in the 1 result, you can use the following, or a variant of;
select first($user).name as User, first($resource).name as Resource, intersect(resource_roles, user_roles).name as Permissions from (
select $resource.out('isOfType').in('ofType') as resource_roles, $user.out('hasARole') as user_roles
let
$resource = (select from Resource where name = "Plan A"),
$user = (select from User where name = "Edi")
)
or
select first($user).name as User, first($resource).name as Resource, intersect($resource_roles, $user_roles).name as Permissions
let
$resource = (select from Resource where name = "Plan A"),
$resource_roles = (select expand(out('isOfType').in('ofType')) from $parent.$resource),
$user = (select from User where name = "Edi"),
$user_roles = (select expand(out('hasARole')) from $parent.$user)
I am looking for a way to check if a logged-in user is Manager for a particular Module like 'Point of sale'. Using SQL I am able to check this condition, but can we accomplish the same using ORM?
Following is my sql query which is listing the users who are Managers for 'Point of sale' module: Looking for an equivalent of the same in ORM OR some other way to accomplish the desired thing:
select login, usr.id as user_id, grp.id group_id, grp.name, cat.name
from res_users usr, res_groups_users_rel rel, res_groups grp, ir_module_category cat
where usr.id = rel.uid
and rel.gid = grp.id
and grp.category_id = cat.id
and cat.name = 'Point of Sale'
and grp.name = 'Manager';
And finally it's done : )
Following is my working code in python:
models.execute_kw(db, uid, password,
'res.users', 'search',[[['id','=',userid],['groups_id.name','=','Manager'],['groups_id.category_id.name','=','Point of Sale']]],{})