Return-Path: Delivered-To: apmail-db-derby-user-archive@www.apache.org Received: (qmail 65282 invoked from network); 30 Apr 2008 09:28:37 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 30 Apr 2008 09:28:37 -0000 Received: (qmail 87854 invoked by uid 500); 30 Apr 2008 09:28:36 -0000 Delivered-To: apmail-db-derby-user-archive@db.apache.org Received: (qmail 87827 invoked by uid 500); 30 Apr 2008 09:28:36 -0000 Mailing-List: contact derby-user-help@db.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: List-Id: Reply-To: "Derby Discussion" Delivered-To: mailing list derby-user@db.apache.org Received: (qmail 87815 invoked by uid 99); 30 Apr 2008 09:28:36 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 30 Apr 2008 02:28:36 -0700 X-ASF-Spam-Status: No, hits=1.2 required=10.0 tests=SPF_NEUTRAL X-Spam-Check-By: apache.org Received-SPF: neutral (nike.apache.org: local policy) Received: from [205.178.146.60] (HELO omr10.networksolutionsemail.com) (205.178.146.60) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 30 Apr 2008 09:27:43 +0000 Received: from mail.networksolutionsemail.com (ns-omr10 [10.49.6.73]) by omr10.networksolutionsemail.com (8.13.6/8.13.6) with SMTP id m3U9Rllq025591 for ; Wed, 30 Apr 2008 05:27:47 -0400 Received: (qmail 27017 invoked by uid 78); 30 Apr 2008 09:28:02 -0000 Received: from unknown (HELO desktop2) (since@opendemand.com@71.58.218.61) by ns-omr10.lb.hosting.dc2.netsol.com with SMTP; 30 Apr 2008 09:28:02 -0000 Message-ID: <5d1501c8aaa4$c8347a60$6e00a8c0@desktop2> From: "Stephen Ince" To: "Derby Discussion" References: <48181493.3080503@atreides.ro> Subject: median query Date: Wed, 30 Apr 2008 05:30:08 -0400 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=response Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.3138 X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2900.3198 X-Virus-Checked: Checked by ClamAV on apache.org I was trying to write a query that would return the median. I tried the following approached. 1) sql window functions, row_number. Can use this because derby can not on a column. 2) Cross-join technique. This fails because derby does not have good case support. I am getting null pointer exception. SELECT P1.weight FROM Parts AS P1, Parts AS P2 GROUP BY P1.weight HAVING SUM(CASE WHEN P2.weight <= P1.weight THEN 1 ELSE 0 END) >= ((COUNT(*) + 1) / 2) AND SUM(CASE WHEN P2.weight >= P1.weight THEN 1 ELSE 0 END) >= (COUNT(*)/2 + 1); Has anyone been success in writing a sql median query. Steve