2016-10-05 3 views
0

아래에 mysql 쿼리가 있습니다. 나는 어느 카운터를 사용 했는가? 카테고리 ID 1을 3 번 입력하면 카운터가 3이 올 것입니다.하지만이 경우에는 다른 coloumn을 입력하지 않으면 NO가 필요합니다. 출력이 KU 및 전기에 mysql 쿼리에 카운터가 표시됩니다.

 
     KU  Electrical 
Yes 6  2 
No 1  2 

내 판매 채널 이름입니다해야합니다. 예 (Yes)는 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 Transaction.category_id; 

아래 제가

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

SQL 구문 : http://sqlfiddle.com/#!9/3f497/1

답변

0

범주별로 그룹화합니다. 즉, 범주 당 하나의 결과 행을 얻습니다. 각 행에 개수와 판매 채널 이름이 표시됩니다. 이 판매 채널 이름은 임의로 선택한 카테고리의 레코드에서 발견 된 이름 중 하나 일뿐입니다.

카테고리 및 판매 채널을 집계하려고한다고 가정 해 보겠습니다. 따라서 절하여 그룹 group by SalesChannel.name, Transaction.category_id해야한다 :

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, Transaction.category_id; 

SQL 바이올린 :이 조합에 대한 거래 없기 때문에 http://sqlfiddle.com/#!9/3f497/2

이 결과는, 그러나, Electricals는/카테고리 2에 대한 항목이 표시되지 않습니다 탁자. 이 값을 0으로 표시하려면 먼저 거래가 있든 없든 전체 결과 집합 (예 : 채널 및 카테고리의 모든 조합)을 만들어야합니다.

select 
    sc.name, 
    c.id as category_id, 
    count(t.id) as "count" 
from sale_channels sc 
cross join categories c 
left join outlets o on o.sale_channel_id = sc.id 
left join transactions t on t.outlet_id = o.id and t.category_id = c.id 
group by sc.name, c.id; 

SQL 바이올린 : http://sqlfiddle.com/#!9/60e998/5

+0

당신이 여기 크로스를 추가하는 대신 내부 조인의 조인 왼쪽 Y 그런 다음 외부하려는 거래에 참여? 우리는 내부 조인을 사용하여 필요한 결과를 얻을 수 없습니까? \ – sukh

+0

아니요. SQL 쿼리는 해당 내용 만 표시 할 수 있습니다. Electricals/category 2의 조합에 대한 기록이 없으면 표시해야합니다. 이것이 교차 결합이하는 것입니다. 당신은 질문을 시도 했습니까? 결과의 차이점을 보시겠습니까? 결과에 만족하십니까? –

+0

그것 판매 채널 이름을 여러 번주는. 카테고리 ID가 6 회 추가 될 경우 KU가 6 회 있어야합니다. 여러 개의 카운터가있을 수 있습니다. – sukh