VBS selecting rows - sql

How do I select a particular row from sql table. Example if value of variable is 2, then select 2nd row from table?
Is there any function in VBS to determine how many rows are in the table?

How do I select a particular row from sql table. Example if value of variable is 2, then select 2nd row from table?
You can execute the following SQL, that uses the RANK() SQL Server function to always get the second row, for example, by ordering the records by id column in DESC order:
SELECT * FROM
(
SELECT *, RANK() OVER (ORDER BY id DESC) 'RowRank' FROM MyTable
) AS A
WHERE RowRank = 2
Is there any function in VBS to determine how many rows are in the table?
I am not sure if VBS has out-of-the-box function to get the number of rows in a table, but you can use a simple SQL query to find that out:
SELECT COUNT(*) FROM MyTable
This would return a value, but the query above that is selecting the second row would return a list of column values, as present in the table.

Related

SQL Query for multiple columns with one column distinct

I've spent an inordinate amount of time this morning trying to Google what I thought would be a simple thing. I need to set up an SQL query that selects multiple columns, but only returns one instance if one of the columns (let's call it case_number) returns duplicate rows.
select case_number, name, date_entered from ticket order by date_entered
There are rows in the ticket table that have duplicate case_number, so I want to eliminate those duplicate rows from the results and only show one instance of them. If I use "select distinct case_number, name, date_entered" it applies the distinct operator to all three fields, instead of just the case_number field. I need that logic to apply to only the case_number field and not all three. If I use "group by case_number having count (*)>1" then it returns only the duplicates, which I don't want.
Any ideas on what to do here are appreciated, thank you so much!
You can use ROW_NUMBER(). For example
select *
from (
select *,
row_number() over(partition by case_number) as rn
) x
where rn = 1
The query above will pseudo-randomly pick one row for each case_number. If you want a better selection criteria you can add ORDER BY or window frames to the OVER clause.

Oracle select specific rows

Can we select specific rows to range in oracle? for example, I have a table of 100 rows I have to select only a range of 10 to 20-row numbers. Is it possible to do that
You can do with an auxiliary operation. Firstly number the rows by row_number() function and then order by them :
select * from
(
select row_number() over (order by 0) rn, t.*
from tab t
)
where rn between 10 and 20;
but this is not a stable operation, since SQL statements are unordered sets. Therefore it's better to define a unique identity column and order depending on it.
Replace zero in the order by clause with some columns of your table to be able to reach a rigid ordering criteria. If a primary key column exists, it might be better to include only it in the order by list.
would LIMIT and OFFSET work?
ie.
SELECT * FROM table
LIMIT 20
OFFSET 20
will read rows 20 -> 40. Is this what you are trying to do?

SQL Table Row Number Column?

I am writing a program in SQL, but every time I execute the program in MSSMS, there is a column on the far left that contains row numbers. I would like to call the the row numbers in this column in a while statement I am using in the same program, but I'm not sure how to refer to it in the code. What is this column called so that I can call it and get the row numbers?
The column to which you are referring is generated by SQL Server and does not actually exist in your result set. If there exists one or more columns in your table which would generate that ordering, then you may add a call to ROW_NUMBER to obtain that column you are seeing. For example:
SELECT *, ROW_NUMBER() OVER (ORDER BY some_col) rn
FROM yourTable
ORDER BY rn;
You can add your own row numbers using row_number():
select row_number() over (order by <order cols>) as seqnum,
. . .
from t
order by <order cols>;
These are in the data and can be referenced in subsequent processing.

How to delete one random row at a time in Oracle database

I am working on a project where I need to delete a random row from an Oracle database. Which query should I use for that?
Well obviously it's a DELETE statement. It's the random part which is tricky.
Oracle has a PL/SQL package to handle randomness, DBMS_RANDOM. If the table is small or performance is not critical this will work:
delete from your_table
where id = ( select id from
( select id from your_table
order by dbms_random.value)
where rownum = 1)
/
The innermost query sorts the table in a random order. The sub-query selects the top row from that set, and that's the row which gets deleted.
Alternatively, if you know how many records you have...
delete from your_table
where id = ( select round(dbms_random.value(1,10000))
from dual )

SQL Row_Count function with Partition

I have a query that returns a set of results as a table called DATA, from several UNION ALL joined queries.
I am then doing ROW_NUMBER() on this, to get the row number for a specific grouping (WorksOrderNo)
ROW_NUMBER() Over(partition by Data.WorksOrderNo order by Data.WorksOrderNo) as RowNo,
Is there an equivalent ROW_Count function where I can specify a partition, and return the count of rows for that partition?
ROW_Count() Over(partition by Data.WorksOrderNo order by Data.WorksOrderNo) as RowNo ???
Reason being, this is query being used to drive a report layout.
As part of this, I need to format based on whether the total row count for each WorksOrderNo is >1 or not.
So for instance if there were three rows for a works order, the row_number function currently returns 1, 2 and 3, where the row count would return 3 on each row.
The function is simply COUNT(). In SQL Server, all the aggregation functions can be used as window functions, as long as they do not use DISTINCT.
Note that for the total count, you do not want the ORDER BY:
COUNT(*) Over (partition by Data.WorksOrderNo) as cnt
If you include the ORDER BY, then the COUNT() is cumulative, rather than constant for all rows in the partition.
It looks like you just need group by and count:
select WorksOrderNo, count(*) as Row_Count
from Data
group by WorksOrderNo