Converting MS Access queries to MariaDB - mysql

I'm struggling to create MariaDB SQL commands which will produce the same output as these three queries (below) which I'm currently using with an MS Access database. My Excel VBA script calls the third SQL query command below (Hours to Heat Elecric WH) with only this SQL command, where the date value is substituted dynamically. For the purposes of this question that command would look like this:
SELECT ElectricWH_Data.*
FROM ElectricWH_Data
WHERE (ElectricWH_Data.Date_Reading) > #06/01/19#;
This is an abstract of the resulting table:
Date_Time Date Time Max WH Out Min WH Out
6/27/18 0:52 06/27/18 00.52 60.38 43.56
6/28/18 0:52 06/28/18 00.52 60.50 44.44
6/29/18 0:32 06/29/18 00.32 60.13 45.38
6/30/18 0:32 06/30/18 00.32 60.19 47.13
7/1/18 0:12 07/01/18 00.12 60.50 47.56
7/2/18 0:42 07/02/18 00.42 60.44 44.94
7/3/18 0:42 07/03/18 00.42 60.38 46.88
I would like to duplicate this process but using a MariaDB database and SQL commands. Can you assist?
By the way, I am aware that dates and date formats are handled differently in MariaDB.
Below are the SQL queries from the MS Access database.
GetTemDataByDay:
SELECT
Min(PiSolarWH.Electric_WH_Out) AS MinOfElectric_WH_Out,
Max(PiSolarWH.Electric_WH_Out) AS MaxOfElectric_WH_Out,
Format(PiSolarWH.Date_Reading,'mm/dd/yy') AS TheDay
FROM
PiSolarWH
GROUP BY
Format(PiSolarWH.Date_Reading,'mm/dd/yy');
ElectricWHData:
SELECT
PiSolarWH.Date_Reading,
Format([PiSolarWH.Date_Reading],'mm/dd/yy') AS TheDate,
Format([Date_Reading],'hh.mm') AS DayTime,
GetTempDataByDay.MaxOfElectric_WH_Out AS Expr1,
GetTempDataByDay.MinOfElectric_WH_Out AS Expr2
FROM
GetTempDataByDay, PiSolarWH
WHERE
Format([PiSolarWH.Date_Reading],'mm/dd/yy') = [GetTempDataByDay].[TheDay]
AND GetTempDataByDay.MaxOfElectric_WH_Out = [PiSolarWH].[Electric_WH_Out];
Hours to Heat Elecric WH:
SELECT
PiSolarWH.Date_Reading,
Format([Date_Reading],'hh.mm') AS DayTime,
GetTempDataByDay.MaxOfElectric_WH_Out,
PiSolarWH.Electric_WH_Out,
Format([PiSolarWH.Date_Reading],'mm/dd/yy') AS Expr1
FROM
GetTempDataByDay,
PiSolarWH
WHERE
GetTempDataByDay.MaxOfElectric_WH_Out = [PiSolarWH].[Electric_WH_Out]
AND Format([PiSolarWH.Date_Reading],'mm/dd/yy') = [GetTempDataByDay].[TheDay];

OK, I figured it out! MariaDB's stored VIEWS work like MS Access Stored Queries. I was able to add the three MS Access queries (of course with modified syntax) to the database as stored VIEWS. The work exacgtly like those in MS Access. Here is one example:
CREATE VIEW GetTempDataByDay AS
SELECT
date_reading,
Min(temps.Electric_WH_Out) AS MinOfElectric_WH_Out,
Max(temps.Electric_WH_Out) AS MaxOfElectric_WH_Out,
date(temps.Date_Reading) AS TheDay
FROM
temps
GROUP BY
date(temps.Date_Reading);
Which is then used in the other two VIEWS which I created to duplicate the MS Access stored queries.
Thanks for your time....RDK

Related

Common Table Expressions -- Using a Variable in the Predicate

I've written a common table expression to return hierarchical information and it seems to work without issue if I hard code a value into the WHERE statement. If I use a variable (even if the variable contains the same information as the hard coded value), I get the error The maximum recursion 100 has been exhausted before statement completion.
This is easier shown with a simple example (note, I haven't included the actual code for the CTE just to keep things clearer. If you think it's useful, I can certainly add it).
This Works
WITH Blder
AS
(-- CODE IS HERE )
SELECT
*
FROM Blder as b
WHERE b.PartNo = 'ABCDE';
This throws the Max Recursion Error
DECLARE #part CHAR(25);
SET #part = 'ABCDE'
WITH Blder
AS
(-- CODE IS HERE )
SELECT
*
FROM Blder as b
WHERE b.PartNo = #part;
Am I missing something silly? Or does the SQL engine handle hardcoded values and parameter values differently in this type of scenario?
Kindly put semicolon at the end of your variable assignment statement
SET #part ='ABCDE';
Your SELECT statement is written incorrectly: the SQL Server Query Optimizer is able to optimize away the potential cycle if fed the literal string, but not when it's fed a variable, which uses the plan that developed from the statistics.
SQL Server 2016 improved on the Query Optimizer, so if you could migrate your DB to SQL Server 2016 or newer, either with the DB compatibility level set to 130 or higher (for SQL Server 2016 and up), or have it kept at 100 (for SQL Server 2008) but with OPTION (USE HINT ('ENABLE_QUERY_OPTIMIZER_HOTFIXES')) added to the bottom of your SELECT statement, you should get the desired result without the max recursion error.
If you are stuck on SQL Server 2008, you could also add OPTION (RECOMPILE) to the bottom of your SELECT statement to create an ad hoc query plan that would be similar to the one that worked correctly.

MSSQL to MYSQL - Select from MSSQL , Insert into MySQL

Im trying to Establish a One Direction Sync from a specific query'd recordset from MSSQL (express 2008) to Mysql. Here is that query.
SELECT [datafk]
,[datahistorypk]
,[date]
,[displayText]
FROM [FCentral].[dbo].[DataHistory]
WHERE [sampleNr] =
(SELECT MAX (sampleNr) FROM [FCentral].[dbo].[DataHistory])
This results in a Multiple results. I need to insert each of those results into my "Linked Server" remotely connected MySQL DB Table.
This code works from SSMS and does insert into my MySQL DB.
EXEC (' INSERT INTO `farms`.`CCData` (
`datafk` ,`datahistorypk` ,`date` ,`displayText` )
VALUES ("222", "13", "2017-10-19 14:25:05", "TEST"); ')
at BPF_REMOTE
Ultimately i will need to schedule this query to run automatically, It would be nice if it could run if a change was detected in the MSSQL table but that may be outside of my abilities.
I feel like im close, im just struggling to get the syntax right to convert from MSSQL to MySQL. Can anyone either point to a good example or help me with this query?
Set up a linked server in SQL Server and just do a regular insert:
INSERT INTO BPF_REMOTE.farms.CCData(datafk, datahistorypk, date, displayText)
SELECT [datafk], [datahistorypk], [date], [displayText]
FROM [FCentral].[dbo].[DataHistory]
WHERE [sampleNr] = (SELECT MAX (sampleNr) FROM [FCentral].[dbo].[DataHistory]);

SQL Query nvarchar to date

I am working on SAP HANA Studio and have tried to run SQL command that converts an entire column of field, nvarchar, into one of field, date.
My dates have format: dd-mon-yyyy (i.e '29-Mar-1997') with field nvarchar(11).
I have looked at previous questions and SQL command documentation (for functions like CAST, CONVERT, TO_DATE, STR_TO_DATE) and have not gotten a solution.
Typical errors I get are: Function not recognized, or, Error while parsing Service Date as DATE at function to_date().
Any suggestions?
Thanks
-Diana
Try TO_DATE():
select to_date(col, 'DD-MON-YYYY')
Obviously your database driver/layer in SAP HANA does not support all mySQL functions.
Please connect to your database directly (using command-line or a gui like HeidiSQL) and create a view in your database:
CREATE VIEW view_tablename AS
SELECT STR_TO_DATE(`Service Date`, '%d-%b-%Y') AS ServiceDateDt, * FROM tablename
Then use view_tablename instead of tablename in all your queries - because view_tablename has the additional date field "ServiceDateDt".

Generate 10 queries to run in SSIS

I have a driver table, date_driver_table that contains 10 dates. Jan 2014, Feb 2014, ... Nov2014.
I need to run a query
select * from records_Jan2014 where recdate='Jan 2014'
This is query 1 . After this runs and puts the result set in a SQL server table, query 2,
select * from records_Feb2014 where recdate='Feb 2014'
will then run and do the same insert into SQL server table , and then query 3, and so forth until no dates left in driver table.
So in ssis I have an execute SQL task with full result set enabled that puts all the dates from date driver table in a variable called date with type object and then feeds into a for each loop with a variable called single date type string. A dat flow with source and a destination of a SQL server table. The problem is how do I set up a source to do query 1 then put the results in the table then do query 2 etc.
I was thinking maybe creating 10 files with SQL and then using the ole db source with file as the SQL that needs to run but sure there is a way to do this with the for each loop. Can anyone point me how to this ? The question is how to set up the for each loop so it runs query 1, puts into the table then runs query 2 and puts it into the table and so on until all the records are done.
Used sql command expression pointing to a variable on the ADO.net Source . Then the variable was fed from an execute sql task which gave the list to process.

Using BIDS to extract data from Informix ODBC source with parameters

As it says in the title, I am trying to extract data from my Informix ODBC source with parameters. I have two parameters that i am trying to pass. Both are DateTime and i am trying to get the current starting date for example 2014-10-10 00:00:00 and the ending date 2014-10-10 23:59:59.
If i do this with a normal query:
SELECT * FROM TABLENAME
WHERE STARTDATETIME BETWEEN '2014-10-10 00:00:00' AND '2014-10-10 23:59:59'
Everything works fine. However, if I try and use the parameters that I have set up:
SELECT * FROM TABLENAME
WHERE STARTDATETIME BETWEEN ? AND ?
I get the following error:
Open Database Connectivity (ODBC) error occurred. state: '07001'.
Native Error Code: -11012. [Informix][Informix ODBC Driver]Wrong number of parameters.
I feel like it has to do with my query, but I have been looking and have found nothing. Would anyone be able to help me out? Thanks!
When you use parameterized query ('?') binding is needed.
Likely you may be using ODBC API SQLBindCol for binding it.
The number of parameter in the query (in this case it is 2)
should be matching with number of bind API calls.
Please check your ODBC code to make sure the binding is correctly done.