SQL Statement to Subsonic Query - subsonic

How do I write this SQL statement with Subsonic 2.2 Query Object?
SELECT * FROM Product
WHERE DateDiff(d, StartDate, GetDate()) >= 0
AND DateDiff(d, EndDate, GetDate()) <= 0

You could do this another way if all your trying to achieve is make sure that a value is between two dates or greater than one and the other.
you'll just have to take the date calculation into the .net realm and feed it into your query using either
.Between()
or
.AndExpression(Products.Columns.DateField).IsGreaterThan(X)
.And((Products.Columns.DateField).IsLessThan(X)

Either use a stored procedure or InlineQuery object.

Related

using week number how to query in Azure Cosmos DB SQL

There is an iso format timestamp string in the Cosmos DB container.
{"timestamp": "2022-08-21T00:00:00"}
Using week number, how can I write the query so that entire week dates should pass in where clause
For week_number: 34 (or 2022-34) the date range will be "2022-08-21","2022-08-22", "2022-08-23","2022-08-24","2022-08-25","2022-08-26","2022-08-27"
One possible solution is to create this date list using code and then pass the start and end date using between clauses.
select * from c where c.timestamp between start_date and end_date
Is there any other way to do this in the Cosmos DB SQL query itself?

Optimization of a query which uses arithmetic operations in WHERE clause

I need to retrieve records where the expiration date is today. The expiration date is calculated dynamically using two other fields (startDate and durationDays):
SELECT * FROM subscription WHERE startDate + durationDays < currentDate()
Does it make sense to add two indexes for these two columns? Or should I consider adding a new column expirationDate and create an index for it only?
SELECT * FROM subscription WHERE startDate + durationDays < currentDate()
I'm wondering how does Cassandra handle such a filter as in my example? Does it make a full scan?
First of all, your question is predicated on CQL's ability to perform (date) arithmetic. It cannot.
> SELECT * FROM subscription WHERE startDate + durationDays < currentDate();
SyntaxException: line 1:43 no viable alternative at input '+' (SELECT * FROM subscription WHERE [startDate] +...)
Secondly the currentDate() function does not exist in Cassandra 3.11.4.
> SELECT currentDate() FROM system.local;
InvalidRequest: Error from server: code=2200 [Invalid query] message="Unknown function 'currentdate'"
That does work in Cassandra 4.0, which as it has not been released yet, you really shouldn't be using.
So let's assume that you've created your secondary indexes on startDate and durationDays and you're just querying on those, without any arithmetic.
Does it execute a full table scan?
ABSOLUTELY.
The reason, is that querying solely on secondary index columns does not have a partition key. Therefore, it has to search for these values on all partitions on all nodes. In a large cluster, your query would likely time out.
Also, when it finds matching data, it has to keep querying. As those values are not unique; it's entirely possible that there are several results to be returned. Carlos in 100% correct is advising you to rebuild your table based on what you want to query.
Recommendations:
Try not to build a table with secondary indexes. Like ever.
If you have to build a table with secondary indexes, try to have a partition key in your WHERE clause to keep the query isolated to a single node.
Any filtering on dynamic (computed) values needs to be done on the application side.
In your case, it might make more sense to create a column called expirationDate, do your date arithmetic in your app, and then INSERT that value into your table.
You'll also want follow the "time bucket" pattern for handling time series data (which is what this appears to be). Say that month works as a "bucket" (it may or may not for your use case). PRIMARY KEY ((month),expirationDate,id) would be a good key. This way, all the subscriptions for a particular month are stored together, clustered by expirationDate, with id on the end to act as a tie-breaker for uniqueness.
One of the main differences between Cassandra and relational databases is that the definition of the tables depend on the query that will be used. The conditional of how the data will be retrieved (WHERE statement) should be included in the primary key as it will perform better than an index on the table.
There are multiple resources regarding the read path, and the quirks of primary keys vs indexes, this talk from the Cassandra Summit may be useful.

How to use basic arithmetic operations in WHERE clause of CQL?

I am trying to write a CQL query which looks like:
select * from mytable
WHERE timestamp >= unixTimestampOf(maxTimeuuid('2016-03-01 00:00:00')) /1000
and timestamp <= unixTimestampOf(minTimeuuid('2016-03-31 23:59:59')) / 1000
and am getting this error:
Invalid syntax at line 1, char XXX
If I change the query to
select * from mytable
WHERE timestamp >= unixTimestampOf(maxTimeuuid('2016-03-01 00:00:00'))
and timestamp <= unixTimestampOf(minTimeuuid('2016-03-31 23:59:59'))
it does not give any error but obviously I don't get the desired result since unixTimestampOf returns milliseconds whereas my timestamp column is storing seconds. How can I fix this?
This JIRA ticket is what you're probably looking for: Operator functionality in CQL. It's still unresolved, but it's moving on. One of its subtasks Add support for arithmetic operators has been marked as resolved recently, and is being merged in Cassandra 3.12, but it works for numeric data types only. I don't think it will work out of the box with your schema, you'll probably need to change timestamps to numeric data types.
HTH.

Cassandra Limit 10,20 clause

I am using Cassandra 1.2.3 and can execute select query with Limit 10.
If I want records from 10 to 20, I cannot do "Limit 10,20".
Below query gives me an error.
select * from page_view_counts limit 10,20
How can this be achieved?
Thanks
Nikhil
You can't do skips like this in CQL. You have have to do paging by specifying a start place e.g.
select * from page_view_counts where field >= 'x' limit 10;
to get the next 10 elements starting from x.
I wrote a full example in this answer: Cassandra pagination: How to use get_slice to query a Cassandra 1.2 database from Python using the cql library.
for that you have to first plan your data model so that it can get records according to your requirement...
Can you tell which sort of example your are doing?
and Are you using hector client or any other ?
sorry mate I did it using hector client & java,but seeing your requirement I can suggest to plan your data model like this :
Use time span as a row key in yyyyMMddHH format,in that store column name as composite key made up of UTF8Type and TimeUUID (e.g C1+timeUUID ).
note: here first composite key would be counter column family column name (e.g. C1)
Now you will only store limited records say 20 in your CF and make this c1 counter 20,now if any new record came for the same timespan you have to insert that with key C2+timeUUID now u will increment counter column family c2 upto 20 records
Now to fetch record you just have to pass value C1 , C2 ...etc with rowkey like 2013061116
it will give you 20 records than another 20 and so on...you have to implement this programmatically..hope you got this and helps you

How can I get the Current Date in a Cognos query expression?

I have a query expression in Cognos where I need to compare a past date to the current date. I don't see one in the functions list and I'm otherwise unsure how to put the query date inside a query object.
How can I use the current date in a query?
Depending on your Database software, the object will be either be current_date (SQL Server) or SYSDATE{} (Oracle). If you don't know which you have, just make an expression of just the function and press the Validate button; if you get an error, you used the wrong function for your database.
You can then use this object like any other Date query object, so you can add/compare it to dates in your query or display it somewhere on the page.
The best way is to use current_date. This method is data source agnostic and will be converted to the appropriate data source equivalent at run-time.
You can use something like this with your query:
SELECT
FIELD1
FROM TABLE
WHERE
FIELD2 = current_date
Asumming that FIELD2 has a date format

Resources