SUM probleem

Pagina: 1
Acties:

Acties:
  • 0 Henk 'm!

  • Willie-wortel
  • Registratie: Mei 2002
  • Laatst online: 19:23
wanneer ik dit heb:

SQL:
1
2
3
4
5
6
7
8
9
10
11
12
13
SELECT IF(productoptions_price IS NULL, product_count*product_price, (product_price + SUM(productoptions_price))*product_count) AS total_price FROM administration_products

LEFT JOIN administration_productoptions 
ON administration_products.product_id = administration_productoptions.productoptions_id

LEFT JOIN administration_transactions
ON administration_products.transaction_number = administration_transactions.transaction_number


WHERE administration_products.transaction_number = 173909


GROUP BY administration_products.product_id



en er rolt dit uit:

total_price
114.2400
50.0000
19.9800


Maar ik wil graag alleen de som zien, dus: 184,22

Hoe moet de Query er dan uit zien? Het moet dan een "Group By administration_products.transaction_number" worden volgens mij, maar dan......

Open source FanX RF Dongle bij vraag en aanbod!


Acties:
  • 0 Henk 'm!

  • RobIII
  • Registratie: December 2001
  • Niet online

RobIII

Admin Devschuur®

^ Romeinse Ⅲ ja!

(overleden)
Hoe werkt dat GROUP BY nu eigenlijk?

Maar op 't eerste oog lijk je een sum te vergeten...

SQL:
1
2
SELECT Sum(IF(productoptions_price IS NULL, product_count*product_price, (product_price + SUM(productoptions_price))*product_count)) AS total_price
...

[ Voor 52% gewijzigd door RobIII op 03-04-2009 18:18 ]

There are only two hard problems in distributed systems: 2. Exactly-once delivery 1. Guaranteed order of messages 2. Exactly-once delivery.

Je eigen tweaker.me redirect

Over mij


Acties:
  • 0 Henk 'm!

  • Willie-wortel
  • Registratie: Mei 2002
  • Laatst online: 19:23
deze werkt dus niet:

SQL:
1
2
3
4
5
6
7
8
9
10
11
12
13
SELECT Sum(IF(productoptions_price IS NULL, product_count*product_price, (product_price + SUM(productoptions_price))*product_count)) AS total_price FROM administration_products

LEFT JOIN administration_productoptions 
ON administration_products.product_id = administration_productoptions.productoptions_id

LEFT JOIN administration_transactions
ON administration_products.transaction_number = administration_transactions.transaction_number


WHERE administration_products.transaction_number = 173909


GROUP BY administration_transactions.transaction_number

Open source FanX RF Dongle bij vraag en aanbod!


Acties:
  • 0 Henk 'm!

  • GlowMouse
  • Registratie: November 2002
  • Niet online
Subquery lijkt me het eenvoudigst:
SQL:
1
SELECT SUM(total_price) FROM ( SELECT ... )

Acties:
  • 0 Henk 'm!

  • pedorus
  • Registratie: Januari 2008
  • Niet online
Dit is een beetje een gekke query. Waarom doe je left-join op administration_transactions als je naar een bepaalde transactie op zoek bent? Ik zou eigenlijk bij die tabel beginnen, dan een inner join doen, en dan pas een left join. :)

Voor dit probleem denk ik dat je een subquery nodig heb, dus:
SQL:
1
2
select sum(betere_naam) as total_price from
 ([huidige query]) as sum_per_row;

Vitamine D tekorten in Nederland | Dodelijk coronaforum gesloten


Acties:
  • 0 Henk 'm!

  • Willie-wortel
  • Registratie: Mei 2002
  • Laatst online: 19:23
ja klopt dat ie er nu gek uit ziet, omdat hij een beetje vereenvoudigt is. Ik heb uiteindelijk ook gegevens nodig uit de transaction tabel en zoek dan ook niet meer op transaction number.

Open source FanX RF Dongle bij vraag en aanbod!


Acties:
  • 0 Henk 'm!

  • Willie-wortel
  • Registratie: Mei 2002
  • Laatst online: 19:23
dit werkt:

SQL:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
SELECT sum(total_price) as testje FROM (SELECT IF(productoptions_price IS NULL, product_count*product_price, (product_count*product_price) + SUM(productoptions_price)*product_count) AS total_price FROM administration_products

LEFT JOIN administration_productoptions 
ON administration_products.product_id = administration_productoptions.productoptions_id

LEFT JOIN administration_transactions
ON administration_products.transaction_number = administration_transactions.transaction_number


WHERE administration_products.transaction_number = 173909


GROUP BY administration_products.product_id)

as sum_per_row



Nu moet ik alleen nog het transactie nummer erbij zien te toveren..... dus:

nr. totaal
173909 184.22

Hoe kan ik deze er in krijgen? Dit werkt niet:

SQL:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
SELECT test2, sum(total_price) as testje FROM (SELECT administration_products.product_id as test2, IF(productoptions_price IS NULL, product_count*product_price, (product_count*product_price) + SUM(productoptions_price)*product_count) AS total_price FROM administration_products

LEFT JOIN administration_productoptions 
ON administration_products.product_id = administration_productoptions.productoptions_id

LEFT JOIN administration_transactions
ON administration_products.transaction_number = administration_transactions.transaction_number


WHERE administration_products.transaction_number = 173909


GROUP BY administration_products.product_id)

as sum_per_row

[ Voor 35% gewijzigd door Willie-wortel op 03-04-2009 20:45 ]

Open source FanX RF Dongle bij vraag en aanbod!


Acties:
  • 0 Henk 'm!

  • pedorus
  • Registratie: Januari 2008
  • Niet online
Gebruik select 173909 as nr, ... ;)

Maar ik neem aan dat je dit wil doen voor meerdere transacties, want anders is het niet zo zinnig. In dat geval moet je in de binnenste query zowel groeperen op transactie als op product (en transactienr ook selecteren), en in de buitenste alleen op transactienr.

Vitamine D tekorten in Nederland | Dodelijk coronaforum gesloten


Acties:
  • 0 Henk 'm!

  • Willie-wortel
  • Registratie: Mei 2002
  • Laatst online: 19:23
ja, haha, inderdaad. Uiteindelijk wil ik een selectie maken op datum.

Open source FanX RF Dongle bij vraag en aanbod!


Acties:
  • 0 Henk 'm!

  • NMe
  • Registratie: Februari 2004
  • Laatst online: 09-09 13:58

NMe

Quia Ego Sic Dico.

Willie-wortel schreef op vrijdag 03 april 2009 @ 19:52:
Nu moet ik alleen nog het transactie nummer erbij zien te toveren..... dus:
Ga nu eerst eens die link doorlezen die RobIII al in de eerste reply gaf: Programming FAQ - SQL

Ik zie nog steeds een veld geselecteerd worden dat niet terug komt in je group by. Elk fatsoenlijk DBMS zou daar een melding van geven, maar MySQL doet dat niet.

'E's fighting in there!' he stuttered, grabbing the captain's arm.
'All by himself?' said the captain.
'No, with everyone!' shouted Nobby, hopping from one foot to the other.

Pagina: 1