How to do alternate column sorting on two columns? - sorting

I know how to sort by two columns. But I wanted to know how to sort by alternate columns in neo4j.
Node name = Product
value | version
1 | 2
4 | 1
2 | 1
4 | 1
2 | 1
3 | 2
There are 2 values of versions 1 and 2. And value can be anything. First it will give higher value of version 1 , then it will give higher value of version 2, then it will give second higher value of version 1 , then it will give second higher value of version 2, and so on.
value | version
4 | 1
3 | 2
4 | 1
1 | 2
2 | 1
1 | 1
I don't know this type of sorting logically done or not through cypher query. I havn't done this type of logic in mysql also. Anyone can give me such clue of ne4j cypher query.
Update :
Match (p:Product)
RETURN p.value as value, p.version as version
ORDER BY version ASC, value DESC
This query sorts by version first then sorts by value. I don't want it.
I want alternate sort.

unwind[1,2,3] AS value unwind[1,2] AS version
RETURN value, version
ORDER BY value DESC , version ASC
value version
3 1
3 2
2 1
2 2
1 1
1 2

Related

How to limit the record of children's attribute in laravel eloquent

I am creating a comment section using laravel with the example entries below:
id | comment | parent_id
1 test 1
2 test 1
3 test 1
4 test 2
5 te 2
6 test 1
7 test 2
I get these data from an API request. Let's say, I want to get all comments and limit the replies up to 2 records per query. From the given test sample above, the result should be:
id | parent_id
1 1
2 1
4 2
5 2
My question is, how to get all replies (parent_id > 0) and limit & offset the query per request?
My current query is:
Comment::where('parent_id', '>' , 0)->get();
I think you should use pagination to limit the number of the elements:
Comment::where('parent_id', '>' , 0)->paginate(15);
Replace the "15" with your limit.
Also, you can check Laravel docs about pagination: https://laravel.com/docs/7.x/pagination

calculate the time difference for same column in Spotfire

I am a beginner for Spotfire. I have a problem about the difference calculation for the some column value. A sample table could be like this:
id timestamp state
1 7/1/2016 12:00:01 AM 1
2 7/1/2016 12:00:03 AM 0
3 7/1/2016 12:00:04 AM 1
4 7/1/2016 12:00:06 AM 0
5 7/1/2016 12:00:09 AM 1
6 7/1/2016 12:00:10 AM 0
7 7/1/2016 12:00:12 AM 1
I want to calculate the time difference for the timestamp when the state is 1,
the final table I want to have is:
id timestamp state time_diffence
3 7/1/2016 12:00:04 AM 1 3
5 7/1/2016 12:00:09 AM 1 5
7 7/1/2016 12:00:12 AM 1 3
it seems that I should identify an expression for the calculation, but I have not idea for the calculation just for one parameter :(. somebody could help me ?
still one more small question: what if the timestamp column value is just number value, how can i calculate the difference, is there any related function like DateDiff() here? for example:
id times state
1 12 1
2 7 0
3 10 1
4 11 0
5 6 1
6 9 0
7 7 1
the result could be :
id times state diffence
3 10 1 -2
5 6 1 -4
7 7 1 1
after running the code: i have the error as below:
for the row if it has the same time stamp as the last previous row, the difference will keep same as before, but actually the difference for the rows which have same time stamp would be as 0
thanks for your help :)
Assuming your data is sorted in ascending order by [timestamp] before you import it, you can partition using the Previous function with Over where the [state]=1.
Insert a calculated column with this expression:
If([state]=1,DateDiff("ss",Min([timestamp]) OVER (Previous([timestamp])),[timestamp]))
You will see it populated in your table like the below:
Then if you ONLY want to see the rows that have the difference you mentioned, on your table you can...
Right Click > Properties > Data > Limit data using expression >
And insert the expression: [time_difference] > 1
This will result in this table:

Different behaviour in "order by" clause: Oracle vs. PostgreSQL

I have the following table (created and populated them in Oracle and PostgreSQL):
> create table foo (a varchar(10));
I populated them with values, and order by clause is behaving differently in PostgreSQL and Oracle (I don't think versions are relevant to this question):
Oracle:
> select a, length(a) from foo order by a;
A LENGTH(A)
---------- ----------
.1 2
01 2
1 1
1#0 3
1#1 3
1.0 3
1.1 3
10 2
11 2
9 rows selected.
I get what I expect. .1 before 01, since . is before 0 in ascii table.
However, in PostgreSQL I have:
=> select a, length(a) from foo order by a;
a | length
-----+--------
01 | 2
1 | 1
.1 | 2
10 | 2
1.0 | 3
1#0 | 3
11 | 2
1.1 | 3
1#1 | 3
(9 rows)
Why the difference? I know it probably has something to do with collate order or similar, but I would like some pointers on where to read more about it.
UPDATE: collate info on the PostgreSQL database:
Encoding: UTF8
Collante: en_US.UTF-8
Ctype: en_US.UTF-8 |
Thanks!
Postgres has only two built-in collations: C and POSIX.
Any other collations are provided by operating system.
On many linux systems in UTF locales all non alphanumeric characters are ignored during sorting.
You can obtain expected result using collate C:
select a, length(a) from foo order by a collate "C";
You can find more detailed explanation in this answer.

SAS Sorting within group

I would like to try and sort this data by descending number of events and from latest date, grouped by ID
I have tried proc sql;
proc sql;
create table new as
select *
from old
group by ID
order by events desc, date desc;
quit;
The result I currently get is
ID Date Events
1 09/10/2015 3
1 27/06/2014 3
1 03/01/2014 3
2 09/11/2015 2
3 01/01/2015 2
2 16/10/2014 2
3 08/12/2013 2
4 08/10/2015 1
5 09/11/2014 1
6 02/02/2013 1
Although the dates and events are sorted descending. Those IDs with multiple events are no longer grouped.
Would it be possible to achieve the below in fewer steps?
ID Date Events
1 09/10/2015 3
1 27/06/2014 3
1 03/01/2014 3
3 01/01/2015 2
3 08/12/2013 2
2 09/11/2015 2
2 16/10/2014 2
4 08/10/2015 1
5 09/11/2014 1
6 02/02/2013 1
Thanks
It looks to me like you're trying to sort by descending event, then by either the earliest or latest date (I can't tell which one from your explanation), also descending, and then by id. In your proc sql query, you could try calculating the min or max of the Date variable, grouped by event and id, and then sort the result by descending event, the descending min/max of the date, and id.

MVC 3 get days of month in table columns

I am learning MVC 3, it is fun, but not always :).
I have one problem I can't solve.
I have this in my table in db
id | worker | working_hours | year | month | day | shift
1 worker1 5 2013 6 1 day
2 worker2 6 2013 6 2 day
2 worker3 7 2013 6 3 night
That works ok. But I would like to make it better :). But I have two problems.
I need table columns do be prepopulated with days of month and for each day I could see working hours
I need table to look something like this
| 1 | 2 | 3 | Total
day 5 6 11
night 7 7
That is just for 3 days in db, but I need table headers to be prepopulated with all days for month.
It seems easy, but I cant solve this one :). Can someone please give me a hand with this. I am doing all this in MVC 3 and using linq to sql connection to my db.
Maybe do something like this if this is REALLY what you want:
id | id(from table 1) | Shift | 1 | 2 | 3 | ect..... Total
1 1 day 5 6 11
2 1 night 7 7
Then in your primary table replace the Day column with the id from the above table and remove the Shift column. You could then add a unique key constraint to the above table so it won't allow a repeat if the id from table 1 and the shift are the same.
To be brutally honest however all this is just a bit confusing. Best bet would be to have two tables:
A:
id | worker
1 worker1
2 worker2
3 worker3
B:
id | workerid | working_hours | year | month | day | day
1 1 5 2013 6 1 true
2 1 6 2013 6 2 true
3 2 7 2013 6 3 false
It doesn't really matter that this table will have loads of entries and doesn't read well because you then write smart queries using LINQ to pull out the data that you need to display on an MVC webpage.

Resources