I have the following sql tables
oitems table
+---------+-----------+----------+
| orderid | catalogid | numitems |
+---------+-----------+----------+
| O737 | 353 | 1 |
| O738 | 364 | 4 |
| O739 | 353 | 3 |
| O740 | 364 | 6 |
| O741 | 882 | 2 |
| O742 | 224 | 5 |
| O743 | 224 | 2 |
+---------+-----------+----------+
Orders table
+-----------------+------------+------------+
| orderid | ocardtype | odate |
+-----------------+------------+------------+
| O737 | Paypal | | 'OK
| O738 | MasterCard | 01.02.2012 | 'OK
| O739 | MasterCard | 02.02.2012 | 'OK
| O740 | Visa | 03.02.2012 | 'OK
| O741 | Sofort | | 'OK
| O742 | | | 'ignore because ocardtype is empty
| O743 | MasterCard | | 'ignore because Mastercard no odate
+-----------------+------------+------------+
the reusltant datatable called result
+-----------+----------+--------------+
| catalogid | numitems | ignoreditems |
+-----------+----------+--------------+
| 353 | 4 | 0 |
| 364 | 10 | 0 |
| 882 | 2 | 0 |
| 224 | 0 | 7 |
+-----------+----------+--------------+
idea is to sum the numitems column for products that have the same catalogid
depinding on the data in the oitems
table with the following conditions
- if
ocardtype
is empty then ignore thenumitems
and consider it as0
in the sum and sum the ignored items to theignoreditems
column - if
ocardtype
for some order isMasterCard
orVisa
and theodate
is empty then ignore thenumitems
and consider it as0
and sum the ignored items to theignoreditems
column - if
ocardtype
isPaypal
orSofort
, then just do thenumitems
sum without checking the date because those types require noodate
basicly i want to save the result datatable to a temporary datatable and load it to a vb.net datatable
i am having a hard time figuring out how to do this in an sql query! i need this as sql command for vb.net , was able to do it programmatically using vb.net datatables using loops and alot of checking using linq is an option, but i just need to get this from the server