MS Access select distinct random values - sql

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()

Related

How can I Order By a column I don't want displayed in the output? [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 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.

Select the first record based of dulpicates [duplicate]

This question already has answers here:
Get top 1 row of each group
(19 answers)
Closed 1 year ago.
I am trying to solve a duplicate issue in SQL server.
I have a lot of duplicate records with the exact information except the column time like:
I would like to select the record that has the max datetime for each subset of duplicates.
I tried to use the MAX() aggregation but that does not work.
Any ideas?
In MS SQL Server, you would do something like this:
SELECT [id], [name], MAX([date])
FROM [table_name]
GROUP BY [id], [name]
SELECT id, name, MAX(date) as maxdate FROM mytable GROUP BY id, name
This should give you exactly the results you describe, in SQL Server.

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;

using row from first part of SQL union in second part

To implement a multiple choice quiz I want to select an answer from my answers table (the "correct" answer) and two "incorrect" answers. Can I do it in one query. I feel I should be able to but I'm not quite getting it. Here is what I have so far:
SELECT correct.answer
FROM (
SELECT answer
FROM answers
ORDER BY random()
LIMIT 1
) correct
UNION
SELECT answer
FROM (
SELECT DISTINCT answer
FROM answers
WHERE answer != correct.answer
ORDER BY random()
LIMIT 2
);
The database engine is SQLite 3 which gives me:
Error: no such column: correct.answer
I can do it with two separate queries but like I said, one should be possible no?

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