I am trying to create a paging system and came across a post in SO that included code for achieving this. However, when I run my query, it throws the quoted error. I have double checked code but cannot see error. I am using access 2010 as db. Can someone point out my mistake. Thanks
The SELECT statement includes a reserved word or an argument name that
is misspelled or missing, or the punctuation is incorrect.
Dim Row_Per_Page As Integer = 4
Dim TotRows As Integer = 17
Dim Page_Number As Integer = 2
Dim oledbCmd As OleDbCommand = New OleDbCommand("Select TOP '" & Row_Per_Page & "' *, Count(*) As '" & TotRows & "' From [Select Top('" & TotRows & "' - (('" & Page_Number & "' - 1) * '" & Row_Per_Page & "'))From Postings Order By [Date] DESC] Order By [Date] ASC", oledbCnn)
It looks to me like you have a problem towards the end of your query, specifically this part.
From Postings Order By [Date] DESC] Order By [Date] ASC"
First you have two Order By's, and second I think you forgot a ', [' before the 'DESC]' at the '[Date] DESC]' of your first Order By.
Edit: That link was very helpful. It's hard to read such a long query like the one you posted on SO when it's just one line long.
Forget what I said before. I think the problem is in bold here.
"Select TOP '" & Row_Per_Page & "' , Count() As '" & TotRows & "' From [Select Top('" & TotRows & "' - (('" & Page_Number & "' - 1) * '" & Row_Per_Page & "'))From Postings Order By [Date] DESC] Order By [Date] ASC"
You're Naming your count field 17, and screwing up the code listed at that site. A select Count query should look like this.
SELECT Count(*) AS TOTAL FROM Table1
That would return a field called 'TOTAL' in Table1 that tells you how many records exist there.
Use the below for your query and it should work.
Select TOP Row_Per_Page *, Count(*) AS TOTAL From [Select TOP (TotRows - ((Page_Number - 1) * Row_Per_Page) From Postings Order By ColumnName DESC] Order By ColumnName ASC
Related
I have a table with a few dozen fields, 18 of which are fields with varying integer values. I'd like to pull the field names and corresponding values for the five fields with the greatest values for a given record. I'll use the below table as example where I'd like to query only the two greatest values for a given record.
Name, FigureA, FigureB, FigureC
John, 40, 73, 81
Luke, 35, 21, 65
I'd like to return the following for, say, John:
FigureB, 73
FigureC, 81
I've gotten this far:
sSQL = "Select t.* " & _
"From (Select 'A' as [FigureA], FigureA " & _
"From Table) as t " & _
"Union All " & _
"Select t.* " & _
"From (Select 'B' as [FigureB], FigureB " & _
"From Table) as t " & _
"Union All " & _
"Select t.* " & _
"From (Select 'C' as [FigureC], FigureC " & _
"From Table) as t"
In MS Access, this is probably simplest with union all:
select top (5) *
from (select t.*
from (select top (1) "field1" as colname, max(field1) as max_val
from t
) as t
union all
select t.*
from (select top (1) "field2" as colname, max(field2)
from t
) as t
union all
. . .
) as t
order by max_val desc;
Some versions of MS Access don't support union all in the from clause. That component might require a view.
I need to insert multiple rows and fields in Access 2016 with SQL VBA. I'm doing this thing below -with union -but it doesn't work, it says my sintax with union is wrong. I need to figure out what's wrong otherwise run queries one by one and I have 9 like this!
(EDIT: solution found!)
query = "INSERT INTO tblInvoice2 ( Quantity, Price, MMDCarrier) SELECT qryTotals.JRN AS SumOfJRN, qryTotals.[Rate JRN],"
query = query & "qryTotals.MMDCarrier from qryTotals where qryTotals.MMDCarrier like " & Me.Combo0 & " union "
query = query & "SELECT qryTotals.MG AS SumOfMG, qryTotals.[Rate MG],"
query = query & "qryTotals.MMDCarrier from qryTotals where qryTotals.MMDCarrier like " & Me.Combo0 & ""
Here is the answer of what worked, very similar from a code someone showed me below but a differences that changed everything (no error messages and it does insert in the table!). I had to put the selects into a select, still gave me an error and for some reason, when I added the -> as Price and as Quantity it worked. I'm still not sure why it works now, let me know if you have an explanation!
query = "INSERT INTO tblInvoice2 ( Quantity, Price, MMDCarrier ) SELECT * FROM (SELECT qryTotals.JRN as Quantity, qryTotals.[Rate JRN] as Price, qryTotals.MMDCarrier FROM qryTotals where qryTotals.MMDCarrier like '" & Me.Combo0 & "'"
query = query & " UNION SELECT qryTotals.MG , qryTotals.[Rate MG], qryTotals.MMDCarrier From qryTotals where qryTotals.MMDCarrier like '" & Me.Combo0 & "')"
If you're using INSERT INTO ... SELECT with unions, you need to put all unions in a subquery, e.g.:
INSERT INTO tblInvoice2 ( Quantity, Price, MMDCarrier)
SELECT * FROM (
SELECT qryTotals.JRN AS SumOfJRN, qryTotals.[Rate JRN], qryTotals.MMDCarrier from qryTotals
where qryTotals.MMDCarrier like 'something'
UNION
SELECT qryTotals.MG AS SumOfMG, qryTotals.[Rate MG],
qryTotals.MMDCarrier from qryTotals where qryTotals.MMDCarrier like 'somethingElse'
) As A
Or, in your VBA implementation:
query = "INSERT INTO tblInvoice2 ( Quantity, Price, MMDCarrier) SELECT * FROM (SELECT qryTotals.JRN AS SumOfJRN, qryTotals.[Rate JRN],"
query = query & "qryTotals.MMDCarrier from qryTotals where qryTotals.MMDCarrier like " & Me.Combo0 & " union "
query = query & "SELECT qryTotals.MG AS SumOfMG, qryTotals.[Rate MG],"
query = query & "qryTotals.MMDCarrier from qryTotals where qryTotals.MMDCarrier like " & Me.Combo0 & ") As A"
Note that your query also contains a trailing comma in the INSERT INTO part, which isn't a syntax error in Access SQL, but is one in most other forms of SQL, and still is a bad practice. Also, LONG was probably right, you want those single quotes (but really, you should use parameters instead)
I am trying to Count the distinct Number of UserID's in a table for each Weekday (e.g. 545 UserID's on Weekday 1 = Monday, 120 UserID's on Weekday 2 = Tuesday etc.). I am doing this in Access Visual Basic, but the syntax should be universal to SQL. Here is my VB Code:
sSQL = " SELECT Weekday(" & tablename & ".[DATE]) AS WEEKDAY, Count(DISTINCT " & tablename & ".[UserID]) AS User_COUNT"
sSQL = sSQL & " FROM " & tablename
sSQL = sSQL & " GROUP BY Weekday(" & tablename & ".[DATE])"
qdf.SQL = sSQL
The plain SQL Syntax should look like this (edited based on comments & test):
SELECT Weekday(tbl.[Date]) AS WEEKDAY, Count(DISTINCT tbl.[UserID]) AS User_COUNT
FROM tbl
GROUP BY Weekday(tbl.[Date])
..whereas [Date] is a field in tbl formatted as Datetime and [UserID] is a field formatted as Long (with duplicates).
When I try to run the command it tells me "Missing Operator in Query-Syntax.."
Is this a problem of my VB Code or is the SQL Syntax wrong?
MS Access database engine does not support COUNT(DISTINCT ...).
To workaroud it, please see this thread: SQL : how can i count distinct record in MS ACCESS where author suggests to solve issue by using subquery:
SELECT
user_id
, COUNT(*) AS count_distinct_clients
FROM
( SELECT DISTINCT
user_id,
client_id
FROM tbl_sActivity
) AS tmp
GROUP BY
user_id ;
Change the query code to your needs.
[EDIT]
SELECT
wday, COUNT(UserId) AS count_distinct_users
FROM
( SELECT DISTINCT WEEKDAY([Date]) AS wday, UserId
FROM tblName
) AS tmp
GROUP BY
wday;
I want to update one duplicate record and delete the rest of them
I have a table...
id start unit
1 1 a
2 1 a
3 2 a
4 2 a
5 3 b
which I want to turn into...
id start unit
1 1 b
3 2 b
5 3 b
I have sorted out finding the duplicates but I'm failing to be able to do anything with them - or in the example below I end up deleting all the duplicates and not keeping the first record.
I'm also not convinced that this method will be the best way to approach the problem
strSQL_dup = "SELECT start_date, id
FROM test
WHERE start_date IN
(SELECT start_date
FROM test WHERE adId = " & adId & " AND Unit = " & scId & "
GROUP BY start_date HAVING (COUNT(start_date ) > 1) )
GROUP BY test.id, start_date"
Set rsSQL_dup = conn.Execute(strSQL_dup)
Do While Not rsSQL_dup.EOF
start_date = rsSQL_dup.Fields("start_date").value
id = rsSQL_dup.Fields("id").value
'Response.Write("<p> "&start_date&" | "& id & " update me </p>"
if bookingstate = "Provisional" Then
dateStatusNo = 8
else
dateStatusNo = 1
End if
'Response.Write("<p> " & id & " update me</p>")
strSQL_dup_update = "UPDATE test SET Unit = '" & dateStatusNo & "' WHERE id = '" & id & "' "
Set rsSQL_dup_update = conn.Execute(strSQL_dup_update)
strSQL_delete = "SELECT top 1 start_date, id FROM test WHERE start_date = " & start_date
Set rsSQL_delete = conn.Execute(strSQL_delete)
Do While Not rsSQL_delete.EOF
start_date = rsSQL_delete.Fields("start_date").value
id = rsSQL_delete.Fields("id").value
'Response.Write("<p> "&start_date&" | "& id & " delete me</p>")
strSQL_dup_delet = "DELETE FROM test WHERE id = '" & id & "' "
Set rsSQL_dup_delet = conn.Execute(strSQL_dup_delet)
rsSQL_delete.MoveNext()
Loop
rsSQL_dup.MoveNext()
Loop 'End Do While Not rsSQL_dup.EOF
Loop
i agree with #Ken white how a change into b but if you want to remove duplicate record
why dont you use distinct command like
select distinct start_date
FROM test
and if you want your query to run then you can combine it with your query like
select distinct start_date
FROM test WHERE start_date IN
(SELECT start_date
FROM test WHERE adId = " & adId & " AND Unit = " & scId & "
GROUP BY start_date HAVING (COUNT(start_date ) > 1) )
GROUP BY test.id, start_date"
and if you want to delete records then use this query
WITH test AS(
SELECT [col1], [col2], [col3], [col4], [col5], [col6], [col7],
RN = ROW_NUMBER()OVER(PARTITION BY col1 ORDER BY col1)
FROM dbo.Table1
)
DELETE FROM test WHERE RN > 1
I'll be honest. You can probably put this together with a merge or multiple statements in a transaction. But, why not just create a temporary table, truncate the original table, and then re-insert the data?
select min(id), start, 'b' as unit
into #test
from test
group by start;
truncate table test;
insert into test(id, start, unit)
select id, start, unit
from #test;
drop table #test;
I have the following SQL string:
sSQL_Select = "SELECT distinct local_insurer, subsidiary, location FROM T_WILMA WHERE PARENT=" & lParent_ID & _
" AND ACC_YEAR=" & lAcc_Year_ID & " AND ZOMBIE = FALSE GROUP BY local_insurer ORDER BY " & _
"subsidiary ASC, location ASC;"
Access throws an error because I cannot do this.
What I need is all records with a distinct local insurer, but in alphabetical order of the subsidiary and location fields.
Somehow, I never seem to get the right results.
Any ideas?
Remove the GROUP BY local_insurer from the query. (distinct will ensure you get only unique combinations of values.)
The reason that you're getting the error is because you did not include all the fields in the SELECT in GROUP BY. For example , if you SELECT a,b,c , you need to GROUP BY a,b,c (or c,b,a...) .
sSQL_Select = "SELECT distinct local_insurer, subsidiary, location FROM T_WILMA WHERE PARENT=" & lParent_ID & _
" AND ACC_YEAR=" & lAcc_Year_ID & " AND ZOMBIE = FALSE GROUP BY local_insurer, subsidiary, location ORDER BY " & _
"subsidiary ASC, location ASC;"