0

私はこのかなり複雑なSQLステートメントを作成するのに数日を費やしました、そしてそれは今私が欲しいものを正確に与えてくれます。今、私はそれを行うためのより良い/より簡単な方法があるかどうか疑問に思っています。また、MySQLはこのステートメントでCREATEVIEWを許可しません

 SELECT name, a.user, liste, c.order, total_1kr, total_5kr, total_8kr, total_10kr, total_paid, differens, sbdato, spaid, sbreg, sfdato, sforbrug, sfreg
 FROM (SELECT t.user, t.paid AS spaid, t.dato AS sbdato, t.registrant AS sbreg FROM( 
    SELECT user,MAX(dato) AS maksdato
    FROM g_kiosk_f WHERE paid!=0
    GROUP BY user) AS x 
    JOIN g_kiosk_f AS t ON x.user =t.user
    AND x.maksdato = t.dato) AS a 
 JOIN (SELECT s.user, (s.1kr+(s.5kr)*5+(s.8kr)*8+(s.10kr)*10) AS sforbrug, s.dato AS sfdato, s.registrant AS sfreg FROM( 
    SELECT user,MAX(dato) AS maksdato
    FROM g_kiosk_f WHERE 1kr!=0 OR 5kr!=0 OR 8kr!=0 OR 10kr!=0
    GROUP BY user) AS y 
    JOIN g_kiosk_f AS s ON y.user=s.user
    AND y.maksdato=s.dato) AS b
 JOIN (SELECT t1.name, t2.user, t1.liste, t1.order, sum( t2.1kr ) AS total_1kr, sum( t2.5kr ) *5 AS total_5kr, sum( t2.8kr ) *8 AS total_8kr, sum( t2.10kr ) *10 AS total_10kr, sum( t2.paid ) AS total_paid, ( sum( t2.1kr ) + sum( t2.5kr ) *5 + sum( t2.8kr ) *8 + sum( t2.10kr ) *10 - sum( t2.paid )) AS differens
    FROM g_kiosk_users AS t1
    INNER JOIN g_kiosk_f AS t2 ON t1.nr = t2.user
    GROUP BY t2.user
    ORDER BY t1.name ASC) AS c
 ON a.user=b.user AND a.user=c.user

ID(ユーザー)、日付(dato)、5種類のトランザクション(1kr、5kr、8kr、10kr、および支払い済み)のレジ係(登録者)を含むテーブル'g_kiosk_f'があります。別のテーブル'g_kiosk_users'には、名前とID(nr)が含まれています。結果セットを表示したい

  1. 支払われた日付、登録者、および最新の取引額!= 0
  2. 1kr!= 0、5kr!= 0、8kr!= 0または10kr!= 0の日付、登録者、および最新のトランザクションの金額
  3. sum(1kr + 5kr + 8kr + 10kr)とsum(paid)の合計差

したがって、結果は次のようになります。

Name | id | difference | newest paid date | newest paid registrant | newest paid amount | newest kr date | newest kr registrant | newest kr amount |

上記では、ある時点で必要だったため、手動で差の計算を行うために必要な数値を含めましたが、現在は廃止されています。これのどれかが意味をなしますか?

4

1 に答える 1

0

クエリを次のように書き直すことができるはずです。

SELECT t1.name AS name, a.user AS user, t1.liste, t1.order,
       SUM(t2.1kr) AS total_1kr, SUM(t2.5kr) * 5 AS total_5kr,
       SUM(t2.8kr) AS total_8kr, SUM(t2.10kr) * 5 AS total_10kr,
       SUM(52.paid) AS total_paid,
       SUM(t2.1kr + 5 * tt.5kr + 8 * t2.8kr + 10 * t2.10kr)
       -SUM(t2.paid) AS differens
       a.dato AS sbdato, a.paid AS spaid, a.registrant as sbreg,
       b.dato as sfdato,
       (b.1kr+(b.5kr)*5+(b.8kr)*8+(b.10kr)*10) AS sforbrug,
       b.registrant AS sfreg
FROM g_kiosk_f AS a
INNER JOIN (SELECT user, MAX(dato) FROM g_kiosk_f
            WHERE paid != 0
            GROUP BY user) AS a2
ON a.user = a2.user AND a.dato = a2.dato
INNER JOIN g_kiosk_f as b  ON b.user = a.user
INNER JOIN (SELECT user, MAX(dato) FROM g_kiosk_f
            WHERE  1kr!=0 OR 5kr!=0 OR 8kr!=0 OR 10kr!=0
            GROUP BY user) AS b2
ON b.user = b2.user AND b.dato = b2.dato
INNER JOIN g_kiosk_f as t2 ON t2.user = a.user
INNER JOIN g_kiosk_users as t1 ON t1.nr = t2.user
GROUP BY a.user
ORDER BY name ASC
于 2012-12-25T12:59:22.997 に答える