I have an error: Type missmatch. I want to get value from Database Access. I want to use Date() function from VBA.
Code:
SELECT TOP 5 Type FROM table Where Date Between #" + Date + "# AND #" + Date - 5 + "# Group By value1, value2 Order By SUM(value3) desc;
Do you have a field called [Date] ? Very bad idea: it's a reserved word.
In the meantime, you can try this way:
SELECT TOP 5 [Type] FROM table
Where [Date] Between Date() and Date()-5
ORDER BY value3 DESC
Your GROUP BY clause does not make sense in this context I think, nor does the SUM() in ORDER BY
Related
I am trying to to run a query like the one below. The goal is to get the total activity count for every user_key but because the user_key has a complex structure and I need only the part after the '|' symbol I had to use a substring function. However, when I'm trying to run the query, I get the
error:
SQL Error [42Y36]: Column reference 'USER_KEY' is invalid, or is part of an invalid expression. For a SELECT list with a GROUP BY, the columns and expressions being selected may only contain valid grouping expressions and valid aggregate expressions.
The substring function works OK outside this query. Any workarounds for this problem? Using Splice Machine (NoSql)
SELECT
substr(user_key, instr(user_key,'|') + 1) AS new_user_key,
SUM(
CAST(
activity_count AS INTEGER
)
) AS Total
FROM
schema_name.table_name
GROUP BY
substr(user_key, instr(user_key,'|') + 1)
Your GROUP BY column needs to match the SELECT
SELECT
substr(user_key, instr(user_key,'|') + 1) AS new_user_key,
SUM(
CAST(
activity_count AS INTEGER
)
) AS Total
FROM
schema_name.table_name
GROUP BY
substr(user_key, instr(user_key,'|') + 1) AS new_user_key
I found the answer myself. I used a table subquery:
SELECT new_table.new_user_key, sum(new_table.total)
from
(
SELECT
substr(user_key, instr(user_key,'|') + 1) AS new_user_key,
CAST(activity_count AS INTEGER) AS Total
FROM schema_name.table_name
)
as new_table
GROUP BY
new_table.new_user_key
Let's hope someone will find this post useful and will save some time to him or her.
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 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'm using a Select 1 from dual statement to see if new data that comes into my system is actually new or not, if it is new then it's gonna be inserted, if it is not then it's gonna be updated in the database.
sql.CommandText = "select 1 from dual where exists (select * from my table where hour = " + hour + " and zone = '" + zone+ "' and date = TO_DATE('" + mydate + "','DD-MM-YY'))"
The problem however is that after running the statement, it returns the 1 value even if the conditions for it aren't met, even if the table is completely empty. How could this happen?
I'm using VB, .NET framework 3.5 and Oracle 10g.
I would suggest something like
SELECT COUNT(*) AS cnt
FROM mytable m
WHERE m.hour = hour
AND m.zone = zone
AND m.date = TO_DATE( mydate,'DD-MM-YY'))
AND ROWNUM = 1
The result will be either 0 or 1. Since you only care about the existence of the row, the ROWNUM = 1 will cause the query to quit as soon as it finds a matching entry and will prevent you from scanning the whole table.
You can use sign(count(1)) to get the count.
select sign(count(1)) cnt
from my_table
where hour = " + hour + " and zone = '" + zone+ "'
and date = TO_DATE('" + mydate + "','DD-MM-YY'))"
I have the following query:
SELECT
title,
(stock_one + stock_two) AS global_stock
FROM
product
ORDER BY
global_stock = 0,
title;
Running it in PostgreSQL 8.1.23 i get this error:
Query failed: ERROR: column "global_stock" does not exist
Anybody can help me to put it to work? I need the availale items first, after them the unnavailable items. Many thanks!
You can always ORDER BY this way:
select
title,
( stock_one + stock_two ) as global_stock
from product
order by 2, 1
or wrap it in another SELECT:
SELECT *
from
(
select
title,
( stock_one + stock_two ) as global_stock
from product
) x
order by (case when global_stock = 0 then 1 else 0 end) desc, title
One solution is to use the position:
select title,
( stock_one + stock_two ) as global_stock
from product
order by 2, 1
However, the alias should work, but not necessarily the expression. What do you mean by "global_stock = 0"? Do you mean the following:
select title,
( stock_one + stock_two ) as global_stock
from product
order by (case when global_stock = 0 then 1 else 0 end) desc, title
In case anyone finds this when googling for whether you can just ORDER BY my_alias: Yes, you can. This cost me a couple hours.
As the postgres docs state:
The ordinal number refers to the ordinal (left-to-right) position of the output column. This feature makes it possible to define an ordering on the basis of a column that does not have a unique name. This is never absolutely necessary because it is always possible to assign a name to an output column using the AS clause.
So either this has been fixed since, or this question is specifically about the ORDER BY my_alias = 0, other_column syntax which I didn't actually need.