How to update duplicate rows within a given condition - laravel

I have the following table:
ID|group_id|subjectlist_id|article_id
1 |1 |2 |1
2 |2 |2 |1
3 |3 |3 |4
4 |4 |1 |1
5 |5 |1 |1
How do I update the table so it looks like this?
ID|group_id|subjectlist_id|article_id|marked
1 |1 |2 |1 |done
2 |2 |2 |1 |done
3 |3 |3 |4 |
4 |4 |1 |1 |
5 |5 |1 |1 |
So far I have this query:
$duplicates = DB::table('table')
->select('subjectlist_id', 'article_id')
->whereIn('group_id', array(1,2,3))
->groupBy('subjectlist_id', 'article_id')
->havingRaw('COUNT(*) > 1')
->update(['marked' => 'done']);

Simply remove ->havingRaw('COUNT(*) > 1').
If you add DB::raw(COUNT(*)) to the select, then you can see how many records each possible combination of subjectlist_id and article_id have.

Related

predicting vehicle utilization

predicting vehicle utilization :
dears this is my data for the full year (8000 records) ...and like to make prediction for next 3 months
could you please advice which Algorithm should i use and any other advice...(i am beginner)
|Branch |month| date |Util |
|:----- |:---:|:-----------:|-----------:|
|1101 |1 | 2022-01-01 | 43.54 |
|1103 |1 | 2022-01-02 | 74.37 |
|1104 |1 | 2022-01-03 | 0 |
|1126 |2 | 2022-01-04 | 65.83 |

How to update multiple values in oracle?

I have two tables:
table 1:
|Project type|Quarter1|Quarter2|Quarter3|Quarter4|
|------------|--------|--------|--------|--------|
|type1 |1 |3 |5 |7 |
|type2 |2 |4 |6 |8 |
table 2:
|Project|Value|Quarter|
|-------|-----|-------|
|type1 | |1 |
|type2 | |1 |
|type1 | |2 |
|type2 | |2 |
|type1 | |3 |
|type2 | |3 |
|type1 | |4 |
|type2 | |4 |
I want to update table 2 value section with data from table 1 and the expected outcome is:
|Project|Value|Quarter|
|-------|-----|-------|
|type1 |1 |1 |
|type2 |2 |1 |
|type1 |3 |2 |
|type2 |4 |2 |
|type1 |5 |3 |
|type2 |6 |3 |
|type1 |7 |4 |
|type2 |8 |4 |
I know updating single one value can be written as:
update table2 a
set a.value = (select Quarter1
from table1
where projecttype = 'type1')
where a.project = 'type1'
and a.quarter = '1';
Please tell me how can I update all value in one time?
Thank you!
One way is with a merge statement:
merge into table_2 t
using table_1 s
on (t.project = s.project_type)
when matched then update
set t.value = case t.quarter when 1 then s.quarter1
when 2 then s.quarter2
when 3 then s.quarter3
when 4 then s.quarter4 end
;
This is my primary thought about using loop to repeat the updating process. The main body refer to mathguy's answer (Thanks again). It may complicate the code in this scenario, but would be useful when there are numerous columns in table1, such as years instead quarters.
declare
quart_num number;
code varchar2(2000);
begin
for quart_num in 1..4
loop
code := 'merge into table2 a
using table1 b
on (a.project = b.projecttype)
when matched then
update set a.value = quarter'||
quart_num || 'where a.quarter =' ||quart_num;
execute immediate(code);
end loop;
end;

How to Get All records with specific Id and LastDate

this is my table
I'm using .Net 3.1 and EFcore.
I have a controller with an input int[] TermIds which contains on or more TermId.
Now I want to select All records which their TermId exist In TemrIds AND their statisticDate is the latest date.
Note that Maximum Date for each TermId is Different from others
| Id | TermID |UniversityId|StatisticDate|FileTypeId|Count|
| :----:| :-----:|:---------: |:------------|:--------:|:----|
| 1 |126 |5 |2012/01/10 |4 |150 |
| 2 |126 |5 |2012/01/10 |3 |250 |
| 3 |126 |4 |2012/01/10 |4 |127 |
| 4 |126 |4 |2012/01/10 |3 |110 |
| 5 |126 |5 |2011/01/10 |4 |107 |
| 6 |126 |5 |2011/01/10 |3 |99 |
| 7 |126 |4 |2011/01/10 |4 |103 |
| 8 |126 |4 |2011/01/10 |3 |96 |
| 9 |123 |5 |2011/01/01 |4 |88 |
| 10 |123 |5 |2011/01/01 |3 |76 |
| 11 |123 |4 |2011/01/01 |4 |98 |
| 12 |123 |4 |2011/01/01 |3 |99 |
| 13 |123 |5 |2010/01/01 |4 |77 |
| 14 |123 |5 |2010/01/01 |3 |66 |
| 15 |123 |4 |2010/01/01 |4 |33 |
| 16 |123 |4 |2010/01/01 |3 |55 |
and at last i want this records
| Id | TermID |UniversityId|StatisticDate|FileTypeId|Count|
| :----:| :-----:|:---------: |:------------|:--------:|:----|
| 1 |126 |5 |2012/01/10 |4 |150 |
| 2 |126 |5 |2012/01/10 |3 |250 |
| 3 |126 |4 |2012/01/10 |4 |127 |
| 4 |126 |4 |2012/01/10 |3 |110 |
| 9 |123 |5 |2011/01/01 |4 |88 |
| 10 |123 |5 |2011/01/01 |3 |76 |
| 11 |123 |4 |2011/01/01 |4 |98 |
| 12 |123 |4 |2011/01/01 |3 |99 |
This is a linq query I can think of based on my understanding. This should only filter out the records that have TermID existing on the termIds and the StatisticDate is the maximum date per term.
using System.Linq;
int[] termIds = new int[] { 126, 127 };
List<Foo> data = new List<Foo>()
{
new Foo() { Id = 1, TermID = 126, UniversityId = 5, StatisticDate = new DateTime(2012, 01, 10), FileTypeId = 4, Count = 150 },
new Foo() { Id = 2, TermID = 126, UniversityId = 5, StatisticDate = new DateTime(2012, 01, 10), FileTypeId = 3, Count = 250 },
new Foo() { Id = 3, TermID = 126, UniversityId = 4, StatisticDate = new DateTime(2012, 01, 10), FileTypeId = 4, Count = 127 },
new Foo() { Id = 4, TermID = 126, UniversityId = 4, StatisticDate = new DateTime(2012, 01, 10), FileTypeId = 3, Count = 110 },
new Foo() { Id = 5, TermID = 126, UniversityId = 5, StatisticDate = new DateTime(2011, 01, 10), FileTypeId = 4, Count = 107 },
};
var records = data.Where(r => termIds.Contains(r.TermID) && r.StatisticDate == data.Where(x => x.TermID == r.TermID).Max(x => x.StatisticDate));

Count leave attributes Hierarchical oralce

I have 2 tables as follows:
============================== ==============================
Directories Items
============================== ==============================
ID |Name |Parent ID |Name |Directory
1 |All |null 1 |Simpson |6
2 |Movies |1 2 |Avatar |5
3 |Clips |1 3 |Tom&Jerry |6
4 |Games |1 4 |CoD |7
5 |Action |2 5 |CS |7
6 |Cartoon |2 6 |NFS |8
7 |Shooter |4 7 |Halo |7
8 |Racing |4 8 |F4 |8
9 |Music |3 9 |Thriller |9
10 |Beat it |3
I want to get results to see how many items in each directory in the Directories
table above, I can only count on 1 specific Directory, but to use one query for all categories is not successful, so how should I do, thanks.
================================
Result
================================
ID |Name |Items Count
1 |All |10
2 |Movies |3
3 |Clips |2
4 |Games |5
5 |Action |1
6 |Cartoon |2
7 |Shooter |3
8 |Racing |2
9 |Music |1
If you're looking for all the descendants use connect by.

How to get distinct rows in dataframe using pyspark?

I understand this is just a very simple question and most likely have been answered somewhere, but as a beginner I still don't get it and am looking for your enlightenment, thank you in advance:
I have a interim dataframe:
+----------------------------+---+
|host |day|
+----------------------------+---+
|in24.inetnebr.com |1 |
|uplherc.upl.com |1 |
|uplherc.upl.com |1 |
|uplherc.upl.com |1 |
|uplherc.upl.com |1 |
|ix-esc-ca2-07.ix.netcom.com |1 |
|uplherc.upl.com |1 |
What I need is to remove all the redundant items in host column, in another word, I need to get the final distinct result like:
+----------------------------+---+
|host |day|
+----------------------------+---+
|in24.inetnebr.com |1 |
|uplherc.upl.com |1 |
|ix-esc-ca2-07.ix.netcom.com |1 |
|uplherc.upl.com |1 |
If df is the name of your DataFrame, there are two ways to get unique rows:
df2 = df.distinct()
or
df2 = df.drop_duplicates()
The normal distinct not so user friendly, because you cant set the column.
In this case enough for you:
df = df.distinct()
but if you have other value in date column, you wont get back the distinct elements from host:
+--------------------+---+
| host|day|
+--------------------+---+
| in24.inetnebr.com| 1|
| uplherc.upl.com| 1|
| uplherc.upl.com| 2|
| uplherc.upl.com| 1|
| uplherc.upl.com| 1|
|ix-esc-ca2-07.ix....| 1|
| uplherc.upl.com| 1|
+--------------------+---+
after distinct you will get back as follows:
df.distinct().show()
+--------------------+---+
| host|day|
+--------------------+---+
| in24.inetnebr.com| 1|
| uplherc.upl.com| 2|
| uplherc.upl.com| 1|
|ix-esc-ca2-07.ix....| 1|
+--------------------+---+
thus you should use this:
df = df.dropDuplicates(['host'])
it will keep the first value of day
If you familiar with SQL language it will also works for you:
df.createOrReplaceTempView("temp_table")
new_df = spark.sql("select first(host), first(day) from temp_table GROUP BY host")
+--------------------+-----------------+
| first(host, false)|first(day, false)|
+--------------------+-----------------+
| in24.inetnebr.com| 1|
|ix-esc-ca2-07.ix....| 1|
| uplherc.upl.com| 1|
+--------------------+-----------------+

Resources