我有下面的查询如果我的计数器即将到来,那么应该显示是,如果没有值,那么将会有。请查看查询。 输出应该如下
KU Electrical
Yes 6 2
No 1 2
6是KU的计数器,Yes表示存在,类似No不存在KU
select SalesChannel.name ,
Transaction.category_id,
count(Transaction.category_id) as count,
from outlets Outlet inner join transactions Transaction on Outlet.id = Transaction.outlet_id inner join sale_channels SalesChannel on SalesChannel.id = Outlet.sale_channel_id group by SalesChannel.name
下面是我使用的三个表
CREATE TABLE IF NOT EXISTS `transactions` (
`id` int(11) NOT NULL,
`zone_id` int(11) NOT NULL,
`state_id` int(11) NOT NULL,
`city_id` int(11) NOT NULL,
`category_id` int(11) NOT NULL,
`sub_category_id` int(11) NOT NULL,
`brand_id` int(11) NOT NULL,
`model_id` int(11) NOT NULL,
`outlet_id` int(11) NOT NULL,
`no_of_units` int(11) NOT NULL,
`mop` decimal(10,2) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `transactions`
--
INSERT INTO `transactions` (`id`, `zone_id`, `state_id`, `city_id`, `category_id`, `sub_category_id`, `brand_id`, `model_id`, `outlet_id`, `no_of_units`, `mop`) VALUES
(1, 2, 2, 2, 2, 1, 1, 1, 1, 3, '6.00'),
(2, 2, 2, 2, 2, 1, 1, 1, 1, 3, '6.00'),
(3, 1, 1, 1, 1, 1, 1, 1, 1, 4, '2.00'),
(4, 2, 2, 2, 1, 1, 1, 1, 2, 4, '2.00');
2.outlets
CREATE TABLE IF NOT EXISTS `outlets` (
`id` int(11) NOT NULL,
`outlet_code` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
`zone_id` int(11) NOT NULL,
`state_id` int(11) NOT NULL,
`city_id` int(11) NOT NULL,
`sale_channel_id` int(11) NOT NULL,
`is_active` tinyint(1) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `outlets`
--
INSERT INTO `outlets` (`id`, `outlet_code`, `name`, `zone_id`, `state_id`, `city_id`, `sale_channel_id`, `is_active`, `created`, `modified`) VALUES
(1, '1508', 'Ashok electricals', 2, 2, 2, 1, 1, '2016-10-03 00:00:00', '2016-10-03 00:00:00'),
(2, '1233', 'vinayak electricals', 1, 1, 1, 2, 1, '2016-10-04 00:00:00', '2016-10-04 00:00:00');
CREATE TABLE IF NOT EXISTS `sale_channels` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`is_active` tinyint(1) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `sale_channels`
--
INSERT INTO `sale_channels` (`id`, `name`, `is_active`, `created`, `modified`) VALUES
(1, 'KU', 1, '2016-10-03 00:00:00', '2016-10-03 00:00:00'),
(2, 'Electricals', 1, '2016-10-04 00:00:00', '2016-10-04 00:00:00');
答案 0 :(得分:0)
你可以使用' case'在查询中
选择SalesChannel.name,
Transaction.category_id,
将count(Transaction.category_id)计为count,计数时的情况(Transaction.category_id)> 0然后'是'否则'否'作为KU
来自网点Outlet内部联接交易Outlet.id = Transaction.outlet_id内部联盟sales_channels SalesChannel,SalesChannel.id = Outlet.sale_channel_id group by SalesChannel.name
答案 1 :(得分:0)
我在outlet表中添加了一行,以便以下查询无法找到事务。 (3,' 3333',' abc electricals',1,1,1,2,1,' 2016-10-04 00:00:00', ' 2016-10-04 00:00:00');
select t.srce
,sum(case when s.name = 'KU' then 1 else 0 end) KU
,sum(case when s.name = 'Electricals' then 1 else 0 end) Electricals
from
(
select 'yes' srce, o.id, o.sale_channel_id, t.category_id
from outlets o
join transactions T on O.id = T.outlet_id
union all
select 'no' srce, o.id, o.sale_channel_id, t.category_id
from outlets o
left outer join transactions T on O.id = T.outlet_id
where t.outlet_id is null
) t
JOIN sale_channels s on t.sale_channel_id = s.id
group by t.srce
order by case
when t.srce = 'yes' then 1
else 2
end;
内部子查询使用连接和出口获取具有事务的出口,而不使用外部join / null方法进行事务。外部查询然后使用条件聚合来生成输出
结果
+------+------+-------------+
| srce | KU | Electricals |
+------+------+-------------+
| yes | 3 | 1 |
| no | 0 | 1 |
+------+------+-------------+
如果您拥有未知数量的sales_channels,则可能需要使用动态SQL。
set @sumstr =
(select str from
(
select @rn:=@rn + 1 rn,@str:=concat(@str,'sum(case when s.name=',char(39),s.name,char(39),' then 1 else 0 end) as ', s.name, ',' ) str
from (select @rn:=0,@str:='') str,sale_channels s
) s
order by rn desc limit 1
);
select concat(
'Select t.srce, ',substr(@sumstr,1,length(@sumstr) -1) ,
' from
(
select ',
concat(char(39),char(121),char(101),char(115),char(39)) ,
' srce, o.id, o.sale_channel_id, t.category_id
from outlets o
join transactions T on O.id = T.outlet_id
union all
select ' ,
concat(char(39),char(110),char(111),char(39)) ,
' srce, o.id, o.sale_channel_id, t.category_id
from outlets o
left outer join transactions T on O.id = T.outlet_id
where t.outlet_id is null
) t
JOIN sale_channels s on t.sale_channel_id = s.id
group by t.srce
order by case
when t.srce = ' ,
concat(char(39),char(121),char(101),char(115),char(39)) ,' then 1
else 2
end;
'
)
;