How to sort data by hierarchy - sorting

Lets say I have some data like this
Name | ID | ParentID | Level
------------+-----+----------+-------
Fruits | 1 | 0 | 1
Vegetables | 2 | 0 | 1
Apple | 3 | 1 | 2
Banana!! | 4 | 1 | 2
Tomato | 5 | 2 | 2
Potato | 6 | 2 | 2
red | 7 | 5 | 3
green | 8 | 5 | 3
How to sort (compare) this data to get a result like this:
Name | ID | ParentID | Level
------------+-----+----------+---------
Fruits | 1 | 0 | 1
Apple | 3 | 1 | 2
Banana!! | 4 | 1 | 2
Vegetables | 2 | 0 | 1
Tomato | 5 | 2 | 2
red | 7 | 5 | 3
green | 8 | 5 | 3
Potato | 6 | 2 | 2
Background is that I have a model-collection with models and I want to add them according to the hierarchy given by ID/ParentID

Related

Laravel leftJoin returns null from 2nd table

I have 2 table duty_sheets
centerId | centerName | p1 | p2 | p3 | p4 | ...p22 | examiId
1 | xyz | 1 | 5 | 8 | 7 | 1 | 1
2 | abc | 9 | 1 | 6 | 6 | 1 | 1
and feedback
id | centerId | inspectorId | A | B | C | examiId
1 | 1 | 1 | 1 | 5 | 8 | 1
2 | 2 | 9 | 9 | 1 | 6 | 1
here is my code
$center = DutySheet::select('duty_sheets.centerId', 'duty_sheets.centerName','feedback.id')
->leftJoin('feedback', function ($leftJoin) {
$leftJoin->on('duty_sheets.examId', 'feedback.examId')
->where("duty_sheets.centerId", 'feedback.centerId')
->where("feedback.inspectorId", 1);
})
->where("duty_sheets.examId", 1)
->where("p20", 1)
->get();
dd($center);
to retrieve "All rows from DutySheet where p20 = 1 and dutysheet.examId = 1, and relevant rows from feedback depend on centerId, inspectorId and examId.
The problem is that the query return feedback.id as null while the record exist in feedback table with the ids.
Laravel version = 9
The problem is in left Join
->where("duty_sheets.centerId", 'feedback.centerId')
This build a where against the value 'feedback.centerId'
duty_sheets.centerId='feedback.centerId'
You need use
->on("duty_sheets.centerId",'=', 'feedback.centerId')
Or
->whereColumn("duty_sheets.centerId", 'feedback.centerId')

Combinate of values in a table to get the sum of each combination

I have a table with numeric data that i need make diferent combinations itself.
For example:
| A |
|---|
| 1 |
| 2 |
| 3 |
| 4 |
I need to combine this single column to get the next result:
| A | B | C | D |
| - | - | - | - |
| 1 | | | |
| 1 | 2 | | |
| 1 | 2 | 3 | |
| 1 | 2 | 3 | 4 |
| 1 | 2 | | 4 |
| 1 | | 3 | |
| 1 | | 3 | 4 |
| 1 | | | 4 |
| | 2 | | |
| | 2 | 3 | |
| | 2 | 3 | 4 |
| | 2 | | 4 |
| | | 3 | |
| | | 3 | 4 |
| | | | 4 |
At the end of the table, i have to create a column with the Count of every column that has data and another column that contains the sums of number of each columns.
Maybe it sound very difficult or impossible, but I haven't a way to make it work.
I have try to "Cross Join" from SQL but didn't got the expected result.
Help!
In this case, you can solve this by counting in binary ending with the digits being the number of numbers in the set. etc. the starting set 2568 would end with 1111. this binary number would decide if you show that number in each row. Heres a table of how it would work.
| A |
|---|
| 2 |
| 5 |
| 6 |
| 8 |
A
B
C
D
Binary
Row number
8
0001
1
6
0010
2
6
8
0011
3
5
0100
4
5
8
0101
5
5
6
0110
6
5
6
8
0111
7
2
1000
8
2
8
1001
9
2
6
1010
10
2
6
8
1011
11
2
5
1100
12
2
5
8
1101
13
2
5
6
1110
14
2
5
6
8
1111
15

LINQ Code that counts employee gender in each position and group by department and place in a matrix table

I just want to ask on how to create an LINQ code that can fill up my html table.
Please look at my Tables below
Table EMP: note* my "Male" is boolean
+----+---------------+--------+--------+
| id | Male| JS_REF |DEPT_ID | POS_ID |
+----+---------------+--------+--------+
| 1 | 1 | 1 | 2 | 3 |
| 2 | 0 | 2 | 2 | 3 |
| 3 | 1 | 3 | 1 | 2 |
| 4 | 1 | 2 | 4 | 2 |
| 5 | 1 | 1 | 5 | 5 |
| 6 | 0 | 4 | 6 | 1 |
| 7 | 1 | 1 | 1 | 1 |
| 8 | 0 | 2 | 2 | 3 |
+----+---------------+--------+--------+
Table:JOB_STATUS
+----+--------------------+
| id | JS_REF| JS_TITLE |
+----+--------------------+
| 1 | 1 |Undefined |
| 2 | 2 |Regular |
| 3 | 3 |Contructual |
| 4 | 4 |Probationary|
+----+--------------------+
Table:DEPTS
+----+--------------------+
| id | DEPT_ID| DEPT_NAME |
+----+--------------------+
| 1 | 1 |Admin |
| 2 | 2 |Accounting |
| 3 | 3 |Eginnering |
| 4 | 4 |HR |
+----+--------------------+
Table: POSITIONS
+----+--------------------+
| id | POS_ID| DEPT_NAME |
+----+--------------------+
| 1 | 1 |Clerk |
| 2 | 2 |Accountant |
| 3 | 3 |Bookeeper |
| 4 | 4 |Assistant |
| 5 | 5 |Mechanic |
| 6 | 6 |Staff |
+----+--------------------+
I'd made a static table on what will be the outcome of the LINQ code
Here's the picture:
Here's what i've tried so far:
SELECT tb.DEPT_NAME,TB.JS_TITLE, TB.Male, TB.Female, (TB.Male + TB.Female) AS 'Total Employees' FROM
(
SELECT JS_TITLE,DEPT_NAME,
SUM(CASE WHEN MALE = 1 THEN 1 ELSE 0 END) AS Male,
SUM(CASE WHEN MALE = 0 THEN 1 ELSE 0 END) AS Female
FROM EMP
left join JOB_STATUS on JOB_STATUS.JS_REF = EMP.JS_REF
left join DEPTS on DEPTS.DEPT_ID = EMP.DEPT_ID
GROUP BY JS_TITLE,DEPT_NAME
) AS TB
ORDER BY CASE WHEN TB.MALE IS NULL THEN 1 ELSE 0 END
If anyone can help me or give me some tips on how can I implement this im stuck in this part.
101 is total count for male, 23 for female. (the values are just copy and pasted, that's why the values are the same)
(Actual data result)

Persistent Command Line Display

I am writing a little Sudoku solving app in Ruby that just outputs to the terminal. For example:
ruboku $ ruby grid.rb
+---+---+---+---+---+---+---+---+---+
| 2 | 8 | 5 | | 3 | 1 | 7 | 6 | 9 |
+---+---+---+---+---+---+---+---+---+
| 5 | | 4 | 8 | 7 | 2 | 1 | 9 | 3 |
+---+---+---+---+---+---+---+---+---+
| | | | | | | | | |
+---+---+---+---+---+---+---+---+---+
| | | | | | | | | |
+---+---+---+---+---+---+---+---+---+
| | | | | | | | | |
+---+---+---+---+---+---+---+---+---+
| | | | | | | | | |
+---+---+---+---+---+---+---+---+---+
| | | | | | | | | |
+---+---+---+---+---+---+---+---+---+
| | | | | | | | | |
+---+---+---+---+---+---+---+---+---+
| | | | | | | | | |
+---+---+---+---+---+---+---+---+---+
Is there a way to keep this as a persistent display such that when there is new information to show it doesn't print a new line, but updates the grid that is already displaying?
I've seen some tools such as ngrok who do this.
Thanks for your time,
Tom
Depending on your needs, erasing the screen and creating the grid again could be good enough :
def show_grid
line = '+---+---+---+---+---+---+---+---+---+'
puts line
9.times do
row = (1..9).map { rand(9) + 1 }
puts '| ' + row.join(' | ') + ' |'
puts line
end
end
def clear_screen
system('clear') || system('cls')
end
loop do
clear_screen
show_grid
sleep 1
end
It outputs
+---+---+---+---+---+---+---+---+---+
| 1 | 2 | 7 | 3 | 4 | 9 | 8 | 6 | 5 |
+---+---+---+---+---+---+---+---+---+
| 1 | 8 | 1 | 7 | 6 | 2 | 2 | 1 | 3 |
+---+---+---+---+---+---+---+---+---+
| 2 | 2 | 1 | 2 | 2 | 8 | 2 | 3 | 6 |
+---+---+---+---+---+---+---+---+---+
| 5 | 3 | 6 | 1 | 5 | 3 | 2 | 7 | 9 |
+---+---+---+---+---+---+---+---+---+
| 9 | 7 | 7 | 4 | 7 | 2 | 2 | 9 | 1 |
+---+---+---+---+---+---+---+---+---+
| 5 | 9 | 1 | 9 | 3 | 7 | 8 | 3 | 1 |
+---+---+---+---+---+---+---+---+---+
| 8 | 5 | 4 | 7 | 3 | 2 | 2 | 5 | 3 |
+---+---+---+---+---+---+---+---+---+
| 4 | 7 | 1 | 1 | 4 | 8 | 4 | 1 | 1 |
+---+---+---+---+---+---+---+---+---+
| 1 | 1 | 8 | 4 | 2 | 4 | 8 | 3 | 8 |
+---+---+---+---+---+---+---+---+---+
then
+---+---+---+---+---+---+---+---+---+
| 6 | 4 | 4 | 3 | 5 | 5 | 8 | 9 | 1 |
+---+---+---+---+---+---+---+---+---+
| 7 | 7 | 7 | 3 | 3 | 2 | 8 | 7 | 6 |
+---+---+---+---+---+---+---+---+---+
| 6 | 2 | 2 | 1 | 4 | 7 | 1 | 1 | 9 |
+---+---+---+---+---+---+---+---+---+
| 3 | 8 | 7 | 7 | 7 | 9 | 7 | 4 | 4 |
+---+---+---+---+---+---+---+---+---+
| 4 | 1 | 2 | 8 | 6 | 7 | 1 | 9 | 3 |
+---+---+---+---+---+---+---+---+---+
| 6 | 7 | 7 | 5 | 1 | 7 | 6 | 7 | 4 |
+---+---+---+---+---+---+---+---+---+
| 8 | 2 | 3 | 5 | 3 | 5 | 4 | 7 | 2 |
+---+---+---+---+---+---+---+---+---+
| 5 | 9 | 3 | 2 | 4 | 2 | 9 | 6 | 3 |
+---+---+---+---+---+---+---+---+---+
| 1 | 7 | 8 | 9 | 4 | 3 | 4 | 1 | 5 |
+---+---+---+---+---+---+---+---+---+
then...
It should work on Windows/Linux/MacOs.
For anything more complex, you'll need an ncurses gem.
So the question is marked as answered I thought I would write up the advice given to me in the comments.
It looks like the ncurses ruby gem and vedeu offer the functionality that I need. Another alternative would be to clear the scren and reprint the output.

How to build pivot table through linq using c#

I have got this datatable in my c# code:
Date | Employee | Job1 | Job2 | Job3 |
---------|----------|------|------|-------|
1/1/2012 | A | 1.00 | 1 | 1 |
1/1/2012 | B | 2.5 | 2 | 2 |
1/1/2012 | C | 2.89 | 1 | 4 |
1/1/2012 | D | 4.11 | 2 | 1 |
1/2/2012 | A | 3 | 2 | 5 |
1/2/2012 | B | 2 | 2 | 2 |
1/2/2012 | C | 3 | 3 | 3 |
1/2/2012 | D | 1 | 1 | 1 |
1/3/2012 | A | 5 | 5 | 5 |
1/3/2012 | B | 2 | 2 | 6 |
1/3/2012 | C | 1 | 1 | 1 |
1/3/2012 | D | 2 | 3 | 4 |
2/1/2012 | A | 2 | 2 | 2 |
2/1/2012 | B | 5 | 5 | 2 |
2/1/2012 | D | 2 | 2 | 2 |
2/2/2012 | A | 3 | 3 | 3 |
2/2/2012 | B | 2 | 3 | 3 |
3/1/2012 | A | 4 | 4 | 2 |
Now I want to create another DataTable which would look like this:
Job1
Employee | 1/1/2012 | 1/2/2012 | 1/3/2012 | 2/1/2012 | 2/2/2012 |
---------|----------|----------|----------|----------|----------|
A | 1.00 | 3 | 5 | 2 | 3 |
B | 2.50 | 2 | 2 | 5 | 2 |
C | 2.89 | 3 | 1 | - | |
D | 4.11 | 1 | 2 | 2 | |
Total | 10.50 | 9 | 10 | 9 | 5 |
Please suggest how to make this pivot table using Linq and C#.
var query = from foo in db.Foos
group foo by foo.Date into g
select new {
Date = g.Key,
A = g.Where(x => x.Employee == "A").Sum(x => x.Job1),
B = g.Where(x => x.Employee == "B").Sum(x => x.Job1),
C = g.Where(x => x.Employee == "C").Sum(x => x.Job1),
D = g.Where(x => x.Employee == "D").Sum(x => x.Job1),
Total = g.Sum(x => x.Job1)
};
You can also apply OrderBy(x => x.Date) to query.
You won't be able to do this with LINQ due to the dynamic nature of the columns. LINQ to SQL needs a static way to map result set fields to property values. Instead, you can look into the PIVOT SQL statement and fill the results into a DataTable
http://msdn.microsoft.com/en-us/library/ms177410(v=sql.105).aspx

Resources