Here is my query below
https://practice.com/services/data/v1.0/query/?query=SELECT name, "I.T. Owner Primary", "I.T. Owner Secondary", "Business Owner Primary", "Business Owner Secondary"
FROM view_device_custom_fields_flat_v2
INNER JOIN view_device_v2
ON view_device_custom_fields_flat_v2.device_fk = view_device_v2.device_pk WHERE "I.T. Owner Primary", "I.T. Owner Secondary", "Business Owner Primary", "Business Owner Secondary"<> '';
I need help because there are multiple devices on the view_devices_v2 that do not have I.T. Owner Primary", "I.T. Owner Secondary", "Business Owner Primary", "Business Owner Secondary" and are blank. I tried to do something like <> '' at the end but it does not work. This is a GET API call by the way. Any suggestions?
Related
As title suggests, I created a result with a formula but now need to break it up into having the formula run for every "owner" but seperate the formula into years. I have the dates 2010-2019. Here is the code
SELECT * FROM finaltab;
Select finaltab.OWNER as OWNER, SUM(finaltab.shares) AS SSUM FROM finaltab WHERE trancode='S' GROUP BY OWNER Order by year;
Select finaltab.OWNER as OWNER, SUM(finaltab.shares) AS PSUM FROM finaltab WHERE trancode='P' GROUP BY OWNER Order by year;
SELECT finaltab.OWNER, SSUM, PSUM FROM
(SELECT finaltab.OWNER AS OWNER, SUM(finaltab.shares) AS SSUM FROM finaltab WHERE trancode='S' GROUP BY OWNER) AS STable
INNER JOIN
(SELECT finaltab.OWNER AS OWNER, SUM(finaltab.shares) AS PSUM FROM finaltab WHERE trancode='P' GROUP BY OWNER) aS PTable
ON (Stable.owner = Ptable.owner);
SELECT STable.OWNER, SSUM, PSUM, (PSUM - SSUM) / (PSUM + SSUM) as "IOF" FROM
(SELECT finaltab.OWNER as OWNER, SUM(finaltab.shares) AS SSUM From finaltab WHERE trancode='S' GROUP BY OWNER) AS STable
INNER JOIN
(SELECT finaltab.OWNER as OWNER, SUM(finaltab.shares) AS PSUM From finaltab WHERE trancode='P' GROUP BY OWNER) AS PTable
ON (STable.OWNER = PTable.OWNER);
Here is the table. Also I would like some direction on if I want to take the "IOF" or formula and implement it into a formula with absolute value for the "IOF" in the numerator. Thank you [1]: https://i.stack.imgur.com/P9GtU.png
I have the following query
SELECT role_uuid FROM users WHERE email = 'email#domain.com'
I also have a roles table the following fields:
uuid
name
created_at
I'm hoping to have 1 query that gives lets me select the role by email and get the name and created_at field from the roles table.
I've tried things like this but I can't quite figure it out.
SELECT *
FROM ( SELECT * FROM users WHERE email = 'email#domain.com') AS A
JOIN ( SELECT * FROM roles WHERE uuid = A.role_uuid) AS B
WHERE A.role_uuid = B.uuid
You JOIN the two tables which gives you a table with all the fields from both source tables. Then you use WHERE to filter and SELECT to specify the fields that you want to be returned.
SELECT r.name, r.created_at
FROM users u JOIN roles r ON (u.role_uuid = r.uuid)
WHERE u.email = 'email#domain.com'
If you run into naming conflicts because of fields from both tables sharing the same name you can use AS to define fieldnames for the output columns:
SELECT r.name AS rolename, u.name AS username, r.created_at
FROM users u JOIN roles r ON (u.role_uuid = r.uuid)
WHERE u.email = 'email#domain.com'
I have 3 tables: Permissions, Roles, and RolePermissions. I would like to have a way to select Roles that are missing new rows in the Permissions table based on the RolePermissions table relationship to insert those values once new permissions are added.
I have had no luck finding how this can be done so that is why I'm asking here.
Table structure
Permissions | Roles | RolePermissions
------------------------------------------
Id | Id | Id
Name | Name | RoleId
| | PermissionId
Idea of sql but I know it's not correct:
-- Looking to be able to do something like
INSERT INTO RolePermissions (RoleId, PermissionId)
SELECT missingpermissions.PermissionId, missingpermissions.RoleId
FROM Permissions as p
INNER JOIN(
Select r.Id as RoleId, p.Id as PermissionId
FROM Role as r
LEFT JOIN RolePermissions as rp
ON r.Id = rp.RoleId
WHERE rp.PermissionId = p.Id
) as missingpermissions
ON p.id = missingpermissions.permissionid
Edited to format
You need to get your new permission and cross join all roles (to get all combinations of roles and new permissions).
INSERT INTO RolePermissions(RoleId, PermissionId)
SELECT r.ID AS RoleId,p.ID AS PermissionId
FROM Role r
CROSS JOIN (
--get all permissions currently not assigned to a role (presumably "new")
select p.*
from Permissions p
left join RolePermissions rp on p.id=rp.PermissionId
where rp.PermissionId is null
) p
I have a bunch of ~600k rows of let's say owner's names (varchar) and pet type (also varchar). For each owner's name I'd like an array with the most frequent pet they have (or pets if they have an equal amount of the same pet type).
An example:
*owner, pet type*
alice, cat
alice, dog
bob, fish
bob, cat
bob, fish
eve, cat
eve, dog
eve, cat
eve, dog
Expected output:
alice, [cat, dog]
bob, [fish]
eve, [cat, dog]
My feeling is that this is some combination of 'distinct on' in an inner query with array_agg on an outer query to do the array aggregation - but I just can't get it right.
You can do this by combining window functions and grouping:
select owner, array_agg(pet order by pet)
from (
select owner, pet, dense_rank() over (partition by owner order by count(*) desc) as rnk
from pet
group by owner, pet
) t
where rnk = 1
group by owner
order by owner;
Online example: http://rextester.com/MTFIQ24341
with data as (
select 'alice' as owner, 'cat' pet_type
union all select 'alice' as owner, 'dog' pet_type
union all select 'bob' as owner, 'fish' pet_type
union all select 'bob' as owner, 'cat' pet_type
union all select 'bob' as owner, 'fish' pet_type
union all select 'eve' as owner, 'cat' pet_type
union all select 'eve' as owner, 'dog' pet_type
union all select 'eve' as owner, 'cat' pet_type
union all select 'eve' as owner, 'dog' pet_type
) , getMaxPet as (select owner , pet_type
from data d1
group by owner,pet_type
having count(pet_type) = (select max(pet_count) from (select count(pet_type) as pet_count
from data d2
where
d1.owner = d2.owner
group by owner,pet_type ) a ) )
select owner , array_agg(pet_type)
from getMaxPet
group by owner
Try this, Main logic is to find all pets counts based on each user and then selects pet who is having max number.
I'm trying to create views that would accumulate all the needed data from joined sources:
CREATE OR REPLACE VIEW dir AS
SELECT
dir_data.id,
dir_data.parent_id,
dir_data.name,
(owner.*)::owner, -- owner_id
FROM
dir_data
LEFT JOIN owner ON owner.id = dir_data.owner_id
For example, this allows to select owner's data in easy way:
SELECT
id,
name,
(owner).id AS owner_id,
(owner).name AS owner_name,
((owner).company).name AS owner_company
FROM
dir
WHERE
id = 7
The problem is that I need to do a self-join with view dir (which is the vew being created) to convert parent_id field in similar way. PostgreSQL does not seem to like it, it says that relation "dir" does not exist.
Any hints?
Answer to Marcelo Cantos comment:
CREATE OR REPLACE VIEW dir AS
SELECT
...
FROM
dir_data
LEFT JOIN owner ON owner.id = dir_data.owner_id -- "standard" join
LEFT JOIN dir AS parent_dir ON parent_dir.id = dir_data.parent_id -- self-join, does not work
You can't create a recursive view, but in the latest postgres you can make recursive queries: http://www.postgresql.org/docs/8.4/static/queries-with.html
WITH RECURSIVE dir AS
(
SELECT dir_data.id,
dir_data.parent_id,
dir_data.name,
owner
FROM dir_data
LEFT JOIN
owner
ON owner.id = dir_data.owner_id
WHERE dir_data.id = 7
UNION ALL
SELECT dir_data.id,
dir_data.parent_id,
dir_data.name,
owner
FROM dir
JOIN dir_data
ON dir_data.id = dir.parent_id
LEFT JOIN
owner
ON owner.id = dir_data.owner_id
)
SELECT *
FROM dir