sql查询字段 (VB datagrid)

来源:百度知道 编辑:UC知道 时间:2024/05/23 02:19:19
就比如,字段销售额 我要同时查询去年的销售额与今年11月份的销售额,显示在datagrid1中。
select 客户、销售额 as 去年销售额 from a where 时间1
select 客户、销售额 as 11月份销售额 from a where 时间2
我只想用一条语句就实现。语法不怎么规范,明白意思就行。

select 客户、销售额 as 去年销售额、销售额 as 11月份销售额 from a where ???
1.查询结果 2、查询结果
客户 去年销售额 客户 11月份销售额
001 500 001 50
002 600 002 30
我目的是:(通过只有一条语句就可以查询出来,前提是,那两个查询都由同一字段“时间”影响。或者你们有什么办法)。
客户 去年销售额 11月份销售额
001 500 50
002 600 30

行加用
select 客户、销售额 as 去年销售额 from a where 时间1 union
select 客户、销售额 as 11月份销售额 from a where 时间2
列加
select * from
(select 客户、销售额 as 去年销售额 from a where 时间1) a join
(select 客户、销售额 as 11月份销售额 from a where 时间2) b on a.客户
也可能是用别的字段连接看你具体的需求了。

方法有2:(假设 时间1和时间2是个输入变量)
dim mStr as String

1:
mStr = "select 客户,销售额 from a where 日期 in('"& 时间1 &"','"& 时间2 &"')"

2:

mStr = "select 客户,销售额 from a where 日期 = '"& 时间1 &"' Union all select 客户,销售额 from a where 日期 = '"& 时间2 &"'"

注:如果[日期]字段是日期类型,要将时间1、时间2转换为日期类型。

如果要查询的是一个时间段,就要用到 BETWEEN

你这个表的结构是怎么设计的呢?
去年的销售额你是怎么存的?是需要加和?还是已经加完了?
去年是什么时候啊?12个月的?
你的表结构都不清楚,怎么告诉你?

应该是下面这样吧,注意select 语句的字符串是单引号。
sqlstr="select 客户,销售额 from a where 日期 = '" & year(data)=07 "'" & "or 日期 = '" & (month(data)=11 and