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.
Related
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;
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
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()
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 want to do multiple queries in SQL at once, I know how to do each query separately but I can't find a way to combine all of them in a single query
for this I use two different tables the first on is called booking
For my first query I want to select only the rows of the Rownumber of which PerfDate=2015-12-10 and PerfTime=16:00:00
My second table is quite huge its called seat and it looks like this
For my second query I want to get all the rows from the seat table which have different RowNumber when combaring with the outcome of the first query
Then from the outcome of the two queries I want to take the rows only of which the zone is front stalls
Can anyone please help me, thanks in advance
You could use the 1st query as a criteria for the first as below. So you would say select everything from the seats table where RowNumber is not in your first query. You could also filter the result of this, so that you only get 'Front Stalls' as below:
Select * from YourSeatTable
where RowNumber not in (Select RowNumber from YourTable1
where PerfDate = '2015-12-10' and PerfTime='16:00:00')
and Zone = 'front stalls'
You could also try this:
select *
from seats s
where not exists (
select 1
from booking
where
perfdate = '2015-12-10'
and perftime = '16:00:00'
and rownumber = s.rownumber
)
and zone = 'front stalls'
This query might run faster than using where in.
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