Laravel Eloquent query on first relation of one to many - laravel

I have Two Tables A and B
A can have more than B
the relation between them is that B has A_id
columns in B have A_id and date
I have a filter with status (status alpha = first B of A date is less than 30 days,beta = first B of A date is less than 60)
when trying to get the status I want to query on the first row in B which has A_id
public function alpha(){
return $this->hasOne("App/B")->where('date','<',now()->subMonth(1)->toDateString())->where('date','>',now()->subMonth(2)->toDateString());
}
public function beta(){
return $this->hasOne("App/B")->where('date','<',now()->subMonth(2)->toDateString());
}
This seems to get the job done but failed in specific situation
imagine table B has two rows
id = 1 A_id=1 and date is 45 days old
id =2 A_id=1 and date is 70 days old
This should be an alpha since I'm only interested in the First date
but the query I've written returns on both alpha and beta
how can I fix my query to only check the first occurrence only
mind that I can't loop over A's after getting them since I'm using pagination

Related

Small detail when using this function in Google Sheets

Small detail when using =INDEX($A$8:$A$11;MATCH(MAX(SUMIF($B$8:$B$11;$B$8:$B$11));SUMIF($B$8:$B$11;$B$8:$B$11);0))) If the values in column B are all different it returns the correct date value, but if two identical values in column B coincide in different dates then it returns the date of the first value; it does not return the correct date and it keeps the first one that has the repeated value.
Any idea?
p.s This question can be added to this post
Even more easier way:
On E2 Try this =TRANSPOSE(INDEX(QUERY(A1:B," select A, sum(B) group by A Order By sum(B) Desc "),2))
and format the date and currency accordingly.
You can do that easily and differently to get:
1 - Make a helper table to get unique dates, You can use two ways
a) - Use SUMIF Function to get the sum of Expenditure in each unique date Like so =IF(D2="",,SUMIF($A$2:$A,D2,$B$2:$B)) and drag it down.
b) - By using QUERY Function =QUERY(A1:B11," select A, sum(B) group by A Order By sum(B) Desc ")
2 - to get SUM BY DATE OF HIGHEST EXPENDITURE: =MAX(E2:E)
3 - to get DATE BY HIGHEST EXPENDITURE: =INDEX($D$2:$D,MATCH($H$3,$E$2:$E,0),1)
Make a copy of this sheet "make it yours."
Hope that answerd your question.

Running distinct count in quicksight

I want to implement a running distinct count with Amazon Quicksight. Here's an example of what that would look like:
Date
ID
Amount
Running Distinct Count
1/1/1900
a
1
1
1/2/1900
a
3
1
1/2/1900
b
6
2
1/4/1900
a
3
2
1/8/1900
c
9
3
1/22/1900
d
2
4
I've tried runningSum(distinct_count, [Date ASC]), but this returns a sum of the distinct counts for each aggregated date field.
Can this be implemented in QuickSight?
You can use this workaround to get the same functionality as runningDistinctCount() as follows:
runningSum(
distinct_count(
ifelse(
datetime=minOver({Date}, [{ID}], PRE_AGG),
{ID},
NULL
)),
[{Date} ASC],
[]
)
This would give you the runningDistinctCount of ID's over the Date field. It achieves it by considering just the first time the ID appears in the dataset, counting these and finally doing a runningSum on these counts.

Coldfusion query of queries count by date

I'm trying to get an count based on two dates and I'm not sure how it should look in a query. I have two date fields; I want to get a count based on those dates.
<cfquery>
SELECT COUNT(*)
FROM Table1
Where month of date1 is one month less than month of date2
</cfquery>
Assuming Table1 is your original query, you can accomplish your goal as follows.
Step 1 - Use QueryAddColumn twice to add two empty columns.
Step 2 - Loop through your query and populate these two columns with numbers. One will represent date1 and the other will represent date2. It's not quite as simple as putting in the month numbers because you have to account for the year as well.
Step 3 - Write your Q of Q with a filter resembling this:
where NewColumn1 - NewColumn2 = 1

Need Only Differing column values from two DataTables

I have two DataTables in which I need only differing column values in a final DataTable as shown below
DataTable(DT1)
ID Name Salary
1 ABC 2000
2 XYZ 4000
3 Suresh 6000
DataTable(DT2)
ID Name Salary
1 ABC 3000
2 XYZ 5000
3 Suresh 6000
DataTable(DT3)
ID Salary(DT1) Salary(DT2)
1 2000 3000
2 4000 5000
How can I accomplish this using linq queries on Datatables or by any other way? I tried looping and comparing each column values both datatables, but I'm not getting how to store in final DataTables.
First we need to join the tables (assuming the ID's have a PK/FK relationship), Then compare the values and place them in a new table.
var unMatchedRows = from DataRow dt1Row in dt1.Rows
join DataRow dt2Row in dt2.Rows
on dt1Row["id"] equals dt2Row["id"]
where Convert.ToDoubl(dt1Row["salary"]) != Convert.ToDouble(dt2Row["salary"])
select new { dt1Row, dt2Row };
This gives us a collection of DataRow objects joined on the ID where the salary's are different. Now we just need to populate a new DataTable with the rows.
foreach (var m in unMatchedrows)
{
dt3.ImportRow(m);
}
It is worth noting that DT3 will need to have the same schema as the other two tables. And this will only place the unmatched rows in the new table. There are several ways to do this and I am not in front of an IDE right now to test it out, but this should get you going. Hope this helps!

Data returned from Oracle Proc in vb.net

I have a VB.Net function which executes a Oracle Stored Proc and returns a dataset.
Below is a list of sample records
OrderID OrderDetail Qty Date Supplier Price
1 Books 10 10-Aug-08 ABC Inc 100.00
1 Pens 20 10-Aug-08 ABC Inc 300.00
2 Keys 1 20-Aug-09 Blue cross 100.00
2 Nots 3 30-Aug-09 Blue Cross 200.00
The above records are returned as a dataset in my function.
Using the above dataset int two different functions how can I return data as shown below.
First function should return only distinct orderID records
Second function should take OrderID as input and return records based on orderID
Any suggestions?
Thanks
For the select distinct, check out Select DISTINCT on DataTable. For the other, use the DataTable.Select method. Assuming that the DataTable is the first in the DataSet ...
var dt = ds.Tables[0];
var rows = dt.Select( "OrderID = 1" );
(Sorry, I don't know any VB :)

Resources