Memoryizz

day 02 主键

 

--day02主键--
drop table sdata.dbo.testday02
create table sdata.dbo.testday02(
bw_id int primary key,                       -- 主键的关键字 即表里面的数据不能完全一致--
bw_no varchar (50) not null,
city varchar (50) not null,                       --不能空--
area_squar int
)

insert into sdata.dbo.testday02
values(
'11'
,'王'
,'yi8'
,'100'
--,GETDATE() --          -- 自动输入
--,GETDATE()--
,'2018-07-07'                --手动输入--
,'2018-07-07')
select *from sdata.dbo.testday02
select * ,YEAR(GETDATE())from sdata.dbo.testday02           --,YEAR(GETDATE())取年份--
select len(city),city from sdata.dbo.testday02                         --求字段里面的字段名称的长度--
select SUBSTRING(city,1,3),city from sdata.dbo.testday02   --从city表里面的第一位截取到第四位显示--
select DATENAME(WEEKDAY,GETDATE())                           --返回周几--
select DATEpart(WEEKDAY,GETDATE())                               --返回的是第几天--

--为表里面增加字段--
alter table sdata.dbo.testday02 add update_date date;
alter table sdata.dbo.testday02 add update_date2 datetime;   --具体的时间--

 

select distinct city , update_date from sdata.dbo.testday02      --去重复操作--

--count计数各地市哪天买的数--

select distinct city
,update_date, count(bw_id) from sdata.dbo.testday02
group by
city,update_date                                                                            --根 据地市分组 count sum max min AVG(bw_id)*1.0都要用group by--


--top3排序--
select top 3 *from sdata.dbo.testday02
where update_date is not null
order by update_date asc

--sql语句套路模板--
select
conlum1           --列名称
,conlum2
,ABS(conlum)   --取绝对值--
,AVG(conlum)
,SUM(conlum)
,MIN(conlum)
,MAX(conlum)
from
sdata.dbo.testday02   --表名称--
where conlum is null   --条件--
group by
conlum1
,conlum2
order by
conlum desc

posted on 2018-08-07 21:23  Memoryizz  阅读(136)  评论(0)    收藏  举报

导航