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;
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 yesterday.
Improve this question
I'm trying to generate a query to display specific data in specific grouping and order.
The only issue I have is that, to get the proper Order of the data in the columns I need to order it by a column that I have not SELECTED and do not want displayed.
So When I try to ORDER BY that column I get the error:
"Column ** is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause."
Is there a syntax that will allow me to do this?
Here's the query I'm working with:
select Pr.EmployeeNo as EmpNo, EmployeeFName as EmpFName, EmployeeLName as EmpLName,
ProjectName, ProjectStartDate as ProjStartDate, JobName as Job, JobRate, HoursWorked as Hours
from Employee as Em join ProjEmp as Pr on Em.EmployeeNo = Pr.EmployeeNo
join Project as Pt on Pr.ProjectID = Pt.ProjectID
join Job as Jb on Em.JobID = Jb.JobID
Group by Pr.EmployeeNo, EmployeeFName, EmployeeLName, ProjectName, ProjectStartDate, JobName, JobRate, HoursWorked
Stop and read the full error again. It tells you exactly what's going on. Hint: the problem is not that it's missing from the SELECT clause. There's no reason you can't do this:
SELECT ColumnA
FROM [Table1]
ORDER BY ColumnB
The problem is you have a GROUP BY clause:
Column is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.
And this makes sense. Say you have this table:
ColumnA
ColumnB
ColumnC
1
2
3
1
4
9
2
6
7
2
8
5
And then try to run this query:
SELECT ColumnA, MAX(ColumnB)
FROM [Table]
GROUP BY ColumnA
ORDER BY ColumnC
This query tries to ORDER BY ColumnC, but there's more than one value for ColumnC in each group! We have two groups on ColumnA: 1 and 2. Group 1 has two "C" values: 3 and 9. Group 2 also has two "C" values: 7 and 5. Depending on which rows is selected, you could end up with different orders.
ColumnA, though, is okay, because it's part of the GROUP BY expression. That means we know what value to use. MAX(ColumnB) is also okay, because MAX() is an aggregate function. It tells us which value from the group to use in a deterministic way. But the ColumnC reference is ambiguous(!), and so is not allowed.
So in the SQL from the question, you are free to use any of these columns for the ORDER BY clause:
Pr.EmployeeNo, EmployeeFName, EmployeeLName, ProjectName, ProjectStartDate, JobName, JobRate, HoursWorked
If you want to use a different column, you must either alter the grouping (and think carefully on the consequences) or use an aggregate function on the column group.
I figured it out. The answer was to add the column to the GROUP BY.
Because I didn't SELECT the column, it won't display. BUT GROUPing BY the column allows me to ORDER BY it, and it won't display because it wasn't actually SELECTED.
Sorry for the trouble.
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 9 years ago.
Improve this question
I am working on Library Management and for making a report I want to get data from 2 tables which are not interlinked i.e issueData and issueRecord.
The Record contains previous book issued data while the Data table contains only the current book issued Report.
Simply, I want to merge these two queries i.e
Select * From issueRecord where issueDate = '19/07/2013'.
Select * From issueData where issueDate = '19/07/2013'.
Please Help.
If your question was more clear we would be able to help more.
From what I understand you are trying for a union all or union. There is also a chance for cross join also, but that may not be the result you wanted.
There are answers for Union and union all, But I would suggest you to use like the below
Select 'Record', Field_1, Field_2, Field_3 From issueRecord where issueDate = '19/07/2013'
UNION
Select 'Data', Field_A, Field_B, Field_C From issueData where issueDate = '19/07/2013'
With this addition you can find which data is from which table.
In addition to this you can also use cross join
select * from issueRecord CROSS JOIN issueData
but check the data what you are getting.
Your question is not very clear but it seems like you are looking for a UNION or UNION ALL query.
Assuming that what you want is a single result set that includes record from each table, but you are not trying to combine the two tables onto a single line, the syntax would be something along the lines of:
Select Field_1, Field_2, Field_3 From issueRecord where issueDate = '19/07/2013'
UNION
Select Field_A, Field_B, Field_C From issueData where issueDate = '19/07/2013'
The type and order of the columns selected from both tables have to match. UNION will collapse identical records into one output row, just like a distinct clause, while UNION ALL will include every record from each query.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I want to execute two queries having same number of columns. Here is my query, please help me to get the result:
SELECT A.*
FROM (SELECT *
FROM yh5lw_onlinecourse
WHERE state = 1
ORDER BY id DESC) A
union
SELECT A1.*
FROM (SELECT *
FROM yh5lw_practicetest
WHERE state = 1
ORDER BY id DESC) A1
I think you want this:
SELECT * FROM
(SELECT A.id as id, A.field1 as outputname1, A.field2 as outputname2, ... and so on
FROM yh5lw_onlinecourse A
WHERE state = 1
UNION
SELECT A1.*
FROM yh5lw_practicetest A1
WHERE state = 1) myUnion
order by myUnion.id DESC
Pay attention:
If you use UNION instead UNION ALL you discard all duplicated value-
First query must have alias because in this way you named yuor output table. The second query (after UNION) can use * wildcard (but the number of fields must be the same)