Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

SQL-Serve query multi-conditional aggregation

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

1. Create form content and add data

-- create a table CREATE TABLE a (--set name field, attribute varchar NAME VARCHAR (255),-- set ERP field, attribute varchar ERP VARCHAR (255),-- set DATE field, property date DATE DATE,--, set name1 field, property varchar NAME1 VARCHAR (255),-- set amount field Attributes int amount int) INSERT a SELECT 'Zhang San', 'meal fee', '2019-12-11' Bank of China, '100'union all SELECT' Zhang San', 'Gift', '2019-12-01' Bank of China', '100'union all SELECT' Zhang San', 'Gift', '2019-11-12' Zhang San', '60'union all SELECT' Zhang San', 'Gift', '2019-12-12', 'American Bank', '60'union all SELECT' Zhang San' 100'union all SELECT, Li Si, meal fee, 2019-12-12, 50'union all SELECT, gifts, 2019-12-12, 2010-12, 2010-12, 2019-12-12, 2010-12-12, 2010-12-12, 2019-12-12, 2019-12-12, 2019-12-12, 2019-12-12, 2019-12-12, 2010-12, 2010-12, 2010-12, 2010-12-12, 2010-12-12, 2010-12-12, 2010-12, 2010-12, 2010-12, 2010-12, 2010-12, 2010-12, 2010-12, 2010-12, 2010-12, 2010-12, 2010-12, 2010-12, 2010-12, 2010-12, 2010-12, 2010-12-12, 2010-12-12, 2010-12-12, 2010-12-12, 2010-12, 2010-12, 2010-12, 2010-12

2. Query and feedback all content information

SELECT * from a

3. Aggregate and summarize according to multiple conditions

-- query select-- View the name field, and set the alias name name as' name',-- add a meal fee field, and set the alias fee type'as' expense type,-- View the date field, and set the alias month CONVERT (char (7), date,120) as' month',-- View the name1 field. And set alias customer name name1 as' customer name'--View Statistics amount field And name the amount SUM (a.amount) as' amount'--View from an or ERP=' from Table a-set query condition where-query condition 1, query name the return value of Zhang San name=' Zhang San'- and to join multiple conditions AND-- query condition, query ERP equals gift or meal fee (ERP=' gift'or ERP=' meal fee')-join condition AND-- condition 3 When the query time is greater than 2019-12-01date > = '2019-12-01 copyright condition 4, combined with the aggregate function, the result set is grouped according to one or more columns. The usage of group by a ~ (dint) b ~ C:-- first grouped according to a, if an is the same, then grouped according to b, and then grouped according to c. The final statistics are the information of the minimum grouping. Group by name,CONVERT (char (7), date,120), name1

4. Summary

First where condition, then group by condition, then order by condition

If the GROUP BY clause is declared, the output is divided into different groups that match one or more values. If the HAVING clause appears, it eliminates groups that do not meet the given condition.

If the ORDER BY clause is declared, the rows returned are sorted in the specified order. If no ORDER BY is given, the data rows are given in the way that the system thinks can be generated as quickly as possible.

Although the complete syntax of the SELECT statement is complex, its main clauses can be summarized as follows:

SELECT select_list

[INTO new_table]

FROM table_source

[WHERE search_condition]

[GROUP BY group_by_expression]

[HAVING search_condition]

[ORDER BY order_expression [ASC | DESC]]

You can use the UNION operator between queries to combine the results of a query into a single result set.

In other words, order by comes at the back.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report