Suppose I have a bucket of documents in Couchbase like below.
user lastUpdate otherField1
a "2015-12-06" 1
a "2015-11-06" 2
b "2015-12-05" 3
b "2015-10-05" 4
How could I obtain in a single N1QL query the last updated document for each user ?
I've started using N1QL a few days ago and I don't know how to do such complex queries. Thank you
You can do the following:
SELECT user, MAX( [ lastUpdate, doc ] ) AS pair
FROM my_bucket AS doc
GROUP BY user;
Related
I have a database table that contains one or more entries for each patient. These contain free text and additional information about a test request. Querying on a patient would for example return:-
TestID PatientID RequestMade FreeText
1 23 13/12/2015 11:00:00 Feeling breathless
1125 23 07/04/2016 09:31:15 Unexplained fractures
2556 23 04/12/2016 16:20:21 Check liver function – on statins
When viewing test results I have to pull up the request information relating to the test which will be the last one prior to the test. The results have a TestDate so a TestDate of '13/04/2016 14:21:30' should display the request of '07/04/2016 09:31:15'. I am unsure how to code this efficiently as returning every entry for a patient and doing a date comparison on each one seems not the best way to tackle it.
If you want the one test before another test for a single patient and the test you are looking for only appears once, then you can do this with a single query as:
select t.*
from tests t
where t.patientid = 23 and
t.requestmade < (select t2.requestmade
from tests t2
where t2.patientid = t.patientid and
t2.testid = ?
)
order by t.requestmade desc
limit 1;
I have a grails application with a mysql database as the datasource. The mysql database is created and maintained by a third party. One of the tables 'visitinfo' contains 2 columns consisting of the 'userid' and 'logindatetime'.
userid is of type TEXT and logindatetime is of type 'datetime'.
In order to access the above table 'visitinfo', I have created a domain class 'VisitInfo' and mapped it to the mysql database table by which my grails application can easily store as well as retrieve data from the database.
On one of the pages, I am required to show visitor information for the last 30 days. So basically I am looking out for a solution to get number of visitors per day for the last 30 days. Something like this:
21-Jan-2012 ------ 36
22-Jan-2012 ------ 85
23-Jan-2012 ------ 115
24-Jan-2012 ------ 236
etc.
Also please note, that if a userid 'williamp' has 2 entries on a particular day, it should be counted as 2. So, am not looking out for uniqueness of users.
Any help will be appreciated.
I know nothing at all about grails. The MySQL query to obtain the desired result is as follows:
SELECT DATE_FORMAT(logindatetime,'%d-%m-%Y') dt
, COUNT(*) total
FROM visitinfo
GROUP
BY dt;
You want to use the countBy gorm method.
numLogins=VisitInfo.countByReleaseDateBetween(startOfDay,endOfDay)
This would need to be in a loop that calculates two date objects for each of the last 30 days. startOfDay would need a time value of 00:00:00:00 and endOfDay would need a time value of 23:59:00:00
I suggest following hql query to match your requirement
VisitInfo.executeQuery("select logindatetime, count(*) from VisitInfo group by logindatetime")
I have a table of nodes:
nid type created status
2 blog 134292319 1
3 forum 134292536 1
4 blog 135921392 0
To graph the number of published (status=1) nodes over time, I execute this query:
SELECT created, type
FROM node WHERE status = 1
ORDER BY created
I then go through this data set in PHP, splitting it into timestamped groups with a node count associated with each group. The results are cached, so slow execution isn't a problem.
I also have a table of comments:
nid timestamp status
2 134292363 1
3 134293234 1
I want to incorporate forum comment counts into the graph of node counts.
To get the comment counts, I would run this query:
SELECT timestamp
FROM comments
INNER JOIN node ON comments.nid = node.nid
WHERE
node.type = 'forum'
AND comments.status = 1
ORDER BY timestamp
I need to somehow combine these two queries, to end up with (for the examples given):
created type
134292319 blog
134292536 forum
134293234 forum_comment
Any ideas?
Thanks.
This will get you your example output but I am not sure it's exactly what you are looking for based on your description of the question.
SELECT created, type FROM
(
SELECT created, type
FROM node WHERE status = 1
UNION ALL
SELECT timestamp as created, 'forum_comment' as type
FROM comments
INNER JOIN node ON comments.nid = node.nid
WHERE node.type = 'forum'
AND comments.status = 1
) AS U
ORDER BY U.created
I've currently got a table as follows,
Column Type
time datetime
ticket int(20)
agentid int(20)
ExitStatus varchar(50)
Queue varchar(50)
I want to write a query which will break this down by week, providing a column with a count for each ExitStatus. So far I have this,
SELECT ExitStatus,COUNT(ExitStatus) AS ExitStatusCount, DAY(time) AS TimePeriod
FROM `table`
GROUP BY TimePeriod, ExitStatus
Output:
ExitStatus ExitStatusCount TimePeriod
NoAgentID 1 4
Success 3 4
NoAgentID 1 5
Success 5 5
I want to change this so it returns results in this format:
week | COUNT(NoAgentID) | COUNT(Success) |
Ideally, I'd like the columns to be dynamic as other ExitStatus values may be possible.
This information will be formatted and presented to end user in a table on a page. Can this be done in SQL or should I reformat it in PHP?
There is no "general" solution to your problem (called cross tabulation) that can be achieved with a single query. There are four possible solutions:
Hardcode all possible ExitStatus'es in your query and keep it updated as you see the need for more and more of them. For example:
SELECT
Day(Time) AS TimePeriod,
SUM(IF(ExitStatus = 'NoAgentID', 1, 0)) AS NoAgentID,
SUM(IF(ExitStatus = 'Success', 1, 0)) AS Success
-- #TODO: Add others here when/if needed
FROM table
WHERE ...
GROUP BY TimePeriod
Do a first query to get all possible ExitStatus'es and then create your final query from your high-level programming language based on those results.
Use a special module for cross tabulation on your high-level programming language. For Perl, you have the SQLCrossTab module but I couldn't find one for PHP
Add another layer to your application by using OLAP (multi-dimensional views of your data) like Pentaho and then querying that layer instead of your original data
You can read a lot more about these solutions and an overall discussion of the subject
This is one way; you can use SUM() to count the number of items a particular condition is true. At the end you just group by the time as per normal.
SELECT DAY(time) AS TimePeriod,
SUM('NoAgentID' = exitStatus) AS NoAgentID,
SUM('Success' = exitStatus) AS Success, ...
FROM `table`
GROUP BY TimePeriod
Output:
4 1 3
5 1 5
The columns here are not dynamic though, which means you have to add conditions as you go along.
SELECT week(time) AS week,
SUM(ExitStatus = 'NoAgentID') AS 'COUNT(NoAgentID)',
SUM(ExitStatus = 'Success') AS 'COUNT(Success)'
FROM `table`
GROUP BY week
I'm making some guesses about how ExitStatus column works. Also, there are many ways of interpretting "week", such as week of year, of month, or quarter, ... You will need to put the appropriate function there.
not a programmer so need help with this one I am using Wordpress 3.0.2 and wp-calendar
i hav looked at the mysql data base in PhpMyAdmin under
wordpress - wp_calendar_categories - There is categories 0 -4 (5)
i wish to out put this data to a page in wordpress in the following format:-
for category 1 which is also in category 4 data output Title- date -time -location
for category 2 which is also in category 4 data output Title- date -time -location
for category 3 which is also in category 4 data output Title- date -time -location
using the commands with the wp-calendar plugin will not give me a output of cat1 in cat4
i can produce a list of all in each category in the following format Title - date - time- location
os any sql masters out there please help can give more info if required
THANKS
If I understand your question correctly you are trying to output information that is in multiple categories. For this you want a join.
SELECT title, date, time, location
FROM wp_calendar_categories c1
JOIN wp_calendar_categories c4
ON c1.title = c4.title
AND c1.date = c4.date
AND c1.time = c4.time
AND c1.location = c4.location
WHERE c1.category = 1
AND c4.category = 4
You can combine results (provided the columns are the same) using a union
SELECT title, date, time, location
....
UNION
SELECT title, date, time, location
I would advise searching on the web for SQL tutorials to help get you started.