我有以下查询:
#!/usr/bin/perl
# Code to get a web-based token that can be stored
# and used later to authorize our spreadsheet access.
# Based on code from https://gist.github.com/hexaddikt/6738162
#-------------------------------------------------------------------
# To use this code:
# 1. Edit the lines below to put in your own
# client_id and client_secret from Google.
# 2. Run this script and follow the directions on
# the screen, which will give step you
# through the following steps:
# 3. Copy the URL printed out, and paste
# the URL in a browser to load the page.
# 4. On the resulting page, click OK (possibly
# after being asked to log in to your Google
# account).
# 5. You will be redirected to a page that provides
# a code that you should copy and paste back into the
# terminal window, so this script can exchange it for
# an access token from Google, and store the token.
# That will be the the token the other spreadsheet access
# code can use.
use Net::Google::DataAPI::Auth::OAuth2;
use Net::Google::Spreadsheets;
use Storable; #to save and restore token for future use
use Term::Prompt;
# Provide the filename in which we will store the access
# token. This file will also need to be readable by the
# other script that accesses the spreadsheet and parses
# the contents.
my $session_filename = "stored_google_access.session";
# Code for accessing your Google account. The required client_id
# and client_secret can be found in your Google Developer's console
# page, as described in the detailed instruction document. This
# block of code will also need to appear in the other script that
# accesses the spreadsheet.
# Be sure to edit the lines below to fill in your correct client
# id and client secret!
my $oauth2 = Net::Google::DataAPI::Auth::OAuth2->new(
client_id => 'your_client_id.apps.googleusercontent.com',
client_secret => 'your_client_secret',
scope => ['http://spreadsheets.google.com/feeds/'],
redirect_uri => 'https://developers.google.com/oauthplayground',
);
# We need to set these parameters this way in order to ensure
# that we get not only an access token, but also a refresh token
# that can be used to update it as needed.
my $url = $oauth2->authorize_url(access_type => 'offline',
approval_prompt => 'force');
# Give the user instructions on what to do:
print <<END
The following URL can be used to obtain an access token from
Google.
1. Copy the URL and paste it into a browser.
2. You may be asked to log into your Google account if you
were not logged in already in that browser. If so, go
ahead and log in to whatever account you want to have
access to the Google doc.
3. On the next page, click "Accept" when asked to grant access.
4. You will then be redirected to a page with a box in the
left-hand column labeled "Authorization code".
Copy the code in that box and come back here.
Here is the URL to paste in your browser to get the code:
$url
END
;
# Here is where we get the code from the user:
my $code = prompt('x', 'Paste the code obtained at the above URL here: ', '', '');
# Exchange the code for an access token:
my $token = $oauth2->get_access_token($code) or die;
# If we get to here, it worked! Report success:
print "nToken obtained successfully!n";
print "Here are the token contents (just FYI):nn";
print $token->to_string, "n";
# Save the token for future use:
my $session = $token->session_freeze;
store($session, $session_filename);
print <<END2
Token successfully stored in file $session_filename.
Use that filename in your spreadsheet-access script to
load the token as needed for access to the spreadsheet data.
END2
;
use Net::Google::Spreadsheets;
use Net::Google::DataAPI::Auth::OAuth2;
use Net::OAuth2::AccessToken;
use Storable;
# Authentication code based on example from gist at
# https://gist.github.com/hexaddikt/6738247
# Get the token that we saved previously in order to authenticate:
my $session_filename = "stored_google_access.session";
# Be sure to edit the lines below to fill in your correct client
# id and client secret!
my $oauth2 = Net::Google::DataAPI::Auth::OAuth2->new(
client_id => 'your_client_id.apps.googleusercontent.com',
client_secret => 'your_client_secret',
scope => ['http://spreadsheets.google.com/feeds/'],
redirect_uri => 'https://developers.google.com/oauthplayground',
);
# Deserialize the file so we can thaw the session and reuse the refresh token
my $session = retrieve($sessionfile);
my $restored_token = Net::OAuth2::AccessToken->session_thaw($session,
auto_refresh => 1,
profile => $oauth2->oauth2_webserver,
);
$oauth2->access_token($restored_token);
# Now we can use this token to access the spreadsheets
# in our account:
my $service = Net::Google::Spreadsheets->new(
auth => $oauth2);
其中SELECT d.currentDate, t.ConfigId, t.value as value
FROM #Dates AS d
OUTER APPLY
( SELECT t.value as value, t.FK_ShipDataSubsystemConfigId
FROM myTable AS t
WHERE t.[Timestamp] <= d.currentDate and t.ConfigId in (' + @IDList + ')
and t.FK_DataSystemId <> 1
) AS t
GROUP BY d.currentDate, t.ConfigId
)
只是一个包含大量日期时间的表,我用它来确保按照我需要的时间间隔获取数据。
我的问题在于Dates
子句。查询不能正常工作,因为group by
不在value
或聚合函数中。我尝试按group by
进行分组以使错误消失,但它只是给了我选择的区间中的每个日期与每个值匹配 - 而不是我想要的。对于每个日期/ ConfigId配对,我最终应该有一行表。
当我从value
移除值并获取日期和select
时,它可以正常工作。我得到了我应该获得的确切行数。
我所拉的表看起来像这样:
ConfigId
我期待得到回复:
PK_DataId Timestamp value ConfigId
1 1/1/2015 12:00 234 5
2 1/1/2015 12:01 456 4
每15分钟我有一个configId / date对的值。当我添加max(value)时,我每次只获得一个值而不是不同的值。当我按值分组时,我获得了数百万行,看起来每个时间戳与任何其他时间戳中的每个值匹配时,我得到一行。我真的不明白发生了什么。
如何在选择Timestamp value ConfigId
1/1/2015 12:00:00 234 5
1/1/2015 12:00:00 456 4
的同时获得这些结果?
答案 0 :(得分:1)
如果我理解正确,您只需要top 1
中的apply
:
SELECT d.currentDate, t.ConfigId, t.value as value
FROM #Dates d OUTER APPLY
(SELECT TOP 1 t.value as value, t.FK_ShipDataSubsystemConfigId
FROM myTable AS t
WHERE t.[Timestamp] <= d.currentDate and
t.ConfigId in (' + @IDList + ') and
t.FK_DataSystemId <> 1
ORDER BY t.[Timestamp] DESC
) t ;
编辑:
如果每个config
和时间需要一行:
SELECT d.currentDate, t.ConfigId, t.value as value
FROM #Dates d CROSS JOIN
(SELECT DISTINCT FK_ShipDataSubsystemConfigId
FROM myTable
WHERE t.ConfigId in (' + @IDList + ')
) c OUTER APPLY
(SELECT TOP 1 t.value as value, t.FK_ShipDataSubsystemConfigId
FROM myTable AS t
WHERE t.[Timestamp] <= d.currentDate and
t.ConfigId = c.FK_ShipDataSubsystemConfigId and
t.FK_DataSystemId <> 1
ORDER BY t.[Timestamp] DESC
) t ;
基本相同的想法,但这需要生成所有配置的所有行以及outer apply
之前的日期。
答案 1 :(得分:0)
没有真正的数据真的很难理解你的问题。
我猜你不想要像Aggregeted这样的功能。
SELECT d.currentDate, t.ConfigId,
MAX(t.value) as maxvalue,
MIN(t.value) as minvalue,
COUNT(t.value) as totalvalue
所以我和你一起寻找
simulating-group-concat-mysql-function-in-sql-server
ANDY | A100
ANDY | B391
ANDY | X010
TOM | A100
TOM | A510
返回:
ANDY | A100 / B391 / X010
TOM | A100 / A510