C# Visual Studio 2008 - SQLite Update loop - c#

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.

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

Working with duration display days without decreasing for more than 31

I want column format to display duration as number of days as whole.
Ex:
ID | StartDate | EndDate | Days (=C-B) | Days (formatted: d)
1 | 2019-9-28 | 2019-10-23 | 25 | 25
2 | 2019-9-23 | 2019-10-23 | 30 | 30
3 | 2019-9-15 | 2019-10-23 | 38 | 7
ID = 3 doesn't display 38 days as formatted. I want column format in MS Excel so that I can display 38 as whole.

Merge 2 rows into one row in SQL

I am trying to merge 2 rows in 1 in SQL Server.
This is what I have:
+---------------+-------+--------+
| Part Number | Code | value |
+---------------+-------+--------+
| 1 | 00 | 12 |
| 1 | 01 | 21 |
+---------------+-------+--------+
This is what I want :
+---------------+-------+--------+
| Part Number | Code | value |
+---------------+-------+--------+
| 1 | 00 | 33 |
I want that remain only the code 00.
You can try something like:
SELECT [Part Number], MIN(Code), SUM(value)
FROM myTable
GROUP BY [Part Number]
use GROUP BY clause
you can try
SELECT PartNumber, MIN(Code), SUM(value) FROM parts GROUP BY PartNumber
#EmanueleVerderame, This is a mock up below, to show you how to prevent errors.
It seems some column, either Code or Value has NULL/Blank values.
DECLARE #parts TABLE (PartNumber INT, Code Char(2), Value INT)
INSERT INTO #parts
SELECT 1,'00', 12 UNION ALL
SELECT 1, '01', 21 UNION ALL
SELECT 2, NULL, 5
--+---------------+-------+--------+
--| Part Number | Code | value |
--+---------------+-------+--------+
--| 1 | 00 | 12 |
--| 1 | 01 | 21 |
--| 2 | | 5 |
--+---------------+-------+--------+
SELECT PartNumber, MIN(isnull(Code, '00') ), SUM(isnull(value, 0)) FROM
#parts
GROUP BY PartNumber
If you want to display only first result then you should use where rownum = 1

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

Grouping rows for calculation using LINQ to SQL

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

Categories