马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
MySQL的低成本来自于其简单性吗?它的普及性是由于其低成本吗?其实,在MySQL的最“好”与最“不好”的功能之间没有明显的分界线,但它们组合在一起就形成了一副让我们欣赏的作品。分组|统计陈优章的专栏
(原创,到如今为至最为庞大的SQL查询代码)完成按部门月卡余额总额分组统计的SQL查询代码(在MsSQLServer中调试经由过程)
SELECTdp.dpname1AS部门,cust_dp_SumOddfre.sum_oddfareAS当月卡总余额
FROM(SELECTT_Department.DpCode1,SUM(custid_SumOddfare_group.sum_oddfare)
ASsum_oddfare
FROM(SELECTl2.CustomerID,SUM(r1.oddfare)ASsum_oddfare
FROM(SELECTCustomerID,MAX(OpCount)ASmax_opcount
FROM(SELECTCustomerID,OpCount,RTRIM(CAST(YEAR(OpDt)
ASchar))+-+RTRIM(CAST(MONTH(OpDt)ASchar))
+-+RTRIM(DAY(0))ASdt
FROMT_ConsumeRec
UNION
SELECTCustomerID,OpCount,RTRIM(CAST(YEAR(cashDt)
ASchar))+-+RTRIM(CAST(MONTH(cashDt)ASchar))
+-+RTRIM(DAY(0))ASdt
FROMT_Cashrec)l1
WHERE(dt<=2005-6-1)/*输出查询月份,可用参数传送*/
GROUPBYCustomerID)l2INNERJOIN
(SELECTCustomerID,OpCount,oddfare
FROMT_ConsumeRec
UNION
SELECTCustomerID,OpCount,oddfare
FROMT_Cashrec)r1ONl2.CustomerID=r1.CustomerIDAND
r1.OpCount=l2.max_opcount
GROUPBYl2.CustomerID)custid_SumOddfare_groupINNERJOIN
T_CustomersON
custid_SumOddfare_group.CustomerID=T_Customers.CustomerIDINNERJOIN
T_DepartmentONSUBSTRING(T_Customers.Account,1,2)
=T_Department.DpCode1ANDSUBSTRING(T_Customers.Account,3,2)
=T_Department.DpCode2ANDSUBSTRING(T_Customers.Account,5,3)
=T_Department.DpCode3
GROUPBYDpCode1)cust_dp_SumOddfreINNERJOIN
(SELECTDISTINCTdpcode1,dpname1
FROMt_department)dpONdp.dpcode1=cust_dp_SumOddfre.DpCode1
附:查询用到的基础表构成剧本:
CREATETABLE[dbo].[T_CashRec](--出纳明细帐本
[StatID][tinyint]NOTNULL,
[CashID][smallint]NOTNULL,
[Port][tinyint]NOTNULL,
[Term][tinyint]NOTNULL,
[CashDt][datetime]NOTNULL,--存存款工夫
[CollectDt][datetime]NOTNULL,
[CustomerID][int]NOTNULL,
[OpCount][int]NOTNULL,--某卡的操纵次数,只累加
[InFare][money]NOTNULL,
[OutFare][money]NOTNULL,
[SumFare][money]NOTNULL,
[OddFare][money]NOTNULL,--此次操纵后该卡的余额
[MngFare][money]NOTNULL,
[Hz][tinyint]NOTNULL,
[CurSum][smallmoney]NULL,
[CurCount][smallint]NULL,
[CardSN][tinyint]NULL
)ON[PRIMARY]
GO
CREATETABLE[dbo].[T_ConsumeRec](--消耗明细帐本
[StatID][tinyint]NOTNULL,
[Port][tinyint]NOTNULL,
[Term][tinyint]NOTNULL,
[CustomerID][int]NOTNULL,
[OpCount][int]NOTNULL,--某卡的操纵次数,只累加
[OpDt][datetime]NOTNULL,--消耗工夫
[CollectDt][datetime]NOTNULL,
[MealID][tinyint]NOTNULL,
[SumFare][smallmoney]NOTNULL,
[OddFare][smallmoney]NOTNULL,--此次操纵后该卡的余额
[MngFare][smallmoney]NOTNULL,
[OpFare][smallmoney]NOTNULL,
[Hz][tinyint]NOTNULL,
[MenuID][smallint]NULL,
[MenuNum][tinyint]NULL,
[OddFarePre][smallmoney]NULL,
[RecNo][smallint]NULL,
[CardSN][tinyint]NOTNULL,
[CardVer][tinyint]NULL
)ON[PRIMARY]
GO
CREATETABLE[dbo].[T_Customers](--客户帐本
[CustomerID][int]NOTNULL,--客户号,主键
[StatCode][varchar](3)COLLATEChinese_PRC_CI_ASNOTNULL,
[Account][varchar](7)COLLATEChinese_PRC_CI_ASNOTNULL,--单元代号
[Name][varchar](12)COLLATEChinese_PRC_CI_ASNOTNULL,
[CardNo][int]NOTNULL,
[CardSN][tinyint]NULL,
[CardType][tinyint]NOTNULL,
[Status][tinyint]NOTNULL,
[OpenDt][datetime]NOTNULL,
[CashID][smallint]NOTNULL,
[SumFare][smallmoney]NOTNULL,
[ConsumeFare][smallmoney]NOTNULL,
[OddFare][smallmoney]NOTNULL,
[OpCount][int]NOTNULL,
[CurSubsidyFare][smallmoney]NOTNULL,
[SubsidyDT][datetime]NOTNULL,
[SubsidyOut][char](1)COLLATEChinese_PRC_CI_ASNOTNULL,
[Alias][varchar](10)COLLATEChinese_PRC_CI_ASNULL,
[outid][varchar](20)COLLATEChinese_PRC_CI_ASNULL,
[UpdateID][tinyint]NOTNULL,
[Pwd][char](4)COLLATEChinese_PRC_CI_ASNULL,
[QuChargFare][smallmoney]NULL,
[HasTaken][tinyint]NULL,
[DragonCardNo][char](19)COLLATEChinese_PRC_CI_ASNULL,
[ApplyCharg][smallmoney]NULL,
[ChargPer][smallmoney]NULL,
[MingZu][varchar](20)COLLATEChinese_PRC_CI_ASNULL,
[Sex][char](2)COLLATEChinese_PRC_CI_ASNULL,
[Memo][varchar](100)COLLATEChinese_PRC_CI_ASNULL,
[WeiPeiDW][varchar](10)COLLATEChinese_PRC_CI_ASNULL,
[CardConsumeType][tinyint]NULL,
[LeaveSchoolDT][datetime]NULL,
[UseValidDT][tinyint]NOTNULL,
[NoUseDate][datetime]NOTNULL
)ON[PRIMARY]
GO
CREATETABLE[dbo].[T_Department](--单元账本,三级单元制,树型布局
[DpCode1][char](2)COLLATEChinese_PRC_CI_ASNOTNULL,
[DpCode2][char](2)COLLATEChinese_PRC_CI_ASNULL,
[DpCode3][char](3)COLLATEChinese_PRC_CI_ASNULL,
[DpName1][varchar](30)COLLATEChinese_PRC_CI_ASNULL,
[DpName2][varchar](30)COLLATEChinese_PRC_CI_ASNULL,
[DpName3][varchar](30)COLLATEChinese_PRC_CI_ASNULL,
[N_SR][int]NOTNULL,
[BatNum][smallint]NULL
)ON[PRIMARY]
GO
有的时候,一些缺失的功能可以通过别的办法来实现,例如,在MySQL4.1以前,你可以通过使用join方法来替代子查询的功能。在MySQL5.0中,大多数关系型数据库所要求的功能已经都具备。 |