There are many commonly used SQL scripts at work

There are many commonly used SQL scripts at work. Today I will share them with you in several chapters.

1、行转列的用法PIVOT

CREATE table test
(id int,name nvarchar(20),quarter int,number int)
insert into test values(1,N'苹果',1,1000)
insert into test values(1,N'苹果',2,2000)
insert into test values(1,N'苹果',3,4000)
insert into test values(1,N'苹果',4,5000)
insert into test values(2,N'梨子',1,3000)
insert into test values(2,N'梨子',2,3500)
insert into test values(2,N'梨子',3,4200)
insert into test values(2,N'梨子',4,5500)
select * from test
结果:

picture

The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly.

select ID,NAME,
[1] as '一季度',
[2] as '二季度',
[3] as '三季度',
[4] as '四季度'
from
test
pivot
(
sum(number)
for quarter in
([1],[2],[3],[4])
)
as pvt

result:

picture

The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly.

2. How to use column to row UNPIOVT

create table test2
(id int,name varchar(20), Q1 int, Q2 int, Q3 int, Q4 int)
insert into test2 values(1,'苹果',1000,2000,4000,5000)
insert into test2 values(2,'梨子',3000,3500,4200,5500)
select * from test2
(提示:可以左右滑动代码)

结果:

picture

The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly.

--列转行
select id,name,quarter,number
from
test2
unpivot
(
number
for quarter in
([Q1],[Q2],[Q3],[Q4])
)
as unpvt

result:

The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly.

3. String replacement SUBSTRING/REPLACE

SELECT REPLACE('abcdefg',SUBSTRING('abcdefg',2,4),'**')
results:

picture

The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly.

SELECT REPLACE('13512345678',SUBSTRING('13512345678',4,11),'********')
results:

picture

The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly.

SELECT REPLACE('[email protected]','1234567','******')
results:

picture

The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly.

4. Query the same record HAVING in a table

If an ID can be distinguished, you can write it like this

SELECT * FROM HR.Employees
results:

picture

The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly.

select * from HR.Employees
where title in (
select title from HR.Employees
group by title
having count(1)>1)
结果:

picture

The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly.

After comparison, it was found that those with IDs 1 and 2 were filtered out because they only had one record.

If several IDs can be used to distinguish them, you can write them like this

select * from HR.Employees
where title+titleofcourtesy in
(select title+titleofcourtesy
from HR.Employees
group by title,titleofcourtesy
having count(1)>1)
结果:

picture

The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly.

After title is spliced ​​with titleofcourtesy, the only ones that meet the conditions are IDs 6, 7, 8, and 9.

5. Convert multi-row SQL data into one multi-column data, that is, add new columns

SELECT
id,
name,
SUM(CASE WHEN quarter=1 THEN number ELSE 0 END) 'First quarter',
SUM(CASE WHEN quarter=2 THEN number ELSE 0 END) 'Second quarter',
SUM(CASE WHEN quarter=3 THEN number ELSE 0 END) 'third quarter',
SUM(CASE WHEN quarter=4 THEN number ELSE 0 END) 'fourth quarter'
FROM test
GROUP BY id,name
results:

picture

The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly.

We increased the original 4 columns to 6 columns. Careful friends may have discovered how this result is exactly the same as the row-to-column conversion above? In fact, the above row-to-column conversion is omitted, which is a more common way of writing.

6. Table copy

语法1:Insert INTO table(field1,field2,…) values(value1,value2,…)

语法2:Insert into Table2(field1,field2,…) select value1,value2,… from Table1

(The target table Table2 is required to exist. Since the target table Table2 already exists, in addition to inserting fields of the source table Table1, we can also insert constants.)

Syntax 3: SELECT vale1, value2 into Table2 from Table1

(It is required that the target table Table2 does not exist, because table Table2 will be automatically created when inserting, and the specified field data in Table1 will be copied to Table2.)

Syntax 4: Use the import and export function to copy the entire table. If you use [Write a query to specify the data to be transferred], will there be a problem with the replication of the large data table? Because the copy stops moving after a certain level, is the memory full? It is also not written to the table. Direct execution using the above three syntaxes will immediately refresh the database table. Just refresh the mdf file and you will know.

7. Update data using the Update statement with associated subquery

–方法1:
Update Table1
set c = (select c from Table2 where a = Table1.a)
where c is null

–Chapter 2:
update A
set newqiantity=B.qiantity
from A,B
where A.bnum=B.bnum

–Chapter 3:
update
(select A.bnum ,A.newqiantity,B.qiantity from A
left join B on A.bnum=B.bnum) AS C
set C.newqiantity = C.qiantity
where C.bnum ='001'

8. Connect to the remote server

–方法1:
select * from openrowset(
‘SQLOLEDB’,
‘server=192.168.0.1;uid=sa;pwd=password’,
‘SELECT * FROM dbo.test’)

–Method 2:
select * from openrowset(
'SQLOLEDB',
'192.168.0.1';
'sa';
'password',
'SELECT * FROM dbo.test')
Of course, you can also refer to previous examples to establish DBLINK for remote connection

9. Date and Time style CONVERT

The CONVERT() function is a general function for converting dates to new data types.
The CONVERT() function can display date/time data in different formats.

grammar

CONVERT(data_type(length),data_to_be_converted,style)

data_type(length) specifies the target data type (with optional length). data_to_be_converted contains the values ​​that need to be converted. style specifies the date/time output format.

Available style values:

The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly.

The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly.
SELECT CONVERT(varchar(100), GETDATE(), 0)
– Result:
12 7 2020 9:33PM
SELECT CONVERT(varchar(100), GETDATE(), 1)
– Result:
12/07/20
SELECT CONVERT(varchar( 100), GETDATE(), 2)
– Result:
20.12.07
SELECT CONVERT(varchar(100), GETDATE(), 3)
– Result:
07/12/20
SELECT CONVERT(varchar(100), GETDATE(), 4 )
– Result:
07.12.20
SELECT CONVERT(varchar(100), GETDATE(), 5)
– Result:
07-12-20
SELECT CONVERT(varchar(100), GETDATE(), 6)
– Result:
07 12 20
SELECT CONVERT(varchar(100), GETDATE(), 7)
– Result:
12 07, 20
SELECT CONVERT(varchar(100), GETDATE(), 8)
– Result:
21:33:18
SELECT CONVERT(varchar(100), GETDATE(), 9)
– Result:
12 7 2020 9:33:18:780PM
SELECT CONVERT(varchar(100), GETDATE(), 10)
– Result:
12 -07-20
SELECT CONVERT(varchar(100), GETDATE(), 11)
– Result:
20/12/07
SELECT CONVERT(varchar(100), GETDATE(), 12)
– Result:
201207
SELECT CONVERT(varchar(100) ), GETDATE(), 13)
– Result:
07 12 2020 21:33:18:780
SELECT CONVERT(varchar(100), GETDATE(), 14)
– Result:
21:33:18:780
SELECT CONVERT(varchar( 100), GETDATE(), 20)
– Result:
2020-12-07 21:33:18
SELECT CONVERT(varchar(100), GETDATE(), 21)
– Result:
2020-12-07 21:33:18.780
SELECT CONVERT(varchar(100), GETDATE(), 22)
– Result:
12/07/20 9:33:18 PM
SELECT CONVERT(varchar(100), GETDATE(), 23)
– Result:
2020-12-07
SELECT CONVERT(varchar(100), GETDATE(), 24)
– Result:
21:33:18
SELECT CONVERT(varchar(100), GETDATE(), 25)
– Result:
2020-12-07 21:33:18.780
SELECT CONVERT(varchar(100), GETDATE(), 100)
– Result:
12 7 2020 9:33PM
SELECT CONVERT(varchar(100), GETDATE(), 101)
– Result:
12/07/2020
SELECT CONVERT(varchar(100 ), GETDATE(), 102)
– Result:
2020.12.07
SELECT CONVERT(varchar(100), GETDATE(), 103)
– Result:
07/12/2020
SELECT CONVERT(varchar(100), GETDATE(), 104)
– Result:
07.12.2020
SELECT CONVERT(varchar(100), GETDATE(), 105)
– Result:
07-12-2020
SELECT CONVERT(varchar(100), GETDATE(), 106)
– Result:
07 12 2020
SELECT CONVERT(varchar(100), GETDATE(), 107)
– Result:
12 07, 2020
SELECT CONVERT(varchar(100), GETDATE(), 108)
– Result:
21:33:18
SELECT CONVERT(varchar(100), GETDATE(), 109)
– Result:
12 7 2020 9:33:18:780PM
SELECT CONVERT(varchar(100), GETDATE(), 110)
– Result:
12 -07-2020
SELECT CONVERT(varchar(100), GETDATE(), 111)
– Result:
2020/12/07
SELECT CONVERT(varchar(100), GETDATE(), 112)
– Result:
20201207
SELECT CONVERT(varchar(100), GETDATE(), 113)
– Result:
07 12 2020 21:33:18:780
SELECT CONVERT(varchar( 100), GETDATE(), 114)
– Result:
21:33:18:780
SELECT CONVERT(varchar(100), GETDATE(), 120)
– Result:
2020-12-07 21:33:18
SELECT CONVERT(varchar (100), GETDATE(), 121)
– Result:
2020-12-07 21:33:18.780

7
SELECT CONVERT(varchar(100), GETDATE(), 112)
– Result:
20201207
SELECT CONVERT(varchar(100), GETDATE(), 113)
– Result:
07 12 2020 21:33:18:780
SELECT CONVERT(varchar (100), GETDATE(), 114)
– Result:
21:33:18:780
SELECT CONVERT(varchar(100), GETDATE(), 120)
– Result:
2020-12-07 21:33:18
SELECT CONVERT( varchar(100), GETDATE(), 121)
– Result:
2020-12-07 21:33:18.780

The above content is commonly used in work and is best remembered. If you can't remember it, just save it and look it up when needed.

Guess you like

Origin blog.csdn.net/mmmmm44444/article/details/132852100