Software and Technology

MySQL – getting two items per source in a single query

Assume you have the following table called stories with the following data. And you have run the following query:

SELECT * FROM stories ORDER BY storydate DESC
id source title storydate
10 cnn Story 10 2010-01-21
9 bbcnews Story 9 2010-01-20
8 bbcnews Story 8 2010-01-19
7 skynews Story 7 2010-01-18
6 cnn Story 6 2010-01-17
5 bbcnews Story 5 2010-01-16
4 cnn Story 4 2010-01-15
3 skynews Story 3 2010-01-14
2 skynews Story 2 2010-01-13
1 cnn Story 1 2010-01-12

Now for the challenge. What happens if you are required to get the top two stories from each source? One option is to first get the order of the sources, and then loop through each one of them:

SELECT DISTINCT source FROM stories ORDER BY storydate DESC

<loop “source” as “sourceid”>

      SELECT * FROM stories WHERE source = "{sourceid}" ORDER BY storydate DESC LIMIT 2

</loop>

Another option is to do the in MySQL itself! Unsure if there is a better way of doing this, but here’s mine using UNION and Sub Selects:

(SELECT * FROM stories WHERE source =
(SELECT DISTINCT source FROM stories ORDER BY storydate DESC LIMIT 0,1) ORDER BY storydate DESC LIMIT 2)
UNION
(SELECT * FROM stories WHERE source =
(SELECT DISTINCT source FROM stories ORDER BY storydate DESC LIMIT 1,1) ORDER BY storydate DESC LIMIT 2)
UNION
(SELECT * FROM stories WHERE source =
(SELECT DISTINCT source FROM stories ORDER BY storydate DESC LIMIT 2,1) ORDER BY storydate DESC LIMIT 2)

Explanation:

The bold section are subselects and form the WHERE clause of the query. Here  we want the first, second and third as per source, but only one at a time. Once we have two results per item, we use UNION to join them together.

One thing to note though. Even though it returns the latest two stories per source, the final table is not sorted by storydate! This can be done using PHP sorting techniques. Is there a way of doing this one time in MySQL?

How to use ExtJS Abstract functions

For some methods in ExtJS, you will see:

Class Ext.grid.GridDragZone

onDrag( Event e ) : void

Abstract method called during the onMouseMove event while dragging an object.

Parameters:

e : Event

the mousemove event

Returns:Class Ext.grid.GridDragZone

void

In this case, GridDragZone is part of GridView which is part of GridPanel. Accessing it will be, GridPanel->GridView->GridDragZone.

The GridDragZone gets implemented for a GridPanel when you set enableDragDrop to true, but it only becomes available when the grid renders, so:

var grid = new Ext.grid.GridPanel({
    // etc
});
grid.on('render', function(obj) {
    obj.getView().dragZone.onDrag=function(evt){
        alert('being dragged');
    }
});

Simple Drag and Drop from Adobe AIR

Quite a few articles and tutorials show you how to drag and drop a file onto an Adobe AIR application. This one will show you how to drag one FROM an Adobe AIR application TO YOUR DESKTOP (or any other location you provide. The code is really simple:





Drag Me to the Desktop

Download: Source Code

To run, extract files from archive, and then run: adl application.xml

Dynamically generating a Sandbox in Adobe AIR

Something that seems to have worked


Thoughts?

Focus Booster App

For the past couple of days, I’ve been experimenting with a tool called Focus Booster. It’s a simple app with an easy method to help you measure and increase your focus and concentration:

 

  1. Firstly, choose the amount of time you would like to complete a particular task. By default it is set to 25 minutes.
  2. Prepare to do the task, clear your PC desktop of any distractions, and start the timer.
  3. Continue working until you’ve completed the task or the timer runs out.
  4. Take a break
  5. Restart the process with your next task

Tiny app, simple process, but it works for me, and these are the benefits I believe are derived:

  • Forces you to plan and think about a single task you would like to perform
  • A quick glance at the timer helps you focus, there is something watching you!
  • Helps you to persevere, (just another 8 more minutes to go, etc.)
  • Provides some type of measurement of how long you can focus, and challenges you to better it. The next version will allow you to record your focus sessions.

Focus Booster is a free application and requires Adobe AIR to run. Download it at: http://www.focusboosterapp.com/ . Follow updates on twitter @focusboosterapp
 

How to: Uninstall Adobe AIR Apps on Ubuntu (via Synaptic)

There are two ways to uninstall Adobe AIR apps on Ubuntu.

1) Access to original .air file

If you have access to the original .air file, run it and it will give you a screen with options to either Uninstall, Run Now or Cancel. Simply click on Uninstall.

Screenshot-Application Install

 

2) Use Synaptic Package Manager

A second more easier approach (particularly if you lost the .air package) is to use Synaptic. From the sidebar, simply choose: Status > Installed (local or obsolete)

Adobe AIR apps list in Synaptic

 

Discovering this makes me less hesitant to try our different AIR apps, trying to find out which one works best.

Cant find/see unread email in Thunderbird

For days now, Thunderbird has been showing one unread email, though I couldn’t find it using all the sort techniques I’ve had. Googling shows it’s not an uncommon occurrence. The solution is also quite easy.

Right click on the folder and choose ‘Compact‘.

Takes a minute, but it will reveal the ‘hidden’ email(s).

Source: https://bugs.launchpad.net/ubuntu/+source/thunderbird/+bug/116642

ExtJS Books coming soon

Here is a list of some of the ExtJS Books, one is already published, others should be out later this year:

 Learning ExtJS

This book has already been published and is available for purchase. As the first and currently only book available, it is quite a useful reference.

What you will learn from this book?

  • Create responsive forms
  • Look at Web Applications from an entirely new perspective
  • Learn to use the major UI components available in Ext JS
  • Understand how external data can be consumed by Ext JS
  • Query and process remote data into your application
  • Use Layouts to bring all of the Ext JS pieces together
  • Use Ext JS effects to manipulate the DOM in exciting ways
  • Provide a consistent look and feel to your application using Components
  • Change the visual style of Ext JS using theming support
  • Find Custom Community Extensions to expand your applications
  • Create your own custom library extensions
   

Practical ExtJS Projects with Gears

This promises to be an exciting book focussing on practical case studies, and also integrate offline usage with Google Gears.

Expected in July 2009

What you’ll learn

  • Get an understanding of one of today’s hottest JavaScript frameworks and libraries, Ext JS.
  • Develop interesting Web 2.0 “front ends,” RIAs (Rich Internet Applications), and more.
  • Create a number of applications from the projects or pragmatic case studies that the author gives you.
  • Create an organizer, timekeeper, a code cabinet, and more.
  • Get back to basics and back ends using a SQL workbench, Google Gears, and Ext BASIC.
  • Pull it all together by designing a game using Ext JS.
  • And more…
   
 

ExtJS in Action

Expected in January 2010, although some chapter are available with the Manning Early Access Program

http://www.manning.com/garcia/

 

Mozilla Prism

Appreciating Mozilla Prism

Mozilla Prism

When Mozilla Prism was first launched, I honestly never gave it much thought as to it’s usefulness, or why anyone would want to use it. Today, it is such an essential tool for me. Let me explain. More >