syntax error coming while execute the query [closed] - sql

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.

Related

Write a query using Union all that performs the function of UNION? [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 2 years ago.
Improve this question
Write a query using Union all that performs the function of UNION?
UNION ALL returns all the records while UNION returns distinct records.
so you can use the DISTINCT with UNION ALL to make query same as UNION as follows:
select distinct t.* from
(select * from table1
UNION ALL
select * from table2) t

How to print 1 ,2,3..10 number in one column using sql query [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 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

Get all values from SQL Server [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 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

Start to display result from the second row [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 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.

SQL find distinct values with same id [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 8 years ago.
Improve this question
Hi I am trying to find duplicates with the same ID. I am not finding all of the duplicates. I would like to find distinct JOIN_FID values, and then duplicate JOIN FID values.
Here is my SQL select statements.
select OBJECTID,
Join_Count,
TARGET_FID,
JOIN_FID,
StrtConcat,
PermID,
Minutes,
FacilityID,
Shape
from
sde.gis.MFD_8_minute_response_ladder
where TARGET_FID
in (
select
JOIN_FID
from
sde.gis.MFD_8_minute_response_ladder
group by
JOIN_FID
having
COUNT(*) > 1
You're missing a closing parenthesis ()), but I assume that's a typo.
I suspect the problem may be that you're comparing two different fields:
where TARGET_FID
in (
select
JOIN_FID
Should that be:
where TARGET_FID
in (
select
TARGET_FID
or
where JOIN_FID
in (
select
JOIN_FID
?
For the query that you added ,an IN subquery can only return one value, so you need something like:
where PermID
in (
select
COUNT(1),
TARGET_FID
making sure you're comparing the right columns.