Update TotalItems
set TotalItems.No_Items_Present = TotalItems.Total_Items - ItemsTable.No_Of_Items
where TotalItems.Item_Name = ItemsTable.Item_Name
My query isn't working. Any solution????
Try this:
UPDATE a SET a.No_Items_Present = a.Total_Items - b.No_Of_Items
FROM TotalItems a INNER JOIN ItemsTable b ON a.Item_Name = b.Item_Name
In MS SQL:
update ttl
set No_Items_Present = ttl.Total_Items - itm.No_Of_Items
from TotalItems ttl
join ItemsTable itm on
ttl.Item_Name = itm. Item_Name
In MySQL:
update TotalItems ttl
join ItemsTable itm on
ttl.Item_Name = itm. Item_Name
set tt1.No_Items_Present = ttl.Total_Items - itm.No_Of_Items
update(
select ti.no_items_present,
ti.total_items,
it.no_of_items
from total_items ti inner join
itemstable it on ti.item_name=it.item_name)
set no_items_present=total_items-no_of_items
In MySQL you would do something like this:
UPDATE TotalItems t, ItemsTable i
SET t.No_Items_Present = t.Total_Items - i.No_Of_Items
WHERE t.Item_Name = i.Item_Name
Related
I need to select the MCUs table if UserID = 7 does not exist in the table. Please help me, what am I doing wrong?
SELECT
MCUs.MCUID
FROM
CASE
WHEN ReqMCUDevs.UserID = 7 THEN
MCUs
INNER JOIN MCUDevs
ON MCUs.MCUID = MCUDevs.MCUID
INNER JOIN ReqMCUDevs
ON MCUDevs.DevID = ReqMCUDevs.DevID
AND ReqMCUDevs.Quantity >= MCUDevs.Quantity
ELSE
MCUs
END
I need the resunt ot this query if UserID=7 exists
SELECT
MCUs.MCUID
FROM
MCUs
INNER JOIN MCUDevs
ON MCUs.MCUID = MCUDevs.MCUID
INNER JOIN ReqMCUDevs
ON MCUDevs.DevID = ReqMCUDevs.DevID
AND ReqMCUDevs.Quantity >= MCUDevs.Quantity
WHERE
ReqMCUDevs.UserID = 7
and this query if not
SELECT
MCUs.MCUID
FROM
MCUs
database schema:
The easiest solution is to use two different queries
db = sqlite3.connect("..\\DB.db", isolation_level = None);
c = db.cursor()
#...
c.execute("SELECT UserID FROM ReqMCUDevs WHEN UserID = ?", (UserID,))
if c.fetchall() == []:
c.execute("SELECT MCUID FROM MCUs")
else:
c.execute("""SELECT MCUID FROM MCUDevs
JOIN ReqMCUDevs ON MCUDevs.DevID = ReqMCUDevs.DevID
AND ReqMCUDevs.Quantity <= MCUDevs.Quantity
WHERE ReqMCUDevs.UserID = ?""", (UserID,))
but it is really not the answer
CASE works only in expressions (i.e., to compute a value); it is not possible to change the basic structure of a query conditionally.
However, there are different mechanisms to get what you want.
First, the query with the joins does not actually return anything from the joined tables, so it is possible to rewrite it with a subquery:
SELECT Name,
Price
FROM MCUs
WHERE MCUID IN (SELECT MCUID
FROM MCUDevs
JOIN ReqMCUDevs ON MCUDevs.DevID = ReqMCUDevs.DevID
AND ReqMCUDevs.Quantity >= MCUDevs.Quantity
WHERE ReqMCUDevs.UserID = 7)
ORDER BY Price
Now, we want to ignore this WHERE filter if the subquery is empty.
This can be done with a separate check:
SELECT Name,
Price
FROM MCUs
WHERE MCUID IN (SELECT MCUID
FROM MCUDevs
JOIN ReqMCUDevs ON MCUDevs.DevID = ReqMCUDevs.DevID
AND ReqMCUDevs.Quantity >= MCUDevs.Quantity
WHERE ReqMCUDevs.UserID = 7)
OR NOT EXISTS (SELECT MCUID
FROM MCUDevs
JOIN ReqMCUDevs ON MCUDevs.DevID = ReqMCUDevs.DevID
AND ReqMCUDevs.Quantity >= MCUDevs.Quantity
WHERE ReqMCUDevs.UserID = 7)
ORDER BY Price
I am trying to update a field in a table from another table using INNER JOIN. Here is the code:
UPDATE TestResults
INNER JOIN Distractors
ON TestResults.DistractorID = Distractors.ID
SET TestResults.DistractorValue = Distractors.DistractorValue
This does not work I don't know why! Any idea? When I run the query I get the following error
There was an error parsing the query. [ Token line number = 2,Token line offset = 1,Token in error = INNER ]
Not all databases support join syntax with update. And when they do, the syntax differs. Here is a way to do your query without an explicit join using standard SQL:
UPDATE TestResults
set DistractorValue = (select max(d.DistractorValue)
from Distractors d
where TestResults.DistractorValue = d.DistractorValue
)
where exists (select 1
from Distractors d
where TestResults.DistractorValue = d.DistractorValue
);
The max() is only needed if there could be more than one matching row.
The where is only needed if the join is intended to do filtering as well as matching.
UPDATE TestResults
SET TestResults.DistractorValue = Distractors.DistractorValue
FROM TestResults
INNER JOIN Distractors
ON TestResults.DistractorID = Distractors.ID
You use the Updated table in the Inner join clause
ex:
UPDATE TestResults
SET TestResults.DistractorValue = Distractors.DistractorValue
FROM TestResults INNER JOIN Distractors
ON TestResults.DistractorID = Distractors.ID
I want to add data to table STATISTICS using INSERT statements.
I also want to move new counts to old counts and new date to old date as the new data comes in.
This is where it gets lil tricky because I don't know if there is such a thing as INSERT INTO table with SET in Oracle.
INSERT INTO STATISTICS
SET
MODEL = '&MY_MODEL',
NEW_COUNT =
(
SELECT COUNT(*)
FROM TABLE CLIENTS
),
NEW_DATE = SYSDATE,
OLD_COUNT = NEW_COUNT,
OLD_DATE = NEW_DATE,
PRNCT_CHANGE = ((NEW_COUNT) - (OLD_COUNT)) / (NEW_COUNT)*100
);
How do I accomplish this in Oracle?
This should upsert statistics, adding new ones as you go. It presumes a unique key on MODEL; if that's not true, then you'd have to do inserts as Angelina said, getting only the most recent row for a single MODEL entry.
MERGE INTO STATISTICS tgt
using (SELECT '&MY_MODEL' AS MODEL,
(SELECT COUNT(*) FROM CLIENTS) AS NEW_COUNT,
SYSDATE AS DATE_COUNT,
NULL AS OLD_COUNT,
NULL OLD_DATE,
NULL AS PRCNT_CHANGE
FROM DUAL) src
on (TGT.MODEL = SRC.MODEL)
WHEN MATCHED THEN UPDATE
SET TGT.NEW_COUNT = SRC.NEW_COUNT,
TGT.NEW_DATE = SRC.NEW_DATE,
TGT.OLD_COUNT = TGT.NEW_COUNT,
TGT.OLD_DATE = TGT.NEW_DATE,
TGT.PRCNT_CHG = 100 * (SRC.NEW_COUNT - TGT.NEW_COUNT) / (SRC.NEW_COUNT)
-- NEEDS DIV0/NULL CHECKING
WHEN NOT MATCHED THEN INSERT
(MODEL, NEW_COUNT, NEWDATE, OLD_COUNT, OLD_DATE, PRCNT_CHANGE)
VALUES
(src.MODEL, src.NEW_COUNT, src.NEWDATE, src.OLD_COUNT, src.OLD_DATE, src.PRCNT_CHANGE);
INSERT INTO STATISTICS(MODEL,NEW_COUNT,NEW_DATE,OLD_COUNT,OLD_DATE,PRNCT_CHANGE)
SELECT MODEL,
( SELECT COUNT(*)
FROM TABLE(USERS)
),
SYSDATE,
NEW_COUNT,
NEW_DATE,
(((NEW_COUNT) - (OLD_COUNT)) / (NEW_COUNT)*100)
FROM SEMANTIC.COUNT_STATISTICS
WHERE MODEL = '&MY_MODEL'
AND trunc(NEW_DATE) = trunc(NEW_DATE -1)
;
I am using sql server 2005. In this query i want the log-in user detail should also display but it is not displaying .
So please modify the query so that log-in user detail should also display with the help of session[userId].tostring();
Query written by me is:
SELECT DISTINCT MUDMEMBER.PK_ID, MUDMEMBER.EMPLOYEE_ID, LKB.BANK_NAME, MUHD.SALARY_ACCOUNT_NO, MUHD.PF_NO,
MUHD.PAN_NO, MUHD.GENDER, LKD.DESIGNATION_NAME FROM M_LEADERLED MLL INNER JOIN M_USER_DETAILS MUDMEMBER ON
MLL.LED_ID = MUDMEMBER.PK_ID AND MLL.START_DATE <= Getdate() AND MLL.END_DATE > Getdate() AND MLL.LEADER_ID = '1' LEFT OUTER JOIN
M_USER_HR_DETAILS MUHD ON MUHD.FK_USER_ID = MUDMEMBER.PK_ID AND MUHD.IS_ACTIVE =1 LEFT OUTER JOIN
LK_BANKS LKB ON LKB.PK_ID = MUHD.FK_BANK_ID LEFT OUTER JOIN LK_DESIGNATION LKD ON
LKD.DESIGNATION_VALUE = MUHD.FK_DESIGNATION_VALUE AND LKD.FK_ORGANIZATION_ID = 1 AND LKD.IS_ACTIVE = 1 WHERE MUDMEMBER.ACTIVE = 1
ASP.Net Page you can fetch the loggedin user's detail as follows:
SELECT DISTINCT MUDMEMBER.PK_ID, MUDMEMBER.EMPLOYEE_ID, LKB.BANK_NAME, MUHD.SALARY_ACCOUNT_NO, MUHD.PF_NO,
MUHD.PAN_NO, MUHD.GENDER, LKD.DESIGNATION_NAME FROM M_LEADERLED MLL INNER JOIN M_USER_DETAILS MUDMEMBER ON
MLL.LED_ID = MUDMEMBER.PK_ID AND MLL.START_DATE <= Getdate() AND MLL.END_DATE > Getdate()
AND MLL.LEADER_ID = '1' LEFT OUTER JOIN
M_USER_HR_DETAILS MUHD ON MUHD.FK_USER_ID = MUDMEMBER.PK_ID AND MUHD.IS_ACTIVE =1
LEFT OUTER JOIN
LK_BANKS LKB ON LKB.PK_ID = MUHD.FK_BANK_ID LEFT OUTER JOIN LK_DESIGNATION LKD ON
LKD.DESIGNATION_VALUE = MUHD.FK_DESIGNATION_VALUE AND LKD.FK_ORGANIZATION_ID = 1
AND LKD.IS_ACTIVE = 1
WHERE MUDMEMBER.ACTIVE = 1 AND MUDMEMBER.PK_ID ="+Convert.ToInt32(Session["UserId"])+"
is it possible to retrieve by using connection.getschema() the description item from a sql server table column, just like it's possible to retrieve the column name, data type, is nullable, column default value, etc? if so, how?
Try this:
SELECT
[Table Name] = i_s.TABLE_NAME,
[Column Name] = i_s.COLUMN_NAME,
[Description] = s.value
FROM
INFORMATION_SCHEMA.COLUMNS i_s
LEFT OUTER JOIN
sys.extended_properties s
ON
s.major_id = OBJECT_ID(i_s.TABLE_SCHEMA+'.'+i_s.TABLE_NAME)
AND s.minor_id = i_s.ORDINAL_POSITION
AND s.name = 'MS_Description'
WHERE
OBJECTPROPERTY(OBJECT_ID(i_s.TABLE_SCHEMA+'.'+i_s.TABLE_NAME), 'IsMsShipped')=0
--AND i_s.TABLE_NAME = 'table_name'
ORDER BY
i_s.TABLE_NAME, i_s.ORDINAL_POSITION
edit: fixed the query :-)
HTH
On Sql Server 2005, you can use this system table value function:
fn_listextendedproperty (Transact-SQL)
or try a query, from from this article, like this:
SELECT
[Table Name] = OBJECT_NAME(c.object_id),
[Column Name] = c.name,
[Description] = ex.value
FROM
sys.columns c
LEFT OUTER JOIN
sys.extended_properties ex
ON
ex.major_id = c.object_id
AND ex.minor_id = c.column_id
AND ex.name = 'MS_Description'
WHERE
OBJECTPROPERTY(c.object_id, 'IsMsShipped')=0
-- AND OBJECT_NAME(c.object_id) = 'your_table'
ORDER
BY OBJECT_NAME(c.object_id), c.column_id
If you already have the DataTable, as you mention - look at its data columns!
foreach(DataColumn col in dataTable.Columns)
{
// check out all the properties on the DataColumn
}
Does that contain what you need to have??