How to use the pypika.fn.Sum function in PyPika

To help you get started, we’ve selected a few PyPika examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kayak / pypika / test / test_criterions.py View on Github external
def test_function_with_only_fields_for_table(self):
        f = fn.Sum(self.t0.foo).for_(self.t1)

        self.assertEqual('SUM(t1.foo)', str(f))
github kayak / pypika / test / test_functions.py View on Github external
def test__sum(self):
        q = Q.from_('abc').select(fn.Sum('foo'))

        self.assertEqual('SELECT SUM(foo) FROM abc', str(q))
github kayak / pypika / test / test_functions.py View on Github external
def test__arithmetic_with_function(self):
        q1 = Q.from_('abc').select(fn.Sum('foo') + 1)
        q2 = Q.from_(self.t).select(fn.Sum(self.t.foo) + 1)

        self.assertEqual('SELECT SUM(foo)+1 FROM abc', str(q1))
        self.assertEqual('SELECT SUM(foo)+1 FROM abc', str(q2))
github kayak / pypika / test / test_selects.py View on Github external
def test_having_join_and_equality(self):
        q = Query.from_(self.t0).join(
            self.t1, how=JoinType.inner
        ).on(
            self.t0.foo == self.t1.foo
        ).select(
            self.t0.foo, fn.Sum(self.t1.bar), self.t0.buz
        ).groupby(
            self.t0.foo
        ).having(
            self.t0.buz == 'fiz'
        ).having(
            fn.Sum(self.t1.bar) > 100
        )

        self.assertEqual('SELECT t0.foo,SUM(t1.bar),t0.buz FROM abc t0 '
                         'INNER JOIN efg t1 ON t0.foo=t1.foo '
                         'GROUP BY t0.foo '
                         "HAVING t0.buz='fiz' AND SUM(t1.bar)>100", str(q))
github kayak / pypika / test / test_selects.py View on Github external
def test_having_greater_than(self):
        q = Query.from_(self.t0).select(
            self.t0.foo, fn.Sum(self.t0.bar)
        ).groupby(
            self.t0.foo
        ).having(
            fn.Sum(self.t0.bar) > 1
        )

        self.assertEqual('SELECT foo,SUM(bar) FROM abc GROUP BY foo HAVING SUM(bar)>1', str(q))
github kayak / pypika / test / test_joins.py View on Github external
def test_select_field_from_missing_table(self):
        with self.assertRaises(JoinException):
            Query.from_(self.t0).select(self.t1.foo)

        with self.assertRaises(JoinException):
            Query.from_(self.t0).where(self.t1.foo == 0)

        with self.assertRaises(JoinException):
            Query.from_(self.t0).where(fn.Sum(self.t1.foo) == 0)

        with self.assertRaises(JoinException):
            Query.from_(self.t0).select(fn.Sum(self.t0.bar * 2) + fn.Sum(self.t1.foo * 2))

        with self.assertRaises(JoinException):
            Query.from_(self.t0).groupby(self.t1.foo)

        with self.assertRaises(JoinException):
            Query.from_(self.t0).groupby(self.t0.foo).having(self.t1.bar)
github kayak / pypika / test / test_selects.py View on Github external
def test_having_greater_than(self):
        q = Query.from_(self.t0).select(
            self.t0.foo, fn.Sum(self.t0.bar)
        ).groupby(
            self.t0.foo
        ).having(
            fn.Sum(self.t0.bar) > 1
        )

        self.assertEqual('SELECT foo,SUM(bar) FROM abc GROUP BY foo HAVING SUM(bar)>1', str(q))
github kayak / pypika / test / test_functions.py View on Github external
def test__arithmetic_with_function(self):
        q1 = Q.from_('abc').select(fn.Sum('foo') + 1)
        q2 = Q.from_(self.t).select(fn.Sum(self.t.foo) + 1)

        self.assertEqual('SELECT SUM(foo)+1 FROM abc', str(q1))
        self.assertEqual('SELECT SUM(foo)+1 FROM abc', str(q2))