Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id CE716B5DC6E for ; Sat, 22 Oct 2011 09:55:46 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 09982-05 for ; Sat, 22 Oct 2011 12:55:40 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from nm22-vm0.bullet.mail.ac4.yahoo.com (nm22-vm0.bullet.mail.ac4.yahoo.com [98.139.53.218]) by mail.postgresql.org (Postfix) with SMTP id A63E2B5DC65 for ; Sat, 22 Oct 2011 09:55:40 -0300 (ADT) Received: from [98.139.52.189] by nm22.bullet.mail.ac4.yahoo.com with NNFMP; 22 Oct 2011 12:55:40 -0000 Received: from [98.139.52.174] by tm2.bullet.mail.ac4.yahoo.com with NNFMP; 22 Oct 2011 12:55:40 -0000 Received: from [127.0.0.1] by omp1057.mail.ac4.yahoo.com with NNFMP; 22 Oct 2011 12:55:40 -0000 X-Yahoo-Newman-Id: 279169.97601.bm@omp1057.mail.ac4.yahoo.com Received: (qmail 54814 invoked from network); 22 Oct 2011 12:55:40 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=DKIM-Signature:X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:References:Mime-Version:In-Reply-To:X-Apple-Yahoo-Original-Message-Folder:Content-Type:Content-Transfer-Encoding:Message-Id:Cc:X-Mailer:From:X-Apple-Yahoo-Replied-Msgid:Subject:Date:To; b=UjSQ2wYToG5tOX8bNKxsSuToC3ynNxrFiyZ/AR5NOX0Pe1+MaXZ2VBx+7AUcf0N9ytSrY07uS75eQp7zwumc0xrtR7q7p3m7BfL8cx1Md44K52An/f5lROzCu177cgrZ6Rzk0HPAN/A7ymwUGhv0YJ871XJjglethKPj8X6Q0pQ= ; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1319288140; bh=SRcld9yGGt1eebFUttVtRv4oxWT7r8iQg/rLTxIVMOc=; h=X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:References:Mime-Version:In-Reply-To:X-Apple-Yahoo-Original-Message-Folder:Content-Type:Content-Transfer-Encoding:Message-Id:Cc:X-Mailer:From:X-Apple-Yahoo-Replied-Msgid:Subject:Date:To; b=Pru+sWiYROlfIG19rDiqlqmzWSxTVlRs686us3fjuh0ib3jixii5X39qvgayG+9ZE+l50MnhsKS1O3Ds2xkhP0oIoyEVFWq3c9beliYeN96UiSa4U0gUX3GIabZZzew0vnQoJ7m2iVNh0w8/9G5SGIWOIaeeIOEinPAnWPTKhHs= X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: W2rE3w8VM1mzTld6HyqGabNG4eULD9qaNwlRGQSGUVq_HmX L8OUVHKDnEEkkJpMLK._qtFBrKK8bgCw0cmF2eGS04FqC9bJamdNtjXS1YXf O1k6.69cHVGG.kMjb3cNGAtb.AWd0aJ1zJj0mZs_pE_RGRg7EcC.9qcH2FQC Cs2VZFSvbAwBLrPggrPLqtzzpb4B0H0YtEt2EYN5yfpbJo9bONEwZDU56Ekk rLghciOKzFcMao_fAlcKlkOzlSPF2yOkZTTWwdxLBBpv0Tcda7CumpoPpEIQ RW8bOsOTc2VJm6WbH.9gVOZwdHCpiM2qofwZ.Z6417oPyQXjkGAyxOZHJj4T tKd1j3wYQS7CzgdBsBMY6ONduNp8ImuifNsNvY3HeTluMZuCg5LerGcrFsGc Vh1rPPQ-- X-Yahoo-SMTP: mpGJl6eswBD2IBufoVEg0Pa8gg-- Received: from [192.12.17.102] (polobo@24.93.23.188 with xymcookie) by smtp108-mob.biz.mail.ac4.yahoo.com with SMTP; 22 Oct 2011 05:55:39 -0700 PDT References: <4EA29DDB.9000300@linos.es> Mime-Version: 1.0 (iPad Mail 8J2) In-Reply-To: <4EA29DDB.9000300@linos.es> X-Apple-Yahoo-Original-Message-Folder: Inbox Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Message-Id: <00655F1E-08DC-42C5-B702-C8D332081557@yahoo.com> Cc: "pgsql-sql@postgresql.org" X-Mailer: iPad Mail (8J2) From: David Johnston X-Apple-Yahoo-Replied-Msgid: 1_514309_AOKkiGIAAStOTqKfxACiJn2j4tM Subject: Re: advice on how to store variable attributes Date: Sat, 22 Oct 2011 08:53:52 -0400 To: Linos X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.403 tagged_above=-5 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RP_MATCHES_RCVD=-0.504 X-Spam-Level: X-Archive-Number: 201110/50 X-Sequence-Number: 35934 On Oct 22, 2011, at 6:41, Linos wrote: > Hi all, > i need a little of advice on what could be the best way to store this i= nformation. >=20 > We need to calculate the difference in costs for our operations, we are al= ready > storing our vendor invoices in the database so calculate the monetary chan= ge it > is a no-brainer but we need to store special attributes for any of the inv= oices > that we need to compare too, for example: > -electric provider: total Kw. > -water provider: total m3. > -car maintenance: kilometers of the car. > -mobile phones provider: international call minutes, national minutes, n= umber > of sms, etc.. >=20 > And much more types/variables, the number of variables can change, not eve= ry day > but still can change, i would like that they can be defined/changed from o= ur > application, so alter table to add columns don't seem the best way (still a= n > option though). We will have "generic" reports that will show us changes i= n > costs and specific reports for the types with "extended attributes" that w= e want > to compare. >=20 > To compare values from this "extended attributes" i think we have two ways= : > 1- have them in columns and use standard SQL. > 2- create the columns with a function that reads this attrs and create t= he columns. >=20 > So far i thin we have this options: > 1- a bunch of columns that would be null except when the type of the in= voice > uses them. > 2- a table related with the vendor invoices table for every type of inv= oice > with his specifics columns. > 3- a key/value in a separate table related with the vendor invoices tab= le where > i store the extended attrs of every invoice that needs them. > 4- use a hstore column in the vendor invoces table to store this attrs.= >=20 > The first two have the problem of probably changes to the number of attrib= utes > of every type and give a more closed solution, apart from that 1- seems to= be a > bit awkward and 2- would need the application that creates the query to kn= ow > with what table should join for every type (other point we will need to ch= ange > if we want to create new invoices types). >=20 > The last two have his own problems too, with 3 i will need to create a fun= ction > that return rows as columns to compare them, with 4- given that i will sto= re the > attrs of every type in the database anyway i can use the operator -> (with= a > CASE using operator ? returning 0 if the searched attr it is not in the hs= tore) > but still don't seem a clean solution for me. >=20 > For me it seems i am missing something, probably any of you have a much mo= re > elegant (or correct) way to handle this situation, what would be your advi= ce? > Thanks. >=20 >=20 Create a table with a single numeric column and multiple category columns. ( amount_value, amount_unit, amount_category, vendor_id ) If necessary each "amount_value" data type should have it's own table since t= he processing logic will vary (I.e., you cannot subtract text or Boolean val= ues). You are , in effect, creating multiple tables but combining them into one an= d using the category column to distinguish between them. David J.=