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 7 years ago.
Improve this question
I have a simple question: I would like to know how to display records from my database starting from the second record.
Thank you!
If you have a column that specifies the ordering, the ANSI standard way to do this is:
select t.*
from table t
order by col
offset 1 row;
Try this:
select * from
(select
*,
ROW_NUMBER() over (order by TableId) as rn
from TableName) dane where rn>1
in MSSQL.
Related
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 years ago.
Improve this question
select count(*) as distinctname from
(select distinct nationalityname from dbo.profile)
Use an alias for your subqueries
select count(*) as distinctname from
(select distinct nationalityname from dbo.profile) alias_name
or make it simpler like this
select count(distinct nationalityname) from dbo.profile
Just give alias to sub-query
select count(*) as distinctname
from (select distinct nationalityname
from dbo.profile) t -- this is alias
In SQL Server, You need to give alias to sub-queries used in FROM clause.
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 5 years ago.
Improve this question
I am using 11g RDBMS and in one interview interviewer ask me that print 1 to 10 numbers using SQL query don't use loop if you are using PLSQL, means using select query he wants to see the result.
like Table:
1
2
3
Here are two approaches, both are SQL Server syntax, but you will find something similar for other RDBMs:
--ROW_NUMBER()
SELECT TOP 10 ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) FROM sys.objects; --any table with more rows than 10 will do
--recursive CTE
WITH recursiveCounter AS
(
SELECT 1 AS Nr
UNION ALL
SELECT r.Nr+1
FROM recursiveCounter AS r
WHERE r.Nr<10
)
SELECT * FROM recursiveCounter
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 7 years ago.
Improve this question
How to generate the bus_day_mtd column from the Above picture, It should be automatically icremented based on the bus_day_flag= 1 and if bus_day_flag= 0, it should stay same
update T
set bus_day_mtd = (
select sum(bus_day) from T t2 where t2.ymd <= T.ymd
--or cast(bus_day_flag as int) if referencing the bit column
)
Are you wanting this to calculate on the fly instead of in a static column?
In SQL Server 2012+, you can do this with a cumulative sum:
with toupdate as (
select t.*,
sum(case when bus_day_flag = 1 then bus_day end) over (order by ymd) as new_bus_day_mtd
from t
)
update toupdate
set bus_day_mtd = new_bus_day_mtd;
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 7 years ago.
Improve this question
Help me with a SQL query. I would get all values, like '01,02,03,20,92,93,94" values from first query, but instead of function MAX in two query. Result:in opposite b_BookNum I would to see one string:'01,02,03,20,92,93,94'
Simply take out the MAX() function and add this column in your group by clause , happy days......
SELECT [b_BookNum]
,[b_CHMAT]
,COUNT(*) iCount
FROM MPA.dbo.SCHM_Books
GROUP BY [b_BookNum] ,[b_CHMAT]
ORDER BY [b_BookNum]
EDIT
I am surprised that you have all the information you need to solve the problem and yet you are so incompetent to solve it. Anyway here is the full solution:
;WITH CTE AS (
SELECT [b_BookNum]
,COUNT(*) iCount
FROM MPA.dbo.SCHM_Books
GROUP BY [b_BookNum]
)
SELECT [b_BookNum]
,STUFF((SELECT ',' + CAST([b_CHMAT] AS VARCHAR(10))
FROM dbo.SCHM_Books
WHERE [b_BookNum] = C.[b_BookNum]
FOR XML PATH(''),TYPE)
.value('.','NVARCHAR(MAX)'),1,1,'') AS [b_CHMAT]
,iCount
FROM CTE C
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I have a SQL query where I want to order the database on type, order and category. Can I have query having all these three?
select * from t_person where name=xyz order by type,category,order
try this:
Use "" (double quotes) to escape the reserved keyword order
select * from t_person where name='xyz'
order by type,category,"order"
Use double quote for correct query:
select * from t_person where name='xyz' order by type,category,"order"