Home > database >  Trouble bosses to look at these two sentences can merge SQL queries
Trouble bosses to look at these two sentences can merge SQL queries

Time:09-27

 select uname, count (cid) from nowday group by uname order by count (cid) desc 

 select uname, count (cid) from nowday the where the status=2 group by uname order by count (cid) desc 


Which means to query the user name, operating capacity, invalid operation quantity (status=2)
If you can, can on the basis of the additional query an invalid operation rate? (invalid operation measure/operation) requires the result is percentage said, keep two decimal places

CodePudding user response:

The select s1 uname, s1 youxiao, s2. Wuxiao from (select uname, count (cid) youxiao from nowday group by uname) s1
Left the join
(select uname, count (cid) wuxiao from nowday the where the status=2 group by uname) s2
On s1. The uname=s2. The uname
If I write is

CodePudding user response:

Two statements from the filter, the returned result set to meet the conditions are not consistent, so I can't directly to merge, but can consider to secondary consolidation of the result set,

CodePudding user response:

Select the uname, count (cid) from nowday group by uname order by count (cid) desc
The union
Select the uname, count (cid) from nowday the where the status=2 group by uname order by count (cid) desc

CodePudding user response:

Can use case, the when, but more troublesome
Select the uname, count (cid1) as cid1, sum (cid2) as cid2 from
(select uname, 1 as cid1, case when status=2 then one else 0 end as cid2 from nowday) as t group by uname, order by cid1
But merge to ordered at the same time satisfy the two fields
  • Related