I have tables customers,campaigns,deals and facts2 .I wrote a query like below to display like
SELECT id,
(select customer.name from customer where customer.id = facts2.customer_id)
AS Customername,
(select campaign.name from campaign where campaign.id = facts2.campaign_id)
AS Campaignname,
(select deal.name from jb_deal where deal.id = facts2.deal_id)
AS Dealname,
revenue
from facts2;
+--------------+-------------+-------------------+----------+ | Customername | Campaignanme | Dealname | revenue | +-------------+-------------+-------------------------------+ | A | Camp1 | Deal1 | 100 | | A | Camp1 | Deal2 | 200 | | A | Camp2 | Deal3 | 300 | | B | CampB | DealB1 | 100 | | B | CampB | DealB2 | 200 | | C | CampC | Deal3 | 300 | +-------------+-------------+--------------------------------+
I want to display sql table开发者_运维问答 without repeating customer names and campaign names as shown below.
+--------------+-------------+-------------------+----------+ | customername | campaignanme | dealname | Revenue | +-------------+-------------+-------------------------------+ | A | Camp1 | Deal1 | 100 | | | | Deal2 | 200 | | | Camp2 | Deal3 | 300 | | B | CampB | DealB1 | 100 | | | | DealB2 | 200 | | C | CampC | Deal3 | 300 | +-------------+-------------+--------------------------------+
You Wont Be able to make it like this still you can use UNION to Take distinct entries. If you want you can get like this
SELECT `result`.`Customername`,`result`.`Campaignname`,
GROUP_CONCAT(`result`.`Dealname`) AS `Dealname`,SUM(`revenue`) AS `revenue` FROM (SELECT id,
(select customer.name from customer where customer.id = facts2.customer_id)
AS Customername,
(select campaign.name from campaign where campaign.id = facts2.campaign_id)
AS Campaignname,
(select deal.name from jb_deal where deal.id = facts2.deal_id)
AS Dealname,
revenue
from facts2) AS `result` GROUP BY `result`.`Campaignname`,`result`.`Customername`;
the result will be some what like this
+--------------+-------------+-------------------+----------+
| Customername | Campaignanme | Dealname | revenue |
+-------------+-------------+-------------------------------+
| A | Camp1 | Deal1,Deal2 | 300 |
| A | Camp2 | Deal3 | 300 |
| B | CampB | DealB1,DealB2 | 300 |
| C | CampC | Deal3 | 300 |
+-------------+-------------+--------------------------------+
精彩评论