2016-10-06 74 views
-1

我有下面的查询。在这个我有,没有的情况下。 是访问,但其他部分无法正常工作。请看看这个。其他未在mysql中运行查询

SELECT SalesChannel.name , count(Transaction.category_id) as count, (case when (Transaction.no_of_units > 0 and Transaction.mop > 0) THEN 'yes' ELSE 'No' END) AS Is_Present 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 

输出应为如下

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 

三个表,其我用 1.交易

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.输出

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'); 

3. sale_chennals

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

您汇总的数据,从而得到每SalesChannel.name一行。可能有一些交易记录导致'是',另一些交易记录是'否'的SalesChannel.name,那么Is_Present应该是什么?

您的查询的另一个问题是销售渠道是在一个表中。目前有两种,但有时候可能会有三四四种。 SQL查询不会产生列数可变的结果。列必须事先知道。因此,一个可能的结果可能是这样的:

 
Name   Yes No 
KU   6  1 
Electrical 2  2 

,因为你知道你希望它是Yes或No只,无论有多少频道。

查询:

select 
    sc.name, 
    count(case when t.no_of_units > 0 and t.mop > 0 then 1 end) as yes, 
    count(case when t.no_of_units <= 0 or t.mop <= 0 then 1 end) as no 
from sale_channels sc 
join outlet o on o.sale_channel_id = sc.id 
join transactions t on t.outlet_id = o.id; 
+0

实际上应该按Transaction.category_id进行分组。但是,当我使用category_id分组,然后只有一行即将到来,请帮助我。我想要的输出,因为我问题部分。 salechannels是多重和动态的。 – sukh

+0

您要求的结果显示频道名称网格和是/否。这个类别与这个有什么关系? –

+0

好的计数(Transaction.category_id)作为计数显示我有多少次我已经进入特定类别。 yes会显示Transaction.no_of_units> 0和Transaction.mop> 0,如果它的<0则没有。 – sukh

1

中有匹配的其他条件表的数据。您的条件是两个字段的表值中不匹配的“Transaction.no_of_units> 0 AND Transaction.mop> 0”大于0.

否则,其他条件正常工作。