特定のアイテムの販売が保存されている MySQL データベースがあります。過去 2 日間、たとえば過去 10 日間と過去 9 日間の売上を比較したいと考えています。そうするためにMySQLクエリを作成しました:
select s1.sales - s2.sales from ProductSales as s1
left join ProductSales as s2 on s2.date >= DATE(DATE_ADD(Now(), INTERVAL '-10' DAY)) and s2.date <= DATE(DATE_ADD(Now(), INTERVAL '-9' DAY)) and s2.product_id = s1.product_id
where s1.date >= DATE(DATE_ADD(Now(), INTERVAL '-9' DAY));
これを Grails アプリケーションでプログラム的に実行したいと考えており、このクエリを条件または HQL クエリに変換する必要があります。どうすればいいですか?
更新:私はこのように試しました:
def date1 = new Date() - 2
def date2 = new Date() - 1
def sales = ProductSales.executeQuery( """
select s1.sales - s2.sales from ProductSales s1
left join ProductSales s2
where s1.date >= :date2
and s2.date >= :date1 and s2.date < :date2 and s2.product_id = s1.product_id""",
[ date1: date1, date2: date2 ] )
残念ながら、これは私に与えます:
ERROR hql.ast.ErrorCounter - Path expected for join!
ERROR hql.ast.ErrorCounter - Invalid path: 's2.sales'
ERROR hql.ast.ErrorCounter - right-hand operand of a binary operator was null
ERROR hql.ast.ErrorCounter - Invalid path: 's2.date'
ERROR hql.ast.ErrorCounter - <AST>:0:0: unexpected end of subtree
ERROR hql.ast.ErrorCounter - left-hand operand of a binary operator was null
ERROR hql.ast.ErrorCounter - Invalid path: 's2.date'
ERROR hql.ast.ErrorCounter - <AST>:0:0: unexpected end of subtree
ERROR hql.ast.ErrorCounter - left-hand operand of a binary operator was null
ERROR hql.ast.ErrorCounter - Invalid path: 's2.product_id'
ERROR hql.ast.ErrorCounter - <AST>:0:0: unexpected end of subtree
ERROR hql.ast.ErrorCounter - left-hand operand of a binary operator was null
ERROR logging.impl.SLF4JLog - QuerySyntaxException occurred when processing request: [GET] /app/dashboard
Path expected for join!
更新 2 : 自分の質問に答えることができないため、ここに回答を追加します。
どうやらこれは次のように機能します。
def now = new Date()
def date1 = now - 2
def date2 = now - 1
def sales = ProductSales.executeQuery( """
select s1.owner, s1.sales - s2.sales from ProductSales s1, ProductSales s2
where s1.date >= :date2 and s1.date < :now
and s2.date >= :date1 and s2.date < :date2 and s2.product = s1.product""",
[ date1: date1, date2: date2, now: now ]
)
left join
ただし、 aと aの違いは完全にはわかりませんselect from T1, T2
。now
それとは別に、最近のエントリに上限 ( ) を追加し、 を に変更しproduct_id
ましたproduct
。