I have 2 conditions on my SQL query that I need to fulfill.
First: I have to count the number of times a person sold a product and then group them by name. I have the below SQL code for this.
select distinct
a.name,
sum(case when m.code = 'A01' then 1 else 0 end) Insulin,
sum(case when m.code = 'S01' then 1 else 0 end) Syringe
from agent a
join medicine m
group by c.name
This results in the below table which is fine.
<table style="text-align:center">
<th>name</th>
<th>insulin</th>
<th>syringe</th>
<tr>
<td>Sarah</td>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>Mike</td>
<td>2</td>
<td>3</td>
</tr>
</table>
The next condition is where I am stuck.
Second: I need to put in the transaction id's on each product sold. So if Mike sold 2 Insulins, I need to put in the transactions ID's of these all in one cell. So something like below
<table style="text-align:center">
<th>name</th>
<th>insulin</th>
<th>syringe</th>
<th>insulin-transactionID's</th>
<th>syringe-transactionID's</th>
<tr>
<td>Sarah</td>
<td>1</td>
<td>2</td>
<td>2017001</td>
<td>2017002,2017006</td>
</tr>
<tr>
<td>Mike</td>
<td>2</td>
<td>3</td>
<td>2017881,2017899</td>
<td>2017456,2017567,2017898</td>
</tr>
</table>
E.g. I have 3 tables. Agent, Medicine, Transactions. I am open to other solutions. Let's just say that the Medicine table has a transaction field on it