开发者

calculate the values from within two queries

is there a way in SQL server that can calculate the results from two seperate query? ex:

Select sum(product) as Ite开发者_StackOverflowms1
From product1

Items1
8,958

Select sum(product) as Items2
From product2

Items2
40,465

Now, I would like to get the the % of Items1 divided by Items2 (8,958/40,465) which gives me 22%. I would like to ask if this can be done using query in SQL server. Thanks a lot!


One way would be to use variables

DECLARE @Sum1 FLOAT,
        @Sum2 FLOAT

Select @Sum1 = sum(product) as Items1 
From product1 

Select @Sum2 = sum(product) as Items2 
From product2 

SELECT  @Sum1 / @Sum2

Another would be something like

SELECT  Items1 / Items2
FROM    (
            Select sum(product) as Items1 From product1 
        ) a,
        (
            Select sum(product) as Items2 From product2
        ) b


select items1 / items2
  from (select sum(product) as items1 from product1)
      ,(select sum(product) as items2 from product2)

or

select (select sum(product) as items1 from product1) / 
       (select sum(product) as items2 from product2)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜