This question already has answers here:
How to write a (MySQL) "LIMIT" in SQL Server?
(3 answers)
Closed 5 months ago.
I am new SQL user. I am trying to limit the number of rows pulled on SQL Server Management Studio using the following query.
SELECT [Column A]
FROM [DB].[Ordering In DB].[Table]
WHERE [Column B] = 30
LIMIT 3;
I get the following result.
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near 'LIMIT'
Can LIMIT not be used in SQL Server Management Studio? In the end I only want to get to 3 or x number of rows. How do I do this?
You can use top 1,2,x
like
SELECT TOP 10 [Column A]
FROM [DB].[Ordering In DB].[Table]
WHERE [Column B] = 30 ;
Will be showed just 10 first rowns.
Here are the two things to limit your rows in SELECT query:
Use TOP(Number_of_Rows_To_Be_Fetched):
SELECT TOP(5) [Column A] FROM [Table] WHERE [Column B] = 30
This will fetch only 5 records.
Use OFFSET & FETCH
Here, OFFSET means the number of rows you want to skip from top.
FETCH means the next number of rows you want to fetch.
SELECT [Column A]
FROM [Table]
WHERE [Column B] = 30
ORDER BY [Column A]
OFFSET 2 ROWS FETCH NEXT 3 ROWS ONLY
This query will skip first 2 rows and fetch next 3 rows.
Related
I am joining a sub query with a table.
Sub query runs for 5 Seconds (returns 20 records) and the table has only 4 rows.
Sub query:
Select ID, Name, JoinID
FROM tableX
JOIN ..
Sub query Sample Result:
1, xx, 1
2, yy, 2
3, zz, 1
4, vv, 2
5, bb, 1
TableY (ID, Description):
Data
1, test1
2, test2
3, test3
4, test4
My below query is taking more than 30 seconds. What am I doing wrong here? I see no issue with table Stats. Also sub query is not returning any NULL record for JoinID column.
Select sub.*, tab.*
from
(
sub query
) sub
Join tableY on tableY.ID = sub.JoinID
Joining subqueries would be slow in some cases. If your tables has not PK or FK indexes joining would be complex for database engines.
First check your tables. Add primary keys to ID columns if you dont have. Create foreign key on JOINID in TABLEX. If you already done and same result then you must create index on JOINID if this not works also then you must check your server configuration, check database engine documentation (Oracle,Mssql,mysql etc.)
You can use this SQL statement to get same result without using subquery,
SELECT X.*, Y.*
FROM TABLEX as X
JOIN TABLEX as Y ON (X.JOIN_ID=Y.ID)
Use subqueries if you have aggregate functions or multiple joins in subquery sql statement.
Here's my code:
SELECT DISTINCT Column 3
FROM [TestTable].[dbo].data
I get an error on "Column 3". The error is Incorrect syntax near '3'.
My table has no column names so I don't know how to run my Select command on the third column.
If the name of you third column is indeed 'Column 3', you need to run this query:
SELECT DISTINCT [Column 3]
FROM [TestTable].[dbo].data
AFAIK it's impossible to have a table with no column names
Run
USE TestTable
GO
select *
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME='data'
to get the column names
I've got a large data set that I need to get into excel to get some pivot tables and analysis going.
I normally am able to do this as the data never reaches the 1 million line mark. I just do a SQL Server data import and specify my SQL statement.
Here is my current SQL
WITH n AS (
Select A1.AccountID, A1.ParentAccountID, A1.Name
FROM Account AS A1
WHERE A1.ParentAccountID = 92
UNION ALL
SELECT A2.AccountID, A2.ParentAccountID, A2.Name
FROM Account AS A2
JOIN n
ON A2.ParentAccountID=n.AccountID
)
select n.*, D.DeviceID, A.*, P.*
FROM n
LEFT OUTER JOIN
Device AS D
ON D.AccountID = n.AccountID
LEFT OUTER JOIN
Audit as A
ON A.AccountID = n.AccountID
RIGHT OUTER JOIN
DeviceAudit As P
ON P.AuditID = A.AuditID
WHERE A.AuditDate > CAST('2013-03-11' AS DATETIME)
ORDER BY n.AccountID ASC, P.DeviceID ASC, A.AuditDate DESC
This right now is returning to me 100% of what I need. 18 million records for the past 30 days. I was hoping there would be a simple way to find the next 100,000 or 500,000 records.
I can use TOP 100000 to get my first chunk, though I do not seem to have an offset available to me.
At present this runs and completes in 20 minutes. This is 1 of many account hierarchies that I have to perform this for. Hopefully this pagination will not be too expensive cpu wise.
I did try exporting to a CSV in hopes of importing it, though that just gives me a 12GB csv file that I do not have time to and break apart.
Yes, you can do paginated subqueries on the row number since SQL 2005. Add a row number to the select clause of your original query:
, ROW_NUMBER() OVER (ORDER BY {whatever id}) AS row
Then you can make your old query a subquery and query against the row:
SELECT TOP {results per page} *
FROM ({your previous sql statement})
WHERE row > {page# * results per page}
This question already has answers here:
SQL 2008 VS 2012 Error: Incorrect syntax near the keyword 'COMPUTE'
(3 answers)
Closed 4 years ago.
I am trying to execute this simple statement on Northwind database
USE Northwind
SELECT productid, orderid,quantity
FROM [order details]
ORDER BY productid, orderid
COMPUTE SUM(quantity)
GO
But I this can't execute , I got this error
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'COMPUTE'.
The COMPUTE clause is no longer supported in SQL Server 2012. The documentation suggests using ROLLUP instead.
Simple way to do compute:
Select columnname, count(*)
From tablename
Group by columnname
Order by columnname
Select 'SUM' , COUNT(columnname) from tablename
I know i need to use this query to get the list of tables for a schema:
select table_name from all_tables where owner='schema'
I know the following query counts the record in a table:
select count(*) from schema.table
There are 2400+ tables in that schema.
My question is how to count the number of records from all the tables using one step?
You can use DBMS_XMLGEN.GETXMLTYPE function to do this in one shot:
SQL> select table_name
2 , to_number
3 ( extractvalue
4 ( dbms_xmlgen.getxmltype('select count(*) c from ' || table_name)
5 , '/ROWSET/ROW/C'
6 )
7 ) cnt
8 from user_tables
9 order by table_name
10 /
TABLE_NAME CNT
------------------------------ ----------
... [output removed] ...
71 rows selected.
But if your schema contains a lot of data, this might take a long time. Just selecting NUM_ROWS might be sufficient if estimations are ok as well.
Regards,
Rob.
The table ALL_TABLES contains the column NUM_ROWS. (You can get a description of the table with the following SQL statement: DESCRIBE ALL_TABLES;)
The following statement shows the number of records for every table:
SELECT TABLE_NAME, NUM_ROWS FROM ALL_TABLES WHERE OWNER='SCHEMA';
To get the number of records in all tables of your schema, use:
SELECT SUM(NUM_ROWS) FROM ALL_TABLES WHERE OWNER='SCHEMA';