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