UNION syntax in Cakephp

Too many coders try to limit themselves to the functionality of a framework. DON’T. Use what the framework provides. If it does not have the functionality you seek, then either: Code the functionality you need into a class extension or Custom spin the code within the framework to suit your needs. Often, developers try to … Read more

Union of intervals

Sort them by one of the terms (start, for example), then check for overlaps with its (right-hand) neighbor as you move through the list. class tp: def __repr__(self): return “(%d,%d)” % (self.start, self.end) def __init__(self, start, end): self.start = start self.end = end s = [tp(5, 10), tp(7, 8), tp(0, 5)] s.sort(key=lambda self: self.start) y … Read more

Union of multiple ranges

Let’s say, (7, 10) and (11, 13) result into (7, 13): a = [(7, 10), (11, 13), (11, 15), (14, 20), (23, 39)] b = [] for begin,end in sorted(a): if b and b[-1][1] >= begin – 1: b[-1] = (b[-1][0], end) else: b.append((begin, end)) b is now [(7, 20), (23, 39)] EDIT: As @CentAu … Read more

Selecting 2 tables from 2 different databases (ACCESS)

You can use IN: SELECT t1.*, t2.* FROM T1 INNER JOIN (SELECT * FROM atable IN ‘C:\Docs\DB2.mdb’) t2 ON t1.ID=t2.ID EDIT: sc = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\docs\other.mdb” cn.open sc s=”SELECT * FROM t1 INNER JOIN ” _ & “[MS Access;PWD=databasePWD;DATABASE=C:\docs\db.mdb].t2 ON t1.ID=t2.ID” rs.Open s, cn EDIT 2: You can use the aliases to identify which database a … Read more

Merge two rows in SQL

Aggregate functions may help you out here. Aggregate functions ignore NULLs (at least that’s true on SQL Server, Oracle, and Jet/Access), so you could use a query like this (tested on SQL Server Express 2008 R2): SELECT FK, MAX(Field1) AS Field1, MAX(Field2) AS Field2 FROM table1 GROUP BY FK; I used MAX, but any aggregate … Read more

Combine two tables for one output

You’ll need to use UNION to combine the results of two queries. In your case: SELECT ChargeNum, CategoryID, SUM(Hours) FROM KnownHours GROUP BY ChargeNum, CategoryID UNION ALL SELECT ChargeNum, ‘Unknown’ AS CategoryID, SUM(Hours) FROM UnknownHours GROUP BY ChargeNum Note – If you use UNION ALL as in above, it’s no slower than running the two … Read more