Mittwoch, 26. Oktober 2011

Moving Average (with gaps) - Sql (Mysql) - Query

The following post shows an example how to create a query for moving averages for mysql database data.


Structure



CREATE TABLE IF NOT EXISTS `quotationtest` (
  `id` int(11) DEFAULT NULL,
  `date` date DEFAULT NULL,
  `close` decimal(5,3) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Data

INSERT INTO `quotationtest` (`id`, `date`, `close`) VALUES
(1, '2011-05-15', 12.706),
(2, '2011-05-16', 4.303),
(3, '2011-05-17', 3.182),
(4, '2011-05-18', 2.776),
(5, '2011-05-19', 2.571),
(6, '2011-05-20', 2.447),
(7, '2011-05-21', 2.365),
(8, '2011-05-22', 2.306),
(9, '2011-05-23', 2.262),
(10, '2011-05-24', 2.228),
(11, '2011-05-25', 2.201),
(12, '2011-05-26', 2.179),
(13, '2011-05-27', 2.160),
(14, '2011-05-28', 2.145),
(15, '2011-05-29', 2.131),
(16, '2011-05-30', 2.120),
(17, '2011-05-31', 2.110),
(18, '2011-06-01', 2.101),
(19, '2011-06-02', 2.093),
(20, '2011-06-03', 2.086),
(21, '2011-06-04', 2.080),
(22, '2011-06-05', 2.074),
(23, '2011-06-06', 2.069),
(24, '2011-06-07', 2.064),
(25, '2011-06-08', 2.060),
(26, '2011-06-09', 2.056),
(27, '2011-06-10', 2.052),
(28, '2011-06-11', 2.048),
(29, '2011-06-12', 2.045),
(30, '2011-06-13', 2.042),
(31, '2011-06-14', 1.960);

For the data above we can use following Sql Code (Mysql) to calculate a moving average (3 day ma in taht case). We use a subquery and a date_sub function for the time window. HAVING COUNT(*) = 3 is used to set moving average to NULL for the first (time window – 1 days).

Query

SELECT t1. id, t1.date, (
SELECT AVG(close)
FROM quotationtest t2
WHERE t2.date > date_sub(t1.date, interval 3 day) AND t2.date <= t1.date HAVING COUNT(*) = 3
) AS ma FROM quotationtest t1


Notice that this code only works for continual data. If there are gaps (e.g. weekends with no stockjobbing) you could not use date_sub function because oft he gaps. In that case you can use the code below. In this query we use some kind of  sorted iterators for doing that job. Notice that the code below is specific Mysql code, but you’ll find possibilities for sequence generation in other databases (e.g. postgres), too.

SET @rowt1 = 0;
SET @rowt2 = 0;

SELECT t1.id, t1.date, (SELECT AVG(close)  FROM (SELECT @rowt2 := @rowt2 +1 AS rownum , id, date, close FROM quotationtest ORDER BY date) t2 WHERE t2.rownum <= t1.rownum AND t2.rownum > (t1.rownum - 3) HAVING COUNT(*) = 3) AS ma FROM (SELECT @rowt1 := @rowt1 +1 AS rownum , id, date, close FROM quotationtest ORDER BY date) t1 ORDER BY t1.date

Have fun




Mittwoch, 5. Oktober 2011

Kostenlose Joomla Amazon Komponente

1sr hat eine kostenfreie Joomla Komponente freigegeben mit deren Hilfe Amazon Produkte über den Amazon Web Service einfach in das CMS Joomla eingebunden / integriert werden können. Über eine einfache Konfiguration können sowohl Produkt-Übersichtsseiten als auch einzelne Produktseiten erzeugt werden.

Folgende Features besitzt die fsramazon Komponente:

  • AWS Konfiguratiton
  • Partnernet (Amazon Advertising) Konfiguration
  • Farb-Konfiguration
  • Produktdetail Konfiguration
  • Erstellung von Seo optimierten Seiten

Nachfolgend noch die offizielle Beschreibung der Joomla Komponente

Bei Fsr Amazon handelt es sich um eine Komponente für das Content Management System (CMS) Joomla, mit deren Hilfe Produkte der Amazon Plattform(en) über einen von Amazon zur Verfügung gestellten Web Service (» AWS) mit Hilfe der Amazon » Product Advertising API in Joomla integriert werden können. Neben der Darstellung der Produkte als Produkt-Übersichts- und –Detailseiten, ist auch Amazons Produktempfehlung integriert, welche die eingebunden Produkte Provisionen auf generierte Umsätze ermöglicht.

sowie ein Link zu mehr Infos

Montag, 12. September 2011

Creates minimal (simple, portable) Http Server (Apache, Xampp)

Following post describes how to build a minimal (portable) Http Server with the help of apache (xampp project)




Download an extract a Xampp Project (e.g. http://freefr.dl.sourceforge.net/project/xampp/XAMPP%20Windows/1.7.3/xampp-win32-1.7.3.zip)

Create following folder structure

PortableHttpServer
|-   apache|    |- bin
|    |- conf
|    |- logs
|    |- modules
|
|-   htdocs


Copy following files:


xampp/apache/bin/httpd.exe   => apache/bin
xampp/apache/bin/libapr-1.dll   => apache/bin
xampp/apache/bin/libapriconv-1.dll   => apache/bin
xampp/apache/bin/libaprutil-1.dll   => apache/bin
xampp/apache/bin/libhttpd.dll   => apache/bin


xampp/apache/conf/mime.types => apache/conf


xampp/apache/modules/mod_dir.so => apache/modules
xampp/apache/modules/mod_mime.so => apache/modules


Create httpd.conf file in apache/conf with following content


#
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to 
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 0.0.0.0:80
#Listen [::]:80
Listen 4444


#
# Dynamic Shared Object (DSO) Support
#
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding `LoadModule' lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Statically compiled modules (those listed by `httpd -l') do not need
# to be loaded here.
#
# Example:
# LoadModule foo_module modules/mod_foo.so
#
LoadModule dir_module modules/mod_dir.so
LoadModule mime_module modules/mod_mime.so


#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
ServerName localhost:4444


#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "../htdocs"


#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
    DirectoryIndex index.html index.htm
</IfModule>
  
#
# DefaultType: the default MIME type the server will use for a document
# if it cannot otherwise determine one, such as from filename extensions.
# If your server contains mostly text or HTML documents, "text/plain" is
# a good value.  If most of your content is binary, such as applications
# or images, you may want to use "application/octet-stream" instead to
# keep browsers from trying to display binary files as though they are
# text.
#
DefaultType text/plain


#
# DefaultType: the default MIME type the server will use for a document
# if it cannot otherwise determine one, such as from filename extensions.
# If your server contains mostly text or HTML documents, "text/plain" is
# a good value.  If most of your content is binary, such as applications
# or images, you may want to use "application/octet-stream" instead to
# keep browsers from trying to display binary files as though they are
# text.
#
DefaultType text/plain


<IfModule mime_module>
    #
    # TypesConfig points to the file containing the list of mappings from
    # filename extension to MIME-type.
    #
    TypesConfig "conf/mime.types"


    #
    # AddType allows you to add to or override the MIME configuration
    # file specified in TypesConfig for specific file types.
    #
    #AddType application/x-gzip .tgz
    #
    # AddEncoding allows you to have certain browsers uncompress
    # information on the fly. Note: Not all browsers support this.
    #
    #AddEncoding x-compress .Z
    #AddEncoding x-gzip .gz .tgz
    #
    # If the AddEncoding directives above are commented-out, then you
    # probably should define those extensions to indicate media types:
    #
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz


</IfModule>

Create start.bat file in PortableHttpServer with following content:


cd "%~dp0apache/bin/"
httpd.exe -f conf/httpd.conf


pause


Create index.html in htdocs with following content:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <title>Title of Webpage</title>
  </head>
  <body>
    <p>Content of webpage</p>
  </body>
</html>

Start start.bat
Open webbrowser with following url: http://localhost:4444

Freitag, 15. Juli 2011

Update multiple columns with select statement in Postgres

Wenn man mehrere Spalten in per SELECT updaten will, muss man in Postgres eine spezielle Syntax verwenden. Die Standard Update Syntax funktioniert (noch) nicht mehr hier unter 'Compatibility'.

Das nachfolgende Beispiel zeigt die Möglichkeiten für das gewünschte Update auf mehrere Spalten:

CREATE TEMP TABLE test (id int, a varchar, b varchar);

INSERT INTO test VALUES (1, 'a', 'b');

UPDATE test SET (a, b) = ('c', 'd') WHERE id = 1; -- << OK

UPDATE test SET a = (SELECT 'c'), b = (SELECT 'd') WHERE id = 1; -- << OK

UPDATE test SET (a, b) = (SELECT 'c', 'd') WHERE id = 1; -- << FAIL

UPDATE test SET a = sub.a, b = sub.b FROM (SELECT 'c' AS a, 'd' AS b) sub WHERE id = 1; -- << OK


Montag, 6. Juni 2011

Neues Kommandofenster in Windows 7 öffnen (Command window/prompt here)

Etwas versteckt, aber doch gut nutzbar hat Microsoft das Öffnen eines neuen Kommandofensters in das Explorer Kontext-Menü eingebaut.

Hierzu muss man mit gedrückter SHIFT-Taste einen Rechtsklick auf den gewünschten Ordner ausführen.





Freitag, 3. Juni 2011

Seiten in joomla nicht mehr erreichbar nach Verzeichnisschutz bei 1und1

Ich wollte für die folgende Seite (ein joomla Projekt) mal einen Verzeichnisschutz bei 1und1 ausprobieren: http://www.korrekt-gera.de/
Danach funktionierte jedoch nix mehr. Die Startseite ist zwar noch erreichbar jedoch keine andere Seite mehr über einen Link in der Navigation. Außerdem gibt es Probleme im Adminbereich bei der Darstellung der Artikel.
Offensichtlich hängt dies mit dem Verzeichnisschutz bei 1und1 zusammen. Nach stundenlangen Gesprächen mit 1und1 gab es auch keine Lösung. Nach weiteren Stunden der Fehlersuche bemerkte ich, dass die .htaccess überschrieben wurde. Da aber auch joomla einiges aus der .htaccess holt, passt dann natürlich nix mehr. Wenn man den Verzeichnisschutz wieder entfernt, wird die .htaccess sogar ganz gelöscht. Das Hochladen der .htaccess Datei funktioniert so auch nicht. Man muss eine htaccess.txt hoch laden und dann wieder in .htaccess umbenennen. Danach lief auch wieder alles glatt im joomla Projekt.

Donnerstag, 2. Juni 2011

Joomla Modul für Google +1 Button

Seit kurzem hat Google eine neue Möglichkeit geschaffen, mit nur einem Klick Google und anderen Nutzern mitzuteilen, dass man eine Seite gut findet. Das wollten wir für die Seite http://www.eheringe-trauringe-online.at einmal ausprobieren. Dafür habe ich ein joomla Modul gesucht. Gefunden habe ich es auf folgender Seite: http://rndm-snippets.blogspot.com/2011/06/how-to-add-google-1-button-to-joomla-16.html
Das Modul gibt es sowohl für die 1.5er Version (Google +1 Button für joomla 1.5) als auch schon für die 1.6er Version (Google +1 Button für joomla 1.6) zum Download.