Looking up through multiple points of similarity - c#

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)
};

Related

How to simplify query from excel to sqlite?

How to achieved the same result in sqlite?
In excel I have this which count the headcount base on assigned letter code per day:
Here's my excel formula: =SUMIF(G$2:G$5,E9,$E$2:$E$5)
In my Sqlite database I have 3 tables:
TABLE 1
+----------------+---------------+--------+
| ID | status_name | status_code | status |
+----+-------------+-------------+--------+
| 1 | Available | A | true |
+------------------+-------------+--------+
| 2 | HalfDay | H | true |
+------------------+-------------+--------+
| 3 | On Leave | OL | true |
+------------------+-------------+--------+
| 4 | Restday | R | true |
+------------------+-------------+--------+
| 5 | Vacation | V | true |
+------------------+-------------+--------+
TABLE 2
+--------------+-------+-------+------+----------+
| EmployeeName | Site | Shift | Team | JobTitle |
+--------------+-------+-------+------+----------+
| Steve | Bldg1 | Night | N1 | Doctor |
+--------------+-------+-------+------+----------+
| Dave | Bldg1 | Night | N2 | Nurse |
+--------------+-------+-------+------+----------+
| Jack | Bldg1 | Night | N2 | Nurse |
+--------------+-------+-------+------+----------+
| Jacob | Bldg2 | Day | D1 | Doctor |
+--------------+-------+-------+------+----------+
| Noah | Bldg2 | Day | D2 | Nurse |
+--------------+-------+-------+------+----------+
| MAX | Bldg2 | Day | D2 | Nurse |
+--------------+-------+-------+------+----------+
TABLE 3
+----------+-------+-------+------+-----+-----+-----+-----+-----+-----+-----+
| JobsType | Site | Shift | Team | SUN | MON | TUE | WED | THU | FRI | SAT |
+----------+-------+-------+------+-----+-----+-----+-----+-----+-----+-----+
| Doctor | Bldg1 | Night | N1 | A | H | A | A | OL | A | A |
+----------+-------+-------+------+-----+-----+-----+-----+-----+-----+-----+
| Nurse | Bldg1 | Night | N2 | A | H | H | A | A | A | A |
+----------+-------+-------+------+-----+-----+-----+-----+-----+-----+-----+
| Doctor | Bldg2 | Day | D1 | H | A | H | H | A | A | OL |
+----------+-------+-------+------+-----+-----+-----+-----+-----+-----+-----+
| Nurse | Bldg1 | Night | N2 | A | H | H | A | A | A | A |
+----------+-------+-------+------+-----+-----+-----+-----+-----+-----+-----+
By using the 3 tables above how could I achieved this result in query?
+--------------+-----+-----+-----+-----+-----+-----+-----+
| STATUS TYPES | SUN | MON | TUE | WED | THU | FRI | SAT |
+--------------+-----+-----+-----+-----+-----+-----+-----+
| Available | 5 | 4 | 4 | 5 | 5 | 6 | 5 |
+--------------+-----+-----+-----+-----+-----+-----+-----+
| HalfDay | 1 | 5 | 5 | 1 | 0 | 0 | 0 |
+--------------+-----+-----+-----+-----+-----+-----+-----+
| On Leave | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
+--------------+-----+-----+-----+-----+-----+-----+-----+
| Restday | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+--------------+-----+-----+-----+-----+-----+-----+-----+
| Vacation | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+--------------+-----+-----+-----+-----+-----+-----+-----+
So far I've come this;
SELECT DISTINCT M.status_name, M.status_code, A.SUN, count(*),count(*) FROM TABLE3 M LEFT join TABLE2 A ON M.status_code= A.SUN LEFT join TABLE2 B ON (A.Team = B.Team AND A.Shift = B.Shift AND A.Site = B.Site)
WHERE M.product_status = 1
GROUP BY M.status_name;
First step, generate a test database (With a couple of helpful indexes):
CREATE TABLE status(id INTEGER PRIMARY KEY, status_name TEXT, status_code TEXT, status TEXT);
INSERT INTO status VALUES(1,'Available','A','true');
INSERT INTO status VALUES(2,'HalfDay','H','true');
INSERT INTO status VALUES(3,'On Leave','OL','true');
INSERT INTO status VALUES(4,'Restday','R','true');
INSERT INTO status VALUES(5,'Vacation','V','true');
CREATE TABLE employees(EmployeeName TEXT, Site TEXT, Shift TEXT, Team TEXT, JobTitle TEXT);
INSERT INTO employees VALUES('Steve','Bldg1','Night','N1','Doctor');
INSERT INTO employees VALUES('Dave','Bldg1','Night','N2','Nurse');
INSERT INTO employees VALUES('Jack','Bldg1','Night','N2','Nurse');
INSERT INTO employees VALUES('Jacob','Bldg2','Day','D1','Doctor');
INSERT INTO employees VALUES('Noah','Bldg2','Day','D2','Nurse');
INSERT INTO employees VALUES('MAX','Bldg2','Day','D2','Nurse');
CREATE TABLE schedule(JobsType TEXT, Site TEXT, Shift TEXT, Team TEXT, SUN TEXT, MON TEXT, TUE TEXT, WED TEXT, THU TEXT, FRI TEXT, SAT TEXT);
INSERT INTO schedule VALUES('Doctor','Bldg1','Night','N1','A','H','A','A','OL','A','A');
INSERT INTO schedule VALUES('Nurse','Bldg1','Night','N2','A','H','H','A','A','A','A');
INSERT INTO schedule VALUES('Doctor','Bldg2','Day','D1','H','A','H','H','A','A','OL');
INSERT INTO schedule VALUES('Nurse','Bldg1','Night','N2','A','H','H','A','A','A','A');
CREATE INDEX employees_idx ON employees(JobTitle, Site, Shift, Team);
CREATE INDEX status_idx_code ON status(status_code);
This query:
SELECT st.status_name, st.status_code
, sum(sc.SUN = st.status_code) AS SUN
, sum(sc.MON = st.status_code) AS MON
, sum(sc.TUE = st.status_code) AS TUE
, sum(sc.WED = st.status_code) AS WED
, sum(sc.THU = st.status_code) AS THU
, sum(sc.FRI = st.status_code) AS FRI
, sum(sc.SAT = st.status_code) AS SAT
FROM status AS st
JOIN schedule AS sc ON st.status_code IN (sc.SUN, sc.MON, sc.TUE, sc.WED
, sc.THU, sc.FRI, sc.SAT)
JOIN employees AS e ON sc.JobsType = e.JobTitle AND sc.Site = e.Site
AND sc.Shift = e.Shift AND sc.Team = e.Team
GROUP BY st.status_name, st.status_code
ORDER BY st.status_name, st.status_code;
will produce
status_name status_code SUN MON TUE WED THU FRI SAT
----------- ----------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
Available A 5 1 1 5 5 6 5
HalfDay H 1 5 5 1 0 0 0
On Leave OL 0 0 0 0 1 0 1
How it works:
For each row in the status table, join each row in the schedule table that has that status for at least one day, and then join all rows from the employee table that match each schedule row's type of job (With this sample data, that results in a total of 14 rows being generated). Then group all the rows on the status, and for each day, add up the number of rows in that group where the schedule code for that day matches the status.

How to join Join two DataRows into a single DataRow through Column Name?

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);

Insert DataTable into nested TreeView c#

I got a database like
CID | CName | CParent
--------+---------+---------
1 | A | 0
2 | b | 1
3 | c | 1
4 | d | 1
5 | e | 2
6 | f | 3
7 | g | 6
8 | h | 2
9 | i | 8
I want to represent this data into a nested treeview connecting each entity to its father which is the CParent.
What is the best way to get this treeview and how?

merge 2 different datatable using DataRelation

I tried to merge 2 datatables like this. But my tables have different schema and different amount of rows. So I get an error here:
DataRelation drel = new DataRelation("EquiJoin",cr1, cr2, true);
dataSet.Relations.Add(drel);//error
As far as I understand, because first table has much more rows than second table.
Error: Cannot evaluate expression because a native frame is on top of the call stack.
Tables look like:
snowFlake: snow:
f_text | f_link_id | f_tabkey | | f_text | f_link_id | f_tabkey |
--------+---------------+-------------| |---------+---------------+-------------|
row1 | 100001 | 1 | | - | 100000 | 1 |
row2 | 100001 | 2 | | + | 100001 | 1 |
row3 | 100001 | 3 | | - | 100001 | 2 |
row4 | 100002 | 1 | | + | 100001 | 3 |
row5 | 100003 | 1 | | + | 100002 | 1 |
| + | 100003 | 1 |
| - | 100003 | 1 |
| + | 100004 | 1 |
| - | 100005 | 1 |
Primary keys that I use:
snowFlake.PrimaryKey = new DataColumn [] { snowFlake.Columns[IndexesField.F_LINK_ID], snowFlake.Columns[IndexesField.F_TABKEY] };
snow.PrimaryKey = new DataColumn [] { snow.Columns[IndexesField.F_LINK_ID], snow.Columns[IndexesField.F_TABKEY] };
DataColumn[] cr1 = { snowFlake.Columns[IndexesField.F_LINK_ID], snowFlake.Columns[IndexesField.F_TABKEY] };
DataColumn[] cr2 = { snow.Columns[IndexesField.F_LINK_ID], snow.Columns[IndexesField.F_TABKEY] };
How can I solve it?
Regards, Alexander.
Doesn't matter if they do not have same number of rows, because they are joined over a relation thus mimicking TSQL's Inner Join behavior. Without telling us the specific error, one can only assume that you haven't properly defined keys for joining (for example, the columns you specified in the relation don't exist).

Linq The sum of hourly data?

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) });

Categories

Resources