Essentially I have the following Union Query:
SELECT ID, ProductID, AssignedColour AS D, "Assigned" AS Cat, "Colour" & DCount("*","ProductColor","ProductID=" & ProductID & " AND ID<" & ID)+1 AS GrpSeq FROM ProductColor
UNION SELECT ID, ProductID, ColourFinding, "Finding", "Colour" & DCount("*","ProductColor","ProductID=" & ProductID & " AND ID<" & ID)+1 & "_Capacity" FROM ProductColor;
Based on the following Table:
The issue I am having is ProductID was changed from a DataType Number to DataType Short Text.
After the change I get the following error:
DataType Mismatch.
How can the above query be modified to account for this change?
Try with:
SELECT
ID,
ProductID,
AssignedColour AS D,
"Assigned" AS Cat,
"Colour" & CStr(DCount("*","ProductColor","ProductID = '" & ProductID & "' AND ID < " & ID & "") + 1) AS GrpSeq
FROM
ProductColor
UNION
SELECT
ID,
ProductID,
ColourFinding,
"Finding",
"Colour" & CStr(DCount("*","ProductColor","ProductID = '" & ProductID & "' AND ID < " & ID & "") + 1) & "_Capacity"
FROM
ProductColor;
Related
I get the following error:
System.Data.OleDb.OleDbException (0x80040E14): Undefined function 'STR_TO_DATE' in expression.
when I run this query
SELECT
ProductCode,
Description,
SUM(Quantity) as Quantity,
STR_TO_DATE(REPLACE(DateIn, '-', '/'), '%m/%d/%Y') as DateIn
FROM
Product as P,
StockIn as S
WHERE
S.ProductNo = P.ProductNo
AND
STR_TO_DATE(REPLACE(DateIn, '-', '/'), '%m/%d/%Y') BETWEEN '" + StartDate.ToString("yyyy-MM-dd") + "' AND '" + EndDate.ToString("yyyy-MM-dd") + "' GROUP BY P.ProductNo, DateIN ORDER BY DateIn, Description";
Try with Access SQL:
"SELECT
ProductCode,
Description,
SUM(Quantity) as Quantity,
DateValue(DateIn) as DateIn
FROM
Product as P,
StockIn as S
WHERE
S.ProductNo = P.ProductNo
AND
DateValue(DateIn) BETWEEN #" + StartDate.ToString("yyyy-MM-dd") + "# AND #" + EndDate.ToString("yyyy-MM-dd") + "# GROUP BY P.ProductNo, DateValue(DateIn) ORDER BY DateValue(DateIn), Description";
I'm looking for help with making a query quicker -
SELECT DISTINCT waits.ride,
T2.daywait,
T3.timewait,
T4.daytimewait,
T5.currentwait
FROM waits
left join (SELECT Avg(waittime) AS dayWait,
ride
FROM waits
WHERE dayofweek = '" . $dow . "'
GROUP BY ride) AS T2
ON T2.ride = waits.ride
left join (SELECT Avg(waittime) AS timeWait,
ride
FROM waits
WHERE currenttime >= '" . $minusTime . "'
AND currenttime <= '" . $plusTime . "'
GROUP BY ride) AS T3
ON T3.ride = waits.ride
left join (SELECT Avg(waittime) AS dayTimeWait,
ride
FROM waits
WHERE currenttime >= '" . $minusTime . "'
AND currenttime <= '" . $plusTime . "'
AND dayofweek = '" . $dow . "'
GROUP BY ride) AS T4
ON T4.ride = waits.ride
left join (SELECT waittime AS currentWait,
ride
FROM waits
GROUP BY ride
ORDER BY wid DESC) AS T5
ON T5.ride = waits.ride
WHERE park = '" . getPark() . "'
http://pastebin.com/DDSpGFWQ
To give an idea of the application of this query I am using it to populate the parks at this link - http://www.wdwhelper.com . If you need additional info, just let me know.
I am also looking for specific help on subquery T5 - I want to get the most recent (last added to DB) waitTime for each ride - any ideas how this would be possible? Thanks!
Just do one subquery in place of four, and read the table once instead of four times,
and you will save 3/4 (75%) of the time:
SELECT ride ,
Avg( CASE WHEN dayofweek = '" . $dow . "' THEN waittime END) AS dayWait,
Avg( CASE WHEN currenttime >= '" . $minusTime . "'
AND currenttime <= '" . $plusTime . "'
THEN waittime END) AS dayWait,
Avg( CASE WHEN currenttime >= '" . $minusTime . "'
AND currenttime <= '" . $plusTime . "'
AND dayofweek = '" . $dow . "'
THEN waittime END) AS dayWait,
waittime AS currentWait
FROM waits
GROUP BY ride
i am trying to make a costum query in node js using sequelize.
So i have my api call:
router.route('/api/divisionData/:division_id')
.get(function (req, res) {
var division = Division.build();
division.retrieveDataById(req.params.division_id, function (users) {
if (users) {
res.json(users);
} else {
res.status(401).send("Data not found");
}
}, function (error) {
res.send("Data not found");
});
});
And then i have my retrieveDataById:
sequelize.query("select division_id, (modules.user_id) as modules_taken, ROUND(avg(scores.score),2) as average_score from " +
"user join " +
"(select user_id, score from test_score " +
"UNION ALL " +
"select user_id, score from task_score WHERE status_id = 1 " +
") as scores " +
" on user.id = scores.user_id " +
" join ( " +
" SELECT user_id FROM test_score " +
" UNION all " +
" SELECT user_id FROM task_score WHERE status_id NOT IN (3) " +
" UNION all " +
" SELECT user_id FROM elearning_score " +
" union all" +
" SELECT user_id FROM academy_screening " +
" union all" +
" SELECT user_id FROM academy_evaluation " +
" union all " +
" select user_id FROM academy_survey " +
" union al " +
" select user_id FROM offline_score " +
" ) as modules on user.id = modules.user_id WHERE division_id = " + user_id +" group by division_id", {type: sequelize.QueryTypes.SELECT})
.then(onSuccess).error(onError)
i have tested the query in MySql and it works fine however the return of this call is:
data not found
Can anyone tell me what i am doing wrong here?
I have MySQL procedure which is collecting data from 3 tables:
dossier
courrier
courrier_concerne_dossier
Simply I just can't use select * from... I need every dossier_str separated (one dossier_str in each column and all in one big result set).
(SELECT dossier_oid from data.courrier_concerne_dossier where courrier_oid = param_oid LIMIT 1) as 'dossier1_oid',
(SELECT concat(a.prefixe_numero, "-", cast(a.numero as char), " - ",DATE_FORMAT(a.date_ouverture, '%e/%c/%Y'), " - ", b.nom, " - ", a.intitule)
FROM data.dossier as a
JOIN data.client as b on
a.client_oid=b.oid
WHERE a.oid = dossier1_oid) as 'dossier1_str',
(SELECT dossier_oid from data.courrier_concerne_dossier where courrier_oid = param_oid LIMIT 1,1) as 'dossier2_oid',
(SELECT concat(a.prefixe_numero, "-", cast(a.numero as char), " - ",DATE_FORMAT(a.date_ouverture, '%e/%c/%Y'), " - ", b.nom, " - ", a.intitule)
FROM data.dossier as a
JOIN data.client as b on
a.client_oid=b.oid
WHERE a.oid = dossier2_oid) as 'dossier2_str',
(SELECT dossier_oid from data.courrier_concerne_dossier where courrier_oid = param_oid LIMIT 2,1) as 'dossier3_oid',
(SELECT concat(a.prefixe_numero, "-", cast(a.numero as char), " - ",DATE_FORMAT(a.date_ouverture, '%e/%c/%Y'), " - ", b.nom, " - ", a.intitule)
FROM data.dossier as a
JOIN data.client as b on
a.client_oid=b.oid
WHERE a.oid = dossier3_oid) as 'dossier3_str',
How to give where condition in the select query?
ACCESS 2003
MY Query
SELECT RECORDNO, PERSONID, EMPNAME, TITLENAME, DEPARTMENT, NATION,
CARDEVENTDATE, INTIME, OUTTIME, (select TOP 1 F1.CARDEVENTDATE from
tmp_cardevent as F1 where F1.RECORDNO < F2.RECORDNO AND F1.PERSONID =
F2.PERSONID order by F1.RECORDNO DESC, F1.PERSONID DESC) AS PrevDate,
(select TOP 1 F1.OUTTIME from tmp_cardevent as F1 where F1.RECORDNO <
F2.RECORDNO AND F1.PERSONID = F2.PERSONID order by F1.RECORDNO DESC,
F1.PERSONID DESC) AS PrevOut FROM tmp_cardevent AS F2
I want to use:
WHERE CARDEVENTDATE BETWEEN '" & sdate & "' AND '" & edate & "'
From the above query how can I give the where condition, I tried this:
SELECT RECORDNO, PERSONID, EMPNAME, TITLENAME, DEPARTMENT, NATION,
CARDEVENTDATE, INTIME, OUTTIME, (select TOP 1 F1.CARDEVENTDATE from
tmp_cardevent as F1 where F1.RECORDNO < F2.RECORDNO AND F1.PERSONID =
F2.PERSONID order by F1.RECORDNO DESC, F1.PERSONID DESC) AS PrevDate,
(select TOP 1 F1.OUTTIME from tmp_cardevent as F1 where F1.RECORDNO <
F2.RECORDNO AND F1.PERSONID = F2.PERSONID order by F1.RECORDNO DESC,
F1.PERSONID DESC) AS PrevOut FROM tmp_cardevent AS F2
WHERE F2.CARDEVENTDATE BETWEEN '" & sdate & "' AND '" & edate & "'
But it showing “DATA TYPE MISMATCH ERROR”
And also I tried:
SELECT RECORDNO, PERSONID, EMPNAME, TITLENAME, DEPARTMENT, NATION,
CARDEVENTDATE, INTIME, OUTTIME, (select TOP 1 F1.CARDEVENTDATE from
tmp_cardevent as F1 where F1.RECORDNO < F2.RECORDNO AND F1.PERSONID =
F2.PERSONID and F2.CARDEVENTDATE BETWEEN '" & sdate & "' AND '" & edate
& "' order by F1.RECORDNO DESC, F1.PERSONID DESC) AS PrevDate, (select
TOP 1 F1.OUTTIME from tmp_cardevent as F1 where F1.RECORDNO <
F2.RECORDNO AND F1.PERSONID = F2.PERSONID order by F1.RECORDNO DESC,
F1.PERSONID DESC) AS PrevOut FROM tmp_cardevent AS F2 WHERE
F2.CARDEVENTDATE BETWEEN '" & sdate & "' AND '" & edate & "'
But it showing same “DATA TYPE MISMATCH ERROR”
Can any one help me please
Access uses # as the delimiter for date literals, not '. You need to replace accordingly.
Rather than squirting literal strings into your SQL text, consider using prepared statements. Not only does this give you SQL injection protection, you can defer the creation of parameter values to your data access library of choice e.g. the following ADO code uses the dedicated OLE DB provider for the Access database engine to create DATETIME values and the VBA function CDate() to cast strings representing dates to Date values using the Windows regional settings on the local machine (which may not be the same as the machine with the database file, of course):
Dim sDate As String
sDate = 4/1/09" ' ambiguous DATETIME value
Dim eDate As String
eDate = "2009-12-31 23:59:59" ' unambiguous DATETIME value
Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
With cmd
.ActiveConnection = CurrentProject.Connection ' Access UI object
.CommandText = _
"SELECT * " & _
" FROM Cards " & _
" WHERE CARDEVENTDATE BETWEEN :start_date AND :end_date;"
.Parameters.Append .CreateParameter( _
":start_date", adDate, adParamInput, , CDate(sDate))
.Parameters.Append .CreateParameter( _
":end_date", adDate, adParamInput, , CDate(eDate))
Dim rs As ADODB.Recordset
Set rs = .Execute
End With
MsgBox rs.GetString
Even better, make it a stored proc e.g. do this once at design time:
CurrentProject.Connection.Execute _
"CREATE PROCEDURE GetCards " & _
"( " & _
" :start_date DATETIME, " & _
" :end_date DATETIME " & _
") " & _
"AS " & _
"SELECT * " & _
" FROM Cards " & _
" WHERE CARDEVENTDATE BETWEEN :start_date AND :end_date;"
Then at run time each time use this:
.CommandText = _
"EXECUTE GetCards :start_date, :end_date;"
This way if your query needs to change (but the required parameters do not -- you can always append optional parameters with default values, though), you can just change the proc in the one back end rather than having to change SQL code in all the front ends.