Grouping rows for calculation using LINQ to SQL - c#

I have a c# program that I am programing where someone inputs production for the whole day like this:
Date | Part Number | Mold Num | Machine Num | Total Parts |Cycle
2/12/2016 | 1185-5B8 | 6580 | 12 | 220 | 56
2/12/2016 | 2249300 | 7797 | 36 | 600 | 13
2/12/2016 | 146865 | 5096789 | 12 | 500 | 15
2/16/2016 | 123456 | 7787 | 56 | 300 | 34
2/16/2016 | 123456 | 787 | 54 | 360 | 36
2/16/2016 | 123456 | 777 | 56 | 500 | 46
2/16/2016 | 123456 | 87 | 54 | 400 | 44
I then calculate a machine usage (MU) for each entry 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
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 I want. 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 have looked for ways of doing this but they either did not make sense or not quite what I was looking for. Please keep in mind that I am very new to c# and LINQ to SQL

Related

How to list the products under each category in sql? side by side

I'm a bit of a beginner in sql and I need your help.
I'm sorry that I don't know if the question is correct.
now the code gives this;
+---------+----------+-------------+------------+
| id | CompanyId| DealId | price |
+---------+----------+-------------+------------+
| 1 | 1 | 1 | 100 |
| 2 | 1 | 2 | 50 |
| 3 | 1 | 3 | 25 |
| 4 | 2 | 1 | 1000 |
| 5 | 2 | 2 | 2000 |
| 6 | 2 | 3 | 2500 |
+---------+----------+-------------+------------+
but this is what i want;
+---------+----------+-------------+------------+------------+--+
| id | companyId| DealName1 | DealName2 | DealName3 | |
+---------+----------+-------------+------------+------------+--+
| 1 | 1 | 100 | 50 | 25 | |
| 2 | 2 | 1000 | 2000 | 2500 | |
| 3 | 3 | value | value | value | |
| 4 | 4 | value | value | value | |
+---------+----------+-------------+------------+------------+--+
select CompanyId
,[1] as DealName1
,[2] as DealName2
,[3] as DealName3
from (select CompanyId, DealId, price from t) t
pivot (sum(price) for DealId in([1],[2],[3])) p
CompanyId
DealName1
DealName2
DealName3
1
100
50
25
2
1000
2000
2500
Fiddle

Aggregate multiple columns in DataTable and put result to new DataTable

Hi i have a DataTable with following data formating. I am trying to aggregate in C# on condition. I am a Newbie to Linq.
Profile | Nr | Result|
------------------------
A | 1 | OK |
A | 2 | NOK |
A | 2 | OK |
A | 3 | OK |
A | 4 | OK |
A | 5 | OK |
BB | 1 | OK |
BB | 2 | NOK |
BB | 2 | NOK |
BB | 2 | OK |
BB | 3 | OK |
BB | 4 | OK |
BB | 5 | OK |
I Want to aggregate by first columnt (Profile) and count rows where the Result is OK.
The expected result should be a new DataTable and should look like this.
Profile | Count|
----------------
A | 5 |
BB | 5 |
How can i do this in C# ?

SQL Get matching values from other table and show in main table

I'm trying to do some SQL in C# along with an Access Database. I have two tables. Main Table and a Second Table:
MAIN TABLE
DoubleValue | | | |
----------------------------------------
1,40 | | | |
1,80 | | | |
2,00 | | | |
1,80 | | | |
1,60 | | | |
1,60 | | | |
----------------------------------------
SECOND TABLE
DoubleValue | Points | Weight |
-------------------------------
1,00 | 100 | 2 |
1,20 | 98 | 2 |
1,40 | 96 | 2 |
1,60 | 94 | 2 |
1,80 | 92 | 2 |
2,00 | 90 | 2 |
-------------------------------
I need to find all matching rows in "SECOND TABLE" based on the column "Double Value". Then for the rows that matches I want to get the value in columns "Points" and "Weight" as well as multplie those two columns and create a columns with the name "Sum" and add all three columns to the "MAIN TABLE":
MAIN TABLE - RESULT/OUTPUT
DoubleValue | Points | Weight | Sum |
-------------------------------------
1,40 | 96 | 2 | 192 |
1,80 | 92 | 2 | 184 |
2,00 | 90 | 2 | 180 |
1,80 | 92 | 2 | 184 |
1,60 | 94 | 2 | 188 |
1,60 | 94 | 2 | 188 |
-------------------------------------
The "MAIN TABLE" doesn't need to actually have the new columns "physically" inserted. I would very much prefer if they could just be displayed in the output very much like "SELECT Points * Weight AS Sum" would produce where "Sum" would be displayed but not actually inserted in the table. BUT OK, if it needs to actually be inserted then I will go with that.
How can this be done?
You are looking for a simpler INNER JOIN statement. Please notice that Sum is wrapped as it is a Reserved word in most SQL variants. Please try to avoid naming items with these words.
SELECT m.DoubleValue
, s.Points
, s.Weight
, [Sum] = s.Points * s.Weight
FROM MainTable AS m
INNER JOIN SecondTable AS s ON m.DoubleValue = s.DoubleValue

Joining two tables in Microsoft Report Viewer (SSRS 2012)

I have two database tables
Invoice Table (PK InvoiceNo)
InvoiceDate | InvoiceNo | Name | Class | AmountPaid
2014-6-5 | B001 | ABC | E1 | 1500.00
2014-6-5 | B002 | BCD | E1 | 2000.00
2014-6-5 | B003 | CDE | E3 | 1000.00
2014-6-5 | B004 | ABC | E3 | 3000.00
2014-6-6 | B005 | BCD | E2 | 5000.00
2014-6-6 | B006 | CCD | E1 | 2000.00
Expences Table (PK Date,Description)
Date | Description | Cost
2014-6-5 | Lunch | 150.00
2014-6-5 | SoftDrink | 50.00
2014-6-6 | BusFair | 10.00
I want to create a report using these two tables. What I expect is
InvoiceDate | InvoiceNo | Name | Class | AmountPaid | Description | Cost
2014-6-5 | B001 | ABC | E1 | 1500.00 | Lunch | 150.00
| B002 | BCD | E1 | 2000.00 | SoftDrink | 50.00
| B003 | CDE | E3 | 1000.00 |
| B004 | ABC | E3 | 3000.00 |
2014-6-6 | B005 | BCD | E2 | 5000.00 | BusFair | 10.00
| B006 | CCD | E1 | 2000.00 |
But my output looks like this
InvoiceDate | InvoiceNo | Name | Class | AmountPaid | Description | Cost
2014-6-5 | B001 | ABC | E1 | 1500.00 | Lunch | 150.00
| B002 | BCD | E1 | 2000.00 | Lunch | 150.00
| B003 | CDE | E3 | 1000.00 | Lunch | 150.00
| B004 | ABC | E3 | 3000.00 | Lunch | 150.00
| B001 | ABC | E1 | 1500.00 | SoftDrink | 50.00
| B002 | BCD | E1 | 2000.00 | SoftDrink | 50.00
| B003 | CDE | E3 | 1000.00 | SoftDrink | 50.00
| B004 | ABC | E3 | 3000.00 | SoftDrink | 50.00
2014-6-6 | B005 | BCD | E2 | 5000.00 | BusFair | 10.00
| B006 | CCD | E1 | 2000.00 | BusFair | 10.00
Please help me to resolve this problem...
You're running into 2 problems here:
You need to add an SSRS Group for both InvoiceDate and Description, and configure the Description field on the table in the same manner that you seem to have already done for InvoiceDate (so it only appears on the group row, not in the details).
Your "Expences" table should NOT be using Date + Description as a PK. It should have its own "ExpenceID" type field, and an "InvoiceNO" FK back to your "Invoice" table. Without proper keys, joining only on "Date" is going to cause a cartesian.
Note that #2 is more critical than #1 - until you fix your data issue, you're going to have a bad time.

C# Visual Studio 2008 - SQLite Update loop

I'm using C# and quite new to SQLite .
I need to update the record field "Value1" by dividing it by a given denominator for a Certain User and date range (First record date to a spicific Date eg. 2012-03-01).
So I need to loop through the data set and if User=John and Date <= than 2012-03-01 then divide Value1 by lets say 10
This obviously can't be done in SQL alone as I need to know what Value1 is, so that I can devided it. How would I go about doing this ?
Below is a example of what data I need to modify
User | Date | Value1 | Value2 | Value3
----------------------------------------------
John | 2012-01-01 | *50 | 5 | 80
John | 2012-02-01 | *100 | 78 | 60
John | 2012-03-01 | *10 | 100 | 5
John | 2012-04-01 | 5 | 89 | 55
John | 2012-05-01 | 15 | 50 | 44
Max | 2012-01-01 | 40 | 25 | 90
Max | 2012-02-01 | 99 | 79 | 30
Max | 2012-03-01 | 100 | 110 | 59
Max | 2012-04-01 | 25 | 29 | 85
Max | 2012-05-01 | 15 | 30 | 88
Modified data divided by 10
User | Date | Value1 | Value2 | Value3
----------------------------------------------
John | 2012-01-01 | *5 | 5 | 80
John | 2012-02-01 | *10 | 78 | 60
John | 2012-03-01 | *1 | 100 | 5
John | 2012-04-01 | 5 | 89 | 55
John | 2012-05-01 | 15 | 50 | 44
Max | 2012-01-01 | 40 | 25 | 90
Max | 2012-02-01 | 99 | 79 | 30
Max | 2012-03-01 | 100 | 110 | 59
Max | 2012-04-01 | 25 | 29 | 85
Max | 2012-05-01 | 15 | 30 | 88
If Value1 was constant then I could use the code below. But it is'nt and it will update 2012-01-01 to 2012-03-01 Value1 will all be 5.
Value1 = 50
Val = Value1 / 10
sql_cmd = sql_con.CreateCommand();
sql_cmd.CommandText = "UPDATE Tablename SET Value1= '"+Val+"' WHERE User = 'John' and Date <= '2012-03-01'";
sql_cmd.ExecuteNonQuery();
You really need to brush up on your SQL syntax, but generally, if you want to use fileds in the current row in an UPDATE statement, you can just put the equation (or string function or whatever is appropriate) in the UPDATE statement itself.
So, for your example, to divide Value1 by 10 for a particular row (or set of rows):
UPDATE Tablename SET Value1 = Value1/10 WHERE User = 'John' and Date <= '2012-03-01'
From your latest comment above, if you need additional conditions to select different rows, put those in the WHERE clause:
UPDATE Tablename SET Value1 = 1 WHERE User = 'John' and Date <= '2012-03-01' and Value1 = 10
So, maybe do some reading about SQL syntax generally (it's a big subject) and then repost any specific questions about syntax.

Categories