Composing Big Query in App Script - sql

I want to compose bigquery in app script with date1 & date2 variable (as mentioned below). What is the format to pass these 2 variable?
var date1="20180601"
var date2="20180606"
var sql = "select * from table_query([project-name:dataset],
'table_id <= "date2" and table_id >= "date1"');";

I could do it in the following way:
var date1="20180601"
var date2="20180606"
var sql = "select * from table_query([project-name:dataset], 'table_id <= \""+date2+"\" and table_id >= \""+date1+"\"');";

Related

SQLKata - Nested queries - How can i write the code for my query in using sqlkata for sqlserver in c#?

How can i write the code for the below query in using sqlkata for sqlserver in c#?
SELECT [t0].Region
FROM ((select * from [dbo].Demo_ReportData )) AS [t0]
GROUP BY [t0].Region
ORDER BY [t0].Region ASC
offset 0 rows fetch next 50 rows only;
To use the offset fetch syntax you have to set the UseLegacyPagination to false on the SqlServerCompiler.
var compiler = new SqlServerCompiler { UseLegacyPagination = false };
var innerQuery = new Query("Demo_ReportData");
var query = new Query().From(innerQuery.As("t0"))
.GroupBy("Region")
.OrderBy("Region")
.Take(50);
var result = compiler.Compile(query);
var sql = result.Sql;
var bindings = result.Bindings;

Query criteria for matching 3 of 6 fields

I would like to select rows in the Corda (M14) database with a criteria that matches at least 3 of 6 fields and sort these results by matching fields.
Here is the SQL syntax to select fields:
WHERE (field1 = ?) + (field2 = ?) + (... = ?) > 3
and to order it:
ORDER BY ((field1 = ?) + (field2 = ?) + (... = ?)) DESC
Another way of doing it :
SELECT *, ((field1 = #inputFirst) + (field2 = #inputLast)) as Matches
FROM mytable
HAVING Matches > 1
ORDER BY Matches DESC
I started to create the criteria:
vaultCriteria
.or(QueryCriteria.VaultCustomQueryCriteria(field1))
.or(QueryCriteria.VaultCustomQueryCriteria(field12))
.or(QueryCriteria.VaultCustomQueryCriteria(field3))
But I am stuck now on how to GROUP theses results by fields' matching number and SORT these, any ideas ?
Thank you,
Loup
I haven't tested how far it's possible to take the API, but I've had success (on current Master) by writing custom SQL - e.g.
val session = services.jdbcSession()
val consensusQuery = """
SELECT COUNT(*) - COUNT(NULLIF(factObject, ?)), COUNT(*)
FROM submission_states
WHERE factSubject = ? AND factPredicate = ?
"""
val consensusStatement = session.prepareStatement(consensusQuery)
consensusStatement.setString(1, factConsensusQuery.factObject)
consensusStatement.setString(2, factConsensusQuery.factSubject)
consensusStatement.setString(3, factConsensusQuery.factPredicate)
log.info("SQL to execute: " + consensusStatement.toString())
val rs = consensusStatement.executeQuery()
For M14 release you have 2 options:
1) Get a jdbcSession directly from the DatabaseTransactionManager:
val jdbcSession1 = DatabaseTransactionManager.current().connection
2) Get a jdbcSession indirectly from a RequeryConfiguration object:
val jdbcSession2 = RequeryConfiguration(<dataSourceProperties>).jdbcSession()
where <dataSourceProperties> looks something like this:
private fun makePersistentDataSourceProperties(): Properties {
val props = Properties()
props.setProperty("dataSourceClassName", "org.h2.jdbcx.JdbcDataSource")
props.setProperty("dataSource.url", "jdbc:h2:~/test/vault_query_persistence;DB_CLOSE_ON_EXIT=TRUE")
props.setProperty("dataSource.user", "sa")
props.setProperty("dataSource.password", "")
return props
}
For advanced custom SQL queries it is recommended you use a standard JDBCSession, obtainable from the ServiceHub.
Please see https://docs.corda.net/head/api-persistence.html?highlight=jdbcsession

SQL Syntax for date range in a multiple search query

I have this code written so far and it works for what I am doing but if I search for June 13 it will only look up until June 12, can someone help me figure whats wrong in my code? or where I can add a day interval? I tried and its just not working for me.
var db = Database.Open("RMS") ;
var selectCommand = "SELECT * FROM RMS";
var formSSO = "";
var fromDate= "";
var toDate= "";
formSSO = Request.QueryString["formSSO"];
fromDate = Request.QueryString["fromDate"];
toDate = Request.QueryString["toDate"];
selectCommand = "(SELECT * from RMS WHERE SSO LIKE #0)";
if(!Request.QueryString["fromDate"].IsEmpty() ) {
selectCommand = "SELECT * FROM RMS WHERE SSO LIKE #0 AND Created BETWEEN #1 AND #2";
}
if(Request.QueryString["formSSO"].IsEmpty() ) {
<div class="simple"><strong>*SSO ID is Required.</strong></div>
}
var data = db.Query(selectCommand, formSSO, fromDate, toDate);
var columns = new[]{"ID", "SSO", "Category", "System", "Subject", "Created"};
var grid = new WebGrid(data, ajaxUpdateContainerId: "grid", defaultSort: "ID", columnNames: columns);
if (Request.QueryString[grid.SortDirectionFieldName].IsEmpty()) {
grid.SortDirection = SortDirection.Descending;
}
}
One thing you can try is using <= and >= instead of BETWEEN like this:
selectCommand = "SELECT * FROM RMS WHERE SSO LIKE #0 AND Created >= #1 AND Created <= #2";
I hope that does the trick!
If not you can also brute force the to date to be one day further into the future and then use the BETWEEN operator just you are now like this:
DateTime to_date = Convert.ToDateTime(to_date_string);
to_date = to_date.AddDays(1);
to_date_string = to_date.ToString();

SQL date comparison in WHERE clause, TypoScript

I want to compare dates in a TypoScript select.
Here's what I have (note that I commented the were clauses) :
lib.my_val = CONTENT
lib.my_val {
select {
pidInList = 100000
max = 1
#where = effective_date < CURDATE()
#where = TIMESTAMP(effective_date) < NOW()
orderBy = effective_date DESC
}
table = tx_my_table
renderObj = COA
renderObj {
5 = TEXT
5{
field = my_field
wrap = <span>|</span>
}
[...]
}
}
Which returns lines.
I tried to add a where statement any way I could with static dates or variables... without success. My understanding of the where clause is that everything after the = is dumped as is in the SQL query. But it seems I missed something.
Basically I want the TypoScript to generate a SQL Query smilar to this :
SELECT * FROM tx_my_table WHERE effective_date < NOW() ORDER BY effective_date DESC LIMIT 1;
This should be simple. Has anyone done this in the past?
Thanks!
Your TypoScript seems to be OK.
What happens if you enter the SQL Query directly into MySQL?
Note that with your code, only one record with pid=100000 is
selected.
Have you tried this:
--
lib.my_val {
select {
pidInList = 100000
max = 1
where = UNIX_TIMESTAMP(effective_date) < UNIX_TIMESTAMP()
orderBy = UNIX_TIMESTAMP(effective_date) DESC
}
table = tx_my_table
}
TYPO3 Wiki on select

Parameterized LIKE clause in SQL statement using Dapper

I want to perform the following query using Dapper, which currently doesn't return expected results (I think it must be treating the #pName param as literal text within the single quotes?):
var q = "SELECT * FROM Users WHERE Name LIKE '#pName%'";
#pName is the param I assign a value to upon executing the query.
Things work if I just build the SQL like:
var q = "SELECT * FROM Users WHERE Name LIKE '" + name + "%'";
.. but I would prefer to use a param if possible.
I am executing the query using the following code:
o = _cn.Query<User>(q, new { pName = new DbString { Value = name, IsFixedLength = false, Length = 25, IsAnsi = true } }).ToList();
How do I got about this using Dapper?
SELECT * FROM Users WHERE Name LIKE #pName + '%'
I would like to add here another possible solution:
var results = cn.Query("SELECT * FROM Table WHERE Column LIKE #value", new { value = value + "%" });
The wildcard is inside the string var itself, and then we reference that var in the SQL. Applies to any wildcard pattern you want.