SQL server脚本语句积累

1:往现有的表中增加一个字段

IF NOT EXISTS ( SELECT 1

  FROM sys.sysobjects so WITH ( NOLOCK )
  INNER JOIN sys.syscolumns sc WITH ( NOLOCK ) ON so.id = sc.id
  WHERE so.name = 'EgPurchase_OrderPlan'
  AND sc.name = 'IsNewSystem' )
  BEGIN
    ALTER TABLE dbo.EgPurchase_OrderPlan ADD IsNewSystem INT NOT NULL DEFAULT 0
    EXECUTE sp_addextendedproperty N'MS_Description', '是否推送新系统', N'user',
    N'dbo', N'table', N'EgPurchase_OrderPlan', N'column', N'IsNewSystem'
  END
GO

2:更新一个字段

UPDATE dbo.EgSys_ExportSet
SET ColumnNames='订单编号|1,店铺名称|1,仓库名称|1,平台|1,商品|1,商品名称|1,商品编号|1,规格编码|1,颜色名称|1,规格名称|1,商品数量|1,吊牌价|1,单价|1,成本价|1,成交价|1,应付金额|1,实付金额|1,优惠|1,运费|1,重量|1,交易单号|1,实付物流费用|1,快递单号|1,物流公司|1,买家昵称|1,收件人|1,国家|1,省|1,市|1,区|1,地址|1,完整地址|1,邮编|1,买家留言|1,卖家留言|1,买家手机|1,买家电话|1,买家邮箱|1,订单状态|1,订单付款时间|1,订单完结时间|1,发货日期|1,换货状态|1,换货数量|1,付款状态|1,删除状态|1,副单状态|1,退款状态|1,退款金额|1,主订单号|1,城市代码|0,订单备注|0,分销商名|0,业务员|0,国家中文|0,国家简写|0,申报费用|0,商品类别|0,商品类别(英)|0,商品备注|0,客户条形码|1,Sku状态|0,商品条形码|1'
WHERE ModuleMark='AllOrder'

3:查询EDL转运单号

SELECT d.*
FROM dbo.EgSys_Delivery d WITH ( NOLOCK )
INNER JOIN dbo.Egsys_PlatformTrade t WITH ( NOLOCK ) ON t.tid = d.tid
WHERE d.deliveryTime < CONVERT(DATE, GETDATE())
AND d.deliveryTime >= CONVERT(DATE, DATEADD(DAY, -70000, GETDATE()))
AND t.orderState >= 4
AND t.order_deleteState != 1
AND d.logisticsCompany LIKE 'EdlExpress%'
AND d.logisticsNo LIKE 'HMZX%'

4:插入一条记录的严谨写法

/*
* =============================================================================
* Designer:
* Description: nsy_1901161114
* Created: 2019/01/16 11:12:47
* =============================================================================
*/

SELECT [disPlatformID] ,
[disPlatformName] ,
[disPlatformValue] ,
[disPlatformNote] ,
[disPlatformOrder] ,
[DefaultSpaceId] ,
[IsDisable] ,
[NotifyUrl]
INTO #EgSys_DistributionPlatform
FROM [dbo].[EgSys_DistributionPlatform]
-- where 1=0 只读取表结构,节约内存
WHERE 1 = 0
UNION ALL SELECT '42','Oberlo','Oberlo','Oberlo','42','0','0',''
INSERT INTO [dbo].[EgSys_DistributionPlatform]
( [disPlatformID] ,
[disPlatformName] ,
[disPlatformValue] ,
[disPlatformNote] ,
[disPlatformOrder] ,
[DefaultSpaceId] ,
[IsDisable] ,
[NotifyUrl]
)
SELECT d.disPlatformID, -- [int] NOT NULL,
d.disPlatformName , -- varchar(50) NULL
d.disPlatformValue , -- varchar(50) NULL
d.disPlatformNote , -- varchar(255) null
d.disPlatformOrder , -- [int] NULL
d.DefaultSpaceId , -- [int] NULL
d.IsDisable , -- [int] NULL,
d.NotifyUrl -- [varchar] (200) NULL
FROM #EgSys_DistributionPlatform d WITH ( NOLOCK )
LEFT JOIN dbo.EgSys_DistributionPlatform dp WITH ( NOLOCK ) ON dp.disPlatformID = d.disPlatformID
WHERE dp.disPlatformID IS NULL
DROP TABLE #EgSys_DistributionPlatform

5:统计用户流失趋势图

SELECT SUM(tt.NewUserNum) AS NewUserNum,tt.NotLogonDays
FROM
(
SELECT sum(t.NewUserNum) AS NewUserNum,CASE WHEN DATEDIFF(DAY,t.LastLogonDate,GetDate()) <7 THEN DATEDIFF(DAY,t.LastLogonDate,GetDate())
ELSE 7
END AS 'NotLogonDays'
FROM
(
SELECT COUNT(*) AS NewUserNum,CONVERT(varchar(10),LastLogonDate,120) AS LastLogonDate
FROM [View_UserInfo]
WHERE IsAndroid=0
AND convert(varchar(10),RegisterDate,120) BETWEEN '2010-3-20 ' AND '2019-3-28'
AND DATEDIFF(DAY,LastLogonDate,GetDate()) IN (3,4,5,6) OR DATEDIFF(DAY,LastLogonDate,GetDate()) >=7
GROUP BY convert(varchar(10),LastLogonDate,120)
) AS t
GROUP BY CONVERT(varchar(10),t.LastLogonDate,120)
) AS tt
GROUP BY tt.NotLogonDays
ORDER BY tt.NotLogonDays

 

posted @ 2019-01-11 21:56 万城归 阅读(...) 评论(...) 编辑 收藏