How to Sum last value of all users in database - laravel

I have a database with different users, with an amount column....all this user all have different value in their amount colume, but i want to pick all the last amount for each user and sum it.
i tried it like this but game an error
$returned = DB::table('sales')->sum('returned')->last();
pls, i dont want the total sum of the amount column....because some users have more than one amount, so i want to pick the last one for each user and sum it

Use this,
DB::table('sales')->latest()->get()->unique('user_id')->sum('returned');
if you have sales Model use
Sales::latest()->get()->unique('user_id')->sum('returned');
Note the code is updated

Related

Get a random record list Dynamics 365 c#

I'm new to Dynamics 365 development. I have an activity where I need to select a certain number of random records in an entity, it would be like a prize draw from a list of records. I will be working with many records. How to best do this in C#?
There isn't a built-in functionality to get a random record, one approach (probably is not the fastest one) is to retrieve all the records first, if you are using C# you can do a RetrieveMultiple (keep in mind that you need pagination if the records are more than 5000) with just the Id (so ColumnSet can be false).
After you retrieved the Ids you can put them in a List and access (randomly) the elements you need (check this link as an example How to access random item in list?).
Building on Guido's answer, another idea would be to get the minimum CreatedOn and maximum CreatedOn for the records in question. Then generate a random date and time within that range (inclusive of the min and max).
To get the random record, query for the top 1 where CreatedOn is greater than the random date, order by CreatedOn.
Or, if you count the records first, you can generate a random number in that range and do a LINQ query with Skip(random #).Take(1).

How would I get total balance after each transaction

Hei guys. I am desperately seeking help.
Please take a look at this image first.
Thanks. Now
Please look at the table below. The rightmost column REMAINING BALANCE is holding the SUM of credit_amount - debit_amount. But when I add a new amount of credit it changes in the entire column. For example, the REMAINING BALANCE column has 102500.0. If I add 500 of credit is supposed to give me 102500 + 500 = 103000 only in that specific row. 102500 will stay same in the previous row. But my problem is, it changes in the entire column.
And I have used this repository to sum the total amount of credit and subtract total amount of debit.
// Find remaining balance
#Query("SELECT SUM(b.credit_amount - b.debit_amount) FROM Mosque b")
double remainingBalance();
Thanks for your attention.
Well, it seems that you need to specify a criteria in your query, otherwise, the SQL query (or JPQL in your case) will be applied to the entire table...
I'd do something like:
#Query("SELECT SUM(b.credit_amount - b.debit_amount) FROM Mosque b where b.id =?")
double remainingBalance(int id);
Otherwise, without criteria, for each Domain Object you have, you will query the entire table. Hence, you will always get the same result. If you run this query in your Database, you will see that you will have only one result, as SUM is "summing" all your rows together.

How to get the sum of values of a column in tmap?

I have 2 columns - Matches(Integer), Accounts_type(String). And i want to create a third column where i want to get proportions of matches played by different account types. I am new to Talend & am facing issue with this for past 2 days & did a lot of research but to no avail. Please help..
You can do it like this:
You need to read your source data twice (I used tFixedFlowInput_1 and tFixedFlowInput_2 with the same data). The idea is to calculate the total of your matches in tAggregateRow_1, it simply does a sum of all Matches without a group by column, then use that as a lookup.
The tMap then joins your source data with the calculated total. Since the total will always be one record, you don't need any join column. You then simply divide Matches by Total as required.
This is supposing you have unique values in Account_type; if you don't, you need to add another tAggregateRow between your source and tMap_1, in order to get sum of Matches for each Account_type (group by Account_type).

Query that discards duplicate keys with parse.com SDK?

I'm researching how to implement leaderboards for my game with the parse.com SDK, my plan is to submit a score for the user every time they finish a level, attached to a "parent" leaderboard. I need to submit all scores because I need to retrieve leaderboards within time ranges (eg "all time", "last week", "last month", etc). The problem is, there'll be multiple scores for each user on the same leaderboard, and I only need to highest one. Is there a way to drop duplicate keys from a query? Is this the correct strategy? Everything else (sorting, paging, etc) seems to be in place.
Thanks.
You just need a table with 'User_id', 'Score', 'Level', and 'Date' (or whatever you need). Each time that a player finishes a level, you put the score into the table.
Then you have to calculate each (all time, last week, etc) in the query.
E.g.
10 Highs of the day:
SELECT TOP 10 User_id, Score, Date FROM Scores
WHERE Date = getdate()
ORDER BY Score DESC
I don't know if I understood the question. Let me know if I didn't.
Hope it helps.
As far as I understand from your question you want to retrieve data from Parse class. At the same time you want to eliminate the duplicate entry because user has multiple scores in different days. So to get the highest one, you have to query the class via query (based on SDK Android,iOS) and order by descending(based on your criteria), then obtain the first item in the result.
Or you can get the user all scores and create a structure where you can store the user scores as array list day by day. Based on day you can get the latest max or min scores. I hope I understand your question.
Hope this helps.Regards

Linq to SQL - Random Select Order and Paging

We have a database with 2,00,000 vendor in 100 plus category, if someone visit the website we want to allow them to select a category and show them 25 Vendor per page, first we kept order by VendorId but it always use to get first 25, but we removed it, but now in paging it sometime repeat the vendor, is there a way to get random 25 vendor and also keep the paging.
Regards
you can randomize your result but everytime you dot he query, it will create new random list so unless you randomize and save the randomized state in your Code and page over it, it cant be done straightforward way.
refer, SQL Query results pagination with random Order by in SQL Server 2008
I believe this requirement is impossible to implement if a new random order is needed every time, there needs to be good performance and every item should have equal chance to get selected. I believe you should redesign the way your application works.
One possible workaround is to have a couple of columns in a table and fill them with random numbers. When a user requests the list assign the random column to him (stick it in the URL for example). Then do an order by that column and display the results. Randomly switch 4-5 columns to create the appearance of randomness. Update the random numbers in the columns once a day.

Resources