|
本帖最后由 平平淡淡的天使 于 2013-9-24 10:49 编辑
little-key 发表于 2013-9-24 10:40
in在Sql Server 中是可以使用的
- Sub jinzhengduishou()
- t = Timer
- On Error Resume Next
- Application.ScreenUpdating = False
- shenghui = "SELECT 'ZZ'as 发货公司,* FROM XXX WHERE 发货公司中文 LIKE '浙CDCDCDC有限公司%'"
- jinyuan = "SELECT 'XX'as 发货公司,* FROM XXX where 发货公司中文 like '宁CDCDCD易有限公%'"
- yangguang = "SELECT 'DD'as 发货公司,* FROM XXX WHERE 发货公司中文 IN('厦CDCDCDC明有限公司','浙CDCDCD股份有限公司')"
- lidaxin = "SELECT 'GFD'as 发货公司,* FROM XXX WHERE 发货公司中文 IN('CDCDCD明有限公司','立CDCDCD有限公司','厦CDCD限公司') "
- aodeliang = "SELECT 'THG'as 发货公司,* FROM XXX WHERE 发货公司中文 LIKE '%CDCDCD%'"
- tongshida = "SELECT 'OIU'as 发货公司,* FROM XXX WHERE 发货公司中文 LIKE '厦门CDCDC'"
- hengdiandebang = "SELECT 'MNB'as 发货公司,* FROM XXX WHERE 发货公司中文 LIKE '浙CDCDCD口有限公司'"
- ningbooushang = "SELECT 'NVDVD'as 发货公司,* FROM XXX WHERE 发货公司中文 in('宁CDCDCD公司','宁CDCDCDCD工工贸有限公司')"
- zhejiangmeike = "SELECT 'FDFD'as 发货公司,* FROM XXXX WHERE 发货公司中文 IN('上CDCDCD有限公司','CDCDCD器有限公司')"
- hangzhouzhuangcheng = "SELECT 'OIU'as 发货公司,* FROM XXXX WHERE 发货公司中文 LIKE '%杭CDCDC出口有限公司%'"
- zm = "SELECT 'FDFDF'as 发货公司,* FROM XXX WHERE 发货公司中文 IN('深圳CDCDCD有限公司','启CDCDCD技有限公司')"
- ln = "SELECT 'FDFDFD'as 发货公司,* FROM XXXX WHERE 发货公司中文 LIKE '%深CDCD照明%'"
- yg = "SELECT 'FDCDC'as 发货公司,* FROM XXXX WHERE 发货公司中文 LIKE '%深CDCDCD电科技%'"
- xingji = "SELECT 'ADDAA'as 发货公司,* FROM XXXX WHERE 发货公司中文 LIKE '%厦CDCD电器%'"
- qiangl = "SELECT 'VDVSD'as 发货公司,* FROM XXXX WHERE 发货公司中文 LIKE '%上CDCDC电子%'"
- ljin = "SELECT 'VSCSDCDS'as 发货公司,* FROM XXX WHERE 发货公司中文 LIKE '%东CDCD电子%'"
- donglin = "SELECT 'XAXASX'as 发货公司,* FROM XXXX WHERE 发货公司中文 LIKE '%厦CSCSC电子%'"
- zhongr = "SELECT 'DEWDW'as 发货公司,* FROM XXXX WHERE 发货公司中文 LIKE '%南CSCSCS器%'"
- zhoumin = "SELECT 'CSDCS'as 发货公司,* FROM XXXX WHERE 发货公司中文 LIKE '%深CSCSCSC份%'"
- juzuo = "SELECT 'CDSCSD'as 发货公司,* FROM XXXX WHERE 发货公司中文 IN('深CSCSCS份有限公司','深CSCSCSCS业有限公司')"
- hailan = "SELECT 'VDVDFV'as 发货公司,* FROM XXXX where 发货公司中文 in('厦CSCSCSCS公司','厦CSCSCSCS限公司')"
- guoxing = "SELECT 'CSDCSD'as 发货公司,* FROM XXXXXX where 发货公司中文='佛CSACSSCS限公司'"
- Set cnn = New ADODB.Connection
- Set rst = New ADODB.Recordset
- rst.CursorLocation = adUseClient
- Range("a5:AT66536").Cells.ClearContents
- ActiveSheet.PivotTables("数据透视表1").TableRange2.Delete
- If TextBox1.Text = "" Or TextBox2.Text = "" Then
- MsgBox "请输入日期", 1 + 16, "系统提示"
- Else
- strcn = "Provider=SQLOLEDB; User ID=XX;Password =XXX.X.XX;Data Source=XXXXXX.XXXX.XXX,1433;Initial Catalog=XXX" 'SQL SERVER版本
- cnn.Open strcn
- shuliang = "select sum(数量) from (" & shenghui & " union all " & jinyuan & " union all " & yangguang & " union all " & lidaxin & " union all " & aodeliang & " union all " & tongshida & " union all " & hengdiandebang & " union all " & ningbooushang & " union all " & zhejiangmeike & " union all " & hangzhouzhuangcheng & " union all " & zm & " union all " & ln & " union all " & yg & " union all " & xingji & " union all " & qiangl & " union all " & ljin & " union all " & donglin & " union all " & zhongr & " union all " & zhoumin & " union all " & juzuo & " union all " & hailan & " union all " & guoxing & ")a WHERE 日期 BETWEEN '" & TextBox1.Value & "' and '" & TextBox2.Value & "'"
- jine = "select sum(美金金额) from (" & shenghui & " union all " & jinyuan & " union all " & yangguang & " union all " & lidaxin & " union all " & aodeliang & " union all " & tongshida & " union all " & hengdiandebang & " union all " & ningbooushang & " union all " & zhejiangmeike & " union all " & hangzhouzhuangcheng & " union all " & zm & " union all " & ln & " union all " & yg & " union all " & xingji & " union all " & qiangl & " union all " & ljin & " union all " & donglin & " union all " & zhongr & " union all " & zhoumin & " union all " & juzuo & " union all " & hailan & " union all " & guoxing & ")a WHERE 日期 BETWEEN '" & TextBox1.Value & "' and '" & TextBox2.Value & "'"
- sql = "select 发货公司,sum(美金金额) as 总金额,sum(数量) as 总数量 from (" & shenghui & " union all " & jinyuan & " union all " & yangguang & " union all " & lidaxin & " union all " & aodeliang & " union all " & tongshida & " union all " & hengdiandebang & " union all " & ningbooushang & " union all " & zhejiangmeike & " union all " & hangzhouzhuangcheng & " union all " & zm & " union all " & ln & " union all " & yg & " union all " & xingji & " union all " & qiangl & " union all " & ljin & " union all " & donglin & " union all " & zhongr & " union all " & zhoumin & " union all " & juzuo & " union all " & hailan & " union all " & guoxing & ")a WHERE 日期 between '" & TextBox1.Value & "' and '" & TextBox2.Value & "' group by 发货公司 ORDER BY sum(美金金额) desc "
- sql1 = "select 发货公司,收货公司英文,sum(美金金额) as 总金额,sum(数量) as 总数量 from (" & shenghui & " union all " & jinyuan & " union all " & yangguang & " union all " & lidaxin & " union all " & aodeliang & " union all " & tongshida & " union all " & hengdiandebang & " union all " & ningbooushang & " union all " & zhejiangmeike & " union all " & hangzhouzhuangcheng & " union all " & zm & " union all " & ln & " union all " & yg & " union all " & xingji & " union all " & qiangl & " union all " & ljin & " union all " & donglin & " union all " & zhongr & " union all " & zhoumin & " union all " & juzuo & " union all " & hailan & " union all " & guoxing & ")a WHERE 日期 between '" & TextBox1.Value & "' and '" & TextBox2.Value & "' group by 发货公司,收货公司英文 ORDER BY sum(美金金额) desc "
- sql2 = "select 原产目的国,month(日期) as 月份,sum(美金金额) AS 总金额 from (" & shenghui & " union all " & jinyuan & " union all " & yangguang & " union all " & lidaxin & " union all " & aodeliang & " union all " & tongshida & " union all " & hengdiandebang & " union all " & ningbooushang & " union all " & zhejiangmeike & " union all " & hangzhouzhuangcheng & " union all " & zm & " union all " & ln & " union all " & yg & " union all " & xingji & " union all " & qiangl & " union all " & ljin & " union all " & donglin & " union all " & zhongr & " union all " & zhoumin & " union all " & juzuo & " union all " & hailan & " union all " & guoxing & ")a WHERE 日期 between '" & TextBox1.Value & "' and '" & TextBox2.Value & "' group by 原产目的国,month(日期)"
- End If
- Set rst = cnn.Execute(sql)
- Cells(5, 1).CopyFromRecordset rst
- Set rst = cnn.Execute(sql1)
- Cells(5, 10).CopyFromRecordset rst
- Set objPivotCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlExternal)
- Set objPivotCache.Recordset = cnn.Execute(sql2)
- With objPivotCache
- .CreatePivotTable TableDestination:=Range("R3"), TableName:="数据透视表1"
- End With
- With ActiveSheet.PivotTables("数据透视表1")
- .PivotFields("原产目的国").Orientation = xlRowField
- .PivotFields("月份").Orientation = xlColumnField
- .AddDataField ActiveSheet.PivotTables("数据透视表1").PivotFields("总金额"), "求和项:总金额", xlSum
- With .PivotFields("求和项:总金额")
- .NumberFormat = "0.00"
- End With
- End With
- ' arr5 = cnn.Execute(sql2).GetRows
- ' arr5 = Application.WorksheetFunction.Transpose(arr5)
- ' Call daquxiaoshao
- rst.Close
- Set rst = Nothing
- cnn.Close
- Set cnn = Nothing
- MsgBox " 运行时间:" & Format((Timer - t), "0.0000000")
- End Sub
复制代码
大师,不好意思,是我搞错了。。我没有SELECT * FROM (SELECT * FROM TABLE UNION ALL SELECT * FROM TABLE)A 这里没有加别名,在SQL SERVER中好像都要加别名才能识别。
大师,您所IN('XX','YY')基本3秒。我才100多一年的数据。。就花了26秒多。。惭愧啊。。代码如下:大师帮忙优化下吧。。
|
|