There are hourly data is captured in my table. I want to sum of these data for a day. For example
this is my table data
| date | value |
| 12.03.2010 01.pm | 10 |
| 12.03.2010 02.pm | 20 |
| 12.03.2010 03.pm | 15 |
| 13.03.2010 04.pm | 15 |
| 13.03.2010 05.pm | 25 |
| 13.03.2010 08.pm | 35 |
And I want to get sum of values in a day.
12.03.2010 total_usage = 45
12.03.2010 total_usage = 75
How Can I do this. How can I get sum of hourly data in a day.
Thanks.
I dont say write codes for me
You can do it through Linq using GroupBy and Sum().
myCollection.GroupBy(x => x.date.Date).Select(x => new { Date= x.Key, Total = x.Sum(y => y.value) });
Related
So basically I have a table like the following:
+----+-----------+---------+-------+-------+------------+
| ID | ProductID | ShopID | Amount| Price | AddedDate |
+----+-----------+---------+-------+-------+------------+
| 1 | 1 | 1 | 1000 | 220 | 2020-07-01 |
| 2 | 1 | 1 | 1000 | 230 | 2020-07-30 |
| 3 | 1 | 1 | 100 | 21 | 2020-07-10 |
| 4 | 1 | 1 | 100 | 22 | 2020-07-31 |
| 5 | 1 | 2 | 100 | 23 | 2020-07-10 |
| 6 | 1 | 2 | 100 | 20 | 2020-07-12 |
Just finding the lowest price for a product is pretty easy with
lowestUnitPrice = product.Sales.Min(s => s.Price / s.Amount)
same for the highest price highestUnitPrice = product.Sales.Max(s => s.Price / s.Amount)
However, as you can see, I'm keeping a price history. As such for determining the lowest latest unit price, I need to use only the latest entry for a given amount. In the table above, it should discard the row with ID 1 as the row with ID 2 has an identical amount, but a newer AddedDate.
I'm trying to find a solution with a single query, but I don't think it's possible. In SQL finding the lowest unit price would look something like this I think, given that table Sales here, are the sales for a specific product.
SELECT MIN(Price / Amount)
FROM productsales
WHERE (ProductID, AddedDate) IN
(SELECT ProductID, max(AddedDate)
FROM productsales
GROUP BY ProductID, ShopID, Amount
)
The expected output from the above table would be 0.20 (row ID 6) for the lowest unit price, 0.23 (row ID 2) for the highest unit price.
SQL demo here: http://sqlfiddle.com/#!9/be74da/2
So one way to do it is
var query = from p1 in products
where
(from p2 in products
group p2 by new { p2.ShopID, p2.Amount } into g
select (g.Max(p2 => p2.AddedDate))
).Contains(p1.AddedDate)
select (p1.Price / p1.Amount);
decimal lowestPrice = query.Min();
decimal highestPrice = query.Max();
Don't know if it can be done shorter or more efficiently?
Demo to be found here: https://dotnetfiddle.net/MRJzHs
I have a GroupBy statement that I would like to add to. I have two tables that I am pulling from like so:
MU_Reports Table
Date | Shift | Machine | MU
1/12/2016 | 1 | 12 | 44%
1/12/2016 | 2 | 12 | 34%
1/12/2016 | 2 | 12 | 34%
1/12/2016 | 3 | 12 | 54%
1/12/2016 | 3 | 13 | 24%
DownTime_Reports Table
Date | Shift | Machine | DT Code | DT_Hours
1/12/2016 | 1 | 12 | No Work | 3
1/12/2016 | 2 | 12 | No Resin | 2
1/12/2016 | 2 | 12 | No Op. | 4
1/12/2016 | 3 | 12 | No Work | 1
1/12/2016 | 3 | 13 | No Work | 5
1/12/2016 | 3 | 12 | No Work | 5
Example output:
Date | Shift | Machine | MU | No_Work_Hours
1/12/2016 | 1 | 12 | 44% | 3
1/12/2016 | 2 | 12 | 68% | 0
1/12/2016 | 3 | 12 | 54% | 6
1/12/2016 | 3 | 13 | 24% | 5
my current GroupBy statement adds all No Work together in one shift (no matter date or machine):
using (var db = new ProductionContext())
{
var query2 = from b in db.MU_Reports
join downtime in db.Downtime_Reports on new { b.Shift, b.Date, b.Machine_Number } equals new { downtime.Shift, downtime.Date, downtime.Machine_Number }
where downtime.DT_Code.Equals("No Work")
group downtime by new { b.Date, b.Shift, b.Machine, b.MU } into g
select new
{
Date = g.Key.Date,
Shift = g.Key.Shift,
Machine = g.Key.Machine,
MU = g.Key.MU,
NWTotal = g.Sum(x => x.No_Work_Hours)
};
}
As you can see in my example output what I want to do is to add all the No Work in the specific date, shift, and machine at the same time. So in other words I have to look through the Down_Time table to get the correct date then look through those dates to get the right shift then to get the correct machine to then get the right DT reason to get the amount of hours.
EDIT: I updated the code because I attempted to do multiple joins, however, now I do not output anything at all.
There are several ways to produce the desired output. All they require to first group the MU_Reports table by Date, Shift and Machine. Then you can for instance do group join the grouped subquery to the DownTime_Reports table by Date, Shift and Machine and perform the aggregates:
var query =
from rg in db.MU_Reports.GroupBy(r => new { r.Date, r.Shift, r.Machine })
join dt in db.Downtime_Reports
on new { rg.Key.Date, rg.Key.Shift, rg.Key.Machine }
equals new { dt.Date, dt.Shift, dt.Machine }
into dtGroup
select new
{
rg.Key.Date, rg.Key.Shift, rg.Key.Machine,
MU = rg.Sum(r => r.MU),
NWTotal = dtGroup.Where(dt => dt.DT_Code == "No Work").Sum(dt => dt.DT_Hours)
};
I am trying to figure out how to join two respective datarows into single datarow in dataset through Department column Name.
In provided dataset output i want to join Gastroenterology and Medical Gastroen(two datrows) through column name to single datarow (similar to Required Final dataset Output with Merged Rows).
Need Your ideas/help how it can be accomplished in asp.net and/or C#.
DataSet Output
Department Male Visit Female Visit Total Count
---------- ---------- ------------ -----------
Endocrinology 10 20 30
Gastroenterology 15 25 40
General Medicine 25 05 30
Medical Gastroen 30 20 50
Required Final Dataset Output with Merged Rows
Department Male Visit Female Visit Total Count
---------- ---------- ------------ -----------
Endocrinology 10 20 30
Gastroenterology 45 45 90
General Medicine 25 05 30
I think you must use joining for this in your database query. that would be better.
Table A
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Table B.
+-----+---------------------+-------------+--------+
| OID | DATE | ID | AMOUNT |
+-----+---------------------+-------------+--------+
| 102 | 2009-10-08 00:00:00 | 3 | 3000 |
| 100 | 2009-10-08 00:00:00 | 3 | 1500 |
| 101 | 2009-11-20 00:00:00 | 2 | 1560 |
| 103 | 2008-05-20 00:00:00 | 4 | 2060 |
+-----+---------------------+-------------+--------+
SQL QUERY
SQL> SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
INNER JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
Resulted Table:
+----+----------+--------+---------------------+
| ID | NAME | AMOUNT | DATE |
+----+----------+--------+---------------------+
| 3 | kaushik | 3000 | 2009-10-08 00:00:00 |
| 3 | kaushik | 1500 | 2009-10-08 00:00:00 |
| 2 | Khilan | 1560 | 2009-11-20 00:00:00 |
| 4 | Chaitali | 2060 | 2008-05-20 00:00:00 |
+----+----------+--------+---------------------+
I hope it will help you.
You can Do something like this.
DataTable _dataTable = new DataTable();
DataRow _dataRow1 = null;
_dataTable.TableName = "Products";
_dataTable.Columns.Add("ID",typeof(int));
_dataTable.Columns[0].AutoIncrementSeed = 1;
_dataTable.Columns[0].AutoIncrement = true;
_dataTable.Columns.Add("ProductsName");
_dataTable.Columns.Add("Price");
_dataRow1 = _dataTable.NewRow();
_dataRow1["ProductsName"] = "Sony Laptop";
_dataRow1["Price"] = "15000";
_dataTable.Rows.Add(_dataRow1);
DataRow _dataRow2 = null;
_dataRow2 = _dataTable.NewRow();
_dataTable.Rows.Add(_dataRow2);
_dataRow2["ProductsName"] = "LG Laptop";
_dataRow2["Price"] = "15000";
DataSet _dataSet = new DataSet();
_dataSet.Tables.Add(_dataTable);
I'm trying to figure out how to start with this. So each customer should be display bi-weekly starting from the start date. I want the current week to display those that should be display for this week.
Here's my table.
Customers_Id(PK)|First|Last | Address | Phone | Start_Date | Tech_Id (FK) |
----------------+-----+------+--------------+---------+-------------+---------------+
1 | Bob |Smith | 123 Fake St. | 3298492 | 8/4/2010 | 1 |
2 | John|Man | 123 Noe St. | 2930482 | 4/15/2008 | 1 |
3 | Tom |Lee | 123 Polk St. | 9308523 | 6/21/2012 | 2 |
Hopefully this is clear enough.
in mysql, you can do something like:
SELECT * FROM Customers WHERE WEEK(NOW())=WEEK(Start_Date)
This is the answer I have.
SELECT *
FROM Customers
Where DATEDIFF (ww, Start_Da, GETDATE())%2 = 0
I have a question on mind for a few long days.
Finally, I made a SQL query, which I want to show in MVC4 View.
I build following SQL query:
select distinct date, max(priority) from Timetables where date between '2013-12-01' and '2013-12-31' group by date
which returns me a collection of dates and max priorities of that dates. That is OK for me. I am totaly okay, when this query returns me a whole Timetables record.
But I need to pass result of this query to View engine of MVC4.
I had some tries, but I am nowhere near finding a result for that.
If you have some other possibilities, how to do that, I am eager to hear :)
Also, I am using an Entity Framework.
Thanks!
e:
The database looks like that:
id | doctor_id | nurse_id | date | start_time | end_time |time_for_pacient| priority |comments
-----------------------------------------------------------------------------------------
1 | 5 | 4 | 13-12-01| 07:00 | 11:30 | 00:20 | 1 |Normal
2 | 5 | 4 | 13-12-02| 07:00 | 11:30 | 00:20 | 1 |Normal
3 | 5 | 4 | 13-12-01| 08:00 | 10:30 | 00:20 | 2 |Shorten
4 | 5 | 4 | 13-12-02| 06:00 | 10:30 | 00:10 | 3 |Extra
the result I want to achieve in View:
id | doctor_id | nurse_id | date | start_time | end_time |time_for_pacient| priority |comments
-----------------------------------------------------------------------------------------------------
3 | 5 | 4 | 13-12-01| 08:00 | 10:30 | 00:20 | 2 |Shorten
4 | 5 | 4 | 13-12-02| 06:00 | 10:30 | 00:10 | 3 |Extra
I just want to show a single record for each day with highest priority.
With EntityFramework and other ORM tools, don't think of selecting specific fields or columns. Rather, you're selecting specific obejcts.
To translate your exact query to LINQ would actually end up returning a collection of anonymous objects which is rarely what you want.
In C#, it might look like this:
var date1 = new Date(2013, 12, 1);
var date2 = new Date(2013, 12, 31);
var timetables = DbContext.Timetables
.Where(x => x.Date.CompareTo(date1) >= 0 && x.Date.CompareTo(date2) <= 0)
.Distinct();