SQL查询问题,高手帮帮忙!

zl194 2009-12-20 08:06:35

我需要这样的一个查询,它是由多个查询联合起来的。
TB表结构是这样的:
typeid,time,value1,value2,value3,value4
1,1,1,2,3,4
1,2,1,2,3,4
1,4,1,2,3,4
2,1,A,B,C,D
2,4,A,B,C,D
2,5,A,B,C,D
3,1,E,F,G,H
3,2,I,J,K,L
3,3,M,N,O,P
……
查询的结果是以time为关系列,列出其中的value如:
SLEECT TIME,VALUE1 FROM TB WHERE TYPEID=1
联合
SELECT TIME,VALUE1 FROM TB WHERE TYPEID=2
联合
SELECT TIME,VALUE1 FROM TB WHERE TYPEID=3
得到的结果应是这样的:
TIME,VALUE1_1,VALUE1_2,VALUE1_3
1 ,1 ,A ,E
2 ,1 ,'' ,''
3 ,'' ,'' ,M
4 ,1 ,A ,''
5 ,'' ,A ,''


其中TIME的数据肯定是1-999直接的数据,表示的是多少小时。可以先创建一个TIME表,填充1-999的TIME值,然后让每个查询都连接TIME表,然后再联合。


不知我说的您能理解吗,请帮我想想怎么处理!
...全文
69 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
zl194 2009-12-20
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 fredrickhu 的回复:]
SQL codedeclare@sqlvarchar(8000)set@sql='select time'select@sql=@sql+' , max(case typeid when'''+ltrim(typeid)+''' then value1 else'''' end) [value'+ltrim(typeid)+']'from (selectdistinct typeidfrom tb?-
[/Quote]

谢谢!
zl194 2009-12-20
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 acmain_chm 的回复:]
引用如果是只有的表,我想要分析x批号的,用您的那中case怎么写呢。根本不需要写到case 中SQL codeselect time,max(case typeidwhen1then value1end)as VALUE1_1,max(case typeidwhen2then value1end)as VALUE1_2,max(case typeidwhen3then value1end)as V?-
[/Quote]

谢谢,是我问问题的方式有问题。
--小F-- 2009-12-20
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql = 'select time '
select @sql = @sql + ' , max(case typeid when ''' + ltrim(typeid) + ''' then value1 else '''' end) [value' + ltrim(typeid) + ']'
from (select distinct typeid from tb) as a
set @sql = @sql + ' from tb where pici='x' group by time'
exec(@sql)
gao__910 2009-12-20
  • 打赏
  • 举报
回复
学习
--小F-- 2009-12-20
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql = 'select time '
select @sql = @sql + ' , max(case typeid when ''' + ltrim(typeid) + ''' then value1 else '''' end) [value' + ltrim(typeid) + ']'
from (select distinct typeid from tb) as a
set @sql = @sql + ' from tb where pici='x' group by time'
exec(@sql)
ACMAIN_CHM 2009-12-20
  • 打赏
  • 举报
回复
[Quote]如果是只有的表,我想要分析x批号的,用您的那中case怎么写呢。[/Quote]根本不需要写到case 中
select time,
max(case typeid when 1 then value1 end) as VALUE1_1,
max(case typeid when 2 then value1 end) as VALUE1_2,
max(case typeid when 3 then value1 end) as VALUE1_3
from TB
where pici(批号)='x'
group by time
另外建议提问时一次把需求全部提出。否则回答一个,紧跟着又是一个这种效果并不好。最好一开始就把问题完整的描述出来。
--小F-- 2009-12-20
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 zl194 的回复:]
SQL code
typeid|time|value1|value2|value3|value4|pici(批号)-----------|-----------|------|------|------|------|------1|1|1|2|3|4|x1|2|1|2|3|4|x1|4|1|2|3|4|z2|1|A|B|C|D|z2|4|A|B|C|D|x2|5|A|B|C|D|x3|1|E|F|G|H|z3|2|I|J|K|L|z3|3|M|N|O|P|x

如果是只有的表,我想要分析x批号的,用您的那中case怎么写呢。
[/Quote]


在后面加条件where pici='x'
--小F-- 2009-12-20
  • 打赏
  • 举报
回复
--动态的
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2009-12-20 20:25:52
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([typeid] int,[time] int,[value1] varchar(1),[value2] varchar(1),[value3] varchar(1),[value4] varchar(1))
insert [tb]
select 1,1,'1','2','3','4' union all
select 1,2,'1','2','3','4' union all
select 1,4,'1','2','3','4' union all
select 2,1,'A','B','C','D' union all
select 2,4,'A','B','C','D' union all
select 2,5,'A','B','C','D' union all
select 3,1,'E','F','G','H' union all
select 3,2,'I','J','K','L' union all
select 3,3,'M','N','O','P'
--------------开始查询--------------------------
declare @sql varchar(8000)
set @sql = 'select time '
select @sql = @sql + ' , max(case typeid when ''' + ltrim(typeid) + ''' then value1 else '''' end) [value' + ltrim(typeid) + ']'
from (select distinct typeid from tb) as a
set @sql = @sql + ' from tb group by time'
exec(@sql)

----------------结果----------------------------
/* time value1 value2 value3
----------- ------ ------ ------
1 1 A E
2 1 I
3 M
4 1 A
5 A

(5 行受影响)
*/
zl194 2009-12-20
  • 打赏
  • 举报
回复

typeid |time |value1|value2|value3|value4|pici(批号)
-----------|-----------|------|------|------|------|------
1| 1|1 |2 |3 |4 |x
1| 2|1 |2 |3 |4 |x
1| 4|1 |2 |3 |4 |z
2| 1|A |B |C |D |z
2| 4|A |B |C |D |x
2| 5|A |B |C |D |x
3| 1|E |F |G |H |z
3| 2|I |J |K |L |z
3| 3|M |N |O |P |x



如果是只有的表,我想要分析x批号的,用您的那中case怎么写呢。
--小F-- 2009-12-20
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2009-12-20 20:25:52
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([typeid] int,[time] int,[value1] varchar(1),[value2] varchar(1),[value3] varchar(1),[value4] varchar(1))
insert [tb]
select 1,1,'1','2','3','4' union all
select 1,2,'1','2','3','4' union all
select 1,4,'1','2','3','4' union all
select 2,1,'A','B','C','D' union all
select 2,4,'A','B','C','D' union all
select 2,5,'A','B','C','D' union all
select 3,1,'E','F','G','H' union all
select 3,2,'I','J','K','L' union all
select 3,3,'M','N','O','P'
--------------开始查询--------------------------
declare @sql varchar(8000)
set @sql = 'select time '
select @sql = @sql + ' , max(case typeid when ''' + ltrim(typeid) + ''' then value1 else '''' end) [' + ltrim(typeid) + ']'
from (select distinct typeid from tb) as a
set @sql = @sql + ' from tb group by time'
exec(@sql)

----------------结果----------------------------
/* time 1 2 3
----------- ---- ---- ----
1 1 A E
2 1 I
3 M
4 1 A
5 A

(5 行受影响)
*/
ACMAIN_CHM 2009-12-20
  • 打赏
  • 举报
回复
如果除了typeid 还有其他过滤条件能写进去吗。

当然可以。
zl194 2009-12-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 acmain_chm 的回复:]
SQL codeselect time,max(case typeidwhen1then value1end)as VALUE1_1,max(case typeidwhen2then value1end)as VALUE1_2,max(case typeidwhen3then value1end)as VALUE1_3from TBgroupby time
[/Quote]

如果除了typeid 还有其他过滤条件能写进去吗。
ACMAIN_CHM 2009-12-20
  • 打赏
  • 举报
回复
1> select * from tb;
2> go
typeid |time |value1|value2|value3|value4
-----------|-----------|------|------|------|------
1| 1|1 |2 |3 |4
1| 2|1 |2 |3 |4
1| 4|1 |2 |3 |4
2| 1|A |B |C |D
2| 4|A |B |C |D
2| 5|A |B |C |D
3| 1|E |F |G |H
3| 2|I |J |K |L
3| 3|M |N |O |P

(9 rows affected)
1> select time,
2> max(case typeid when 1 then value1 end) as VALUE1_1,
3> max(case typeid when 2 then value1 end) as VALUE1_2,
4> max(case typeid when 3 then value1 end) as VALUE1_3
5> from TB
6> group by time
7> go
time |VALUE1_1|VALUE1_2|VALUE1_3
-----------|--------|--------|--------
1|1 |A |E
2|1 |NULL |I
3|NULL |NULL |M
4|1 |A |NULL
5|NULL |A |NULL
Warning: Null value is eliminated by an aggregate or other SET operation.
1>
zl194 2009-12-20
  • 打赏
  • 举报
回复
就是有个表是这样的。
标识,时间(小时),值1, 值2, 值3, 值4
1 1小时 ,标识1的在1小时的值1 ,标识1的在1小时的值2……
1 2小时 ,标识1在2小时的值2,……
……
2 1小时 ,标识2的在1小时的值1 ,……
2 2小时 ,标识2在2小时的值2,……
……
3 1小时 ,标识3的在1小时的值1 ,……
3 2小时 ,标识3在2小时的值2,……
……

我想对比 各个标识在各时间上的值。即
对比值1
时间,标识1的值, 标识2的值, 标识3的值
1, 标识1的在1小时的值1 ,标识2的在1小时的值1 ,标识3的在1小时的值1
2, 标识1的在2小时的值1 ,标识2的在2小时的值1 ,标识3的在2小时的值1
3, 标识1的在3小时的值1 ,标识2的在3小时的值1 ,标识3的在3小时的值1
……

不知道这样能不能说清。
ACMAIN_CHM 2009-12-20
  • 打赏
  • 举报
回复
select time,
max(case typeid when 1 then value1 end) as VALUE1_1,
max(case typeid when 2 then value1 end) as VALUE1_2,
max(case typeid when 3 then value1 end) as VALUE1_3
from TB
group by time
zl194 2009-12-20
  • 打赏
  • 举报
回复
天啊,胡老师别下线,我从新说明一下。
qiqi860819 2009-12-20
  • 打赏
  • 举报
回复
不明白,
--小F-- 2009-12-20
  • 打赏
  • 举报
回复
没看懂
zl194 2009-12-20
  • 打赏
  • 举报
回复
就是多个查询用time列关联后组合成一个视图这样的一个需求。
一个java编程问题,关于点菜 浏览次数:629次悬赏分:15 | 解决时间:2010-1-14 13:10 | 提问者:aimiaozi 现在有一份菜单给客人察看,并要计算他们的消费。 要求:客人菜时要打印出菜单,而且只需输入菜号即点菜,并要打印出客人所点的菜,最后计算客人应付的菜钱。 编号 菜名 价钱 01 扬州炒饭 5.0 02 辣子鸡丁 9.0 03 羊肉串 5.0 04 鸡汤 15.0 各位高手帮帮忙吧~~~~~能做得了全部最好,谢谢!!!问题补充: 程序是要输入菜号就能知道菜名、菜价,我就是不会这点。。。 如果可以,希望能用一维数组做出来。我是初学者!!!!! 答得好能加分哦,谢谢!! 麻烦各位了,请多包涵! 最佳答案 package ddd; public class Dish { private String name; private String id; private double unit; private int number; private String text; public Dish(String id,String name,double unit,int number){ this.id=id; this.name=name; this.unit=unit; this.number=number; } public Dish(){ } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public double getUnit() { return unit; } public void setUnit(double unit) { this.unit = unit; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public String getText() { return text; } public void setText(String text) { this.text = text; } public Dish clone(){ Dish d=new Dish(); d.setId(id); d.setName(name); d.setUnit(unit); d.setNumber(number); return d; } } ----------------------------------------- package ddd; import java.sql.Date; import java.text.SimpleDateFormat; import java.util.LinkedList; import java.util.List; public class Order { private String user; public List list; private double sumMoney; private String date; public Order(String user){ this.user=user; SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date=new Date(System.currentTimeMillis()); this.date=format.format(date); list=new LinkedList(); } public List getList() { return list; } public void setList(List list) { this.list = list; } public double getSumMoney() { return sumMoney; } public void setSumMoney(double sumMoney) { this.sumMoney = sumMoney; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public void printOrder(){ System.out.println("--------------------------------------------------------------"); System.out.println("+用户名:"+user+"+"); System.out.println("+消费日期:"+date+"+"); System.out.println("--------------------------------------------------------------"); System.out.println("编号 \t 名称 \t 价格(元) \t数量 \t 合计(元)"); for(Dish dish:list){ System.out.printf("%-5s \t %-12s \t %-5s \t %-2s \t %-5s\n",dish.getId(),dish.getName(),dish.getUnit(),dish.getNumber(),dish.getUnit()*dish.getNumber()); } System.out.println("--------------------------------------------------------------"); System.out.println("+消费合计:"+sumMoney+"+"); } } -------------------------------------------- package ddd; import java.io.IOException; import java.util.LinkedHashMap; import java.util.Map; import java.util.Scanner; public class Main { private Map map; private Order order; public Main(String username){ map=new LinkedHashMap(); map.put("01",new Dish("01","扬州炒饭",5.0,10)); map.put("02",new Dish("02","辣子鸡丁",9.0,10)); map.put("03",new Dish("03","羊肉串 ",5.0,10)); map.put("04",new Dish("04","鸡汤 ",15.0,10)); map.put("05",new Dish("05","红烧排骨",25.0,10)); map.put("06",new Dish("06","清蒸鲤鱼 ",35.0,10)); map.put("07",new Dish("07","杂酱面 ",8.0,10)); map.put("08",new Dish("08","绿豆粥 ",2.0,10)); order=new Order(username); } public void printDishList(){ System.out.println("-------------------菜 谱-------------------"); System.out.println("编号 \t 名称 \t 价格(元) \t数量"); System.out.println("--------------------------------------------"); for(Dish dish:map.values()){ System.out.printf("%-5s \t %-12s \t %-5s \t %-2s\n",dish.getId(),dish.getName(),dish.getUnit(),dish.getNumber()); } System.out.println("--------------------------------------------"); } public static void main(String[] str) throws IOException{ Main main=new Main("佚名"); Scanner s=new Scanner(System.in); main.printDishList(); while(true){ String id=""; System.out.print("请输入菜单编号:"); id=s.nextLine(); if(!main.map.containsKey(id)) {System.out.println("没有该菜名编号!");continue;} Dish dish=main.map.get(id).clone(); System.out.println(dish.getName()); System.out.print("请输入所要菜名【"+dish.getName()+"】的数量(0表示全部都要;必须小于等于"+dish.getNumber()+"):"); int number=Integer.parseInt(s.nextLine()); if(numberdish.getNumber()){System.out.println("数量输入错误!");continue;} else if(number==0) ; else dish.setNumber(number); main.order.setSumMoney(main.order.getSumMoney()+dish.getNumber()*dish.getUnit()); main.order.list.add(dish); System.out.println("一种菜品添加成功!继续直接any key;结束输入exit并回车"); String tag=s.nextLine(); if("exit".equalsIgnoreCase(tag)) break; } main.order.printOrder(); } }

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧