1. 有表P如下:

  sqlserver 临时表应用

问题:求表中红色多于蓝色的商品名称和差额数量

    (1)首先分成两个临时表

use tempdb

go
if object_id('#table1','U')is not null //判断表是不是存在
   drop table #table1
go
if object_id('#table2','U')is not null
    drop table #table2
go

use admin1

select productName, sum(num) as num into #table1 from P where color='红色' group by productName select productName, sum(num) as num into #table2 from P where color='蓝色' group by productName

    (2)从两个表中找出所要的值

select #table1.productName, #table1.num as numh ,#table2.num as numl ,(#table1.num-#table2.num) as margin from #table1 ,#table2 where #table1.num>#table2.num and #table1.productName=#table2.productName

相关文章:

  • 2021-08-04
  • 2021-05-19
  • 2021-08-09
  • 2021-12-10
  • 2022-01-27
  • 2022-12-23
  • 2022-12-23
  • 2021-12-05
猜你喜欢
  • 2021-12-05
  • 2021-08-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案