Access database Date type mismatch - vb.net

'Sql = "Select ItemName,Count(itemID) from tblItem where Item ='" & "A" & "' AND ExpireDate < '" & Now().Date() & "' Group By ItemName"
I'm FACING Datatype miss match in this query in DATE Field....
How could I do this?

I think you need to escape your date with the pound "#" symbol.
Try this:
"Select ItemName,Count(itemID) from tblItem where Item ='" & "A" & "' AND ExpireDate < #" & Now().Date() & "# Group By ItemName"

If you want to filter with current date in MS Access just use Date. In where clause date should be surrounded by # sign
SQL = "Select ItemName,Count(itemID) from tblItem where Item ='" & "A" & "' AND ExpireDate < #" & Date & "# Group By ItemName"

Use the Date() function of Access:
Sql = "Select ItemName, Count(itemID) From tblItem Where Item ='A' And ExpireDate < Date() Group By ItemName"

Related

Multiple columns from one column

I track the aging of customer invoices.
The below code example returns the balance of customer invoices by customer that are between 0 and 30 days old.
However, I want to run one query that pulls separate columns into Excel for each customer's balance aged between 0 and 30 days, 31 and 60, 61 and 90, and finally over 90 days.
I am hoping to pull this into Excel so the columns are as follows-
Customer Number, Customer Name, Balance(That is under 30 days old), Balance(30-59 days), Balance(60-90), Balance(Over 90)
vtSql = ""
vtSql = vtSql & " SELECT CUSTNUM, CUSTNAME, SUM(BALANCE) "
vtSql = vtSql & " FROM VIEWALLINVOICES "
vtSql = vtSql & " WHERE BALANCE <> '0' AND INVDATE BETWEEN #" & Application.Text(Range("TODAY") - 30, "mm/dd/yyyy") & "# AND #" & Application.Text(Range("TODAY"), "mm/dd/yyyy") & "#"
vtSql = vtSql & " GROUP BY CUSTNUM, CUSTNAME "
vtSql = vtSql & " ORDER BY SUM(BALANCE) DESC;"
I am using a MS Access database and I am pulling this directly into Excel using an ADODB Connection and "Microsoft.Jet.OLEDB.4.0".
You seem to want conditional aggregation. In MS-Access, you should be able to phrase this as:
SELECT CUSTNUM, CUSTNAME,
SUM(IIF(INVDATE BETWEEN DateAdd('d', -30, date()) AND date() , BALANCE, 0)) AS BALANCE_0_30,
SUM(IIF(INVDATE BETWEEN DateAdd('d', -60, date()) AND DateAdd('d', -31, date()), BALANCE, 0)) AS BALANCE_31_60,
SUM(IIF(INVDATE BETWEEN DateAdd('d', -90, date()) AND DateAdd('d', -61, date()), BALANCE, 0)) AS BALANCE_61_90,
SUM(IIF(INVDATE <= DateAdd('d', -91, date()) , BALANCE, 0)) AS BALANCE_OVER_91
FROM VIEWALLINVOICES
GROUP BY CUSTNUM, CUSTNAME
This computes the dates ranges dynamically by offseting the current date - which is how I understood your question.

Adding an IFNULL condition for a count

I am having an issue getting a record produced if my count = 0.
Basically my query is counting logons based on some criteria, but if there has never been a logon for a specific customerID I still want it to show as 0.
I have tried IFNULL on the count but with no joy.
SELECT
CUSTOMERID,
CASE
WHEN STATUSCODE = 600 THEN 'Successful Logon'
ELSE 'Unsuccessful Logon'
END as LogonStatus,
COUNT( * ) COUNTOFACCOUNTS
FROM
SCEMEA.TABLENAME
WHERE
CUSTOMERID in ('"+join(Parameters!CustomerID.Value, "','")+"')
AND (Cast(DATETIME as Date) >= '"& Format(Parameters!FromDate.Value, "yyyy-MM-dd") & "'
AND Cast(DATETIME as Date) <= '" & Format(Parameters!ToDate.Value, "yyyy-MM-dd") & "')
AND COMPONENTDESCRIPTION = 'RandomText'
AND METHOD = 'RandomText'
GROUP BY
CUSTOMERID,
CASE
WHEN STATUSCODE = 600 THEN 'Successful Logon'
ELSE 'Unsuccessful Logon'
END
ORDER BY
CUSTOMERID ASC
Please let me know if you need anymore information, any help would be appreciated.

Would like a way to improve my my SQL query in ASP

I would simply like to get the latest record from the Division_Program column.
The data within the "JMSDay" column data are populated with the seven days of the week. I used an array to abbreviate the days (i.e. Mon thru Sun). the "JMS_UpdateTime" column data uses the following format 8/4/2013 1:02:12 PM
SELECT top 1 *
FROM CensusFacility_Records
WHERE JMSDay = '" & smDateArray (Weekday(date)) & "'
AND Division_Program ='" & divrec &"'
order by JMS_UpdateDateTime desc
WITH Census AS
(
SELECT
*
,ROW_NUMBER() OVER(ORDER BY JMS_UpdateDateTime DESC) AS RowID
FROM CensusFacility_Records
WHERE JMSDay = '" & smDateArray (Weekday(date)) & "'
AND Division_Program ='" & divrec &"'
)
SELECT * FROM Census
WHERE RowID=1;

(using join) I get not contained in either an aggregate function or the GROUP BY error

I get the following error.
[Microsoft][ODBC SQL Server Driver][SQL Server]Column 'STATS.VisitorIP' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I used VISITORIP column in both select lists. I used GROUP BY clause in one of them so I addded VISITORIP to it. But I dont have GROUP BY clause in other select so did not add it. Why do I receive this error?
SELECT S.VISITORIP, S.HTTPADDRESS, S.DATEENTERED
FROM STATS S
JOIN (SELECT S1.VISITORIP, MAX(S1.DATEENTERED) FROM STATS S1 WHERE DATEENTERED BETWEEN '" & OnlineTime & "' AND '" & NOW() & "') S1
ON S.VISITORIP = S1.VISITORIP AND S.DATEENTERED = S1.DATEENTERED
WHERE S.DATEENTERED BETWEEN '" &TIME& "' AND '" & NOW() & "'
GROUP BY S.VISITORIP, S.HTTPADDRESS, S.DATEENTERED
ORDER BY S.DATEENTERED DESC
You need the GROUP BY in the nested subquery because you are trying to get the MAX DATEENTERED for each VISITORIP. VISITORIP exists in the SELECT for that subquery, so you need the GROUP BY in there also in order for it to return the latest date for each visitorip.
SELECT S.VISITORIP, S.HTTPADDRESS, S.DATEENTERED
FROM STATS S
JOIN (SELECT S1.VISITORIP, MAX(S1.DATEENTERED) FROM STATS S1 WHERE DATEENTERED BETWEEN '" & OnlineTime & "' AND '" & NOW() & "' GROUP BY S1.VISITORIP) S1
ON S.VISITORIP = S1.VISITORIP AND S.DATEENTERED = S1.DATEENTERED
WHERE S.DATEENTERED BETWEEN '" &TIME& "' AND '" & NOW() & "'
GROUP BY S.VISITORIP, S.HTTPADDRESS, S.DATEENTERED
ORDER BY S.DATEENTERED DESC
This (nested query) is not valid:
SELECT S1.VISITORIP, MAX(S1.DATEENTERED)
FROM STATS S1
WHERE DATEENTERED BETWEEN '" & OnlineTime & "' AND '" & NOW() & "'
It should be:
SELECT S1.VISITORIP, MAX(S1.DATEENTERED) AS DATEENTERED
FROM STATS S1
WHERE DATEENTERED BETWEEN '" & OnlineTime & "' AND '" & NOW() & "'
GROUP BY S1.VISITORIP
Your Inner select uses MAX on DATEENTERED but does not include a groupby on the VisitorIP.
SELECT S.VISITORIP, S.HTTPADDRESS, S.DATEENTERED
FROM STATS S
JOIN (SELECT S1.VISITORIP, MAX(S1.DATEENTERED)
FROM STATS S1
WHERE DATEENTERED BETWEEN '" & OnlineTime & "' AND '" & NOW() & "'
GROUPBY S1.VISITORIP) S1
ON S.VISITORIP = S1.VISITORIP AND S.DATEENTERED = S1.DATEENTERED
WHERE S.DATEENTERED BETWEEN '" &TIME& "' AND '" & NOW() & "'
GROUP BY S.VISITORIP, S.HTTPADDRESS, S.DATEENTERED
ORDER BY S.DATEENTERED DESC
You're getting the error because your subselect in the JOIN:
SELECT S1.VISITORIP, MAX(S1.DATEENTERED) FROM STATS S1 WHERE DATEENTERED BETWEEN '" & OnlineTime & "' AND '" & NOW() & "'
Is attempting to aggregate "DATEENTERED", but needs to group on "VISITORIP" in order to do so.
Try:
SELECT S1.VISITORIP, MAX(S1.DATEENTERED) FROM STATS S1 WHERE DATEENTERED BETWEEN '" & OnlineTime & "' AND '" & NOW() & "' GROUP BY S1.VISITORIP

tracking IP addresses

I want to track IP addresses visited my website. I want to know what time and what page they visit. I store ip address in VISITORIP, date entered in DATEENTERED and page URL in HTTPADDRESS columns.
I want to group them by dates. My outcome should be like:
TIME PAGE
7/12/2009
3:16:27 PM ?Section=products&SubSection=products&CATEGORYID=1
2:46:50 PM ?Section=products&SubSection=products&CATEGORYID=8
2:20:45 PM ?Section=products&SubSection=products&CATEGORYID=11
7/11/2009
9:34:28 AM ?Section=products&SubSection=products&CATEGORYID=7
9:33:31 AM ?Section=products&SubSection=products&CATEGORYID=2
7/10/2009
9:53:16 PM ?Section=products&SubSection=products&CATEGORYID=9
9:34:14 PM ?Section=products&SubSection=products&CATEGORYID=4
9:27:11 PM ?Section=products&SubSection=products&CATEGORYID=3
9:15:47 PM ?Section=products&SubSection=products&CATEGORYID=15
Problem is, I could not group them by dates. I get the outcome as:
07/12/2009
7/12/2009 3:16:27 PM /html/default.aspProcess=HomeNewSeason&IMAGECONTENT=bg_home_newtaste.gif
7/12/2009 3:16:27 PM /html/default.aspProcess=HomeBestSeller&IMAGECONTENT=bg_home_customerschoice.gif
07/12/2009
7/12/2009 3:16:27 PM /html/default.aspProcess=HomeNewSeason&IMAGECONTENT=bg_home_newtaste.gif
7/12/2009 3:16:27 PM /html/default.aspProcess=HomeBestSeller&IMAGECONTENT=bg_home_customerschoice.gif
07/09/2009
7/9/2009 5:37:02 PM /html/default.aspSection=checkout
07/09/2009
7/9/2009 5:37:02 PM /html/default.aspSection=checkout
07/09/2009
7/9/2009 5:37:02 PM /html/default.aspSection=checkout
My codes:
<%
Case "TrackIP"
IPADDRESS = Request.QueryString("IPADDRESS")
SQL = "SELECT CONVERT(VARCHAR(10), DATEENTERED, 101) AS DATEENTERED"
SQL = SQL & " FROM STATS"
SQL = SQL & " WHERE VISITORIP = '"& IPADDRESS &"'"
SQL = SQL & " GROUP BY DATEENTERED"
SQL = SQL & " ORDER BY DATEENTERED DESC"
Set objDateEntered = objConn.Execute(SQL)
%>
<table>
<thead>
<tr>
<th>Time</th>
<th>Page</th>
</tr>
</thead>
<tbody>
<% Do While Not objDateEntered.EOF %>
<tr class="row-title">
<td colspan="2"><%=objDateEntered("DATEENTERED")%></td>
</tr>
<%
SQL = "WITH LASTVISIT AS ("
SQL = SQL & " SELECT VISITORIP, MAX(DATEENTERED) AS DATEENTERED"
SQL = SQL & " FROM STATS"
SQL = SQL & " WHERE VISITORIP = '"& IPADDRESS &"' AND CONVERT(VARCHAR(10), DATEENTERED, 101) = CONVERT(VARCHAR(10), '"& objDateEntered("DATEENTERED") &"', 101)"
SQL = SQL & " GROUP BY VISITORIP"
SQL = SQL & " )"
SQL = SQL & " SELECT STATS.VISITORIP, STATS.HTTPADDRESS, STATS.DATEENTERED"
SQL = SQL & " FROM STATS"
SQL = SQL & " INNER JOIN LASTVISIT"
SQL = SQL & " ON LASTVISIT.VISITORIP = STATS.VISITORIP AND LASTVISIT.DATEENTERED = STATS.DATEENTERED"
SQL = SQL & " ORDER BY STATS.DATEENTERED DESC"
Set objTrackIP = objConn.Execute(SQL)
%>
<% Do While Not objTrackIP.EOF %>
<tr class="row-a">
<td><%=objTrackIP("DATEENTERED")%></td>
<td class=""><%=objTrackIP("HTTPADDRESS")%></td>
</tr>
<%
objTrackIP.MoveNext
Loop
%>
<%
objDateEntered.MoveNext
Loop
%>
</tbody>
</table>
edit: I think problem is with my outer query. But I grouped them by DATEENTERED field, I dont know what else can I do.
First query should be
"SELECT
DISTINCT CONVERT(VARCHAR(10), DATEENTERED, 101) AS vDATE
FROM
STATS
WHERE
VISITORIP = '"& IPADDRESS &"'"
ORDER BY
DATEENTERED DESC"
then print the vData column
and the 2nd SQL
SELECT
VISITORIP, HTTPADDRESS, CONVERT(VARCHAR(8), DATEENTERED, 8) AS vTIME
FROM
STATS
WHERE
CONVERT(VARCHAR(10), DATEENTERED, 101) = '" & objDateEntered("vDATE") &"'
AND VISITORIP = '"& IPADDRESS &"'
ORDER BY VISITORIP ASC, DATEENTERED DESC
I think this will do the trick..
The problem is in next code line:
SELECT CONVERT(VARCHAR(10), DATEENTERED, 101) AS DATEENTERED
and then you group by this date
GROUP BY DATEENTERED
ORDER BY DATEENTERED DESC
You group and order by the varchar.
As I think, you need to use another alias to date when you convert it ro varchar, but you really need to order by dateentered that is datetime type.
It you're trying to get some visitor tracking happening have you thought of using something like Google Analytics?