Hierarchical Table and Query in SQL Server - sql

I have a scenario as follows:
ManufacturerID Name
1 XXX
2 YYY
DeptID Name ManufacturerID
1 abc 1
2 bcd 1
3 efg 2
ProductID name deptid
1 dfdfg 1
2 dfdg 2
3 sdfsd 2
PartsID name productid
1 sdfs 1
2 sfdfs 2
3 sdd 1
I want the above table structure to be made as hierarchical using levels. How do I design the table?

I cannot understand what hierarchy you need to make, but if you're using sql server 2008 please take a look at hierachyId datatype.
And please provide more desdcription about you process. Now it looks straightforward:
Manufacturer has many departments, department has many products, product has many parts.

Related

Translate sql query on Django

I have two tables: students (that has all the students of a school) and suspensions (all the students that are suspended)
id
name
school_grade
1
Jeff
1
2
Dave
1
3
Susan
2
4
Will
2
5
Peter
3
id
reason
student_id
1
Missed class
1
2
Arrived 20 times late
2
3
Fight
5
So I need to get statistics of which students of different grades are suspended.
So, my query is this.
SELECT school_grade, count(school_grade)
FROM students JOIN suspensions ON students.id=suspensions.student_id
GROUP BY school_grade;
And this query gives me exactly what I want.
school_grade
number of suspension
3
1
1
2
But I don't understand how to make this query on django.
Try:
students.objects.values("school_grade").annotate(Count("suspensions"))
This should work as expected

Combining related organisation records in SQL where there is a Parent-Child relationship between organisations

I am trying to build a table of data for use in Yellowfin BI reporting. One limitation of this is that no temporary tables can be created and then dropped in the database. I am pulling the data from an existing database, which i have no control over. I can only use SQL to query the existing data.
There are two tables in the source database i need to work with. I've simplified them for clarity. The first contains organisations. It has an ORG_ID column which contains a unique ID for each organisation and a PARENT_ORG_ID column indicating which organisation is the Parent Company of others in the list:
ORG_ID PARENT_ORG_ID
1 Null
2 1
3 5
4 5
5 Null
6 1
Using the table above i can see that there are the following relationships between organisations:
ORG_ID RELATED_ORGANISATIONS
1 2 and 6
2 1 and 6
3 5 and 4
4 5 and 3
5 4 and 3
6 1 and 2
I'm not sure the best way to represent these connections in a query as i need to use these relationships with a second table.
The second table i have is a list of organisations and money owed:
ORG_ID MONEY_OWED
1 5
2 10
3 0
4 15
5 20
6 5
What i need to achieve is a table that i can search for any single ORG_ID, and see the combined data for that Organisation and all related Organisations. In the case of my example, this could be a results table something like this:
ORG_ID MONEY_OWED_BY_ALL_RELATED_ORGS
1 20
2 20
3 35
4 35
5 35
6 20
I'm thinking i should use a CTE to handle the relationships between organisations but i can't get my head around it.
Any help would be greatly appreciated!
For your particular example, you can use:
select o.*,
sum(mo.money_owed) over (partition by coalesce(o.parent_org_id, o.org_id)) as parent_owed
from organizations o left join
money_owed mo
on mo.org_id = o.org_id;
This works because your organizations are only one level deep -- which is consistent with your sample data.

How do I add rows in one table in specific conditions?

I work on an Oracle database.
I have a table (it is a join table) but this is how it looks:
CustomerID days_attached Startdate enddate team
1 7 01-01-2016 08-01-2016 A
1 2 09-01-2016 10-01-2016 B
1 8 01-02-2016 09-02-2016 A
2 1 01-02-2017 02-02-2016 C
2 8 08-05-2017 16-05-2017 C
I need to know how long a person is attached to a specific team. A person can be attached to a person for a X amount of days. That person could be in a team. For instance in this case, how long is a person attached to team A = 7+8 15 days.
How do I get this in a SQL statement?
Our app only supports SQL not PL/sql .
I expect an output like:
CustomerID days_attached team
1 15 A
1 2 B
2 9 C
select customer, team, sum(dayattached) from table_name group by customer, team
hopefully this will help u

Splitting the data through SSIS

I have a table "Employee" as shown below
Id Name
1 John
2 Jaffer
3 Syam
4 Aish
5 Gidson
1 Aboo
2 Sindhu
3 Saravanan
I want to get two outputs like
Id
1
2
3
Id
4
5
Which transformation should i use?
Could you Please help on that?
You will have to write two queries.
SELECT Id
FROM Employee
GROUP BY Id
HAVING COUNT(Id)>1
The above query will give you first output
SELECT Id
FROM Employee
GROUP BY Id
HAVING COUNT(Id)=1
This will give you 2nd output.

Help with SQL and table optimization

I have two things to do: optimize table, write a query.
customer
id customer year_in_business is_discountable customers_friend_id
1 a 2 true 2
2 b 3 false 1
3 c 2 true 1
4 d 3 true null
Here customer_friend_id is pointing back to pk(id) of the same table.
product
id product
1 xxx
2 yyy
3 aaaa
customer_product
customer_id product_id
1 2
2 3
3 1
Questions
How to make these tables flexible or extensible in the future?
Get Name of each customer that has a friend and the name of that customer’s friend.
Here is one suggestion: remove customer.customers_friend_id and create a new CustomerFriend table like so:
CustomerFriend
customerFriendID customerID friendID
1 1 2
2 2 1
3 3 1
This will allow customers with more than one friend, and avoids nulls for customers without friends.