I am using Laravel 7.
I am having some problems with sorting while using Laravel 7.
To put it briefly. My table has a json column.
Example column name: "jsonData" json data:
$data1 =
{
"rank":12,
"value":"test",
}
$data2 =
{
"rank":105,
"value":"test-2",
}
According to these data, the following query is made.
DB::table('tablename')->orderBy('jsonData->rank', 'ASC')->get();
While the output I get should be 12 - 105, normally from small to large. When I print the data here, it becomes 105 - 12.
Another example of sorting like this:
1
1
10
100
108
113
12
120
1231885
13631
144
You may have noticed how absurd this is. I have done a lot of research on how to solve this. But I could not come to a conclusion.
Related
My table as below
How i want to show data from
name score
Riyal 17
demo2 11
demo3 9
demo1 1
I want to show from higest to low and will show only 10 data from table.
my code is
public function index()
{
$score_board = ScoreBoard::orderBy('id')->max('score');
return new ScoreBoardResource($score_board);
}
but it gives me nothing. Any idea how can i do it
->max('score') would return 17... If you want 10 records, sorted highest to lowest, you need to use the proper methods:
$scoreBoard = ScoreBoard::orderBy('score', 'DESC')->limit(10)->get();
Please read the Documentation:
https://laravel.com/docs/8.x/queries#ordering-grouping-limit-and-offset
I would have liked to convert a SQL query like this one in Laravel Eloquent.
This query works on MariaDb but i don't know about other engines (might be the reason why it isn't implented):
id
year
country
enabled
0
2000
"France"
0
1
2001
"Spain"
0
2
2002
"France"
1
3
2003
"Germany"
1
SELECT id FROM my_db.countries WHERE (name, enabled) IN (("France", 1), ("Spain", 0));
This returns 1 and 2.
This is possible with Eloquent (as suggested here: laravel whereIn multiple columns but it wouldn't return the expected results:
DB::table('countries')->select('id')->whereIn('name', ["france", "Spain"])->whereIn('enabled', [0, 1])->all();
This returns 0, 1 and 2.
I gave a shot at adapting the Illuminate/Database library to fit my needs but the databinding started to get really complexe.
I managed to make it with a whereRaw query but it isn't really clean enough for production code as there are no data binding (values shows up with ->toSql()).
Does anyone have an idea?
The query syntax you want to get:
SELECT id FROM my_db.countries WHERE (name, enabled) IN (("France", 1), ("Spain", 0));
is exclusive to Oracle. The whereIn method of Laravel supports (String, Array), so I think you only have the options to do it through raw queries or using the solution proposed by V-K
I suppose you wanna get records that satisfy two conditions. whereIn checks if the column contains one of the values in the array.
`DB::table('countries')->select('id')->whereIn('name', ["france", "Spain"])->whereIn('enabled', [0, 1])->all();`
that code returns combinations france 0, france 1, Spain 0, Spain 1.
TO get combinations france 0 and Spain 1 you can use this code
DB::table('countries')
->select('id')
->where(function(Builder $builder) {
$builder
->where('name', 'france')
->where('enabled', 0);
})
->orWhere(function(Builder $builder) {
$builder
->where('name', 'Spain')
->where('enabled', 1);
})
->get();
It checks the conditions name = france and enabled = 0 work together
If you add a new column that will keep the concatenation of the country and enabled fields, then you can use a simple whereIn method.
id
year
country
enabled
country_enabled
0
2000
"France"
0
"France-0"
1
2001
"Spain"
0
"Spain-0"
2
2002
"France"
1
"France-1"
3
2003
"Germany"
1
"Germany-1"
DB::table('countries')->select('id')->whereIn('country_enabled ', ["France-1", "Spain-0"])->get();
You may even add an index to that column to speed up the search.
Of course, the provided solution will add some slight overhead to the writing in that table.
I have a block of data like this in a new spreadsheet:
GOODS Count Sort Index
111770999 128 9
111771000 32 0
111771005 64 5
111771010 64 0
111771011 64 1
number of rows are dynamic, columns are fixed (3). How can i write a script to sort by column 3 like using Data-Sort Range in the spreadsheet? Many thanks in advance!
You can sort like this using appscript:
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('YOUR_SHEET_NAME');
sheet.getRange(2,1,sheet.getLastRow()-1,sheet.getLastColumn()).sort(3);
//Here, getRange is(startingRow,startingColumn,NumRows,NumCols)
//and sort(x) represents sort by 'x'th column
I am trying the following
https://data.gov.in/api/datastore/resource.json/?resource_id=e16c75b6-7ee6-4ade-8e1f-2cd3043ff4c9&api-key=APIKEY&limit=200
I still get only 100 records. If I change the limit to 50 it gives me 50 records. How do I get the records from 101 - 200 and beyond?
I also tried using the offset parameter like so :
&offset=50
expecting it to give me record number 50-150, but it doesn't.
Does anyone have an Idea?
Try this query from OGD Platform.
you will find total_records = 2947 and count=100.
Here, you have total of 2947 records and maximum of 100 records can be fetched in one query. If you want next 200 result, set offset=2 which will give results from 201 to 300 and so on. You need to increase your offset by 1 in each query till 2947/100 = 29 (29th query will give 47 records) to get all data.
Parameter limit is used to fetch total number of records in each query and that will be between 0 to 100 (max). That's why when you set limit=50, you got 50 records but if you set limit=110, still you will get 100 records only.
Hope I my answer is clear enough. Good luck.
I am new to Pig, so this might be a trivial question. I could not get a reasonable answer hence asking this.
Have 3 columns as follows:
userid itemid action
245 4 'view'
245 6 'click'
149 12 'buy'
149 1 'click'
and so on...
I have a mapping given such as : 'view'=1 , 'click'=1.4 , 'buy'= 2.1 etc.
My desired output is:
userid itemid action
245 4 1
245 6 1.4
149 12 2.1
149 1 1.4
Simple commands that can help me achieve this?
I ll need to perform some cacluation on the 3rd column and hence can't have it in string format.
Create a mapping file in HDFS with these mapping values, like:
action_string action_value
view 1
click 1.4
buy 2.1
Say this file is stored at <mapping_file>. Then just load this file and join your original dataset with this file:
mapping = LOAD '<mapping_file>' USING PigStorage() AS action_string, action_value;
joined = JOIN original BY action, mapping BY action_string USING 'replicated';
output = FOREACH joined GENERATE userid, itemid, action_value;
There are other ways depending on your use case and your file size. But I think this is the most flexible.