Optimizing an SQL statement for a number database [closed] - sql

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a list of over a million numbers. I need to put the numbers in one table numerically sorted, and another as is, referencing the ID of the sorted number, the table is structured as so:
sortedNumbers
Id | SortedNumber
1 01356
2 45789
Numbers
Id | Number | SortedNumberId
1 35601 1
2 94578 2
3 97548 2
Currently I am filling the sorted numbers table first, then filling the standard numbers table, and doing a select statement to get the sorted numbers Id. All these select statements are making it dog slow though, is there a way to do it all in one statement, never using a select statement to get the Id of the sorted number?
Current Queries on each iteration of a loop, sortedNumber is made by sorting the number in code
loop 1
query = "INSERT into SortedNumbers VALUES (" + SortedNumber + ")";
loop 2
query = "SELECT Id FROM SortedNumbers WHERE SortedNumber = " + sortedNumber;
query = "INSERT into Numbers VALUES (" + Number + ", " + SortedId + ")";

Well for a start you don't put sorted numbers into a database. You put the numbers into a table then query the table ordering by whatever sort criteria you want.
SELECT number from table ORDER BY number
But I expect I've missed the point of whatever you are trying to do. Can you post some examples?

You could wrap your two inserts into a Transaction and get the ID value from the sortedNumbers table by calling SCOPE_IDENTITY(). That is assuming that the ID column in the sortedNumbers table is an Identity column:
BEGIN TRANSACTION
INSERT sortedNumbers (SortedNumber) VALUES (sortednumbervalue)
INSERT Numbers (Number, SortedNumberId) VALUES (numbervalue, SCOPE_IDENTITY())
COMMIT

Related

Remove sorting order differences while comparing 2 tables using sql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 days ago.
Improve this question
I am trying to compare cell to cell of 2 tables.
There is a situation wherein a record in column C is in row 4 of table 1 while same record is in row 7 in table 2.
Hence during comparison, record in col C and row 4 is giving a mismatch as it's not same in table 2.
In reality, such mismatches are to be ignored since it exists somewhere within table 2 but just in different row.
What is the best way to ignore such mismatches.
I am not able to get with Exist function syntax.
For example record in tbl1."Col C" is to be seen if available in tbl2."Col C" and if this record is not found, then the mismatch has to be reported.
I am not able to get right syntax here either for exist function or sorting 2 tables and then comparing.

How to merge two or more rows where some columns have same values and some have different values? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
Sample Data output after joining 2 different tables on ProductNumber
ProductNumber
QuantityOnHand
VendorID
1
5
401
1
5
501
I want to write a query that would return this output
ProductNumber
QuantityOnHand
VendorID
1
10
401 & 501
I'm still pretty new to SQL. Stuck on a homework problem here. I don't really know which aggregated function to use to make it work. Sum() but I don't want to add the vendorIDs. Concat() but they're from the same column name.
If it is SQL Server, then you need STRING_AGG function for cancatenating the same column in different rows and SUM for totalling:
SELECT ProductNumber,
SUM(QuantityOnHand),
STRING_AGG(VendorID, ' & ')
FROM Product
GROUP BY ProductNumber
I assumed the table name is Product, as you did not provide one. You can update it to your actual table name.

SQL Dynamic Query generation [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last year.
Improve this question
I have 2 tables like below
here i need to get all the rows from second table based on each row from the first table which matches Field1value and field2value combination.Second table select column will decide by first table field1 and field2 respectively,and I need to remove the duplicate row if any,for example last row in second table satisfies the condition of 1 and 3 row of first table.
How to format this query?.
Are you looking for something like this?
select 'select distinct percentage from table2 where '+Field1+' ='+ ''''+Field1value+ ''''+' and '+Field2+' = '+ ''''+Field2value+ '''' from table11
result:

Picking unique records in SQL [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Say I have a table with multiple records of peoples name, I draw out a prize winner every month. What is a query in SQL that I can use so that I pick up a unique record every month and the person does not get picked on the next month. I do not want to delete the record of that person.
create a new column as a flag named anything like 'prizeFlag' make it boolean take only 0 and 1 or anything as you like, make it's default value is 0 means not get a prize yet
when you select a random column update this filed with 1 means take a prize
when you select a random column next month, Add a condition in WHERE Clause say the prizeFlag not equal 1 to avoid duplication
One should store whether a person has already won. A date would make sense to allow people after say 10 years to win again.
ALTER TABLE ADD won DATE;
A portable way would be to use a random number function outside the SQL.
SELECT MIN(id), MAX(id), COUNT(*) FROM persons
With a random number one can get the next valid ID.
SELECT MIN(ID) FROM persons WHERE won NOT IS NULL AND ID >= ?
int randomno = minId + new Random().nextInt(maxId - minId);
preparedStatement.setInt(1, randomno)
UPDATE persons SET won = CURRENT_DATE WHERE ID = ?

SQL Select and sort on relevance [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to search an table column. and select all rows that contains a string.
I do that on this way:
select * from memos where contains (article, '"test*"')
The next problem is to order the acticles on relevance. So if a record contains 4 times the word of wordpart 'test' i want it on top and if it contains 3 times the word 'test' i want it below 4. And so order it on how many times a word is in a row.
Assuming the article is stored as a varchar() or nvarchar(), then you can do this
select *
from memos
where contains (article, '"test*"')
order by len(replace(article, 'test', 'test1')) - len(article) desc;
This replaces test with a string one character longer, measures the length, and then subtracts the original length. Voila. The number of times that test occurs. This should take place only on articles that have the search term.
I'm not sure if SQL Server has something like this built-in to the full text engine.