SQL Get minimum hour from multiple datetimes registers - sql-server

I need display INFO column, having the minimum hour for each date in REGISTRATION column, one for LOG
Log CAT INFO REGISTRATION
10 1 551203 2018-06-04 08:47:54.000
10 1 551549 2018-06-05 08:59:02.000
579 1 551675 2018-06-05 10:13:36.000
579 1 553681 2018-06-05 11:31:44.000
579 1 551707 2018-06-05 12:57:33.000
579 1 551364 2018-06-04 10:16:04.000
579 1 551378 2018-06-04 10:39:01.000
579 1 551379 2018-06-04 10:40:22.000
579 1 551406 2018-06-04 15:47:52.000
580 1 550922 2018-06-04 11:21:01.000
580 1 551001 2018-06-04 12:43:22.000
580 1 553321 2018-06-04 15:37:52.000
exactly this, where each INFO are the minimum hour of each date, of each LOG
INFO
551203 -->(2018-06-04 08:47:54.000)
551675 -->(2018-06-05 10:13:36.000)
551364 -->(2018-06-04 10:16:04.000)
550922 -->(2018-06-04 11:21:01.000)
thanks!!

Assuming that info values appear in increasing order then I believe this is what you're looking for:
select min(info) as info, min(registration) as registration
from log
group by log, cast(registration as date);
Or just use row_number() to avoid making that assumption:
with data as (
select *,
row_number() over
(partition by log, cast(registration as date) order by registration) as rn
from log
)
select * from data where rn = 1;

Related

Do Loops (with multiple rows of id's) with conditional statements?

Please see my data below;
data finance;
input id loan1 loan2 loan3 assets home$ type;
datalines;
1 93000 98000 45666 new 1
1 98000 45678 98765 67 old 2
1 55000 56764 435371 54 new 1
2 7000 6000 7547 57 new 1
4 67333 87444 98666 34 old 1
4 98000 68777 986465 23 new 1
5 4555 334 652 12 new 1
5 78999 98999 80000 34 new 1
5 889 989 676 3 new 1
;
data finance1;
set finance;
if loan1<80000 then conc'level1';
if loan2 <80000 and home='new' then borrowcap = 'high';
run;
I would like the following dataset, as you can see although there are multiple rows for each ID initially, if there was a level1 or high in any of those rows, I would like to capture that in the same row.
data finance;
input id conc$ borrowcap$;
datalines;
1 level1 high
2 level1 high
4 level1
5 level1 high
;
Any help is appreciated!
Use retain statement, you can keep value from any row for each ID. Use by statement + if last.var statement, you can keep only one row for each ID.
data finance;
input id loan1 loan2 loan3 assets home$ type;
datalines;
1 93000 98000 45666 . new 1
1 98000 45678 98765 67 old 2
1 55000 56764 435371 54 new 1
2 7000 6000 7547 57 new 1
4 67333 87444 98666 34 old 1
4 98000 68777 986465 23 new 1
5 4555 334 652 12 new 1
5 78999 98999 80000 34 new 1
5 889 989 676 3 new 1
;
data finance1;
set finance;
by id;
retain conc borrowcap;
length conc borrowcap $8.;
if first.id then call missing(conc,borrowcap);
if loan1<80000 then conc='level1';
if loan2<80000 and home='new' then borrowcap = 'high';
if last.id;
run;

merging multiple data frames keeping the date sort

In R, I have 4 data frames with different dates and PL values:
head(Array1) gives:
Dates P&L
1 2014-10-01 900
2 2014-10-02 -3185
3 2014-10-03 3800
4 2014-10-07 -2300
5 2014-10-08 2100
6 2014-10-09 2400
head(array2) gives:
Dates P&L
1 2015-03-02 -6962.5
2 2015-03-03 -14237.5
3 2015-03-04 7862.5
4 2015-03-05 925.0
5 2015-03-09 -3725.0
6 2015-03-10 262.5
head(array3) gives:
Dates P&L
1 2014-10-08 7160
2 2014-10-09 7600
3 2014-10-10 2260
4 2014-10-13 4820
5 2014-10-15 -1500
6 2014-11-06 3030
head(array4) gives:
Dates P&L
1 2015-02-24 1245
2 2015-03-06 10650
3 2015-03-10 -200
4 2015-04-17 -9690
5 2015-05-15 -28740
6 2015-05-26 3970
I would like to aggregate all these arrays in just one array, keeping the date sort and summing when there are multiple values for one date. Can someone please help me? Joe
One option is to rbind all the data frames into a single data frame, then aggregate the values against Dates:
agg <- aggregate(`P&L` ~ Dates, rbind(array1, array2, array3, array4), FUN = sum)
agg[order(as.Date(agg$Dates)),]
# Dates P&L
#1 2014-10-01 900.0
#2 2014-10-02 -3185.0
#3 2014-10-03 3800.0
#4 2014-10-07 -2300.0
#5 2014-10-08 9260.0
#6 2014-10-09 10000.0
# ...
Or put the four arrays in a list, use do.call(rbind, ... to bind the data frames together:
lst <- list(array1, array2, array3, array4)
agg <- aggregate(`P&L` ~ Dates, do.call(rbind, lst), FUN = sum)
agg[order(as.Date(agg$Dates)),]

Max value of collect_list(column) in Hive

I am using below command in Hive. and getting correct result.
select acct_id,collect_list(expr_dt) from experiences
> group by acct_id;
Output:
900 ["2015-03-31"]
707 ["2015-03-31","2014-12-10"]
903 ["2015-03-31"]
-435 ["2015-03-31"]
718 ["2015-03-31","2014-06-03"]
I want to get the max date for each account.
When I am trying execute below query I am getting error.
select acct_id,max(collect_list(expr_dt)) from experiences
> group by acct_id;
and the error is -
SemanticException [Error 10128]: Line 1:19 Not yet supported place for
UDAF 'collect_list'
I want to do total operation in a single query.
You can go with max without collect_list if your goal is to only find out max expr_dt for each acct_id group
input:
hive> select * from experiences;
OK
900 2015-03-31
707 2015-03-31
707 2014-12-10
903 2015-03-31
-435 2015-03-31
718 2015-03-31
718 2014-06-03
query:
hive> select acct_id,max(expr_dt) from experiences group by acct_id;
output:
Total MapReduce CPU Time Spent: 4 seconds 30 msec
OK
-435 2015-03-31
707 2015-03-31
718 2015-03-31
900 2015-03-31
903 2015-03-31

Convert varchar to date in SQL Server 2008

I have two different table for date and time in one application. In date table "date" is stored in "datetime" format and in time table "time" part is stored in varchar format.
Both the table id is stored in transaction table for date time value.
I have an issue while querying the database specifying particular datetime value from transaction table.
Date table
ID_DAT DATE_DAT (smalldatetime)
20000101 01/01/2000 0:00
20000102 02/01/2000 0:00
20000103 03/01/2000 0:00
20000104 04/01/2000 0:00
20000105 05/01/2000 0:00
20000106 06/01/2000 0:00
20000107 07/01/2000 0:00
20000108 08/01/2000 0:00
20000109 09/01/2000 0:00
20000110 10/01/2000 0:00
Time Table
ID_TIM HOUR_TIM MINUTE_TIM STRING_TIM (varchar)
0 0 0 00:00
1 0 1 00:01
2 0 2 00:02
3 0 3 00:03
4 0 4 00:04
5 0 5 00:05
6 0 6 00:06
7 0 7 00:07
8 0 8 00:08
9 0 9 00:09
10 0 10 00:10
Transaction data sample (id may not match with master provided)
SEQNUM ID_DAT ID_TIM ORIGINAL_VALUE_PER
2495089 20130424 30 10.0000000000
2495089 20130424 60 12.0000000000
2495089 20130424 90 15.0000000000
2495089 20130424 120 20.0000000000
2495089 20130424 150 24.0000000000
2495089 20130424 180 28.0000000000
2495089 20130424 210 34.0000000000
now I want to query transaction data let's say after 03:30 for the particular day.
Please guide me how can i achieve the same.
Thanks
Something vaguely like this:
SELECT * from TRANSACTION_TABLE TT
join DATE_TABLE DT on TT.ID_DAT = DT.ID_DAT
join TIME_TABLE TM on TT.ID_TIM = TM.ID_TIM
where (DT.DATE_DAT > '10/19/2013' and DT.DATE_DAT < '10/21/2013')
or (DT.DATE_DAT = '10/21/2013' and HOUR_TIM < 3 and MINUT_TIME < 30)
or (DT.DATE_DAT = '10/19/2013' and HOUR_TIM >= 3 and MINUT_TIME >= 30)

Formatting link lists using TSQL

Shog9 keeps on making my link lists look awesome.
Essentially, I write a bunch of queries that pull out results from the Stackoverflow data dump. However, my link lists look very ugly and are hard to understand.
Using some formatting magic Shog9 manages to make the link lists look a lot nicer.
So, for example, I will write a query that returns the following:
question id,title,user id, other info
4,When setting a form’s opacity should I use a decimal or double?,8,Eggs McLaren, some other stuff lots of text
And I want it to paste it into an answer on meta and make it look like this:
Question Id User Name Other Info
When setting a form’s opacity... Eggs Mclaren Some other stuff...
So assuming my starting point is the query that returns the start info.
What are the least amount of steps I can run in query analyser to turn the results into:
<h3> Question Id User Name Other Info </h3>
<pre>
When setting a form’s opacity... Eggs Mclaren Some other stuff...
</pre>
My initial thoughts are to insert the results into a temp table and then run a stored proc that will iron the data into my desired structure. Run the proc, cut and paste and be done.
Any candidate TSQL based solutions to this problem?
EDIT: Accepting my answer, its the only solution with an implementation.
Not sure of your exact requirements, but have you considered selecting the data as XML and then applying an XSLT transform to the results?
I'll update this post with my progress as I refine my proc:
Example:
select top 20
UserId = u.Id,
UserName = u.DisplayName,
u.Reputation,
sum(case when p.ParentId is null then 1 else 0 end) as Questions,
sum(case when p.ParentId is not null then 1 else 0 end) as Answers
into #t
from Users u
join Posts p on p.OwnerUserId = u.Id
where p.CommunityOwnedDate is null and p.ClosedDate is null
group by u.Id, u.DisplayName, u.Reputation
having sum(case when p.ParentId is not null then 1 else 0 end) < sum(case when p.ParentId is null then 1 else 0 end) / 6
order by Reputation desc
exec spShog9
Results:
User Reputation
Questions Answers
Edward Tanguay 8317 465 24
me 5767 311 29
Joan Venge 4844 226 14
Blankman 4546 310 1
acidzombie24 4359 371 32
Thanks 4350 416 21
Masi 4193 555 74
LazyBoy 3230 94 12
KingNestor 3187 92 11
Nick 2084 79 6
George2 1973 263 1
Xaisoft 1944 174 12
John 1929 160 24
danmine 1901 53 3
zsharp 1771 145 16
carrier 1742 56 8
JC Grubbs 1550 50 5
vg1890 1534 56 2
Coocoo4Cocoa 1514 143 0
Keand64 1513 83 5
Masi 4193 555 74
LazyBoy 3230 94 12
KingNestor 3187 92 11
Nick 2084 79 6
George2 1973 263 1
Xaisoft 1944 174 12
John 1929 160 24
danmine 1901 53 3
zsharp 1771 145 16
carrier 1742 56 8
JC Grubbs 1550 50 5
vg1890 1534 56 2
Coocoo4Cocoa 1514 143 0
Keand64 1513 83 5
The proc is on gist: http://gist.github.com/165544
You could do something like:
with
data (question_id,title,user_id, username ,other_info) as
(
select 4,'When setting a form''s opacity should I use a decimal or double?',8,'Eggs McLaren', 'some other stuff lots of text'
union all
select 5,'Another q title',9,'OtherUsername', 'some other stuff lots of text')
select
(select 'http://stackoverflow.com/questions/' + cast(question_id as varchar(10)) as [#href], title as [*] for xml path('a')) as questioninfo
,(select 'http://stackoverflow.com/users/' + cast(user_id as varchar(10)) + '/' + replace(username, ' ', '-') as [#href], username as [*] for xml path('a')) as userinfo
, other_info
from data
...but see how you go. I personally find that FOR XML PATH is very powerful for getting marked-up results in a way that suits me.
Rob

Resources