我正在加入两个表格,我正在获取一些列和值
Select
tbl_orderdetails.category_name,
tbl_orderdetails.branch_name,
tbl_ordermaster.created_date,
tbl_ordermaster.user_id,
tbl_orderdetails.order_details_id,
tbl_orderdetails.branch_id
From tbl_orderdetails Inner Join tbl_ordermaster ON
tbl_orderdetails.order_master_id=tbl_ordermaster.ordermasterid
where tbl_ordermaster.user_id='12'
我希望获得特定分支名称的次数。我使用了计数,但它不起作用,我只希望分支名称的最大次数和前三名显示。 例如:
vellore=100,
chennai=18,
tvl=80,
harithuwar=90
它应该只显示
vellore
harithwar
tvl
示例数据 orderdetails
masterdetails
答案 0 :(得分:1)
试试这个
SELECT T.*,S.* FROM
(
Select TD.category_name,TD.branch_name,TM.created_date,TM.user_id,TD.order_details_id,TD.branch_id
From tbl_orderdetails TD Inner Join tbl_ordermaster TM ON
TD.order_master_id = TM.ordermasterid
Where TM.user_id='12'
) T Left Join
(
Select T1.branch_name,Count(T1.branch_name) As No_Of_Branch
From tbl_orderdetails T1 Inner Join tbl_ordermaster T2 ON
T1.order_master_id = T2.ordermasterid
Where T2.user_id='12'
Group By T1.branch_name
) S On S.branch_name = T.branch_name
<强>更新强>
如果您想Max
count
试试这个
SELECT T.*,S.* FROM
(
Select TD.category_name,TD.branch_name,TM.created_date,TM.user_id,TD.order_details_id,TD.branch_id
From tbl_orderdetails TD Inner Join tbl_ordermaster TM ON
TD.order_master_id = TM.ordermasterid
Where TM.user_id='12'
) T Inner Join
(
SELECT M.branch_name,Max(M.No_Of_Branch) FROM
(
Select T1.branch_name,Count(T1.branch_name) As No_Of_Branch
From tbl_orderdetails T1 Inner Join tbl_ordermaster T2 ON
T1.order_master_id = T2.ordermasterid
Where T2.user_id='12'
Group By T1.branch_name
) M Group By M.branch_name
) S On S.branch_name = T.branch_name
答案 1 :(得分:0)
尝试此计数在示例下工作正常,
create table orderdetails(orderdatailsid number, order_master_id number,branchname varchar(20), category varchar(20), branchid number);
insert into orderdetails values (1 , 112 ,'vellore','nad', 123);
insert into orderdetails values (2 , 112 ,'vellore','hu', 123);
insert into orderdetails values (3 , 113 , 'chennai','ji', 121);
insert into orderdetails values (4 , 112 , 'vellore' ,'hi', 123);
insert into orderdetails values (5 , 134 , 'tvl','ui', 145);
insert into orderdetails values (6 , 134 , 'tvl','jo', 145);
insert into orderdetails values (7 , 113 , 'chennai','ji', 121);
insert into orderdetails values (8 , 112 , 'vellore','hi', 123);
insert into orderdetails values (9 , 134 , 'tvl','ui', 145);
从orderdetails中选择*;
create table masterdetails(ordermasterid number, userid number);
insert into masterdetails values(112 , 12);
insert into masterdetails values(113 , 13);
insert into masterdetails values(134 ,14);
从masterdetails中选择*;
SELECT * FROM (
SELECT orderdetails.branchname, count(*) as Cout
From orderdetails left Join masterdetails ON
orderdetails.order_master_id=masterdetails.ordermasterid
group by orderdetails.branchname
order by cout desc
) WHERE ROWNUM <4;