注册 登录
编程论坛 SQL Server论坛

数据库查询

kingorwei 发布于 2017-07-17 13:30, 2546 次点击
大家好,刚开始学习SQL,从查询开始弄起,现在有一个问题挡住了,不知如何处理,请各位高手指点一二,谢谢!
1,有2个表:
表一:PrintData
Id      PId            PrinterModel    PrinterId             TonerId        TotalPrint    InkLevel    Time
5690285    10450042     M7218W        HP00093503HP06080838    1511MJ30001031     1705        30    2017/7/10 0:00
5690286    10450043     LJ2208W       HP00058821HP0605        1605ML68004178     901          0    2017/7/10 0:00
5690287    10450044     M7208W        HP00117725HP0702        1702ML68000816     203          0    2017/7/10 0:00
5690288    10450045     M7208W        HP00149918HP0705        1705ML68006496     2            0    2017/7/10 0:00
表二:PrintInfo
Id                DeviceCode           Mobile                            Platform        Version        Time            Location
10450042    e83c4da6d5dcb11483150c21920617f72a302cfc                        iOS            0.2.9        2017/7/10 0:00   
10450043    14:2D:27:46:5F:5F                                                WinPC         1.2.4.6      2017/7/10 0:00   
10450044    60:45:CB:73:01:53                                                WinPC         1.2.4.6      2017/7/10 0:00   
10450045    9C:D2:1E:84:B5:1D                                                WinPC         1.2.4.6      2017/7/10 0:00   
10450051    8.64618E+14                                                      Android       0.2.4        2017/7/10 0:01   
10450052    AC:E0:10:55:59:CE                                                WinPC         1.2.4.0      2017/7/10 0:02   
10450053    8.6392E+14                                                       Android       0.2.3        2017/7/10 0:02   
10450054    8.6392E+14                                                       Android       0.2.3        2017/7/10 0:02   
10450057    6a14992b6b9a86f3e3a3d9bf1d5f213ebbc52c12                         iOS           0.2.9        2017/7/10 0:02   
10450058    44:37:E6:35:0B:A7                                                WinPC         1.2.4.7        2017/7/10 0:02   
其中,PrintData.PId与PrintInfo.Id 相互匹配,相互对应,也就是相同的值,数据库设置了权限,不能创建新表
要进行查询的工作内容如下:
1,PrintData中PrinterId是打印机的设备,找出PrinterId中在PrintInfo.Platform的对应情况,分为下面3种情况进行统计
a,PrinterId仅仅单独分别对应Android、iOS、WinPC的数量(3种)
b,PrinterId对应WinPC +(Android 或iOS)的数量
c,PrinterId对应Android +iOS的数量
其中查找WinPC的情况时,要做特殊处理,要过滤PrintData.TotalPrint的重复项,即PrinterId 对应WinPC后,PrintData.TotalPrint数据发送变化一次才能算为1,如果PrintData.TotalPrint数据重复,不列入计数范围





[此贴子已经被作者于2017-7-17 15:51编辑过]

4 回复
#2
kingorwei2017-07-17 13:36
回复 楼主 kingorwei
SELECT PrinterData.Id,PrinterData.PId,PrinterData.PrinterId,PrinterData.TotalPrint,PrinterInfo.Platform,PrinterInfo.[Time]
FROM PrinterData INNER JOIN PrinterInfo
ON PrinterData.PId = PrinterInfo.Id
AND(PrinterData.[Time] BETWEEN '2016-10-01 00:00:00' and '2017-06-30 23:59:59')
ORDER BY TIME
我做了上述的工作后,下一步就不知该如何做了
#3
mywisdom882017-07-17 14:21
if object_id('tempdb..#PrintData') is not null drop table #PrintData
create table #PrintData( Id int,PId varchar(10),PrinterModel varchar(20),PrinterId varchar(30),TonerId varchar(20),TotalPrint int,InkLevel int,Time datetime)
insert into #PrintData
select '5690285','10450042','M7218W','HP00093503HP06080838','1511MJ30001031','1705','30','2017/7/10 0:00' union all
select '5690286','10450043','LJ2208W','HP00058821HP0605','1605ML68004178','901','0','2017/7/10 0:00' union all
select '5690287','10450044','M7208W','HP00117725HP0702','1702ML68000816','203','0','2017/7/10 0:00' union all
select '5690288','10450045','M7208W','HP00149918HP0705','1705ML68006496','2','0','2017/7/10 0:00'

select * from #PrintData

if object_id('tempdb..#PrintInfo') is not null drop table #PrintInfo
create table #PrintInfo(Id int,DeviceCode varchar(60),Mobile varchar(20),Platform varchar(20),Version varchar(20),Time datetime,Location varchar(20))
insert into #PrintInfo
select '10450042','e83c4da6d5dcb11483150c21920617f72a302cfc','','iOS','0.2.9','2017/7/10 0:00','' union all
select '10450043','14:2D:27:46:5F:5F','','WinPC','1.2.4.6','2017/7/10 0:00','' union all
select '10450044','60:45:CB:73:01:53','','WinPC','1.2.4.6','2017/7/10 0:00','' union all
select '10450045','9C:D2:1E:84:B5:1D','','WinPC','1.2.4.6','2017/7/10 0:00','' union all
select '10450051','8.64618E+14','','Android','0.2.4','2017/7/10 0:01','' union all
select '10450052','AC:E0:10:55:59:CE','','WinPC','1.2.4.0','2017/7/10 0:02','' union all
select '10450053','8.6392E+14','','Android','0.2.3','2017/7/10 0:02','' union all
select '10450054','8.6392E+14','','Android','0.2.3','2017/7/10 0:02','' union all
select '10450057','6a14992b6b9a86f3e3a3d9bf1d5f213ebbc52c12','','iOS','0.2.9','2017/7/10 0:02','' union all
select '10450058','44:37:E6:35:0B:A7','','WinPC','1.2.4.7','2017/7/10 0:02',''

select * from #PrintInfo
只有本站会员才能查看附件,请 登录
#4
mywisdom882017-07-17 14:22
你表数据是不是这样?你想要的结果是什么
#5
kingorwei2017-07-17 15:46
回复 3楼 mywisdom88
谢谢答复,数据列出的是正确的,但是,我们不能新建表,也不能copy,我们只进行查询操作,并且数据量很大,差不多有1G,速度慢点不碍事,但要准确,上面只是其中很小的几行,我的要求如下:
1,PrintData中PrinterId是打印机的设备,找出PrinterId中在PrintInfo.Platform的对应情况,分为有如下几种情况
a,PrinterId仅仅单独分别对应Android、iOS、WinPC的数量(3种)
b,PrinterId对应WinPC +(Android 或iOS)的数量
c,PrinterId对应Android +iOS的数量
 其中查找WinPC的情况时,要做特殊处理,要过滤PrintData.TotalPrint的重复项,即PrinterId 对应WinPC后,PrintData.TotalPrint数据发送变化一次才能算为1,如果PrintData.TotalPrint数据重复,不列入计数范围

[此贴子已经被作者于2017-7-17 15:50编辑过]

1