Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Get data from 2 tables
Message
 
 
To
08/02/2019 19:48:52
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows Server 2008 R2
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Desktop
Miscellaneous
Thread ID:
01666006
Message ID:
01666212
Views:
55
I see and also from the combined result, right?

If so, then:
;with cteAllData as (select [Date], datepart(hour, [Date]) as [Hour], Reading from table1
UNION ALL 
select [Date], datepart(hour, [Date]) as [Hour], Reading from table2), 
cteNumbered as (select *, row_number() over (partition by [Date] Order by [Hour]) as Rn from cteAllData)

select [Date], [Hour], Reading from cteNumbered where Rn = 1
----------------------
The above can be converted into the view as
create view HourlyReadings as 
with cteAllData as (select [Date], datepart(hour, [Date]) as [Hour], Reading from table1
UNION ALL 
select [Date], datepart(hour, [Date]) as [Hour], Reading from table2), 
cteNumbered as (select *, row_number() over (partition by [Date] Order by [Hour]) as Rn from cteAllData)

select [Date], [Hour], Reading from cteNumbered where Rn = 1
I didn't test, wrote from the top of my head, hopefully it'll work.




>>Can you please start from the beginning and explain the problem you're trying to solve? Show some samples of the data as well.
>>
>>Why you're joining based on the hour? If that's the requirement, may be you need to store hour directly in addition to the date? Or have a computed column?
>>
>
>Dear Professor Naomi
>
>I am describing again
>
>I have this data
>
>
>CREATE CURSOR table1(date t,reading n(4))
>INSERT INTO table1 values({^2019-02-09 11:19:21 AM},	1100)
>INSERT INTO table1 values({^2019-02-09 11:25:38 AM},	1105)
>INSERT INTO table1 values({^2019-02-09 12:42:07 PM},	1110)
>
>
>CREATE CURSOR table2(date t,reading n(4))
>INSERT INTO table2 values({^2019-02-08 01:46:27 AM},	2480)
>INSERT INTO table2 values({^2019-02-08 01:55:58 AM},	2485)
>INSERT INTO table2 values({^2019-02-08 02:12:33 AM},	2490)
>INSERT INTO table2 values({^2019-02-09 03:43:29 AM},	2495)
>INSERT INTO table2 values({^2019-02-09 03:50:18 AM},	2500)
>INSERT INTO table2 values({^2019-02-09 04:12:21 AM},	2505)
>INSERT INTO table2 values({^2019-02-09 04:25:38 AM},	2510)
>INSERT INTO table2 values({^2019-02-09 05:37:29 AM},	2515)
>
>
>
>
>I want to get the data of only first row of every hour of current date (2019-02-09) like shown in red font
>
>
>
>It will be great kindness if I get a view like this
>
>
>
>(I need data only for current date hour wise, no previous date)
>
>Thanks
If it's not broken, fix it until it is.


My Blog
Previous
Reply
Map
View

Click here to load this message in the networking platform