There is a horrible legacy tool at my workplace that has been updated/patched to output information to an SQLite database. No one wants to touch the back end of the system, and I don't know enough to dig into it - not my problem. Here is my problem however:
I have an SQLite database, with a number of tables in it. Each table represents a day and contains all of the bus runs, listed such as such:
| Direction | Time | RunNumber | StopIndex |
----------------------------------------------
| In | 6:30 | 1 | 1 |
| In | 6:35 | 1 | 2 |
| In | 6:40 | 1 | 3 |
| In | 6:45 | 1 | 4 |
| In | 7:30 | 2 | 1 |
| In | 7:35 | 2 | 2 |
| In | 7:40 | 2 | 3 |
| In | 7:45 | 2 | 4 |
| Out | 6:40 | 1 | 1 |
| Out | 6:45 | 1 | 2 |
| Out | 6:50 | 1 | 3 |
| Out | 6:55 | 1 | 4 |
Some days can have more run's than others. The numbers of stops will be the same across all tables.
The definition of equal is that both direction and time are the same. Each run is treated as a set - if one of the times for a stop differs, then the entire run needs to be merged into a table.
My question is:
What would be the easiest way, given a list of tables, to generate another table that contains values that were not present on all the given tables? This can be done on the SQLite side, or by C# .NET 4.0.
In reality, I only need to know the run number that differs, so I can later extract it and put it into an Excel Spreadsheet, if that makes it any easier.
Try using EXCEPT. For example, the following query will list all rows that exist in Table1 but do not exist in Table2:
SELECT Direction, Time, RunNumber, StopIndex
FROM Table1
EXCEPT
SELECT Direction, Time, RunNumber, StopIndex
FROM Table2
Related
I just followed this tutorial on loading json from a WebAPI into MVC: https://www.c-sharpcorner.com/article/consuming-asp-net-web-api-rest-service-in-asp-net-mvc-using-http-client/
This all worked, but it only displays the data in the index page.
Im also using a DbContext for loading data directly from a database. The advantage of DbContexts is that the data gets stored in a DbSet which I can call at any time to get data.
I would like the same for the WebAPI data.
I have 2 endpoints containing information from 2 tables.
Items
-----------------------------------------------
| Id | Name | Material |
-----------------------------------------------
| 1004 | Chain | Gold |
| 1402 | Vase | Ceramic |
-----------------------------------------------
Retailers
-----------------------------------------------
| Id | Name | City |
-----------------------------------------------
| 3829 | StoreA | New York |
| 1827 | StoreB | London |
-----------------------------------------------
I can retrieve the information with /api/items and /api/retailers.
I currently have a DbContext for the other data, who use repositories to link the data from the DbContext to the Views.
In my c# program I have someone input production for the whole day and I calculate machine usage (MU) like so:
Date | Part Number | Mold Num | Machine Num | MU
2/12/2016 | 1185-5B8 | 6580 | 12 | .428
2/12/2016 | 2249300 | 7797 | 36 | .271
2/12/2016 | 146865 | 5096789 | 12 | .260
2/16/2016 | 123456 | 7787 | 56 | .354
2/16/2016 | 123456 | 787 | 54 | .45
2/16/2016 | 123456 | 777 | 56 | .799
2/16/2016 | 123456 | 87 | 54 | .611
All of this data is in my SQL server and what I want to do is to make something like a pivot table and it takes all similar dates/Mold Numbers/Machine Numbers and makes an average the MU and display it in any way that the user wants. Example:
Date | MU
2/12/2016 | 32.0%
2/16/2016 | 55.4%
or
Machine Num. | MU
12 | 34.4%
36 | 27.1%
54 | 53.0%
56 | 57.6%
etc. Basically I want it to be variable and to show whatever the person that is looking at it needs. I want to keep it in my c# program but I can use LINQ to SQL. Please keep in mind that I am very new to c# and LINQ to SQL.
I did try to do this but it was not exactly what I wanted to do. I also could not figure out how I was going to display it on the windows form nor how to change what was in each column.
It seems you need to build pivot table with row dimension (Date, Machine Num), without column dimension and with average by MU.
You can easily do that with help of NReco PivotData library:
var pivotData = new PivotData(
new string[] {"Date","Machine Num"},
new AverageAggregatorFactory("MU"),
new DataTableReader(t) ); // just a sample - you can use DB data reader
var pivotTable = new PivotTable(
new []{"Date"}, // row dimension(s)
new [0], // column dimension(s)
pivotData );
// use pivotTable.RowKeys and indexer for accessing pivot table values
PivotData library can be used for free (I'm an author), and you can download examples package on the component's page. Also you may check advanced PivotData Toolkit components (for rendering pivot table to HTML/CSV/Excel/PDF, aggregating data on DB level with GROUP BY and many others), but they are not free.
According to your description, what you want isn't a pivot table, as already mentioned by #juharr.
For sqlite, this sould be accomplished with the following statement. (I assume the table name is tmp, and what you want is the sum of MU with each MachineNUm, naming it with SMU).
SELECT MachineNum,SUM(MU) AS SMU From tmp GROUP BY MachineNum ORDER BY MachineNum;
Hope it helps.
I have searched and tried various code methods for days now, to accomplish what I need and I am almost convinced that no on has done this yet. Please help someone (Not to sound desperate, but I will name my next child after you)...
I have a grid I bind to a typical SQL query.
And it displays like this:
MonthYearText | BegInventory | Refunds | BrokenItems | EndingInventory | etc |
2013-01 | 120 | 10 | 5 | 125 | 22 |
2013-02 | 100 | 5 | 2 | 105 | 11 |
2013-03 | 66 | 2 | 1 | 67 | 5 |
I need this:
MonthYearText | 2013-01 | 2013-02 | 2013-03 | etc
BegInventory | 120 | 100 | 66 | ##
Refunds | 10 | 5 | 2 | ##
BrokenItems | 5 | 2 | 1 | ##
EndingInventory | 125 | 105 | 67 | ##
etc | 22 | 11 | 5 | ##
Could you post your coding approaches please ? Not sure why you think nobody has ever done this yet.. all you're doing is translating the axes, have you tried doing this at SQL level instead of code ? Would be easier IMO - Efficiently convert rows to columns in sql server
(Assuming you're using SQL Server, apologies if you're using something else but similar techniques are available in Oracle, MySQL, etc)
Thank you "Goat CO" and "sh1rts" for the quick replies. I'm using SQL Server 2008 R2. I am fairly new to SQL code. The problem I have is PIVOT is not a true transpose. It wants to transpose on a column and aggregate that column with a function. I looked at PIVOT, UNPIVOT and the CASE statement solutions and in the end I READ() the query into a C# DataTable() and transposed the data through a method and into a new DataTable().
Ends up being a good solution for what I need. Thanks again guys I appreciate your time!
I want the data in the database to become the column header names by retrieving data from a table. This table has two fields, TimingID and Timing, I want to display the Timing as Column header in the reportviewer. Actually I am creating a schedule which shows the timing as the column so as to slot employees inside their respective timing. I want all the Timing to be displayed as a header. I wanted the Report to look like this.
LocationStation | 0000 | 0100 | 0200 | 0300 | 0400 | 0500 | 0600 | 0700 | 0800 | 0900 | 1000 | 1100 | 1200
T5-PML | John | John | John | John | John | John | John | John | John | John | John | John
So it means that the employee, 'John' works from 00:00 to 12:00. The Timing stores all the time slot as shown at the column header. I want to show it like that.
Please give me some ideas of how to write in SQL statement?
Thanks in advance.
I want to generate pdf file as attached in the picture. i want to harcode the contents shown in the picture into a pdf file on button click.
How do i proceed, using table structure will work?I am unable to form the table stucture here.
Please help me to sort out my problem.i want the output as shown in below image.
Here you have a good tutorial about using iTextSharp
http://www.mikesdotnetting.com/Category/20
First create one table with two columns like below
_______________________________________________
| | |
| | |
| | |
| COL.0.1 | COL.0.2 |
| | |
| | |
-----------------------------------------------
keep reference to its columns, then create table for left side content and insert it as child content of left column COL.0.1
------------------------------
| designation: xxxxxxx | <---- colspan = 2
------------------------------
| Audit No. | xxxx |
------------------------------
| Received on ....... |
-------------------------------
and you can continue for the rest of content similar to this.
The idea is to split content to smaller tables and nest them.