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!
Related
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 am designing a database table that will hold the column headers of many differently-formatted Excel files. I need to do this because I ultimately need to know the "format" of the Excel files that will be created dynamically upon the user needing certain reports.
I am wondering if there is a common practice/pattern on doing this, i.e. what format is best to essentially store a Dictionary<key, value> in a database? Maybe XML? Individual rows with a 2-column design (index, value)? Or how do I story a Dictionary or List in a database?
Say my Excel file looks like this:
| FirstName | LastName | PhoneNo |
I need to store the three cells, i.e. their corresponding index and value, e.g. [0, "FirstName"], [1, "LastName"], [2, "PhoneNo"].
I am thinking this can be stored in my Business Object as a List<int, string> ColumnHeaders, but am not sure how best to store this in a database (SQL Server) since when working with List<> objects in the past, they usually correspond to rows in the database and it does not make sense (at the moment at least) to store all of these column headers in each row, i.e. something like this:
ID | ProjectID | ColIndex | ColValue
1 | 32 | 0 | FirstName
2 | 32 | 1 | LastName
3 | 32 | 2 | PhoneNo
4 | 54 | 0 | Name
5 | 54 | 1 | City
6 | 54 | 2 | State
7 | 54 | 3 | Country
Any suggestions/tips?
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
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.