I have a set of records showing the status of implementation of software components. Each row has a feature area, a feature within that area, the product where that feature appears, and its implementation status (green, red, or yellow), like this:
Feature_Area
Feature
Product
Status
User experience
Sign Up
App1
Green
User experience
Sign Up
App2
Red
User experience
Log off
App1
Green
User experience
Log off
App2
Red
Back End
Update User
App3
Green
Back End
Delete User
App3
Red
I'd like to pivot this as shown here:
Specifically, I'd like:
A single row for each feature and columns for each app to show the status of that feature
Don't repeat the same feature area value over and over in the first column
I tried using the below, which got me a column for each app. I couldn't figure out how to group properly with this to get the output I was looking for. I'd welcome any ideas.
case when Product = 'App1' then title end as App1,
case when Product = 'App2' then title end as App2,
case when Product = 'App3' then title end as App3
Based on provided description I suggest to group by feature_area and feature and select arbitrary value in group (or maybe array_agg if you anticipate different ones) (note, I used Presto/Trino specific if for concise syntax instead of case):
-- sample data
with dataset(Feature_Area, Feature, Product, Status) as (
values ('User experience', 'Sign Up', 'App1', 'Green'),
('User experience', 'Sign Up', 'App2', 'Red'),
('User experience', 'Log off', 'App1', 'Green'),
('User experience', 'Log off', 'App2', 'Red'),
('Back End', 'Update User', 'App3', 'Green'),
('Back End', 'Delete User', 'App3', 'Red')
)
-- query
select Feature_Area,
Feature,
arbitrary(if(Product = 'App1', Status)) App1,
arbitrary(if(Product = 'App2', Status)) App2,
arbitrary(if(Product = 'App3', Status)) App3
from dataset
group by Feature_Area, Feature
order by Feature_Area desc, Feature desc;
Output:
Feature_Area
Feature
App1
App2
App3
User experience
Sign Up
Green
Red
NULL
User experience
Log off
Green
Red
NULL
Back End
Update User
NULL
NULL
Green
Back End
Delete User
NULL
NULL
Red
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?
I have association as following
A user has many subscriptions (only one subscription can have its status as active)
A subscription belongs to a user
I'm looking for an active record query that would help me select users whose all subscriptions are cancelled or in other words who doesn't have any active subscription.
Raw Sql queries are also welcome for this question.
We can do it in two ways
1st one:
User.where.not(id: Subscription.where(status: 'active').pluck(:user_id))
2nd one:
active_subscription_user_ids = Subscription.where(status: 'active').pluck(:user_id)
User.left_outer_joins(:subscriptions).where.not(id: active_subscription_user_ids)
You can do this with this query
User.where.not(id: Subscription.select(:user_id).where(status: 'Active'))
Please try below query
User.joins(:subscriptions).where('subscriptions.status = "active"').group('subscriptions.id').having("count(subscriptions.id) < 1")
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.
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']]],{})