Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'output': 0.04; 'subsequent': 0.04; 'column': 0.07; 'function,': 0.07; 'mysql,': 0.07; 'terminated': 0.07; 'trailing': 0.07; 'python': 0.09; 'integer,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sqlite': 0.09; 'stored': 0.10; 'index': 0.13; '(there': 0.16; 'distinct': 0.16; 'presume': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sqlite3': 0.16; 'statement.': 0.16; 'mon,': 0.16; 'integer': 0.17; 'subject:request': 0.17; 'feb': 0.19; '+0000': 0.20; 'skip:= 10': 0.20; 'suggested': 0.20; 'demonstrate': 0.23; 'insert': 0.23; 'matching': 0.23; '----------': 0.26; 'select': 0.26; 'values': 0.26; 'separate': 0.27; 'entries': 0.27; "doesn't": 0.28; 'header:X-Complaints- To:1': 0.28; 'equivalent,': 0.29; 'statements': 0.29; 'unique,': 0.29; 'probably': 0.29; "i'm": 0.29; 'maybe': 0.29; 'function': 0.30; 'primary': 0.30; 'club': 0.33; 'function.': 0.33; 'url:home': 0.33; 'to:addr:python-list': 0.33; 'another': 0.33; 'that,': 0.34; 'version': 0.34; 'skip:- 20': 0.34; 'table': 0.35; 'something': 0.35; 'received:org': 0.36; 'test': 0.36; 'should': 0.36; 'keeps': 0.37; 'does': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'to:addr:python.org': 0.39; 'space': 0.39; 'called': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'your': 0.60; 'leading': 0.61; 'skip:n 10': 0.63; 'within': 0.64; 'here': 0.65; 'limit': 0.65; 'union': 0.66; 'foreign': 0.72; '2");': 0.84; '2013': 0.84; '3");': 0.84; '359': 0.91; 'by.': 0.91; 'dennis': 0.91; 'received:108': 0.91; 'poorly': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: Improve reduce functions of SQLite3 request Date: Mon, 04 Feb 2013 15:45:27 -0500 Organization: > Bestiaria Support Staff < References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: adsl-108-79-219-20.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 3.3/32.846 X-No-Archive: YES X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 117 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360010737 news.xs4all.nl 6904 [2001:888:2000:d::a6]:36743 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38138 On Mon, 4 Feb 2013 14:30:35 +0000 (UTC), Steffen Mutter declaimed the following in gmane.comp.python.general: > > here the output of your suggested solution: > SELECT DISTINCT * FROM ( > SELECT HeimNr as nr, Heim as club FROM Runde20122013 > WHERE kreis ="karlsruhe" > UNION > SELECT GastNr as nr, gast as club FROM Runde20122013 > WHERE kreis ="karlsruhe") LIMIT 10; > > 359|TV Calmbach > 21101|SG Heidel/Helm > 21236|JSG Neuth/Büch > 23108|TG Eggenstein > 23108|TGEggenstein 2 <- > 23109|TV Ettlingenw > 23109|TV Ettlingenw 2 <- > 23112|TSV Jöhlingen > 23112|TSV Jöhlingen 2 <- > 23112|TSV Jöhlingen 3 <- > > Still not like what I'm looking for. I presume you mean the " 2" and " 3" on those entries should not be considered when matching "distinct". I suspect you have a poorly normalized database (what does that trailing number identify? Heck, are the leading initials unique to the subsequent name?). The trailing number should probably be something stored as a separate field. If the initials are unique, they should be a separate field used as a foreign reference to retrieve the longer name. For MySQL, MAYBE (there is a single space in the ' ') SELECT DISTINCT nr, substring_index(cb, ' ', 2) as club FROM ( SELECT HeimNr as nr, Heim as cb FROM Runde20122013 WHERE kreis ="karlsruhe" UNION SELECT GastNr as nr, gast as cb FROM Runde20122013 WHERE kreis ="karlsruhe") LIMIT 10; This function keeps only the part before the 2nd space (if such is found) in the club name. Oh wait -- you are using SQLite3... SQLite3 doesn't have the substring_index function, and I don't even find an index function. However, you CAN write a Python function to do the equivalent, and register it with SQLite3 so that it can be called from within the SQL statement. I'm not going to demonstrate that, though it may be better than another alternative: SELECT DISTINCT nr, min(cb) as club FROM ( SELECT HeimNr as nr, Heim as cb FROM Runde20122013 WHERE kreis ="karlsruhe" UNION SELECT GastNr as nr, gast as cb FROM Runde20122013 WHERE kreis ="karlsruhe") GROUP BY nr LIMIT 10; Let's see... E:\UserData\Wulfraed\My Documents>sqlite3 demo SQLite version 3.6.3 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite> create table test ...> ( ...> ID integer primary key autoincrement, ...> nr integer, ...> club varchar(50) ...> ); sqlite> insert into test (nr, club) values (359, "TV Calmbach"); sqlite> insert into test (nr, club) values (21101,"SG Heidel/Helm"); sqlite> insert into test (nr, club) values (23112,"TSV Johlingen 3"); sqlite> insert into test (nr, club) values (23109,"TV Ettlingenw 2"); sqlite> insert into test (nr, club) values (23112,"TSV Johlingen"); sqlite> insert into test (nr, club) values (23109,"TV Ettlingenw"); sqlite> insert into test (nr, club) values (23108,"TG Eggenstein"); sqlite> insert into test (nr, club) values (21236,"JSG Neuth/Buch"); sqlite> insert into test (nr, club) values (23112,"TSV Johlingen 2"); sqlite> insert into test (nr, club) values (23108,"TG Eggenstein 2"); sqlite> .mode column sqlite> .head on sqlite> .width 10, 25, 25 sqlite> select * from test; ID nr club ---------- ------------------------- ------------------------- 1 359 TV Calmbach 2 21101 SG Heidel/Helm 3 23112 TSV Johlingen 3 4 23109 TV Ettlingenw 2 5 23112 TSV Johlingen 6 23109 TV Ettlingenw 7 23108 TG Eggenstein 8 21236 JSG Neuth/Buch 9 23112 TSV Johlingen 2 10 23108 TG Eggenstein 2 sqlite> select nr, min(club) from test group by nr; nr min(club) ---------- ------------------------- 359 TV Calmbach 21101 SG Heidel/Helm 21236 JSG Neuth/Buch 23108 TG Eggenstein 23109 TV Ettlingenw 23112 TSV Johlingen sqlite> Don't even need the DISTINCT with the GROUP BY. -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/