I have a one-column table that can vary in size depending on the day. Let's say the table has 6,000 rows. Is there a way you can pivot this table in T-SQL so that is 6,000 columns without having to specify each column name?
I'm trying to work a excel table than contains more than 300000 records, I need to apply a filter based on #N/A criteria on one column and then get those records (only the data of two columns) and used to create a vlook up to extract some information on another table. Filter result is no more than 5 records
I have a table named "A" which have 2 columns, "A1" and "A2".
I want each unique value in column "A1" to have MAX 2 rows in the table, if a unique value in column "A1" have 5 rows, 3 rows should be deleted.
Which 3 rows to delete is determinated by the lowest values in column "A2".
The table consist of +20 million rows, +300000 unique values in column "A1" and up to 3000 rows per unique value in column "A1".
I have solved this with the following query:
with excess as
(
select
id,
row_number() over(partition by A1 order by A2 desc) as rownum
from
A
)
delete from excess
where rownum > 2
I'm satisfied with this query since it took 8 minutes for the initial batch and ~20 seconds in recurring executions.
Is this the most efficient query to achieve the requirements?
yes, this is is the most efficient query without copying the data into another table because it is making it in a single run against the table instead of joining back to itself. I would suggest that you use "delete top(N)" and keep the number under 5,000, if there are any other consumers of the table. this will attempt to prevent the lock escalation from escalating to a full table lock. it will also free up the tlogs on the server to be reused in between batches. if you do it all in one go, all of the deleted rows have to be accounted for in the tlogs, and the space can't be reused until the statement is complete. I would also suggest creating a composite index on (A1, A2).
if the number of rows that need to be deleted are a significant percentage, it would be faster to copy the rows where rownum <= 2 into a new table. then, drop the original table and rename the new table back to the original. if you have other consumers of the table and/or don't want to copy the data, then this may not be a valid solution.
So I have eight Excel sheets, all with two columns a 13000 rows each. I want to create a table in my database with all this data. Also, I want the rows in the different sheets to match each other, meaning that row 1 in each of the columns in all the sheets, should form one row in the database. Is there any convenient way of doing so?
I know the title might seem confusing but the real situation is as follow: I have two tables with existing data and both of them have n rows but different columns. The order of the rows in two tables do match. So the goal is such that after appending, the first row of the second table is appended to the first row of the first table, etc. and all the columns from the second table are added to the first table - basically just like paste two tables together.