Question : Merging two SQL Statements

Hello, I am trying to merge two SQL statements that I have to create a single output.  

I currently have 2 sqls (attached).

SQL 1 - Displays how many records in the pictures table have total votes (wins+losses) between two values.

SQL 2 - Displays how many records in the pictures table have a given winning percentage (wins/(wins+losses).  

What I want - I want a single SQL statement that merges the two.  For example, for records with between 0 and 500 votes, the columns should be how many records between 0 and 500 votes, how many records with a win % (wins/(wins+losses))between 0 and 50 and how many with a records win % between 51 and 100.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
--- SQL 1
 
Select b.min as voteslow, b.max as voteshigh, count(totalvotes) as totalinrange from 
(Select wins+losses as
totalvotes from pictures where status='A') a,
(
select 0 as min, 500 as max union
select 501 as min, 1000 as max union
select 1001 as min, 100000 as max union
select 0 as min, 100000 as max) b
where totalvotes >= b.min and totalvotes <=b.max
group by b.min, b.max
order by b.max, b.min desc
 
--- SQL 2
 
SELECT sum(total) as total, sum(zero_fifty) as p0_50, sum(fifty_onehund) as p50_100
FROM 
(SELECT  
SUM (CASE WHEN winpct >=0 and winpct <= 100 THEN 1 ELSE 0 END) as total, 
SUM (CASE WHEN winpct >=0 and winpct <= 50 THEN 1 ELSE 0 END) as zero_fifty, 
SUM (CASE WHEN winpct >=50 and winpct <= 100 THEN 1 ELSE 0 END) as fifty_onehund
FROM 
(select wins, losses, wins+losses as total, (CONVERT(decimal(10, 5), wins) / (wins + losses)) * 100 as winpct
from pictures
where status='A' and wins+losses > 0 and wins+losses <= 100000) as winpcttable) 
as raw_results

Answer : Merging two SQL Statements

Sorry, not a MS-SQL guru. I just wanted to show you another approach, but I won't be able to debug. From the look of your first query, it seemed you were up to the task.

I made some minor changes, don't know if that will help (I did forget one 'union' for instance).

Cheers!
(°v°)
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
SELECT
  b.min as voteslow,
  b.max as voteshigh,
  count(*) as total,
  sum(case a.wins > a.losses then 1 else 0 end) as totwins,
  sum(case a.losses > a.wins then 1 else 0 end) as totlosses
 
FROM
  ( SELECT wins, losses, wins+losses as tot
    FROM pictures
    WHERE status='A'
  ) a,
  ( Select 0 as min, 100000 as max union
    Select 1, 500 union
    Select 501, 1000 union
    Select 1001, 100000 union
  ) b
 
WHERE tot >= b.min and totalvotes <=b.max
GROUP BY b.min, b.max
ORDER BY b.max, b.min desc
Random Solutions  
 
programming4us programming4us