Assuming you are using SQL Server, you could adapt this to work:
CREATE TABLE [dbo].[IP2Location](
[ID] [int] NOT NULL CONSTRAINT PK_IP2Location PRIMARY KEY CLUSTERED IDENTITY(1,1) ,
[ip] [bigint] NULL,
[country_code] [varchar](2) NULL,
[country_name] [varchar](255) NULL,
[region_name] [varchar](255) NULL,
[city_name] [varchar](255) NULL,
[latitude] [float] NULL,
[longitude] [float] NULL,
[zip_code] [varchar](255) NULL,
[time_zone] [varchar](255) NULL,
[isp] [varchar](255) NULL,
[domain] [varchar](255) NULL,
[net_speed] [varchar](255) NULL,
[idd_code] [varchar](255) NULL,
[area_code] [varchar](255) NULL,
[IP2LocationTimeStamp] [timestamp] NOT NULL,
);
/*
Create a non-clustered index on the numeric IP address to speed lookups
I indexed the country_code field here as well since our lookups use it.
I INCLUDED country_name, region_name, and time_zone since that is what we
typically request in a SELECT statement from this table.
*/
CREATE NONCLUSTERED INDEX [IX_IP2Location_IP2LocationServiceQuery] ON [dbo].[IP2Location]
(
[ip] ASC,
[country_code] ASC
)
INCLUDE ( [country_name],
[region_name],
[time_zone])
GO
/*
Create BIGINT versions of the dotted-ip field ip_address
*/
UPDATE IP2Location SET IP =
(CAST(substring(ip_address,1,charindex('.',ip_address)-1) AS BIGINT) * 0x01000000)
+ (CAST(substring(ip_address, charindex('.',ip_address,charindex('.',ip_address))+1, (charindex('.', ip_address, charindex('.', ip_address, charindex('.',ip_address))+1)-charindex('.',ip_address,charindex('.',ip_address)))-1) AS BIGINT) * 0x00010000)
+ (CAST(substring(ip_address, charindex('.',ip_address,charindex('.',ip_address,charindex('.',ip_address))+1)+1, charindex('.', ip_address, charindex('.',ip_address,charindex('.',ip_address,charindex('.',ip_address))+1)+1) - (charindex('.',ip_address,charindex('.',ip_address,charindex('.',ip_address))+1)+1)) as BIGINT) * 0x00000100)
+ (CAST(substring(ip_address, charindex('.',ip_address,charindex('.',ip_address,charindex('.',ip_address,charindex('.',ip_address)+1))+1)+1, len(ip_address) - (charindex('.',ip_address,charindex('.',ip_address,charindex('.',ip_address,charindex('.',ip_address)+1))+1))) as BIGINT)
);
/*
Function to return a TVF containing location info about the given numeric IP address
For example:
SELECT country_code, country_name, region_name, city_name, latitude, longitude, time_zone
FROM GetLocationFromIPv4(16777215);
*/
CREATE FUNCTION [dbo].[GetLocationFromIPv4]
(
@IP bigint
)
RETURNS @IPLoc TABLE
(
country_code varchar(2)
, country_name varchar(255)
, region_name varchar(255)
, city_name varchar(255)
, latitude float
, longitude float
, time_zone varchar(255)
, isp varchar(255)
, domain varchar(255)
, net_speed varchar(255)
, idd_code varchar(255)
, area_code varchar(255)
)
AS
BEGIN
/*
TVF to return (typically) a single record from IP2Location as quickly as possible
*/
INSERT INTO @IPLoc
SELECT country_code
, country_name
, region_name
, city_name
, latitude
, longitude
, time_zone
, isp
, domain
, net_speed
, idd_code
, area_code
FROM IP2Location
WHERE ip = @IP;
RETURN
END
GO
/*
Function to return a TVF containing location info about the given dotted-decimal IP address
For example:
SELECT country_code, country_name, region_name, city_name, latitude, longitude, time_zone
FROM GetLocationFromIPv4('192.168.0.1');
*/
CREATE FUNCTION [dbo].[GetLocationFromIPv4String]
(
@IP nvarchar(15)
)
RETURNS @IPLoc TABLE
(
country_code varchar(2)
, country_name varchar(255)
, region_name varchar(255)
, city_name varchar(255)
, latitude float
, longitude float
, time_zone varchar(255)
, isp varchar(255)
, domain varchar(255)
, net_speed varchar(255)
, idd_code varchar(255)
, area_code varchar(255)
)
AS
BEGIN
/*
TVF to return (typically) a single record from IP2Location as quickly as possible
*/
DECLARE @IPInt64 bigint;
DECLARE @n1 bigint;
DECLARE @n2 bigint;
DECLARE @n3 bigint;
DECLARE @n4 bigint;
set @n1 = substring(@IP
,1
,charindex('.',@IP)-1
);
set @n2 = substring(
@IP
, charindex('.',@IP,charindex('.',@IP))+1
, (charindex('.', @IP, charindex('.', @IP, charindex('.',@IP))+1)-charindex('.',@IP,charindex('.',@IP)))-1
);
set @n3 = substring(
@IP
, charindex('.',@IP,charindex('.',@IP,charindex('.',@IP))+1)+1
, charindex('.', @IP, charindex('.',@IP,charindex('.',@IP,charindex('.',@IP))+1)+1) - (charindex('.',@IP,charindex('.',@IP,charindex('.',@IP))+1)+1)
);
set @n4 = substring(
@IP
, charindex('.',@IP,charindex('.',@IP,charindex('.',@IP,charindex('.',@IP)+1))+1)+1
, len(@IP) - (charindex('.',@IP,charindex('.',@IP,charindex('.',@IP,charindex('.',@IP)+1))+1))
);
SET @IPInt64 = (@n1 * 0x01000000) + (@n2 * 0x00010000) + (@n3 * 0x00000100) + @n4;
INSERT INTO @IPLoc
SELECT TOP(1)country_code
, country_name
, region_name
, city_name
, latitude
, longitude
, time_zone
, isp
, domain
, net_speed
, idd_code
, area_code
FROM IP2Location
WHERE ip = @IPInt64;
RETURN
END
GO
Best Answer
I think this could help you.
I create this structure:
I fill the tables with the information you post on the screenshot (Only the
RTS
information is from2015-06-01
to2015-06-09
).Employees
:Domains
:Projects
:ProjectTypes
:RTS
:Query
:Result
:The only problem with this is that you have to put the week date parameters you want to calculate. You can make a
Stored Procedure
that could readeStartDate
andeEndDate
variables and calculate the weeks between those dates, and all you have to do is make aDynamic Query
that concatenate every week between the parameters.Example
:Between
2015-06-01
and2015-06-31
exists 4 weeks monday-friday, you'll need to concatenate 4 columns with those dates. I could help you but I dont know if my post if what are you looking for, if it is, let me know.