如何实现用SQL查询出同产品编号不同单价变动条件如何写

basketballers 2014-01-13 09:25:33
如数据库SQL的表为A, A表中列有产品编号,名称,单价,数量,金额,要实现SQL查询出同产品编号不同单价变动条件如何写
产品编号----名称----- 单价-------数量------金额
001 记事本 12 10 120
002 计算机 20 42 840
001 记事本 25 33 825
002 计算机 13 12 156
执行查询条件结果如下
产品编号----名称----- 单价1---单价2
001 记事本 12 25
002 计算机 20 12
...全文
799 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sqlkxr 2014-02-15
  • 打赏
  • 举报
回复

create table tk3(spbh varchar(20),spmch nvarchar(30),price int,qty int,je as price*qty )


select identity(int,1,1) as id,* into #tk3 from tk3


select a.spbh,a.spmch,a.price,(select price from #tk3 where id>a.id and spmch=a.spmch) price2 from #tk3 a

where (select price from #tk3 where id>a.id and spmch=a.spmch) is not null

drop table #tk3
哥们你这个表结构是有问题的,实际的设计比如会员就和售价,总有个字段能标示,这样行转列就有判断的依据,你这样的。只能自己增加定义咧了。
唐诗三百首 2014-02-14
  • 打赏
  • 举报
回复

create table A表
(产品编号 varchar(10),名称 varchar(10),
 单价 int,数量 int,金额 int)

insert into A表
 select '001', '记事本', 12, 10, 120 union all
 select '002', '计算机', 20, 42, 840 union all
 select '001', '记事本', 25, 33, 825 union all
 select '002', '计算机', 13, 12, 156


declare @tsql varchar(6000)

select @tsql=isnull(@tsql+',','')
            +'max(case when 产品编号=t.产品编号 and rn='+rtrim(number)+' then 单价 else 0 end) ''单价'+rtrim(number)+''' '
 from master.dbo.spt_values
 where type='P' and number>=1
 and number<=
 (select max(qty)
  from (select count(单价) 'qty' from A表 group by 产品编号) t)

select @tsql='select 产品编号,名称,'+@tsql+
             ' from (select 产品编号,名称,单价,
                            row_number() over(partition by 产品编号 order by rn0) ''rn''
                     from (select *,row_number() over(order by getdate()) ''rn0'' from A表) u) t
               group by 产品编号,名称
               order by 产品编号 '

exec(@tsql)

/*
产品编号       名称         单价1         单价2
---------- ---------- ----------- -----------
001        记事本        12          25
002        计算机        20          13

(2 row(s) affected)
*/
LongRui888 2014-02-14
  • 打赏
  • 举报
回复
你这个还是得用动态语句来实现
xxfvba 2014-02-14
  • 打赏
  • 举报
回复
我用了个临时表,当然可以用存储过程 create table T (id varchar(4),nam varchar(10),up decimal(12,2)) insert into T select '001','laptop',10 union all select '002','computer',20 union all select '001','laptop',25 union all select '002','computer',13 select rn=ROW_NUMBER() over (partition by id order by getdate()),* into #T from (select distinct id,nam,UP from T) a declare @s varchar(max) select @s=ISNULL(@s+',','')+'['+convert(varchar,rn)+']' from #t group by rn set @s='select id,nam,'+@s+ 'from #t pivot(max(up) for rn in ('+@s+'))a order by id' exec (@s) drop table #T drop table T
發糞塗牆 2014-01-14
  • 打赏
  • 举报
回复
给出的信息不够,只能写这么多
----------------------------------------------------------------
-- Author  :DBA_Huangzj(發糞塗牆)
-- Date    :2014-01-14 07:54:57
-- Version:
--      Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64) 
--	Dec 28 2012 20:23:12 
--	Copyright (c) Microsoft Corporation
--	Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go 
create table [huang]([产品编号] varchar(3),[名称] varchar(6),[单价] int,[数量] int,[金额] int)
insert [huang]
select '001','记事本',12,10,120 union all
select '002','计算机',20,42,840 union all
select '001','记事本',25,33,825 union all
select '002','计算机',13,12,156
--------------开始查询--------------------------
SELECT [产品编号],[名称],MAX(CASE WHEN id=1 THEN 单价 ELSE NULL END)[单价1]	,MAX(CASE WHEN id=2 THEN 单价 ELSE NULL END )[单价2]
FROM (
select * ,ROW_NUMBER()OVER(PARTITION BY [产品编号] ORDER BY [产品编号])id
from [huang])a
GROUP BY [产品编号],[名称]
----------------结果----------------------------
/* 
产品编号 名称     单价1         单价2
---- ------ ----------- -----------
001  记事本    12          25
002  计算机    13          20
*/
shoppo0505 2014-01-13
  • 打赏
  • 举报
回复
如果变动次数有限,可以将变动次数设为静态列。 如果变动次数未知,可以将单价变动设为1列,里面的值可以使用函数拼接各个单价,最后作为字符串输出。

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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