How to do order in grouping(aggregation) in Couchbase? - couchbase

I have SQL query like this:
select count(id) as ct, city from table group by city order by ct desc
How can I perform this query in Couchbase?
I have map function like This:
function (doc, meta) {
if(doc.city!==null) {
emit(doc.city,1)
}
}
and reduce:
_count
my query url is like this:
_view/q7?group_level=1&descending=true
but it it sort only for key not for value.

Related

Spring Data JPQL How to create a query with dynamic where clause?

I'm creating custom JPQL queries to fetch some data.
I have certain cases where I would like to add a where clause depending on if the parameter value is non null.
For example equivalent sql query:
//If parameter status is not null.
SELECT sum(A.sal) FROM TABLE A WHERE A.STATUS = 'X' GROUP BY A.CURRENCY;
//else
SELECT sum(A.sal) FROM TABLE A GROUP BY A.CURRENCY;
Can someone help me with it.
The two queries:
//If parameter status is not null.
SELECT sum(A.sal) FROM TABLE A WHERE A.STATUS = 'X' GROUP BY A.CURRENCY;
//else
SELECT sum(A.sal) FROM TABLE A GROUP BY A.CURRENCY;
can be compacted to one.
First lets declare a named parameter statusParam. This param can take any value (like 'X' or null from above). So the two queries above can be re-written to:
SELECT sum(A.sal) FROM A WHERE (A.STATUS = :statusParam OR :statusParam is null ) GROUP BY A.CURRENCY;
Come on, JPQL is just normal String :)
You want something like this?
private String generateHql(String status) {
String jpql = "SELECT sum(A.sal) FROM TABLE A ";
if (null != status) {
jpql += "WHERE A.STATUS = " +status ;
}
jpql += " GROUP BY A.CURRENCY "
return jpql;
}

database query to get lowest price based on last crawel date

I would like to get lowest price of product based on last crawled dates by various resellers. My current function is very basic, it gets me lowest price from table without considering reseller ids and crawled timestamps.
I've rough idea that we can SELECT * FROM "custom_data_table" and process the data using php. Please have a look at attachment for further clarification.
function get_lowest_price($table_id) {
global $wpdb;
$table_prices = $wpdb->get_results(
$wpdb->prepare(
"SELECT price FROM `custom_data_table` WHERE tableid= %d"
,$table_id)
);
if (!empty($table_prices) && $table_prices !== NULL)
return rtrim(min($table_prices)->price, '00');
}
The right query here is:
SELECT price
FROM custom_data_name cdn, (
SELECT MAX(crawled) AS maxCrawled, resellerid
FROM custom_data_name
GROUP BY resellerid
) cdnFiltered
WHERE cdn.crawled = cdnFiltered.maxCrawled AND
cdn.resellerid = cdnFiltered.resellerid AND
tableid = %d;
Try this:
SELECT B.price
FROM (SELECT resellerid, MAX(crawled) max_crawled
FROM custom_data_table
GROUP BY resellerid) A
JOIN custom_data_table B
ON A.resellerid=B.resellerid AND A.max_crawled=B.crawled;
Maybe use ORDER BY crawled and LIMIT 1

how to optimize a long query mysql

How to optimze this query mysql :
SELECT * FROM rooms WHERE caption LIKE #query OR owner LIKE #query AND roomtype = 'private' ORDER BY users_now DESC LIMIT 50
This is my code:
internal ServerMessage SerializeSearchResults(string SearchQuery)
{
DataTable Data = new DataTable();
using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().getQueryreactor())
{
if (SearchQuery.Length > 0)
{
if (SearchQuery.ToLower().StartsWith("owner:"))
{
dbClient.setQuery("SELECT * FROM rooms WHERE owner = #query AND roomtype = 'private' ORDER BY users_now DESC LIMIT 50");
dbClient.addParameter("query", SearchQuery.Remove(0, 6));
}
else
{
dbClient.setQuery("SELECT * FROM rooms WHERE caption LIKE #query OR owner LIKE #query AND roomtype = 'private' ORDER BY users_now DESC LIMIT 50");
dbClient.addParameter("query", "%" + SearchQuery + "%");
}
Data = dbClient.getTable();
}
}
List<RoomData> Results = new List<RoomData>();
if (Data != null)
{
foreach (DataRow Row in Data.Rows)
{
RoomData RData = PlusEnvironment.GetGame().GetRoomManager().FetchRoomData(Convert.ToUInt32(Row["id"]), Row);
Results.Add(RData);
}
}
The best way to optimize the performance of the query is to add indexes to the fields that you are using more frequently in your where clauses. For example, in MySQL you can add an index like this:
ALTER TABLE `rooms` ADD INDEX `owner` (`owner`)
The query doesn't look particularly complicate so you might try adding an index by roomtype and users_now to speed up SELECT.
CREATE INDEX index_name ON rooms (roomtype ASC, users_now DESC)
Also try to limit the usage of LIKE in a query as it severely affects performance.

How to do a count(*) in Hibernate?

How can I count the records in a MySQL table in Hibernate? I tried the following HQL, but it does not work.
SELECT COUNT(*) FROM MEMBERS WHERE `username` =:USERNAME OR `email` =:EMAIL
It is used in the following method:
public boolean checkInfos() {
Session newSession = NewHibernateUtil.getSessionFactory().openSession();
int count = (Integer) newSession.createSQLQuery("SELECT COUNT(*) FROM MEMBERS WHERE `username` ='admin' OR `email` ='admin'").uniqueResult();
if (count >= 1) {
return false;
} else {
return true;
}
}
I guess you need that
Query q = newSession.createSQLQuery("SELECT COUNT(*) FROM MEMBERS WHERE username = ? OR email =?");
q.setParameter( 1, "your username");
q.setParameter(2, "your email");
Select count(*) returns a Long, not an Integer.
One more thing: The backticks, which you are using, are not accepted by every database.
By the way, you can use count(*) also in HQL. This means, you can use createQuery instead of createSQLQuery. An advantage of HQL is, it is portable from one database to another, which is not always the case for SQL statements.
You also can achieve this by using criteria also.
private Number getCount(){
Criteria criteria = session.createCriteria(Members.class);
criteria.add(Restrictions.or(Restrictions.eq("username", "admin"), Restrictions.eq("email","admin")));
criteria.setProjection(Projections.rowCount());
return (Number) criteria.list();
}

MySQL, select records with at least X characters matching

I am trying to accomplish the following. Let's say we have a table that contains these fields (ID, content)
1 | apple
2 | pineapple
3 | application
4 | nation
now, I am looking for a function that will tell me all possible common matches. For example, if the argument is "3", the function will return all possible strings from 3 characters that appear in more then one record.
In this case, I get "app","ppl","ple","ati","tio","ion"
If the argument is "4", i get: "appl","pple","atio","tion"
If the arugment is "5", i get: "apple","ation"
If the argument is "6", nohting is returned.
Untill now, I did not find a function that accomplishes this.
Thx!
Some extra information:
I am using this in a PHP script with a MySQL database. I really just want to give the amount of characters as an argument and of course the table to search in.
Well, this is kind of ugly, but it does work fine. It's generic SQL and will work in any environment. Simply generate a number of selects of a substring that is greater than the maximum length of the field that you're reading. Change the number 50 in the function to a number that exceeds your fieldlength. It may return a realllly long query, but like I said, it'll work fine. Here is an example in Python:
import sqlite3
c = sqlite3.connect('test.db')
c.execute('create table myTable (id integer, content varchar[50])')
for id, content in ((1,'apple'),(2,'pineapple'),(3,'application'),(4,'nation')):
c.execute('insert into myTable values (?,?)', [id,content])
c.commit();
def GenerateSQL(substrSize):
subqueries = ["select substr(content,%i,%i) AS substr, count(*) AS myCount from myTable where length(substr(content,%i,%i))=%i group by substr(content,%i,%i) " % (i,substrSize,i,substrSize,substrSize,i,substrSize) for i in range(50)]
sql = 'select substr FROM \n\t(' + '\n\tunion all '.join(subqueries) + ') \nGROUP BY substr HAVING sum(myCount) > 1'
return sql
print GenerateSQL(3)
print c.execute(GenerateSQL(3)).fetchall()
The query generated looks like:
select substr FROM
(select substr(content,0,3) AS substr, count(*) AS myCount from myTable where length(substr(content,0,3))=3 group by substr(content,0,3)
union all select substr(content,1,3) AS substr, count(*) AS myCount from myTable where length(substr(content,1,3))=3 group by substr(content,1,3)
union all select substr(content,2,3) AS substr, count(*) AS myCount from myTable where length(substr(content,2,3))=3 group by substr(content,2,3)
union all select substr(content,3,3) AS substr, count(*) AS myCount from myTable where length(substr(content,3,3))=3 group by substr(content,3,3)
union all select substr(content,4,3) AS substr, count(*) AS myCount from myTable where length(substr(content,4,3))=3 group by substr(content,4,3)
... )
GROUP BY substr HAVING sum(myCount) > 1
And the results it produces are:
[(u'app',), (u'ati',), (u'ion',), (u'nat',), (u'pin',), (u'ple',), (u'ppl',), (u'tio',)]
I'm sorry as I haven't been playing with php for a while & I don't have a proper test environment for it, but I quickly devised a way of doing this in c# 3.5
pseudocode: build a table with strings of the specified length & a count of occurences next to it. Select where count > 1:
static void Main(string[] args)
{
string[] data = { "apple", "pinapple", "application", "nation" };
string[] result = my_func(3,data);
foreach (string str in result)
{
Console.WriteLine(str);
}
Console.ReadKey();
}
private static string[] my_func(int l, string[] data)
{
Dictionary<string,int> dict = new Dictionary<string,int>();
foreach (string str in data)
{
for (int i = 0; i < str.Length - l + 1; i++)
{
string part = str.Substring(i, l);
if (dict.ContainsKey(part))
{
dict[part]++;
}else {
dict.Add(part,1);
}
}
}
var result = from k in dict.Keys
where dict[k] > 1
orderby dict[k] descending
select k;
return result.ToArray<string>();
}
One obvious option is to use REGEX. I have no prior experience in this but this might be of help to you:
http://dev.mysql.com/doc/refman/5.1/en/regexp.html
You'll need to find a suitable expression to match what you need.