i have the following sql query to get an idea of what it does please read the description below
select catalogid, numitems, allitems - numitems ignoreditems
from (
select i.catalogid,
sum(case when (ocardtype in ('PayPal','Sofort') OR
ocardtype in ('mastercard','visa') and
odate is not null) AND NOT EXISTS (
select * from booked b
where b.ignoredoid = o.orderid
) then numitems
else 0 end) numitems,
sum(numitems) allitems
from orders o
join oitems i on i.orderid=o.orderid
group by i.catalogid
) X
and 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
+-----------+----------+--------------+
| 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 is MasterCard or Visa and theodate
is empty then ignore thenumitems
and consider it as0
and sum the ignored items to theignoreditems
column - if ocardtype is Paypal or Sofort, then just do the
numitems
sum without checking theodate
because those types require noodate
- in another table called booked i have a column called ignoredoid,
this columns contains
orderids
from the above table that i want to ignore even if the 3 conditions above are satsfied
To this point the query is working perfectly thanks to @Richard aka cyberkiwi to his answer in this question
The Question is, i need the result datatable to look like the following
+-----------+----------+--------------+-------------------+
| catalogid | numitems | ignoreditems | orderidcollection |
+-----------+----------+--------------+-------------------+
| 353 | 4 | 0 | O737,O739 |
| 364 | 10 | 0 | O738,O740 |
| 882 | 2 | 0 | O741 |
| 224 | 0 | 7 | |'O742 & O743 are ignored
+-----------+----------+--------------+-------------------+
as you can see the only change is the addition of orderidcollection
column, it have to add the orderid
to the new column seperated by a comma only if that order is not ignored in the code, i've been googling for couple hours with no luck!
is this even possible with SQL?