I am not familiar with SQL that much. I'm trying to insert multiple rows of data into a table that if there exist a row with with duplicate value in BusinessFilterPhrase column then just don't insert. I wrote a pseudocode of what I think it should be.
if (filterCategoryList != null)
{
foreach (KeyValuePair<string, int> filter in filterCategoryList)
{
cmd.CommandText = "insert into tblBusinessName (BusinessFilterPhrase,BusinessCategoryID)" +
"select #BusinessFilterPhrase,#BusinessCategoryID" +
"from tblBusinessName as t1" +
"where NOT EXISTS" +
"( select * from tblBusinessName as d1 where d1.BusinessFilterPhrase = #BusinessFilterPhrase) ";
cmd.Parameters.AddWithValue("#BusinessFilterPhrase", filter.Key);
cmd.Parameters.AddWithValue("#BusinessCategoryID", filter.Value.ToString());
cmd.ExecuteNonQuery();
}
}
You code looks correct. I would write it as:
insert into tblBusinessName(BusinessFilterPhrase, BusinessCategoryID)
select #BusinessFilterPhrase, #BusinessCategoryID
from tblBusinessName t1
where NOT EXISTS (select 1
from tblBusinessName d1
where d1.BusinessFilterPhrase = #BusinessFilterPhrase
)
(The changes are only cosmetic.)
EDIT:
If performance is an issue, create an index on BusinessFilterPhrase:
create index idx_tblBusinessName_BusinessFilterPhrase on tblBusinessName(BusinessFilterPhrase);
You can make this a unique index, if you want the database to enforce the uniqueness of the column (it will generate an error when duplicate values would be inserted).
Related
Need to modify the data in an SQLite database that has fields similar to:
hash (string)
modTime (long, time in ms)
data (any data, e.g. string)
I know the hash value of the record to set the data for (it's really xxHash of some binary blob). There can be several records with the same hash, but I need to modify only the one with MAX(modTime) value with the matching hash... Can't figure out how to construct the UPDATE command.
You can use NOT EXISTS in the WHERE clause:
UPDATE tablename
SET data = new_data
WHERE hash = ?
AND NOT EXISTS (SELECT 1 FROM tablename t WHERE t.hash = tablename.hash AND t.modTime > tablename.modTime)
Or use the column rowid to find the row that you want to update:
UPDATE tablename
SET data = new_data
WHERE rowid = (SELECT rowid FROM tablename WHERE hash = ? ORDER BY modTime DESC LIMIT 1)
or with FIRST_VALUE() window function:
UPDATE tablename
SET data = new_data
WHERE rowid = (SELECT DISTINCT FIRST_VALUE(rowid) OVER (ORDER BY modTime DESC) FROM tablename WHERE hash = ?)
Replace new_data with the new value of data and ? with the value of hash you search for.
I have tried several different queries and cannot seem to find one that works without causing an error
Incorrect syntax near keyword ' '
I have one table called Scores that saves the userID, week number, and that weeks score from a quiz.
Schema:
Scores (Id, userName, weekNumber, currentScore)
There are 12 weeks total, so in the end each user will have 12 entries in this table
I have another table Leaderboard that has schema :
Leaderboard (Id, userName, week1, week2, week3 ..... week12, totalScore)
Each user will only have one entry in this table.
I have been trying to save the sum of the currentScore from Scores into the totalScore attribute of Leaderboard and cannot seem to figure out the correct syntax.
My query is as follows:
UPDATE t1
SET t1.totalScore = t2.completeScore
FROM dbo.Leaderboard AS t1,
((SELECT Id, SUM(weeklyScore) AS completeScore FROM dbo.Scores) as F
INNER JOIN
(SELECT Id FROM dbo.Scores GROUP BY Id) AS S ON F.Id = S.Id) AS t2
WHERE t1.Id = t2.Id
Try this and let us know how it goes:
var total1 = ("UPDATE t1 " +
"SET t1.totalScore = t2.completeScore " +
"FROM dbo.Leaderboard AS t1, (SELECT F.Id, F.completeScore FROM " +
"(SELECT Id, SUM(weeklyScore) AS completeScore FROM dbo.Scores GROUP BY ID) as F " +
"INNER JOIN (SELECT Id FROM dbo.Scores/* GROUP BY Id*/) AS S "+
"ON F.Id = S.Id) AS t2 "+
"WHERE t1.Id = t2.Id");
By the way I think you were missing a GROUP BY clause in the definition of the F derived table and that you don't need the GROUP BY in the definition of the S derived table.
example: SELECT title,ROW_NUM FROM article ORDER BY count_read.
What should ROW_NUM be replace by ?
I don't like to after getting the results generate the index by program, because I want to insert into a table Rank with the result data by querying the example DQL above.
What I want to achieve maybe like :
"INSERT INTO RANK r (title, index, lastIndex)
SELECT title,ROW_NUM,(SELECT index FROM RANK WHERE id = :id - 1) FROM article ORDER BY count_read"
Thanks in advance..
I think you might use variables, like this:
"
SET #row_num := 1;
INSERT INTO RANK r (title, index, lastIndex)
SELECT title,
(#row_num := #row_num + 1),
(SELECT index FROM RANK WHERE id = :id - 1)
FROM article ORDER BY count_read
"
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)
;
How can I get a single row result (e.g. in form of a table/array) back from a sql statement. Using Lua Sqlite (LuaSQLite3). For example this one:
SELECT * FROM sqlite_master WHERE name ='myTable';
So far I note:
using "nrows"/"rows" it gives an iterator back
using "exec" it doesn't seem to give a result back(?)
Specific questions are then:
Q1 - How to get a single row (say first row) result back?
Q2 - How to get row count? (e.g. num_rows_returned = db:XXXX(sql))
In order to get a single row use the db:first_row method. Like so.
row = db:first_row("SELECT `id` FROM `table`")
print(row.id)
In order to get the row count use the SQL COUNT statement. Like so.
row = db:first_row("SELECT COUNT(`id`) AS count FROM `table`")
print(row.count)
EDIT: Ah, sorry for that. Here are some methods that should work.
You can also use db:nrows. Like so.
rows = db:nrows("SELECT `id` FROM `table`")
row = rows[1]
print(row.id)
We can also modify this to get the number of rows.
rows = db:nrows("SELECT COUNT(`id`) AS count FROM `table`")
row = rows[1]
print(row.count)
Here is a demo of getting the returned count:
> require "lsqlite3"
> db = sqlite3.open":memory:"
> db:exec "create table foo (x,y,z);"
> for x in db:urows "select count(*) from foo" do print(x) end
0
> db:exec "insert into foo values (10,11,12);"
> for x in db:urows "select count(*) from foo" do print(x) end
1
>
Just loop over the iterator you get back from the rows or whichever function you use. Except you put a break at the end, so you only iterate once.
Getting the count is all about using SQL. You compute it with the SELECT statement:
SELECT count(*) FROM ...
This will return one row containing a single value: the number of rows in the query.
This is similar to what I'm using in my project and works well for me.
local query = "SELECT content FROM playerData WHERE name = 'myTable' LIMIT 1"
local queryResultTable = {}
local queryFunction = function(userData, numberOfColumns, columnValues, columnTitles)
for i = 1, numberOfColumns do
queryResultTable[columnTitles[i]] = columnValues[i]
end
end
db:exec(query, queryFunction)
for k,v in pairs(queryResultTable) do
print(k,v)
end
You can even concatenate values into the query to place inside a generic method/function.
local query = "SELECT * FROM ZQuestionTable WHERE ConceptNumber = "..conceptNumber.." AND QuestionNumber = "..questionNumber.." LIMIT 1"