SQL Server temporary table Application

Source: Internet
Author: User

1. Table P is as follows:

Question: Calculate the product name and the difference quantity in red and blue in the table.

(1) first, it is divided into two temporary tables.

Use tempdb

Go
If object_id ('# Table1', 'U') is not null // you can check whether a table exists.
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 = 'red' group by productnameselect productname, sum (Num) as num into # Table2 from P where color = 'blue' group by productname

(2) Find the expected values from the two tables.

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.