求助,我做了一个交费程序,中间有2个表:用户信息表,交费记录表。 表如下:
用户信息表 交费记录表
id name address id year cash(元) re
1 李 长春 1 2005 50 已交
2 张 长春 1 2006 0 未缴
。。 2 2005 30 已交
2 2006 40 已交
把这2个表导出到excel中,要求excel格式如下:
id name address 2005 2006
1 李 长春 已交 未缴
2 张 长春 已交 已交
2005,2006都是re中的记录。
1个表如何导出excel,我已经实现。但是2个表还要特殊格式我应该怎么做?
请各位高手指点一下,先谢谢了!!
用户信息表 交费记录表
id name address id year cash(元) re
1 李 长春 1 2005 50 已交
2 张 长春 1 2006 0 未缴
。。 2 2005 30 已交
2 2006 40 已交
把这2个表导出到excel中,要求excel格式如下:
id name address 2005 2006
1 李 长春 已交 未缴
2 张 长春 已交 已交
2005,2006都是re中的记录。
1个表如何导出excel,我已经实现。但是2个表还要特殊格式我应该怎么做?
请各位高手指点一下,先谢谢了!!
000001 吉林省扬辉经贸有限公司 已交 已交
000003 长春市万力工程咨询有限公司 已交 已交
000004 长春市朝阳区建设服装厂 已交 已交
000005 吉林省力旺房地产开发有限公司
以上是实际导出后的结果。
1.创建两个表,user和cash表CREATE TABLE [user] (
[id] [varchar] (8) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[address] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
CONSTRAINT [PK_user] PRIMARY KEY CLUSTERED
(
[id]
) ON [PRIMARY]
) ON [PRIMARY]
GOCREATE TABLE [cash] (
[id] [varchar] (8) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[year] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[cash] [money] NULL ,
[re] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
CONSTRAINT [PK_cash] PRIMARY KEY CLUSTERED
(
[id],
[year]
) ON [PRIMARY] ,
CONSTRAINT [FK_cash_user] FOREIGN KEY
(
[id]
) REFERENCES [user] (
[id]
)
) ON [PRIMARY]
GO2.向user表中输入以下数据
id name address
-----------------------
1 王 娄底
2 李 双峰
3 周 涟源3.向cash表中输入以下数据
id year cash re
------------------------------
1 2005 50.0000 NULL
1 2006 50.0000 NULL
2 2005 30.0000 NULL
2 2006 .0000 NULL
3 2005 .0000 NULL4.假设缴费数量大于等于30元的就认为是已交费者可以用以下SQL语句找出提问中输出格式的表格:
select u.id,
u.name,
u.address,
(case when (select cash from cash where year='2005' and id=u.id)>20.0 then '已交' else '未交' end) as '2005',
(case when (select cash from cash where year='2006' and id=u.id)>20.0 then '已交' else '未交' end) as '2006'
from
[user] as u结果集为:
id name address 2005 2006
------------------------------------
1 王 娄底 已交 已交
2 李 双峰 已交 未交
3 周 涟源 未交 未交以上刚好符合问题要求,将该查询建立成视图就可以了,然后导入EXCEL即可
cash表中的re不是要判断,而是用户输入的值,就是说在cash表中re是有植的, 那时不是就不需要写case when了?十分感谢,你的帮助,真是太感激了
select userinfobase.code ,userinfobase.username,
(select re from userdata where yearnum= '2005' and code = userinfobase.code)as '2005'
from userinfobase提示:
在这一子查询或聚合表达式中,text、ntext 和 image 数据类型无效。
re在数据库中的数值:未缴,已交这该如何是好
SQL语句这样试试:select a.id,a.name,
max(case when b.year='2004' then re end) as '2004',
max(case when b.year='2005' then re end) as '2005'
from 用户信息表 a inner join 交费记录表 b
on a.id=b.id
group by a.id,a.name建议:
1:你的表字段名称用了许多数据库关键字,建议修改掉
2:交费记录表中的re字段如果只是标志是否已经交费,好象是多余的,因为是否交费可以通过cash字段判断,这样SQL语句如下:select a.id,a.name,
max(case when b.year='2004' then (case when cash>0 then '已交' else '未交' end) end) as '2004',
max(case when b.year='2005' then (case when cash>0 then '已交' else '未交' end) end) as '2005'
from 用户信息表 a inner join 交费记录表 b
on a.id=b.id
group by a.id,a.name
2.我6搂得回答中有建立表的和输入数据的说明只是为了使问题说明更清楚。
3.你这问题还可以通过表的旋转解决。
select userinfobase.code ,userinfobase.username,
(select re from userdata where yearnum= '2005 ' and code = userinfobase.code)as '2005 '
from userinfobase 修改为:
select userinfobase.code ,userinfobase.username,
(select re from userdata where yearnum= '2005 ' and userdata.code = userinfobase.code)as '2005 '
from userinfobase
即可
@scmd varchar(8000)DECLARE years_cursor CURSOR FOR
select distinct year from cash
OPEN years_cursorFETCH NEXT FROM years_cursor INTO @yearswhile @@FETCH_STATUS = 0
begin
select @scmd='alter table #a ADD s'+@years+' VARCHAR(20) NULL '
exec( @scmd)
select @scmd='update #a set s'+@years+'=(select c.re from cash as c,#a as t where c.id=t.id and c.year='''+@years+''' and c.id=#a.id)'
exec( @scmd) FETCH NEXT FROM years_cursor INTO @years
endDEALLOCATE years_cursorselect * from #adrop table #a
呵,最近没什么事情干,没天在看贴,总想着用自认为最好的办法解决问题
还是提示这个:在这一子查询或聚合表达式中,text、ntext 和 image 数据类型无效。是不是因为是字符形式就不可以啊
你的一劳永逸的办法,我没有看懂,实在是不好意思,我太丢人了T_T
select userinfobase.code ,userinfobase.username,
(select re from userdata where yearnum= '2005 ' and code = userinfobase.code)as '2005 '
from userinfobase
中涉及到的字段肯定有这3种类型中的某一种,估计你的re是text类型的,你用varchar(8000)代替掉text,一般情况下8000个字符已经足够用了,代替后试试