0

I've spent a couple of days creating this rather complicated SQL statement, and it now gives me exactly what I want. Now I'm wondering if there is a better/simpler way to do it. Also MySQL wont let me CREATE VIEW on this statement

 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

I have a table 'g_kiosk_f' containing id (user), date (dato), 5 kinds of transactions (1kr, 5kr, 8kr, 10kr, and paid) a cashier (registrant). Another table, 'g_kiosk_users', contains name and id (nr). I want a result set showing

  1. the date, registrant and amount of newest transaction where paid!=0
  2. the date, registrant and amount of newest transaction where 1kr!=0, 5kr!=0, 8kr!=0 or 10kr!=0
  3. the total difference of sum(1kr+5kr+8kr+10kr) and sum(paid)

So the result should look something like

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

In the above I've included the numbers needed to do the difference calculation by hand, because it was needed at some point, but is now obsolete. Does any of this even make sense?

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 回答