Query the total order amount of each person and associate multiple tables

Source: Internet
Author: User

A stored procedure was recently written by lone wolf. The approximate content is to query the total order amount of each person, using Table Association, and the execution speed is acceptable. If you have any good suggestions or questions, please let us know more.

There are two tables below

Userinfo table

Order table

If you want to query the total order amount of each person, the first method is to use a loop, but this method is not only complicated, but also slow to execute, which is not recommended by lone wolf. The second method is to associate the table mentioned in this article. The statement is as follows:

Select UI. ID, UI. Name, isnull (X. totalmoney, 0.0) totalmoney from DBO. userinfo UI
Left join
(Select sum (money) as totalmoney, userid from [order] group by userid) X on UI. ID = x. userid

The following result is displayed:

The lone wolf only knows these two methods, but there must be other methods. I will share them with you later. If I know something, I hope I can come up with them and learn from each other, mutual progress.

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.