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();
Related
I was wondering if what is this called or is this even possible in MySql, or what should I do to achieve this. This is what Mysql Table Looks Like:
Desired Output:
you can use a query like this:
SELECT customerid
, CONCAT( SUM(CAST(item as INTEGER)), 'pcs,', SUBSTRING_INDEX(item, ',', -1) ) as item
FROM myitems
GROUP BY SUBSTRING_INDEX(item, ',', -1);
Sample
MariaDB [bernd]> SELECT * FROM myitems;
+----+------------+---------------+
| id | customerid | item |
+----+------------+---------------+
| 1 | 15 | 7pcs, Car |
| 2 | 15 | 2pcs, Car |
| 3 | 15 | 3pcs, Engine |
| 4 | 15 | 2pcs, Engine |
| 5 | 15 | 5pcs, Exhaust |
+----+------------+---------------+
5 rows in set (0.07 sec)
MariaDB [bernd]> SELECT customerid
-> , CONCAT( SUM(CAST(item as INTEGER)), 'pcs,', SUBSTRING_INDEX(item, ',', -1) ) as item
-> FROM myitems
-> GROUP BY SUBSTRING_INDEX(item, ',', -1);
+------------+---------------+
| customerid | item |
+------------+---------------+
| 15 | 9pcs, Car |
| 15 | 5pcs, Engine |
| 15 | 5pcs, Exhaust |
+------------+---------------+
3 rows in set, 5 warnings (0.02 sec)
MariaDB [bernd]>
Your table structures wont allow (anything near as easy). A better format of your table might be more like
id customerid item qty
1 15 Car 7
2 15 Car 2
3 15 Engine 3
4 15 Engine 2
5 15 Exhaust 5
Then, you could get what you are looking for easily such as
select
t.customerid,
t.item,
sum( t.qty )
from
YourTable t
group by
t.customerid,
t.item
This WOULD give your your
customerid item qty
15 Car 9
15 Engine 5
15 Exhaust 5
Having a properly formatted table vs trying to have a description requires parsing numeric values vs rest of description is never a good solution such as what you have here. Too many opportunities for bad answers.
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'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
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) });
I'm using MySQL.
This is table name item_supplier
supplier_ID Item_ID Date Price QTY
1 1 2012-01-01 00:00:00 500.00 2
1 1 2012-01-03 00:00:00 450.00 10
2 1 2012-01-01 00:00:00 400.00 5
3 1 2012-05-01 00:00:00 500.00 1
I need a select query showing a table something like this.
supplier_ID 2012-01-01 2012-01-03 2012-05-01
1 500.00(2) 450.00(10) null
2 400.00(5) null null
3 null null 500.00(1)
or, at least,
supplier_ID 2012-01-01 2012-01-03 2012-05-01
1 500.00 450.00 null
2 400.00 null null
3 null null 500.00
I hope someone can help me on this or give me a hint.
If there aren't a finite number of dates that are known beforehand, then you can't do what you want in MySQL alone.
Your best bet is to get a table like:
+---------+------------+-------------+-------------+
| Item_ID | Date | supplier_ID | price |
+---------+------------+-------------+-------------+
| 1 | 2012-01-01 | 1 | 500.00 (2) |
| 1 | 2012-01-01 | 2 | 400.00 (5) |
| 1 | 2012-01-03 | 1 | 450.00 (10) |
| 1 | 2012-05-01 | 3 | 500.00 (1) |
| ... | ... | ... | ..... |
Which can be done with:
SELECT Item_ID,Date,supplier_ID,CONCAT(FORMAT(Price,2),' (',QTY,')') AS price
FROM item_supplier
ORDER BY Item_ID,Date,supplier_ID;
Then on the C# side, loop through the results and print your desired output.
Since the output is now sorted by Item_ID, Date, and then supplier_ID, it's simple to loop through the results and then output in the format you want.
First of My SQL does not support to Crosstab/Pivot Query. So you need to create Dynamic temp Table for Columns and then inset record into it.
Like, First you have to fetch all date in one cursor and then create temp table and insert columns based on date's Cursor . After Creating table create another cursor for inserting rows. and fetch every row and update temp table.I have also done this using this way....
If you have any query please contact.