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;
Related
I have two tables and I would like to have an sql query to get the minimum value of a field of the union of the maximums of the tables.
Set tmpRS = CurrentDb.OpenRecordset(" SELECT MIN(s.TorqueMax) FROM ( " & _
" (SELECT MAX(ds.TorqueMax) FROM tblUIOpTorqueRangeDS ds)" & _
" UNION " & _
" (SELECT MAX(cv.TorqueMax) FROM tblUIOpTorqueRangeCV cv )) as s")
You can do something like this:
SELECT MIN(s.TorqueMax)
FROM (SELECT MAX(ds.TorqueMax) as TorqueMax
FROM tblUIOpTorqueRangeDS ds
UNION
SELECT MAX(cv.TorqueMax) as TorqueMax
FROM tblUIOpTorqueRangeCV cv
) as s;
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've been working on a system service for a client for a while and need some help, I need to merge two sql queries into one. The first part of the query is to look at the master sequence number and count it, after which the query must update a field. I have the two queries below if anyone can help with this problem.
Count query
SELECT master_seq, count(master_seq) as NofH
FROM [ZS_CS_EVO_Integration].[dbo].[CS_Consolidation]
where delivery_date = '2016-07-01'
GROUP BY master_seq
order by master_seq
Update Query
(" UPDATE [dbo].[CS_Consolidation]"
+ " SET [split_dlv] = 1"
+ " FROM [dbo].[CS_Consolidation]"
+ " WHERE"
+ " [master_seq] <> 0 AND CONVERT(DATE,delivery_date) = '" + yesterday + "'", IntConnect);
You can put the first part into CTE, then join and UPDATE:
DECLARE #delivery_date DATE = '2016-07-01'
;WITH cte AS (
SELECT master_seq
FROM [ZS_CS_EVO_Integration].[dbo].[CS_Consolidation]
where delivery_date = #delivery_date and [master_seq] <> 0
GROUP BY master_seq
HAVING count(master_seq) > 1
)
UPDATE c
SET [split_dlv] = 1
FROM [dbo].[CS_Consolidation] c
INNER JOIN cte t
ON t.master_seq = c.master_seq and c.delivery_date = #delivery_date
the code below is my sqlcommand for select statement. Inside have a lots of data including two date data inside.
string sql = "SELECT * FROM TASKMASTER WHERE TASKNAME ='" + TaskName + "'";
can I add in ('DD-MM-YYYY HH24:MI:SS')AS CREATEDATE into the sqlcommand and at the same time will call out all the column in the table?
You could use a table alias.
For example,
SELECT TO_CHAR(t.dt_column, 'DD-MM-YYYY HH24:MI:SS') as CREATEDATE,
t.*
FROM TASKMASTER t
WHERE t.TASKNAME = '" + TaskName + "'"
So, you added your desired column in the beginning of the column list, and also selecting all other columns followed by it.
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