INSERT-SELECT not working with FETCH NEXT [x] ROWS [closed] - sql

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have query
INSERT INTO MYTABLE(NAME,NUM)
SELECT NAME,NUM
FROM DATA
INNER JOIN T3
ON MYTABLE.NUM = T3.NUM
ORDER BY MYTABLE.NUM
OFFSET 0 ROWS
FETCH NEXT 5 ROWS ONLY;
Running this results in following error:
SQL Error: ORA-00918: column ambiguously defined
But when I run above query without the OFFSET part it works.
Is FETCH incompatible with INSERT SELECT?

Yes, FETCH is compatible with INSERT...SELECT. I'm posting this as answer rather than a comment, because it is the answer to the question you posted.
The most common cause of ORA-00918 with the FETCH clause is the caveat that none of the variables in the select list may have the same name. E.g., this SELECT is legal:
SELECT object_type, object_name, object_name
FROM dba_objects
WHERE OBJECT_NAME LIKE 'USER%'
ORDER BY 1, 2
.. and this one is not ...
SELECT object_type, object_name, object_name
FROM dba_objects
WHERE OBJECT_NAME LIKE 'USER%'
ORDER BY 1, 2
OFFSET 0 ROWS
FETCH NEXT 5 ROWS ONLY;
Check your query and make sure all the column names / aliases are unique.
UPDATE
Reviewing your SQL Fiddle, this is a garden variety ORA-00918. It has nothing to do with either your INSERT or your FETCH. You are joining two tables that have the same columns in them and you are not specifying which you want to select and order by. Change your statement to this:
insert into t2(id, val)
select t.id, t.val
from t
inner join t3
on t.val=t3.val
order by t.id desc
offset 3 rows
fetch first 5 rows only

Related

Difference between two similar queries that cause different results [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
The community reviewed whether to reopen this question last month and left it closed:
Original close reason(s) were not resolved
Improve this question
I have a table tbl and I want to extract count of some data from it.
As simplified, I have two choices:
select count(if(c10=7,1,null)) as recordCount
from tbl
where cId in (
select max(cId)
from tbl
group by c37
);
and
select count(*) as recordCount
from (
select max(cId)
from tbl
where c10 = 7
group by c37
) t1;
The first query returns 151 and the second returns 156.
Why do these two queries have different results?
Select Count(if(c10=7,1,null)) as recordCount
from tbl
where cId in (select max(cId)
from tbl
group by c37);
In English -- for every grouping of c37 take the one with max id -- from records that match those ids if c10 = 7 then count them
Select Count(*) as recordCount
from (Select max(cId)
from tbl
where c10=7
group by c37) t1;
In English -- for every grouping of c37 with c10 is 7 take the max id -- count those.
I wrote the above to try and figure out what the queries are doing. Then I saw the issue as described below.
As for your question, it makes no sense the numbers would be different until you consider that max(cID) could be null in the second one but can't be in the first one. -> You can have null in a row in a table, but you can't have it when you use an IN statement.
To show this is the case run the following query
SELECT X
FROM (
Select max(cId) as X
from tbl
where c10=7
group by c37
) Z
WHERE X IS NULL
I'd expect you to get 4 rows back.
To get the same result for both queries use the following for the 2nd query
Select Count(X) as recordCount
from (Select max(cId) X
from tbl
where c10=7
group by c37) t1;

COUNT(*) returns less number of rows [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 4 years ago.
Improve this question
I have view that returns 5167 rows when doing
SELECT * FROM MY_VIEW -- returns 5167 rows
But, if I do the COUNT(*) on that, I get a totally different number
SELECT COUNT(*) FROM MY_VIEW -- returns 3112 rows
I've tried EXEC sp_refreshview 'MY_VIEW' but still keep getting above results.
I thought COUNT(*) is supposed to return a full row count (not filter out any null content as its not being executed for a column and thus should match number of rows returned using SELECT *
Any reason why that could be different?
I am using SQL Server 2012
** Updating with more info based on comments **
Did the following as mentioned in the comments
I modified the command to reflect server name and DB name.
confirmed that they are running in the same SSMS tab.
Added schema name 'dbo.MY_VIEW' to the query
Ran 'sp_updatestats'
Queries thus run in the SSMS tab are -
select ##SERVERNAME, DB_NAME(), count(*) from dbo.MY_VIEW;
-- returns 3112
select ##SERVERNAME, DB_NAME(), * from dbo.MY_VIEW;
-- returns 5167 rows in results
select ##SERVERNAME, DB_NAME(), *, COUNT(*) over () cnt FROM dbo.MY_VIEW;
-- returns 5167 rows in results and 5167 as the count
** Update 2 **
I found that if I run the COUNT('column_1'), then I get the result 3112, but if I do the COUNT('any_other_column'), am getting 5167! I checked and there are no NULLs in column_1. Also, COUNT(*) should count the NULL's anyways and should not be focussed on a column, but should count the number of rows. So, fail to further understand any of the above results.

MS Access select distinct random values

How can I select 4 distinct random values from the field answer in MS Access table question?
SELECT TOP 4 answer,ID FROM question GROUP BY answer ORDER BY rnd(INT(NOW*ID)-NOW*ID)
Gives error message:
Run-time error '3122': Your query does not include the specified
expression 'ID' as part of an aggregate function.
SELECT DISTINCT TOP 4 answer,ID FROM question ORDER BY rnd(INT(NOW*ID)-NOW*ID)
Gives error message:
Run-time error '3093': ORDER BY clause (rnd(INT(NOWID)-NOWID))
conflicts with DISTINCT.
Edit:
Tried this:
SELECT TOP 4 *
FROM (SELECT answer, Rnd(MIN(ID)) AS rnd_id FROM question GROUP BY answer) AS A
ORDER BY rnd_id;
Seems to work sofar..
I suggest:
SELECT TOP 4 answer
FROM question
GROUP BY answer
ORDER BY Rnd(MIN(ID));
I don't think the subquery is necessary. And including the random value on the SELECT doesn't seem useful.
I've creted a simple quiz application 2 years ago, and this is the query that I use to get a random question from the table.
SELECT TOP 4 * FROM Questions ORDER BY NEWID()

Selecting all columns using distinct against one specific column [duplicate]

This question already has answers here:
SELECT DISTINCT on one column
(7 answers)
Closed 8 years ago.
I appreciate that this question has been asked before but I am struggling to find an answer that will even run within Oracle 10g (10.2.0.5.0)
I have a table called BASIC which contains approximately 70 columns. Currently, I return a specified number of rows using the following code (as an example) - the result being the first 20 members who have a MEMBNO after 5000
SELECT * FROM BASIC WHERE MEMBNO>5000 AND ROWNUM <=20 ORDER BY MEMBNO;
Within the 20 rows returned, several of the rows have the same value in the NINO column
I would like to modify my SELECT statement to return the next 20 rows with distinct/unique NINO values
Simply wrapping a DISTINCT around the * gives me an ORA-00936: missing expression error, plus it would not be as precise as I would like.
Can you try the code below:- I have used analytical query concept to fetch only distinct nino values.
select * from
(SELECT b.*,row_number() over (partition by nino order by MEMBNO ) rn
FROM BASIC b WHERE MEMBNO>5000)
where rn =1 AND ROWNUM <=20 ORDER BY MEMBNO;
Let me know in case you encounter any issues.
I think I have found a solution via another source
This shows the rows where there are duplicates...
select * from basic where rowid not in (select min(rowid) from basic group by nino)
This shows the rows with the duplicate rows removed...
select * from basic where rowid in (select min(rowid) from basic group by nino)
Then I can add my row count and membno filters for the final result...
select * from basic where rowid in (select min(rowid) from basic where membno>6615 group by NINO) and rownum <=20 order by membno;

Remove duplicate rows on a SQL query [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Duplicate result
Interview - Detect/remove duplicate entries
I have a SQL Query, which returns a table with one column.
The returned data may be duplicate. for example, my query may be something like:
SELECT item FROM myTable WHERE something = 3
and the returned data may be something like this:
item
-----
2
1
4
5
1
9
5
My Question is, How to remove duplicated items from my query?
I mean, I want to get these results:
item
-----
2
1
4
5
9
Please note that I don't want to change or delete any rows in table. I just want to remove duplicates in that query.
How to do that?
SELECT DISTINCT item FROM myTable WHERE something = 3
As noted, the distinct keyword eliminates duplicate rows—where the rows have identical values in column—from the result set.
However, for a non-trivial query against a properly designed database, the presence of duplicate rows in the result set — and their elimination via select distinct or select ... group by is, IMHO, most often a "code smell" indicating improper or incorrect join criteria, or a lack of understanding of the cardinalities present in relationships between tables.
If I'm reviewing the code, select distinct or gratuitous group by without any obvious need present will get the containing query flagged and that query gone over with a fine toothed comb.
You need to add the DISTINCT keyword to your query.
This keyword is pretty standard and supported on all major databases.
See DISTINCT refs here
SELECT DISTINCT item FROM myTable WHERE something = 3
You just have to use distinct