SQLite ODBC Query Output in Microsoft Query - excel

I have connected a SQLite database to an Excel worksheet via Microsoft Query and an ODBC connection (using the driver at this link). In this SQLite database, I have several views that do various calculations and I need them rendered in Excel. All the data is coming through as expected in SQLite (example data below). The problem I am having is that Excel (the table object specifically) is only identifying the correct numeric data type for the [Diff] column when the first value in the column is NOT NULL. This is causing some headaches on the Excel side... I need to manually convert the [Diff] column to numeric. But if the first value is numeric (View2), this is done automatically by Excel.
Is there any workaround for this? I think this might be an Excel issue... or maybe this has to do with SQLite decltypes? Any help would be appreciated. Thanks
CREATE Table DateTest(
"A" DATE,
"B" DATE
);
INSERT INTO DateTest VALUES (NULL, NULL), ('2022-01-01', '2022-02-02'), (NULL, NULL), ('2022-02-01', '2022-03-01')
;
CREATE VIEW View1 AS
SELECT
[A]
, [B]
, julianday([B]) - julianday([A]) AS [Diff]
FROM DateTest
;
CREATE VIEW View2 AS
SELECT
[A]
, [B]
, julianday([B]) - julianday([A]) AS [Diff]
FROM DateTest
ORDER BY julianday([B]) - julianday([A]) DESC
;

Related

SQLite returns int instead of real

I'm working on a website in node js and use SQLite as a database for the first time.
I want to be able to use real for some form data and I noticed that every real in my database are converted to integer once the query is made.
To vizualize the database i am using DB Browser and i checked if the columns are defined as REAL which they are.
If i try to query a data set as 0.1 in my DB I get this :
sqlite> select step_variable
from variables
where id=38;
0.0
After trying as suggested the command TYPEOF(step_variable) it returned :
0.0|real
In the SQLite CREATE TABLE command, one defines a data type affinity, not a data type. SQLite supports the following five column affinities: TEXT, NUMERIC, INTEGER, REAL, NONE.
Thus the data type you specify when creating a table does not enforce a certain data type. You can supply any data type you want or even omit the data type.
CREATE TABLE table1(
column1 ABC,
column2 Others,
column3 WHATEVER);
CREATE TABLE table2(column1, column2, column3);
Populate tables:
INSERT INTO table1 VALUES( 1, 'my text', 123.45);
INSERT INTO table2 VALUES( 1, 'my text', 123.45);
Now let us check what SQLite made out of it:
SELECT column1, TYPEOF(column1) from table1
SELECT column2, TYPEOF(column1) from table1
SELECT column3, TYPEOF(column1) from table1
With:
column TYPEOF(column)
------------------------
1 INTEGER
my text TEXT
123.45 REAL
When you go through a query result e.g. by using sqlite2_step you can use the sqlite3_column_type statement to confirm the column type - unless you know the result anyway and simply cast the result to the data type expected.
Martin
I found the solution it was simply that i didn't save my file after modifying it.

Excel 2016 query editor, filter/where clause

I'm struggling with probably the simplest thing. How do I filter my data using a where clause in Excel 2016, ODBC sql database source.
I'm new to Excel 2016, I used to just modify the SQL Command via the odbc properties. Now i can't even see the SQL command when I create the odbc.
So from just playing around. I gathered that there is now a series of "Applied Steps", but I struggle to find where I can find how to limit the rows to a date range. (I don't want to display the date because I'm aggregating by inventory type) But there must be somewhere I can say return only for date '2020-05-01' for example. (where date = '2020-05-01')
the next steps will then be making that a dynamic parameter so I can have some sort of input when the data refresh.
Any tips?
The SQL statement is greyed out
Right clicking a step -> view native SQL will gray out when that step cannot be query folded. Previous steps are still viewable.
You can find the sql query and server name if you choose advanced editor -- which displays all steps in one editor.
native sql
If you want raw sql, import the table as a new connection and paste your query into that box.
note: [] in powerquery is a Record literal.
// indented for readability
let
Source = Sql.Database("server", "dbo"),
// using parameterized queries
RawSQL = "SELECT * FROM users as
WHERE EnglishMonthName=#MonthName
AND
EnglishDayNameOfWeek=#DayName",
TableName = Value.NativeQuery(
Source, RawSQL
[
MonthName="March",
DayName="Tuesday"
]
)
in
TableName
I don't want to display the date because I'm aggregating by inventory type
Using the UI, you would import the table keeping the date column. Filter rows. Then choose remove column as your last step. Your model will not display removed columns.
Where clause in PowerQuery aka: Get Data
Where is the function Table.SelectRows The UI will generate the code for you
Right click your date column -> filter -> date/time -> equals
If you want 2020/05/09 It generates:
= Table.SelectRows(#"Source Table", each [Date] = #date(2020, 5, 9))
You may use multiple conditions, like a date range
= Table.SelectRows(#"Table Name", each
[Date] >= #date(2020, 5, 9) and
[Date] <= #date(2020, 6, 1)
)

Excel Power Query - from web with dynamic worksheet cell value

We have a spreadsheet that gets updated monthly, which queries some data from our server.
The query url looks like this:
http://example.com/?2016-01-31
The returned data is in a json format, like below:
{"CID":"1160","date":"2016-01-31","rate":{"USD":1.22}}
We only need the value of 1.22 from the above and I can get that inserted into the worksheet with no problem.
My questions:
1. How to use a cell value [contain the date] to pass the date parameter [2016-01-31] in the query and displays the result in the cell next to it.
2. There's a long list of dates in a column, can this query be filled down automatically per each date?
3. When I load the query result to the worksheet, it always load in pairs. [taking up two cells, one says "Value", the other contains the value which is "1.22" in my case]. Ideally I would only need "1.22", not the title, can this be removed? [Del won't work, will give you a "Column 1" instead, or you have to hide the entire row which will mess up with the layout].
I know this is a lot to ask but I've tried a lot of search and reading in the last few days and I have to say the M language beats me.
Thanks in advance.
Convert your Web.Contents() request into a function:
let
myFunct = ( param as date ) => let
x = Web.Contents(.... & Date.ToText(date) & ....)
in
x
in
myFunct
Reference your data request function from a new query, include any transformations you need (in this case JSON.Document, table expansions, remove extraneous data. Feel free to delete all the extra data here, including columns that just contain the label 'value'.
(assuming your table of domain values already exists) add a custom column like
=Expand(myFunct( [someparameter] ))
edit: got home and got into my bookmarks. Here is a more detailed reference for what you are looking to do: http://datachix.com/2014/05/22/power-query-functions-some-scenarios/
For a table - Add column where you get data and parse JSON
let
tt=#table(
{"date"},{
{"2017-01-01"},
{"2017-01-02"},
{"2017-01-03"}
}),
add_col = Table.AddColumn(tt, "USD", each Json.Document(Web.Contents("http://example.com/?date="&[date]))[rate][USD])
in
add_col
If you need only one value
Json.Document(Web.Contents("http://example.com/?date="&YOUR_DATE_STRING))[rate][USD]

How to restrict decimal values in openbravoerp

If user is entering 678.98 then it should display only 678 in one of the field of a window.
I have declare that field as numeric in database and number type reference in tables and columns in openbravo.
Use Math.floor()
import java.lang.Math;
...
value = 678.98;
roundeddown = Math.floor(value); // roundeddown == 678
See also this page about another rounding example.
Working directly with PostgreSql, use the floor() function
To insert data
INSERT INTO table ... VALUES( floor(678.98) ) ...
To select data
SELECT floor( field ) FROM table WHERE ....
Set Integer type reference in tables and columns in openbravo.

Adapt SQL Query with an Excel valua

I'm using Excel 2013 to get data from a SQL Server 2012 database,
I successfully get data using an SQL Query, but now I want that query to be dynamic, based on the Month and Year.
How is it possible to use a cell value (a spreadsheet cell) as a parameter for such a query?
I use this Query:
SELECT [Class_02] AS [MS Part Number]
,[701].[dbo].[ItemClasses].[Description] AS [Item Name]
,ROUND([701].[dbo].[frhsrg].[esr_aantal]/3, 0) AS [Aantal]
,[701].[dbo].[ItemAccounts].[SlsPkgsPerPurPkg] AS [Prijs]
,SUM(DISTINCT [701].[dbo].[frhsrg].[esr_aantal]) * [701].[dbo].[ItemAccounts].[SlsPkgsPerPurPkg] as Totaalprijs
FROM [701].[dbo].[Items]
WHERE DATEPART(YEAR, fakdat) = '2013'
AND DATEPART(QUARTER, fakdat) = '1'
AND [701].[dbo].[frhsrg].[docnumber] LIKE '%kwartaal%'
I have two values in Excel (Month and Year) like March 2013, and then the DATEPART statement in SQL has to be based on that. I use the Excel 2013 Add-In PowerPivot to fill the Excel sheet with the SQL Query output. How does the SQL Query need to be to do this?
In forward, many thanks!
... I you have the query saved as a string in your Excel VBA macro (I'm assuming that is where this query is run from), simply write it as follows:
WHERE DATEPART(YEAR, fakdat) = <<YEAR>>
AND DATEPART(QUARTER, fakdat) = <<MONTH>>
Basically having <<YEAR>> and <<MONTH>> as placeholders, then you can just use the Replace function looking something like this:
Replace strSQL, "<<YEAR>>", Range("A1").Text
where strSQL would be the SQL string
Customize a parameter query - http://office.microsoft.com/en-us/excel-help/customize-a-parameter-query-HP010216113.aspx

Resources