I am using ASPMaker. It's a Classic ASP code generator. I am using MySQL database as well:
I am trying to get values from one table to another here's the code used to get the value from table1:
Dim Balance
Balance = ew_ExecuteScalar("SELECT Balance FROM [Balance] " )
ListOptions.GetItem("Balance").Body = Balance
The problem is that I am getting only the last value for all records in the table. For example if the last [Balance] = 700, all the records in the table have [Balance] = 700.
Any idea?
Related
I am working on developing an inventory application. I have a table ShopInventory_Parts
When a user selects an item from the table, a window appears.
From here, the data from the item selected in the table is displayed in their respective fields. The last field is where the user is able to enter in the quantity that they will be "Checking Out" from the inventory.
When the user enters in the quantity checked out, I have a SQL script that will write to another table called ShopInventory_Parts_Checkout - and this is working correctly
My query for this is:
query = "INSERT INTO ShopInventory_Parts_Checkout (Name,Manufacturer,PartNum,assetID,quantityCheckedOut,t_stamp) VALUES (?,?,?,?,?,?)"
args = [Name,Manufacturer,PartNum,assetID,quantityCheckedOut,t_stamp]
result = system.db.runPrepUpdate(query,args)
My question is, how can I get the 'quantity' in my ShopInventory_Parts table to subtract from the quantity based on the user entry?
Right now, I have the following SQL query and it is returning "NULL"
query = "UPDATE ShopInventory_Parts SET quantity = (quantity - updatedQuantity) WHERE assetID=(?)"
args = [assetID]
result = system.db.runPrepUpdate(query,args)
I apologize if this seems very basic, but I just cannot get it working. Any help would be greatly appreciated.
Thank you
The Python variable needs to be in the args list, not the SQL. And pass the quantity being checked out, not the updated quantity (otherwise the two calculations cancel each other out).
query = "UPDATE ShopInventory_Parts SET quantity = quantity - ? WHERE assetID = ?"
args = [quantityCheckedOut, assetID]
result = system.db.runPrepUpdate(query,args)
I've been struggling to build a query that calculate the sum of column called 'TIDAL_VOLUME' with respect to date value that's coming from another table.
Please see the content of the Table_1:
Please see the content of the Table_2:
Note: TIDAL_VOLUME might have NULL as well.
Now, the start time for O2_Device value 'Endotracheal tube' is '2013-08-06 08:10:05' for same HADM_ID and SUBJECT_ID. and end time is whenever new O2_Device value comes in. In this case which is 'Nasal cannula'. Which means start time for 'Endotracheal tube' is '2013-08-06 08:10:05' and end time is '2013-08-06 10:15:05' for HADM_ID = 1 and SUBJECT_ID = 100.
Using that start time and end time criteria, I have to look for TIDAL_VALUE in Table_2. In this example it's 700, 800. Ans for TIDAL_VOLUME is 1500.
Please see the resultant output look like this:
Thanks in advance.
If you can add End_Time to the first table, you can use BETWEEN when you join the tables.
SELECT t1.HADM_ID, t1.Subject_ID, t1.ChartTime, SUM(t2.tidal_volume) AS tidal_volume
FROM Table_1 AS t1
JOIN Table_2 AS t2
ON t1.HADM_ID = t2.HADM_ID
AND t1.Subject_ID = t2.Subject_ID
AND t2.ChartTime BETWEEN t1.ChartTime AND t1.End_Time
GROUP BY t1.HADM_ID, t1.Subject_ID, t1.ChartTime
DISCLAIMER: I'm still new to this website so I'm still learning the etiquette of the site, I apologize for any errors. Also, I previously posted a questions similar to this but some fantastic people recommended I rework my database to the current format. This was a great help however it was one step forward and one step back. I have an improved database but my question now continues to stand with a few minor tweaks.
To elaborate, I'm currently building an app that has the user create an account and login. Their information that they provided is saved into my database. My database contains two tables, one holds the users information, and one holds the users inventory, both are generated upon the completion of a create account GUI. For this question, only the second table is necessary. This table has three columns, the first is the users username, the second is their inventory slot number, and the third is the item id for the item that is in that slot. When the user creates an account, forty rows are created in this table, in each row their username remains constant. However, the slot number increments from one to forty and the item id column defaults to zero. Here is a visual representation:
Now to get to my code, when the user clicks a button, a random method gets called which sets an int variable which is current named "i" to a specific number. This number is the ID of an item in my app. At this point the user is prompted with two buttons that ask whether they want to keep the item or discard it. If they decide to keep the item I need it to be added to their inventory in the database. This is where my question comes into play. My app knows which user is logged in because when someone properly logs in the app sets their username (which is a primary key) to a global string variable which the rest of the app can user. so it knows which user to update but I need it to check through each of the rows in order, and if it finds a row with a zero in the ItemID column, it will update it to what the variable "i" currently is and end the query.
This is my current code, I'm very new to SQL but I'm trying to teach myself, I apologize if this offends you (because it's so bad):
EDIT: I've updated my code to this new query however I get an error that states java.sql.SQLException: You can't specify target table 'userinv' for update in FROM clause
try{
//get connection to database
Connection con = DataBaseConnect.getConnection();
//create a statement
PreparedStatement addInfo = con.prepareStatement("UPDATE userinv SET "
+ "ItemID = "+i+" "
+ "WHERE Username = '"+LoginController.userLog+"' "
+ "AND Slot = ("
+ "SELECT MIN(Slot) FROM userinv "
+ "WHERE ItemID = 0 "
+ "AND Username = '"+LoginController.userLog+"')");
//process result set
addInfo.executeUpdate();
}
catch(Exception e){
e.printStackTrace();
}
at this point I know it needs to update the userinv table and I know it needs to do this where the users username is but I'm not sure how to write the code in between. Does anyone have any ideas?
This works in Oracle and should work for MySql:
update userinv set itemid = 815
where username = 'test'
and slot = (
select min(slot) from userinv
where itemid = 0
and username = 'test'
)
For more complex cases where you need the first row according to some ordering, but can't express this as a minimum this approach works on Oracle:
update userinv set itemid = 815
where username = 'test'
and slot = (
select slot from (
select count(*) over (partition by username order by slot) cnt,
slot
from userinv
where itemid = 0
and username = 'test'
) where cnt = 1
)
It uses analytic functions so it won't work on MySql, but there is an article how to fake them in MySQL.
With analytic functions, this should also work (didn't try, so it does contain typos and stuff)
update (
select count(*) over (partition by username order by slot) cnt,
u.*
from userinv u
where itemid = 0
and username = 'test'
order by slot
)
set itemid = 815
where cnt = 1
This accesses the table only once, which should be way faster when your table is huge.
I'm trying to write a MYSQL Query that updates a cell in table1 with information gathered from 2 other tables;
The gathering of data from the other 2 tables goes without much issues (it is slow, but that's because one of the 2 tables has 4601537 records in it.. (because all the rows for one report are split in a separate record, meaning that 1 report has more than 200 records)).
The Query that I use to Join the two tables together is:
# First Table, containing Report_ID's: RE
# Table that has to be updated: REGI
# Join Table: JT
SELECT JT.report_id as ReportID, REGI.Serienummer as SerialNo FROM Blancco_Registration.TrialTable as REGI
JOIN (SELECT RE.Value_string, RE.report_id
FROM Blancco_new.mc_report_Entry as RE
WHERE RE.path_id=92) AS JT ON JT.Value_string = REGI.Serienummer
WHERE REGI.HardwareType="PC" AND REGI.BlanccoReport=0 LIMIT 100
This returns 100 records (I limit it because the database is in use during work hours and I don't want to steal all resources).
However, I want to use these results in a Query that updates the REGI table (which it uses to select the 100 records in the first place).
However, I get the error that I cannot select from the table itself while updateing it (logically). So I tried selecting the select statement above into a temp table and than Update it; however, then I get the issue that I get to much results (logically! I only need 1 result and get 100) however, I'm getting stuck in my own thougts.. I ultimately need to fill the ReportID into each record of REGI.
I know it should be possible, but I'm no expert in MySQL.. is there anybody that can point me into the right direction?
Ps. fixing the table containing 400k records is not an option, it's a program from an external developer and I can only read that database.
The errors I'm talking about are as follows:
Error Code: 1093. You can't specify target table 'TrialTable' for update in FROM clause
When I use:
UPDATE TrialTable SET TrialTable.BlanccoReport =
(SELECT JT.report_id as ReportID, REGI.Serienummer as SerialNo FROM Blancco_Registration.TrialTable as REGI
JOIN (SELECT RE.Value_string, RE.report_id
FROM Blancco_new.mc_report_Entry as RE
WHERE RE.path_id=92) AS JT ON JT.Value_string = REGI.Serienummer
WHERE REGI.HardwareType="PC" AND REGI.BlanccoReport=0 LIMIT 100)
WHERE TrialTable.HardwareType="PC" AND TrialTable.BlanccoReport=0)
Then I tried:
UPDATE TrialTable SET TrialTable.BlanccoReport = (SELECT ReportID FROM (<<and the rest of the SQL>>> ) as x WHERE X.SerialNo = TrialTable.Serienummer)
but that gave me the following error:
Error Code: 1242. Subquery returns more than 1 row
Haveing the Query above with a LIMIT 1, gives everything the same result
Firstly, your query seems to be functionally identical to the following:
SELECT RE.report_id ReportID
, REGI.Serienummer SerialNo
FROM Blancco_Registration.TrialTable REGI
JOIN Blancco_new.mc_report_Entry RE
ON RE.Value_string = REGI.Serinummer
WHERE REGI.HardwareType = "PC"
AND REGI.BlanccoReport=0
AND RE.path_id=92
LIMIT 100
So, why not use that?
EDIT:
I still don't get it. I can't see what part of the problem the following fails to solve...
UPDATE TrialTable REGI
JOIN Blancco_new.mc_report_Entry RE
ON RE.Value_string = REGI.Serinummer
SET TrialTable.BlanccoReport = RE.report_id
WHERE REGI.HardwareType = "PC"
AND REGI.BlanccoReport=0
AND RE.path_id=92;
(This is not an answer, but maybe a pointer towards a few points that need further attention)
Your JT sub query looks suspicious to me:
(SELECT RE.Value_string, RE.report_id
FROM Blancco_new.mc_report_Entry as RE
WHERE RE.path_id=92
GROUP BY RE.report_id)
You use group by but don't actually use any aggregate functions. The column RE.Value_string should strictly be something like MAX(RE.Value_string) instead.
I am working with MS Access 2007.
I have a table called [tblDonors] linked to child records in a table called [tblReceipts].
I am trying to set the value of a new Boolean(y/n) field [tblDonors].[blInactive]. I would like to run a script which goes through every record in [tblDonors] and set the [blInactive] field to "True" if the most recent receipt year([tblReceipts].[Date]) is older than year(now())-2.
Here is my query:
SELECT tblDonors.ID, Year([tblReceipts].[Date]) AS [Year], tblDonors.Active
FROM tblDonors LEFT JOIN tblReceipts ON tblDonors.ID = tblReceipts.ID
WHERE (((Year([tblReceipts].[Date]))>=Year(Now())-2))
ORDER BY Donors.ID;
This query shows the parent record multiple times if the donor has donated more than once within the past two years, which creates duplicate records in a report.
How can I go through each record of this query to set tblDonors.Active to TRUE using VBA?
The following helps demonstrate the gist of what I am attempting to accomplish.
Function fnActivity() 'locates active donors
For each record in recordset 'Go through each record in the query...
record.[Active].Value = True '...and flag as active member.
Next record
End Function
It seems to me that you could accomplish that by setting [blInactive] to Yes for everyone...
UPDATE tblDonors SET blInactive = Yes
...and then update the current donors like this:
UPDATE tblDonors SET blInactive = No
WHERE ID IN
(
SELECT ID FROM tblReceipts
WHERE Year([tblReceipts].[Date]) >= (Year(Now()) - 2)
)