如何在SQL Server中使用循环来通过调用no来获取值。和国家 [英] How to use loop in SQL server to get values through called no. And country

查看:120
本文介绍了如何在SQL Server中使用循环来通过调用no来获取值。和国家的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个表包含国家/地区代码,下表包含通话详细信息,我想通过使用被叫号码显示国家/地区名称和总通话费率。从第二个表开始。

首先,它会从被叫的子串中输入2个零,然后它将遍历国家/地区表并找到被叫国家的国家名称,并且将与第二个*通话率相乘以获得总通话费用。 PLZ指导我



致电详细信息表

OrigParty1 calledNo。 DateOfcall秒

4011 0024991229 2016年3月31日289

4160 0020111453552 2016年4月19日137

4120 0020115511222 2016年4月10日8

4120 00209792006 2016年4月10日9

4303 0094776071370 2016年4月10日180

4120 00209792006 2016年4月10日12

4120 002097920006 2016年4月10日30

4303 0094776071370 2016年4月10日168

4120 002097920006 2016年4月10日132

4120 00201274500007 2016年4月10日147



国家表

ID国家CountryCode RatePerMin

248尼泊尔97 3.2

1阿富汗93 4

2阿尔巴尼亚355 3.5

3阿尔及利亚213 2.2

4美属萨摩亚1684 2

5安道尔376 4.2

6安哥拉244 4.2

7安圭拉1264 4.5

8南极洲/诺福克岛672 5

9安提瓜和巴布达1268 4.5



我尝试了什么:



选择OrigParty1,DestParty1,SUBSTRING(DateTimeConnect,21,16)作为DateOfcall,

DATEDIFF(第二,CAST(x AS DATETIME),CAST (y DATETIME))AS [秒],

呼叫目的地=

CASE

当SUBSTRING时(DestParty1,0,6) = 00249然后'苏丹'

当SUBSTRING(DestParty1,0,6)= 00974然后'卡塔尔'

当SUBSTRING(DestParty1,0,5)= 0020那么'埃及'

ELSE'未知'

结束

来自Tbl_Dummy

i have 2 tables on contains Country code and next table contains call details, i wants to display 'country name' and 'total call rate' by using called no. from second table.
First it will substring 2 zeros from called no then it will loop through country table and find called country then its country name and and will multiply with second*call rate to get total call cost. plz guide me

Call Details table
OrigParty1 calledNo. DateOfcall seconds
4011 0024991229 Mar 31 2016 289
4160 0020111453552 Apr 19 2016 137
4120 0020115511222 Apr 10 2016 8
4120 00209792006 Apr 10 2016 9
4303 0094776071370 Apr 10 2016 180
4120 00209792006 Apr 10 2016 12
4120 002097920006 Apr 10 2016 30
4303 0094776071370 Apr 10 2016 168
4120 002097920006 Apr 10 2016 132
4120 00201274500007 Apr 10 2016 147

Country Table
ID Country CountryCode RatePerMin
248 Nepal 97 3.2
1 Afghanistan 93 4
2 Albania 355 3.5
3 Algeria 213 2.2
4 American Samoa 1684 2
5 Andorra 376 4.2
6 Angola 244 4.2
7 Anguilla 1264 4.5
8 Antarctica/Norfolk Island 672 5
9 Antigua and Barbuda 1268 4.5

What I have tried:

SELECT OrigParty1,DestParty1,SUBSTRING( DateTimeConnect ,21 , 16 ) as DateOfcall,
DATEDIFF(second, CAST(x AS DATETIME), CAST(y AS DATETIME)) AS [seconds] ,
"Call Destination" =
CASE
WHEN SUBSTRING(DestParty1 ,0 , 6 ) = 00249 THEN 'Sudan'
WHEN SUBSTRING(DestParty1 ,0 , 6 ) = 00974 THEN 'Qatar'
WHEN SUBSTRING(DestParty1 ,0 , 5 ) = 0020 THEN 'Egypt'
ELSE 'Unknown'
END
FROM Tbl_Dummy

推荐答案

你的尝试有问题​​ - 被叫号码必须是一个varchar列,所以你可以使用SUBSTRING,但你没有用引号包围数字。



我'我们假设以下数据模式:

You have a problem with your attempt - the called Number must be a varchar column so you can use SUBSTRING but you haven't surrounded the number with quotes.

I've assumed the following data schemas:
create table [Call Details]
(
	OrigParty1 bigint,
	calledNo varchar(50),
	DateOfCall Date,
	seconds int
)
create table Country 
(
	id int,
	Country varchar(30),
	CountryCode int, 
	RatePerMin decimal(15,2)
)





此查询将获取每个详细信息致电:



And this query will get the details for each call:

select OrigParty1, CalledNo, Country, seconds * RatePerMin as TotalCost 
from [Call Details] CD
LEFT OUTER JOIN Country C 
   ON CD.calledNo LIKE '00' + CAST(C.CountryCode AS varchar) + '%'



注意会有问题,如果有的话2位数代码以与3位代码相同的两位数字开头。我不认为这可能是真正的国家代码的情况,它只是因为我使用了补充数据而变得明显。



- 我注意到了要拨打电话号码的另一个问题,您应首先使用国际拨号代码,然后是国家代码,后跟号码。我在这里介绍的解决方案不能处理这种情况 - 你需要用适当的代码替换硬编码的'00'


Note there will be an issue if any of the 2 digit codes start with the same two digits as a 3-digit code. I don't think this can be the case with genuine country codes, it only became apparent because I was using made up data.

- I have noticed another issue in that to call a number you should first use the international dialling code, followed by the country code followed by the number. The solution I've presented here doesn't handle that situation - you would need to replace the hard-coded '00' with the appropriate code


这篇关于如何在SQL Server中使用循环来通过调用no来获取值。和国家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆