简体   繁体   中英

T-SQL select process variable

In SQL Server 2008 I have a Table- Fruits

Items   Orders
Bananas 6
Bananas 2
Bananas 1
Mangos  4
Mangos  3
Apples  7
Apples  1
Apples  3
Apples  3

Using variables, how I can get below output. I am requesting variables because I would like perform several mathematical operations not described in this example.

Items   Number of Orders    Total Order Quantity    Average Order Quantity
Bananas 3   9   3
Mangos  2   7   3.5
Apples  4   14  3.5

'Total Order Quantity' shows sum of all orders for given item 'Average Order Quantity' = 'Total Order Quantity'/'Number of Orders'

Many thanks.

Create table Fruits (Items varchar(10), Orders int)

insert into Fruits values ('Bananas',6)
insert into Fruits values ('Bananas',2)
insert into Fruits values ('Bananas',1)
insert into Fruits values ('Mangos',4)
insert into Fruits values ('Mangos',3)
insert into Fruits values ('Apples',7)
insert into Fruits values ('Apples',1)
insert into Fruits values ('Apples',3)
insert into Fruits values ('Apples',3)
select Items, count(Orders) as NumberOfOrders, sum(Orders) as TotalOrderQuantity, avg(Orders + 0.0) as AverageOrderQuantity
from Fruits
group by Items
declare csrCursor cursor for
    select Items, count(Orders) as NumberOfOrders, sum(Orders) as TotalOrderQuantity, avg(Orders + 0.0) as AverageOrderQuantity
    from Fruits
    group by Items
    order by 1

declare @Item varchar(10)
declare @OrderCount int
declare @QuantityTotal int
declare @AvgQuantity numeric(9, 2)

open csrCursor

fetch next from csrCursor into @Item, @OrderCount, @QuantityTotal, @AvgQuantity
while (@@fetch_status = 0)
    -- Do stuff with variables @Item, @OrderCount, @QuantityTotal, @AvgQuantity
    -- Insert results in Temp Table

    fetch next from csrCursor into @Item, @OrderCount, @QuantityTotal, @AvgQuantity
end

close csrCursor
deallocate csrCursor

Yes, really should steer clear of cursors when possible. Something like this approach would probably be best, storing the results of the query in a temporary table, and then running update statements to get your calculations:

 declare @Table table
 (
  @Item varchar(10)
  @OrderCount int
  @QuantityTotal int
  @AvgQuantity numeric(9, 2)
  @Calc1 numeric(9, 2)
  @Calc2 numeric(9, 2)
 )

 insert into @Table (@Item, @OrderCount, @QuantityTotal, @AvgQuantity)
 select Items, count(Orders) as NumberOfOrders, sum(Orders) as TotalOrderQuantity, avg(Orders + 0.0) as AverageOrderQuantity
 from Fruits
 group by Items
 order by 1

 update @Table set @Calc1 = @OrderCount / @AvgQuantity,
 @Calc2 = ...

 select * from @Table

or if you can get all your calculations in a single row or joins with another table, you can do it in a single statement, like:

select *, (OrderCount / AvgQuantity) as Calc1, (... as Calc2)
from
(
 select Items, count(Orders) as OrderCount, sum(Orders) as TotalQuantity, avg(Orders + 0.0) as AvgQuantity
 from Fruits
 group by Items
) t

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM