I'm unsure into how to ask this so i will try to illustrate with an example.
What i am trying to achieve is an application that will be able to discern different levels of privileges. and i can't figure out how my database and queries should be made.
there are this levels of user:
Director
Manager
Executive
So a column in my user table will be a privilege INT to know what type of user, and another column will be Parent user, since x amount of Executives will be assigned to a manager, and x amount of managers will be assigned to a Director.
Example table USERS:
ID username privilege parent
1 Director1 1 null
2 Director2 1 null
3 Manager1 2 1
4 Manager2 2 1
5 Manager3 2 2
6 Executive1 3 3
7 Executive2 3 3
8 Executive3 3 4
9 Executive4 3 4
10 Executive5 3 5
11 Executive6 3 5
And they will have their "costumers".
Example table COSTUMERS
ID name seller
1 c1 11
2 c2 10
3 c3 10
4 c4 9
5 c5 8
6 c6 7
7 c7 6
8 c8 5
9 c9 4
10 c10 3
11 c11 2
12 c12 1
so if an Executive went into the app it would only be able to see their own costumers.
but if a manager went in he would be able to see his costumers and also from the executives assigned to him. and the same happens to the directors.
Is this the right way to make it? if so what would be the best approach to an effective query?
example diagram( you can see the organization diagram and each of the costmer every user has.
for example if users was to check his costumers he should see:
Director1: C12,C10,C9,C7,C6,C5,C4
Director2: C11,C8,C3,C2,C1
Manager1: C10,C7,C6
Manager2: C9,C5,C4
Manager3: C8,C3,C2,C1
and the executives only would see their own costumers.
Related
Given the table below of categories, would there be a way where I return the names alphabetically, but include the ones with a parent_id under its parent?
ID
parent_id
name
1
NULL
Hardware
2
NULL
Software
3
NULL
Networking
4
1
Desktop
5
1
Printer
6
1
Laptop
7
2
Office
8
2
Windows
9
2
Other
10
3
Outage
11
3
Firewall
12
NULL
Accounts
13
12
New Account
14
12
Password Reset
This query is used with PHP, so I could always organized them through php code but it got me thinking if there was a way to do this in the query directly. I'm drawing a blank on how to approach this. This particular example only goes 1 level deep with children, so a solution that handles that I would be fine with at the moment. But is there an approach that could allow multiple levels of children as well?
For example, if I just pulled only parent categories (rows with a null parent_id) and ordered by name I'd have Accounts, Hardware, Software, Networking. But I'd like to have:
ID
parent_id
name
12
NULL
Accounts
13
12
New Account
14
12
Password Reset
1
NULL
Hardware
4
1
Desktop
6
1
Laptop
5
1
Printer
3
NULL
Networking
11
3
Firewall
10
3
Outage
2
NULL
Software
7
2
Office
9
2
Other
8
2
Windows
I finally came up with a solution, much simpler than I was expecting. This will work for my example, but will not take into account multiple levels deep.
SELECT p.id, p.parent_id, p.cat_name, COALESCE(c.cat_name, p.cat_name) as g
FROM category p
LEFT JOIN category c
ON p.parent_id = c.id
ORDER BY g, parent_id, cat_name
To account for children who's parent may be a child itself, I do not yet have a solution but feel like that answer lies in using WITH RECURSIVE.
I have a table which lists all the employees in an org with their immediate parent.
id
name
parent
type
1
A
0
1
2
B
0
1
3
C
1
2
4
D
2
2
5
E
3
3
6
F
4
3
7
E
5
4
8
F
6
4
I have another table which lists actions by last node employees:
id
action
type
emp_id
1
Action Name
0
7
2
Action Name
0
8
3
Action Name
1
7
4
Action Name
2
7
5
Action Name
3
8
6
Action Name
4
8
7
Action Name
5
7
8
Action Name
6
8
I need to show a hierarchical view of the action count. As evident above, all the actions are created by either employee with an id of 7 or 8. I need to show the number of actions by employees with type 1 based on their own actions or by any sub down the chain.
At the moment, I pull the counts on the end node using sql and then use the scripting language to build the hierarchy bottom up.
Is there a better way to achive this with SQL?
I'm in a situation here. I want to normalize my table:
Exam_Papers
ID Country_Code Level
1 UK 1
2 UK 2
3 UK 3
4 UK 4
5 UK 5
6 UK 6
7 UK 7
8 SA 1
9 SA 2
10 SA 3
11 SA 4
12 SA 5
13 SA 6
14 SA 7
15 IN 1
16 IN 2
I understand that I could normalize this by putting Levels in a separate table, but then Country_Code would still contain duplicated data, so how normalized should this table be?
Such normalized that Country_Code and Level have their own table?
Also, in this example how is normalization beneficial because either way, making 2 separate tables would mean the FK's would still be duplicated (for example if UK had the ID of 1, my table would contain 7 1's)
Thanks in advance
You don't need to make two tables in this case. Because as you mentioned you will still require the entry (may be in their id format). Also, I feel if you do it the other way, your performance would degrade in terms of SQL queries.
I'm a bit confused when trying to create a specific query with the following data tables:
**table 1 - referral_data:**
ID attribution_name
------------------------
1 Category
2 Brand
3 Size
4 Color
5 Processor
6 OS
7 Screen Size
.....
**table 2 - referral_values:**
ID ref_data_id attribution_value
---------------------------------------------
1 1 Cell Phones
2 1 Tablets
3 1 Laptops
4 1 Computers
5 1 LCD Monitors
6 2 Nokia
7 2 Motorola
8 2 Samsung
9 2 Lenovo
10 2 Philips
11 3 10x10x11
12 3 100x100x20
13 3 10x200x200
14 3 2x2x3
15 4 Black
16 4 Cyan
17 4 Magenta
18 4 White
19 4 Blue
20 5 ARM Cortex A11
21 5 Snapdragon 11
22 5 Intel I3 XXXXX
23 5 Exynos XXXX
24 6 Android 4.1
25 6 Android 3.0
26 6 Windows Phone 3
27 6 Windows 8 Professional
28 7 18.5"
29 7 11.8"
30 7 7.0"
31 7 5.0"
32 7 3.5"
......
**table 3 - product_specs:**
ID product_id referral_data_id referral_value_id
--------------------------------------------------------
1 1050 1 1 // <-- Product 1 - Category: Cell Phones
1 1050 2 8 // <-- Product 1 - Brand: Samsung
1 1050 4 19 // <-- Product 1 - Color: Blue
1 1050 6 24 // <-- Product 1 - Processor: Exynos XXXX
1 1050 7 30 // <-- Product 1 - Screen Size: 7.0"
1 1068 1 4 // <-- Product 2 - Category: Computers
1 1068 2 9 // <-- Product 2 - Brand: Samsung
1 1068 6 22 // <-- Product 2 - Processor: Intel Core I3
1 1068 7 28 // <-- Product 2 - Screen Size: 18.5"
......
These tables consists in a "Product Catalog" that I'm planning to use in a e-commerce website.
This is intended to optimize "client side" search functions and organize internal product data information, turning the "content-administrators" tasks in a simplest and easiest environment as possible. (Letting them, for example, choosing an "already-entered" data values instead of re-entering an "already entered data", avoiding duplicated data or typo errors).
The "content administrators" will have options, according to that "table dynamics", to insert new attribution data (product characteristics) and or new attribution values to them (attribution values).
Info: the product code field named "product_id", is outside the tables relation, this is just used to create a link to attach informations to products that they belong to.
In general SQL Joins to get data over the tables, I'm Ok. But there some kind of informations that I need to get / manage, I'm getting nuts. I've spent A LOT of hours and I just have found a headache.
My question is about how to build, in a single query, to get commonly used referral data, based on a CATEGORY.
(when the contents admin choose, for a example, "Cell Phones" in "Category" field, they will get a commonly used "data table information" about that Category, like Brand, Color, Screen Size, etc .... to just choose their category attribution ) and to create a similar query to highlight or order by the commonly used attribution values ( i.e. commonly used screen sizes ).
In a single query?
SELECT ps2.product_id,rv2.attribution_value,rd.attribution_name
FROM ((
(select ps.* from product_specs ps JOIN referral_values rv
ON rv.ref_data_id=ps.referral_value_id
WHERE rv.attribution_value = 'Cell Phones'
) ps1
JOIN product_specs ps2 on ps1.product_id=ps2.product_id)
JOIN referral_values rv2 ON ps2.referral_value_id = rv2.id)
JOIN referral_data rd ON rd.id = rv2.ref_data_id;
The inner select is used to get the right product_id based on criteria 'Cell Phones'. The others are used to populate this value with all details. To do this the first JOIN is a self-JOIN of product_specs to get all data, the following two joins are used to get the string values of them.
By the way: The column product_specs.referral_data_id is redundant and can/should be removed
I've got an issue where I've been presented data in this format from SQL, and have directly imported that into SSRS 2008.
I do have access to the stored procedure for this report, however I don't want to change it as a few other reports rely on it.
Project HoursSpent Cost
1 5 45
1 8 10
1 7 25
1 5 25
2 1 15
2 3 10
2 5 15
2 6 10
3 6 10
3 4 5
3 4 10
3 2 5
I've been struggling all morning to understand how/when I should be implementing the SUM() function with this.
I have tried already to SUM() the rows, but it still outputs the above result.
Should I be adding any extra groups?
Ideally, I need to have the following output:
Project HoursSpent Cost
1 25 105
2 15 40
3 16 30
EDIT: Here is my current structure:
"LineName" is a group for each project
You have to add a row group on "Project" since you want to sum up the data per each project.