一个两表联合查询的问题!

秀小川 2012-10-17 09:55:40
有两张表
表1存储用户的基本信息
"userid" (主键)
"用户姓名"
.
.
.
表2:存储所用用户的购买信息
"userid"
"时间"
"购买金额"

现在是想查询某位用户具体的 "姓名", "最近一次消费记录", "平均每月消费金额"该怎样去做这样的一个查询?知道具体的userid

...全文
184 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
IEEE_China 2012-10-17
  • 打赏
  • 举报
回复

declare @userid int
declare @avgMoney decimal(12,2)
set @userid=4---这里取你要查询的用户ID

CREATE TABLE #a(
[ID] [int] IDENTITY(1,1) NOT NULL,
[userid] [int] NULL ,
[用户姓名][nvarchar](50) NULL

)
CREATE TABLE #b(
[ID] [int] IDENTITY(1,1) NOT NULL,
[userid] [int] NULL ,
[时间] datetime NULL,
[购买金额] decimal(12,2) NULL

)

insert into #a
select 1,'张三' union all
select 2,'李四' union all
select 3,'王五' union all
select 4,'赵六'

insert into #b
select 1,'2012-01-01',10.00 union all
select 1,'2012-02-01',20.00 union all
select 1,'2012-03-01',30.00 union all
select 1,'2012-04-01',40.00 union all
select 2,'2012-01-01',10.00 union all
select 2,'2012-02-01',100.00 union all
select 2,'2012-03-01',200.00 union all
select 2,'2012-04-01',300.00 union all
select 3,'2012-01-01',111.00 union all
select 3,'2012-02-01',222.00 union all
select 3,'2012-03-01',333.00 union all
select 3,'2012-04-01',444.00 union all
select 4,'2012-01-01',1.00 union all
select 4,'2012-02-01',2.00 union all
select 4,'2012-03-01',3.00 union all
select 4,'2012-04-01',4.00


select @avgMoney =(select sum(购买金额)/datediff(m,min(时间),max(时间)) from #b where userid=@userid)
select top 1 a.用户姓名,b.时间,b.购买金额, @avgMoney 平均每月消费
from #a a join #b b on a.userid=b.userid
where a.userid=@userid
order by b.时间 desc

drop table #a
drop table #b

---查询结果
(4 行受影响)

(16 行受影响)

(1 行受影响)

----------------------------------------
用户姓名 时间 购买金额 平均每月消费

赵六 2012-04-01 00:00:00.000 4.00 3.33

秀小川 2012-10-17
  • 打赏
  • 举报
回复
或许我表达没表达清楚,
"平均每月消费"是这个意思: 用户一个月可能有多次消费,也可能几个月消费一次!` 这里平均每个月就是所有的消费金额加起来 除以 第一次消费到当前时间间隔的月数!
IEEE_China 2012-10-17
  • 打赏
  • 举报
回复
求平均消费可以和最后一条消费记录写在一起,
每月平均消费????
唐诗三百首 2012-10-17
  • 打赏
  • 举报
回复

-- 最近一次消费记录
select b.用户姓名,a.时间,a.购买金额
from 表2 a
inner join 表1 b on a.userid=b.userid
inner join
(select userid,max(时间) '时间'
from 表2 where userid=[userid]) c
on a.userid=c.userid and a.时间=c.时间
where a.userid=[userid]

-- 平均每月消费金额
select b.用户姓名,
rtrim(datepart(mm,a.时间))+'月' '月份',
avg(a.购买金额) '平均消费金额'
from 表2 a
inner join 表1 b on a.userid=b.userid
where a.userid=[userid]
group by datepart(mm,a.时间)
以学习为目的 2012-10-17
  • 打赏
  • 举报
回复
楼主测试下楼上的
IEEE_China 2012-10-17
  • 打赏
  • 举报
回复


declare @userid int
set @userid=3---这里取你要查询的用户ID

CREATE TABLE #a(
[ID] [int] IDENTITY(1,1) NOT NULL,
[userid] [int] NULL ,
[用户姓名][nvarchar](50) NULL

)
CREATE TABLE #b(
[ID] [int] IDENTITY(1,1) NOT NULL,
[userid] [int] NULL ,
[时间] datetime NULL,
[购买金额] decimal(12,2) NULL

)

insert into #a
select 1,'张三' union all
select 2,'李四' union all
select 3,'王五' union all
select 4,'赵六'

insert into #b
select 1,'2012-01-01',10.00 union all
select 1,'2012-02-01',21.00 union all
select 1,'2012-03-01',32.00 union all
select 1,'2012-04-01',43.00 union all
select 2,'2012-01-01',10.00 union all
select 2,'2012-02-01',24.00 union all
select 2,'2012-03-01',35.00 union all
select 2,'2012-04-01',46.00 union all
select 3,'2012-01-01',10.00 union all
select 3,'2012-02-01',25.00 union all
select 3,'2012-03-01',34.00 union all
select 3,'2012-04-01',43.00 union all
select 4,'2012-01-01',10.00 union all
select 4,'2012-02-01',22.00 union all
select 4,'2012-03-01',33.00 union all
select 4,'2012-04-01',44.00



select top 1 a.用户姓名,b.时间,b.购买金额,(select avg(购买金额) from #b where userid=@userid) 平均消费
from #a a join #b b on a.userid=b.userid
where a.userid=@userid
order by b.时间 desc

drop table #a
drop table #b

-----查询结果

用户姓名 时间 购买金额 平均消费
王五 2012-04-01 00:00:00.000 43.00 28.000000


(4 行受影响)

(16 行受影响)

(1 行受影响)

  • 打赏
  • 举报
回复
求:最近一次消费记录", "平均每月消费金额"
--》》最好写成两个sql。最近一次消费记录是一条,而每月平均消费金额可能是多条记录,写不到一块去。

最近一次消费记录",
select 姓名,时间,购买金额
from tb t
where not exists(select 1 from tb wher 姓名=t.姓名 and 时间>t.时间)

平均每月消费金额":
select 姓名,convert(char(8),时间),avg(金额)
from tb
group by 姓名,convert(char(8),时间)
IEEE_China 2012-10-17
  • 打赏
  • 举报
回复
昨天你不是问了这个问题了么
秀小川 2012-10-17
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

SQL code

declare @userid int
declare @avgMoney decimal(12,2)
set @userid=4---这里取你要查询的用户ID

CREATE TABLE #a(
[ID] [int] IDENTITY(1,1) NOT NULL,
[userid] [int] NULL ,
[用户姓名][nvarchar](50……
[/Quote]
谢谢了,就是这样的!
这个是完整源码 python FastAPI实现 vue 深度学习 大模型 【深度学习毕业设计】基于BERT的电商商品评论情感分析系统(PyTorch+FastAPI+Vue3) 模型微调训练 深度学习毕业设计 python课程设计 完整版 源码+sql脚本+论文 完整版 数据库是mysql 随着电子商务规模持续扩大,商品评论已成为消费者决策与商家改进产品的重要依据。海量评论文本具有口语化、领域词汇密集、正负情感交织等特点,传统基于词典或浅层机器学习的情感分析方法难以充分刻画上下文语义,分类精度受到限制。针对上述问题,本文设计并实现了一套基于 BERT 的电商商品评论情感分析系统,完成从评论采集、模型推理、结果存储到可视化分析的闭环。 系统采用前后端分离架构。后端以 Python 语言和 FastAPI 框架构建 RESTful 接口,使用 SQLAlchemy 访问 MySQL 8 数据库 db_bert_sentiment,核心推理模块基于 PyTorch 与 Transformers 加载中文 BERT 微调模型 BertForSequenceClassification,对评论进行 1 至 5 星五分类,并映射为正面、中性、负面三类情感;当微调模型文件缺失时自动回退到电商情感词典规则引擎,保证系统可用性。前端采用 Vue3、Vite、Element Plus、Pinia 与 ECharts 实现管理端界面,支持单条实时分析、CSV 批量导入、评论维护、统计分析、模型管理、个人中心和操作日志等功能。 在数据库设计方面,系统围绕管理员、评论、分析任务、模型信息、情感关键词和操作日志六类实体建立概念模型,给出独立的实体属性图与实体间关系图,并以表格形式详细列出各表字段名称、类型、长度、是否为空及备注。测试表明,系统能够稳定完成登录鉴权、情感推理、批量任务与多维图表展示,B
内容概要:本文提出了一种基于角蜥蜴优化算法(Harris Hawks Optimization-inspired Lizard Search Algorithm, HLOA)优化BP神经网络的风电功率预测模型,旨在解决传统BP神经网络在风电功率预测中易陷入局部最优、收敛速度慢、预测精度不高等问题。通过HLOA算法对BP网络的初始权重和阈值进行全局优化,提升了模型的泛化能力与训练效率。研究在Matlab平台上完成算法实现,并采用真实风电场数据进行实验验证,结果表明,相较于标准BP及其他优化算法(如GA、PSO)优化的模型,HLOA-BP模型在均方根误差(RMSE)、平均绝对误差(MAE)等关键评价指标上表现更优,具有更强的预测稳定性和准确性。该方法为可再生能源领域的时间序列预测提供了有效的技术路径与实践参考。; 适合人群:具备机器学习、智能优化算法及电力系统基础知识的研究生、科研人员以及从事新能源预测、电力调度等相关工作的工程技术人员。; 使用场景及目标:①提升风电功率预测精度,支撑电网安全稳定运行与能源调度决策;②学习并掌握智能优化算法与神经网络融合建模的方法论;③开展基于Matlab的仿真实验、算法对比与性能评估;④拓展应用于光伏发电、负荷预测等其他非线性时间序列预测任务。; 阅读建议:建议结合提供的Matlab代码深入实践,重点理解HLOA算法的搜索机制及其对BP网络参数的优化过程,通过更换数据集、调整参数配置等方式进行消融实验与对比分析,全面掌握模型构建与调优技巧,进而将其迁移至实际工程项目中应用。

34,876

社区成员

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

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