当前位置:首页 » 编程语言 » sqlserver交叉查询
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

sqlserver交叉查询

发布时间: 2022-10-30 08:49:39

① asp + sqlserver 交叉表问题

select
学号,
姓名,
flash成绩=(select 成绩 from cj b where b.课程名='flash' and b.学号=a.学号),
dw成绩=(select 成绩 from cj b where b.课程名='dw' and b.学号=a.学号),
asp成绩=(select 成绩 from cj b where b.课程名='asp' and b.学号=a.学号)
from cj a
group by 学号,姓名

② SQLSERVER的多表查询问题

属于多表链接
在两个表之间使用【,】进行的链接,默认是交叉链接,也就是会生笛卡尔积

③ SqlServer2005交叉表查询问题

DECLARE @str as nVARCHAR(max)
set @str='yyq,'
select @str=@str+''''+fkmc+''''+'=sum(case fkmc when
'+''''+fkmc+''''+' then fk else null end),'
from hb group by fkmc
set @str='select '+substring(@str,1,len(@str)-1)+',sum(fk) as 合计 from hb group by yyq'
print @str
exec(@str)

你这样写,我执行了一下是可以的,除了数据为空时,其他都可以的

④ SQLServer中的交叉查询

没有直接的转换方法,建议这样:

select货号,品名=dbo.shfcoltostr(货号)fromTABLENAME

输出结果

货号品名
1A,AB,ABB
2B,BC

前题条件:在查询分析器中执行建立以下自定义函数

CREATEfunctionshfColToStr
(@HHvarchar(18))
returnsvarchar(5000)
begin
declare@resultvarchar(5000)
select@result=isnull(@result+',','')+品名fromTABLENAMEwhere货号=@HH
return@result
end
go

OK 了没有啊?

好吧,再给你一个方法,与你的要求分毫不差:


需要先根据品名或品名ID生成一个按货号分组的序号,存到临时表,以最多10个品名为例:

临时表生成:

SELECT*,SN=(SELECTCOUNT(1)FROMTABLENAMEAWHEREA.品名>=tablename.品名anda.货号=tablename.货号)into#tablenameFROMTABLENAME

看看这个临时表,多出了一个SN(序号),然后用下面的语句查询:

selectDISTINCTt.货号,
品名1=isnull((selecttop1a.品名from#tablenameawherea.货号=t.货号anda.sn=1),''),
品名2=isnull((selecttop1a.品名from#tablenameawherea.货号=t.货号anda.sn=2),''),
品名3=isnull((selecttop1a.品名from#tablenameawherea.货号=t.货号anda.sn=3),''),
品名4=isnull((selecttop1a.品名from#tablenameawherea.货号=t.货号anda.sn=4),''),
品名5=isnull((selecttop1a.品名from#tablenameawherea.货号=t.货号anda.sn=5),''),
品名6=isnull((selecttop1a.品名from#tablenameawherea.货号=t.货号anda.sn=6),''),
品名7=isnull((selecttop1a.品名from#tablenameawherea.货号=t.货号anda.sn=7),''),
品名8=isnull((selecttop1a.品名from#tablenameawherea.货号=t.货号anda.sn=8),''),
品名9=isnull((selecttop1a.品名from#tablenameawherea.货号=t.货号anda.sn=9),''),
品名10=isnull((selecttop1a.品名from#tablenameawherea.货号=t.货号anda.sn=10),'')
from#tablenamet

看上去复杂,写起来容易,只需复制 复制 <品名n=....sn=n>10次,然后改品名后缀和SN值即可。

结果:

货号品名1品名2品名3品名4品名5品名6品名7品名8品名9品名10
1AABABB
2BBC

⑤ sqlserver 时间段交叉查询语句(最简短的)

你说的对,少一种情况(ab在起至时间之间的)。
where (起始时间 between a and b) or (截止时间 between a and b) or (a between 起始时间 and截止时间)

⑥ sqlserver中两个表交叉连接查询结果有多少列和多少条记录

X+Y,MN

⑦ SQLSERVER 两表生成动态交叉表

--静态SQL
select [商品款号],[品名],[采购价],[零售单价],[颜色],[码系],
case [序号] when 0 then [数量] else null end "0",
case [序号] when 1 then [数量] else null end "1",
case [序号] when 2 then [数量] else null end "2",
case [序号] when 3 then [数量] else null end "3",
case [序号] when 4 then [数量] else null end "4"
from [出入库明细]

--动态SQL
declare @sql varchar(8000)
set @sql = 'select [商品款号],[品名],[采购价],[零售单价],[颜色],[码系]'
select @sql = @sql + ' , case [序号] when ' + seq + ' then [数量] else 0 end "' + seq + '"'
from (select distinct cast([序号] as varchar) seq from [出入库明细]) as a
set @sql = @sql + ' from [出入库明细]'
print @sql
exec(@sql)

⑧ SqlServer如何生成动态交叉表查询

这里指的交叉表,就是象Access的交叉表查询一样的效果,比如Employees表中City字段代表了城市的名称,TitleOfCourtesy代表称呼,我们希望按照City和TitleOfCourtesy的情况来统计ReportsTo字段的合计数(本统计没有任何实际意义,只是挑选一些记录包含重复内容的字段来说明情况),并显示成以下格式:(TitleOfCourtesy作为行,City作为列) ,�0�2�0�2SUM(CASE�0�2City�0�2WHEN�0�2'Redmond'�0�2THEN�0�2ReportsTo�0�2ELSE�0�2NULL�0�2END)�0�2AS�0�2[Redmond�0�2City] ,�0�2�0�2SUM(CASE�0�2City�0�2WHEN�0�2'Seattle'�0�2THEN�0�2ReportsTo�0�2ELSE�0�2NULL�0�2END)�0�2AS�0�2[Seattle�0�2City]FROM�0�2Employees�0�2GROUP�0�2BY�0�2TitleOfCourtesy 其中利用了CASE语句判断,如果是相应的列,则取需要统计的ReportsTo数值,否则取NULL,然后再合计 其中有两个常见问题说明一下: a、用NULL而不用0是有道理的,假如用0,虽然求和函数SUM可以取到正确的数,但类似COUNT函数(取记录个数),结果就不对了,因为Null不算一条记录,而0要算,同理空字串("")也是这样,总之在这里应该用NULL,这样任何函数都没问题。 b、假如在视图的设计界面保存以上的查询,则会报错“没有输出列”,从而无法保存,其实只要在查询前面加上一段:Create View ViewName AS ...,ViewName是你准备给查询起的名称,...就是我们的查询,然后运行一下,就可以生成视图了,对于其他一些设计器不支持的语法,也可以这样保存。 总体思路其实很简单,首先检索列头信息,形成一个游标,然后遍历游标,将上面查询语句里Case判断的内容用游标里的值替代,形成一条新的Sql查询,然后执行,返回结果,就可以了,以下是我写的一个存储过程,供大家参考:CREATE�0�2procere�0�2CorssTab�0�2 @strTabName�0�2as�0�2varchar(50)�0�2=�0�2'Employees' ,�0�2--此处放表名@strCol�0�2as�0�2varchar(50)�0�2=�0�2'City' ,�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2--表头分组依据字段@strGroup�0�2as�0�2varchar(50)�0�2=�0�2'TitleOfCourtesy',--分组字段@strNumber�0�2as�0�2varchar(50)�0�2=�0�2'ReportsTo' ,�0�2�0�2�0�2�0�2--被统计的字段@strSum�0�2as�0�2varchar(10)�0�2=�0�2'Sum'�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2--运算方式ASDECLARE�0�2@strSql�0�2as�0�2varchar(1000 ),�0�2@strTmpCol�0�2as�0�2varchar(100)EXECUTE�0�2('DECLARE�0�2corss_cursor�0�2CURSOR�0�2FOR�0�2SELECT�0�2DISTINCT�0�2'�0�2+�0�2@strCol�0�2+�0�2'�0�2from�0�2'�0�2+�0�2@strTabName�0�2+�0�2'�0�2for�0�2read�0�2only�0�2')�0�2--生成游标begin�0�2�0�2SET�0�2nocount�0�2ON�0�2�0�2�0�2SET�0�2@strsql�0�2='select�0�2'�0�2+�0�2@strGroup�0�2+�0�2' ,�0�2'�0�2+�0�2@strSum�0�2+�0�2'('�0�2+�0�2@strNumber�0�2+�0�2')�0�2AS�0�2['�0�2+�0�2@strSum�0�2+�0�2'�0�2of�0�2'�0�2+�0�2@strNumber�0�2+�0�2']'�0�2--查询的前半段�0�2�0�2OPEN�0�2corss_cursor�0�2�0�2while�0�2(0=0)�0�2�0�2BEGIN�0�2�0�2�0�2�0�2FETCH�0�2NEXT�0�2FROM�0�2corss_cursor�0�2--遍历游标,将列头信息放入变量@strTmpCol �0�2�0�2�0�2�0�2INTO�0�2@strTmpCol�0�2�0�2�0�2�0�2if�0�2(@@fetch_status<0)�0�2break�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2SET�0�2@strsql�0�2=�0�2@strsql�0�2+�0�2' ,�0�2'�0�2+�0�2@strSum�0�2+�0�2'(CASE�0�2'�0�2+�0�2@strCol�0�2+�0�2'�0�2WHEN�0�2'''�0�2+�0�2@strTmpCol�0�2+�0�2'''�0�2THEN�0�2'�0�2+�0�2@strNumber�0�2+�0�2'�0�2ELSE�0�2Null�0�2END)�0�2AS�0�2['�0�2+�0�2@strTmpCol�0�2+�0�2'�0�2'�0�2+�0�2@strCol�0�2+�0�2']'�0�2--构造查询�0�2�0�2END�0�2�0�2�0�2�0�2�0�2�0�2�0�2�0�2SET�0�2@strsql�0�2=�0�2@strsql�0�2+�0�2'�0�2from�0�2'�0�2+�0�2@strTabname�0�2+�0�2'�0�2group�0�2by�0�2'�0�2+�0�2@strGroup�0�2--查询结尾�0�2�0�2EXECUTE(@strsql)�0�2--执行�0�2�0�2IF�0�2@@error�0�2<0�0�2RETURN�0�2@@error�0�2--如果出错,返回错误代码�0�2�0�2CLOSE�0�2corss_cursor�0�2�0�2�0�2DEALLOCATE�0�2corss_cursor�0�2RETURN�0�20�0�2--释放游标,返回0表示成功endGO几点说明:a、这是一个通用存储过程,使用时@strTabName、@strCol、@strGroup、@strNumber、@strSum几个变量设置一下就可以用到其他表上,其中结果集的第二列我加了个合计列 b、为了测试方便,我在存储过程中设置了默认值,就是前面提到的Employees表,这样直接运行时就可以出来我上面提到的结果。 c、使用时,可以把上面的代码复制到企业管理器的查询设计界面Sql窗格,或者查询分析器里运行一下(注意正确选择NorthWind数据库),就可以生成一个存储过程:CorssTab,然后直接运行CorssTab,如果出现本文前面类似的窗格,就表示运行成功了。 d、假如用于其它表,首先需要在你的用户数据库里生成此存储过程(当然也可以放到Master里,然后再加个变量:@DataBase,赋值为数据库名称,然后在上面代码打开指定数据库,这样所有的数据库都可以调用它),当你调用时,采取以下格式:

⑨ 关于sqlserver 的一个查询, 解决再加50分

这是个交叉表,很容易的。用存储过程+临时表就最简单了。找了半天,终于找到我N年前写的一个给你参考一下,至少这种是相当灵活的。记得给分,兄弟!

if object_id('dbo.Proc_AP_BY_HPN') is not null
drop procere dbo.Proc_AP_BY_HPN
go

create procere Proc_AP_BY_HPN
@HPN varchar(18) = null
as

create table #temp_list
(
vendor_id varchar(50) not null,
HPN varchar(50) not null,
Currency CHAR(3) null,
Payment varchar(120) null,
Rec_Qty numeric(18,2) not null default 0.00,
Unit_Price float not null default 0.00,
GR_Dt varchar(50) not null
)

insert into #temp_list
(
vendor_id,HPN,Rec_Qty,Unit_Price,GR_Dt)
select a.vendorid,b.itemNo,0.000,Rec_qty, convert(char(4),datepart(yy,Rec_date))+'/'+convert(char(2),datepart(mm,Rec_date))
from base_Recdepot a,base_RecDepotc b
where a.add_id = b.single_id and Itemno like '%'+@hpn

select hpn,max(unitprice) as Unit_Price into #price_List
from ord_item where hpn like '%CK002'
group by hpn

update #temp_list
set unit_price = a.Rec_qty*b.unit_price

from #temp_list a,#price_list b where a.hpn = b.hpn

select a.vendor_id as [Vendor ID],B.description as [Vendor Name],a.Currency,a.Payment, convert(numeric(18,5),sum(a.unit_price)) as Amount,GR_dt
into #temp_final_list
from #temp_list a,ven_cust_mst b where a.vendor_id = b.id
group by a.vendor_id,b.description,a.Currency,a.Payment,GR_dt

--select * from #temp_final_list
declare @sql varchar(8000)
set @sql = 'select [Vendor ID],[Vendor Name],Currency,Payment,'

select @sql = @sql + 'sum(case GR_dt when '''+GR_dt+'''
then Amount else 0 end) as '''+GR_dt+''','
from (select distinct GR_dt from #temp_final_list ) as a

select @sql = left(@sql,len(@sql)-1) + '
from #temp_final_list
group by
[Vendor id],
[Vendor Name],
Currency,Payment

order by [Vendor ID]'
exec(@sql)
drop table #temp_final_list
drop table #price_list
drop table #temp_list

go
Proc_AP_BY_HPN 'CK002'

楼主,我的是全动态的,列是不确定的,也可以用变量的。用SQL CASE 直接写成是只适用于固定列的情形。

⑩ SQL中进行交差表查询的方法和代码

交叉查询你可以通过两中方法实现

建表语句:注意数据库为sqlserver
create table A(aID int,aNum nvarchar(10))

insert into A (aID,aNum) values (1,'a1')
insert into A (aID,aNum) values (2,'a2')

create table B(bID int,bName nvarchar(10))

insert into B (bID,bName) values (1,'b1')
insert into B (bID,bName) values (2,'b2')
insert into B (bID,bName) values (3,'b3')

create table C(ca int,cb int)

insert into C (ca,cb) values (1,2)
insert into C (ca,cb) values (1,3)
insert into C (ca,cb) values (2,1)
insert into C (ca,cb) values (2,3)
第一:通过full join
select aa.*,cc.*,bb.* from A aa
full join B bb on 1=1
full join C cc on cc.ca=aa.aID and cc.cb=bb.bID

得到的结果是
aID aNum ca cb bID bName
----------- ---------- ----------- ----------- ----------- ----------
1 a1 NULL NULL 1 b1
1 a1 1 2 2 b2
1 a1 1 3 3 b3
2 a2 2 1 1 b1
2 a2 NULL NULL 2 b2
2 a2 2 3 3 b3

(6 件処理されました)
第二种方法是 cross join
select N.aid,N.anum,N.ca,T.CB,T.BID,T.BNAME
from
(select a.aid,a.anum,c.ca
from a
right join
c
ON
a.aid=c.ca
) N
CROSS JOIN

(select B.Bid,B.BNAME,c.ca,c.cb
from B
right join
c
ON
B.aid=c.cB) T
结果如上