Teradata. How to sum all values within a group? - teradata

My initial table looks like this
id value
1 20
1 50
1 30
2 60
2 5
2 35
I need the following resulting table
id value cum | ( this is explanation not a field)
_ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _
1 20 20 | (0 + 20 = 20)
1 30 50 | (30 + 20 = 50)
1 50 100 | (50 + 50 = 100)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _
2 5 5 | (0 + 5 = 5)
2 35 40 | (5 + 35 = 40)
2 60 100 | (40 + 60 = 100)
The logic is
1) ORDER the original table BY value ASC
2) SUM up all the previous values resulting in a cumulative cum field . So the cum column is the SUM of all value less than the current value .
I need to do this with sql only without a stored procedure
How can I do this?

Well, you describe a Cumulative Sum:
sum(value)
over (partition by id
order by values
rows unbounded preceding)
The rows unbounded preceding is needed in Teradata, because it defaults to rows unbounded preceding and unbounded following (a Group Sum), which is different than Standard SQL's default of range unbounded preceding.

Related

Select a single element from a complex ID

I have IDs such as "3_K97_T12_High_2_Apples". I want to select just "T12" and store it in a character vector (Tiles) so I can call it in text() when I label my points in the plot() with label = Tiles. I want to label each point with just the 3rd element of the ID (i.e T12).
How can I do this?
Use a regex to extract - capture the third set of characters that are not a _ (([^_]+) from the start (^) of the string as a group and in the replacement specify the backreference (\\1) of the captured group
Tiles <- sub("^[^_]+_[^_]+_([^_]+)_.*", "\\1", str1)
Tiles
[1] "T12"
^ - start of the string
[^_]+ - one or more characters not a _
_ - the _
[^_]+ - one or more characters not a _
_ - the _
([^_]+) - one or more characters not a _ captured
data
str1 <- "3_K97_T12_High_2_Apples"

SQL count all of the column that has a value greater than 10 and sum it up

I want to count all of the column in my table that has value >= 10.
here is my table :
Date ##### || Value1 || Value2 || Value3
23/04/2014 || __ 1,2 || __ 12,3 ||__ 10 ||
23/04/2014 ||__ 11,2 || ____ 3 || __ 10,3 ||
24/04/2014 || __ 10,9 || ____ 3 || __ 1 ||
I want it to display:
Date ##### || Count ||
23/04/2014 || __ 4 ||
24/04/2014 || __ 1 ||
Assume that I have a lot of date, I want it to display only the last 3 rows.
here is my first code :
Dim strCommand As String = "Select Date, count(*) as tcount from tbBooth having count(*) >= 10 group by date"
already changed based on the solution from Collapsar into this:
Dim strCommand As String = "Select t.d, sum(t.valcount) cnt from (select [date] AS d, CASE WHEN t1.ManualAssists1 >= 10 THEN 1 ELSE 0 END + CASE WHEN t1.ManualAssists2 >= 10 THEN 1 ELSE 0 END + CASE WHEN t1.ManualAssists3 >= 10 THEN 1 ELSE 0 END AS valcount from tbBooth t1) t group by t.d"
it's works, but I want to display only the last 3 row based on ASC order.
Is there anyway how to do it?
Thanks in advances....
try
select t.d
, sum(t.valcount) cnt
from (
select [date] AS d
, CASE WHEN t1.value1 >= 10 THEN 1 ELSE 0 END
+ CASE WHEN t1.value2 >= 10 THEN 1 ELSE 0 END
+ CASE WHEN t1.value3 >= 10 THEN 1 ELSE 0 END
AS valcount
from table t1
) t
group by t.d
;
SELECT
D,
SUM(one + two + three) AS tcount
FROM
(
SELECT
[Date] AS D,
CASE WHEN Value1 >= 10 THEN 1 ELSE 0 END AS one,
CASE WHEN Value2 >= 10 THEN 1 ELSE 0 END AS two,
CASE WHEN Value3 >= 10 THEN 1 ELSE 0 END AS three
FROM
tbBooth
)
GROUP BY
D

LINQ query throwing 'Conversion from string to type 'Long' is not valid' error

I'm having an issue with the following LINQ query which is throwing the exception in the post title:
Dim test = From Match In _
(From Load In dtContacts _
Group Join Siebel In dtSiebel _
On Load("Email") Equals Siebel("EMAIL_ADDRESS") _
Into g = Group _
From LoadResults In g.DefaultIfEmpty _
Where Not LoadResults Is Nothing _
Select Email_Address = Load.Field(Of String)("Email"), _
Load_Account_Number = Load.Field(Of String)("AccountNum")) _
Group Join Acct In dtSiebel _
On Match.Email_Address Equals Acct("EMAIL_ADDRESS") _
And Not Match.Load_Account_Number Equals Acct("ACCOUNT_NUMBER") _
Into h = Group _
From MatchResults In h.DefaultIfEmpty _
Where Not MatchResults Is Nothing _
Select Contact_Row_ID = MatchResults.Field(Of String)("CONTACT_ROW_ID"), _
Match.Email_Address, _
Match.Load_Account_Number, _
Account_Num_Chk = MatchResults.Field(Of String)("ACCOUNT_NUMBER")
The culprit is this line:
And Not Match.Load_Account_Number Equals Acct("ACCOUNT_NUMBER")
What isn't making sense to me is if I remove the 'Not' clause (which I use for another use case) leaving the rest of the query intact, I get the expected result w/o any exceptions. What is the problem here?
You could try
And Also Not Match.Load_Account_Number Equals Acct("ACCOUNT_NUMBER")

inner join using Linq Vb.net

I'm trying to join two datatables of same keyfields.
table1
ID Class
---- -----
1 10
2 9
table2
ID Class
---- -----
1 8
2 7
Result
ID Class1 Class2
1 10 8
2 9 7
following is code for joining between user and userclients you can replace your table and get the result of join
Follwing image is for the c# but will give you idea in detail
Dim user = From u In Users Join uc In UserClients On u.Id = uc.UserId New From { _
u.Id, _
u.FirstName, _
u.LastName, _
uc.MobileNo, _
uc.imeiNO, _
uc.Id _
}
if you are beginner you can look this :
SQL to LINQ ( Visual Representation )
try something like this:
Dim test = From t1 in table1_
Join t2 in table2 on t1.ID Equals t2.ID _
Select ID = t1.ID,
Class1 = t1.Class,
Class2 = t2.Class

T-Sql to linq conversion issues (order by)

I'm having a bit of issue converting the following t-sql statement to Linq (using 4.0 entity framework)
I'm getting
Unable to cast the type
'System.Linq.IOrderedQueryable1' to
type 'System.Linq.IQueryable1'. LINQ
to Entities only supports casting
Entity Data Model primitive types.
T-Sql
SELECT
I.Id
, I.Name
FROM Inventory I
WHERE I.Id in
(
select top 5 applicationId
from LastViewed
group by ApplicationId, SomeUserId
having SomeUserId = #SomeUserId
order by Max(id) desc
)
This is what I have right now (with help from Linqer)
Dim query As IQueryable(Of Inventory) =
From d In ctx.Inventories
Where
((From e In ctx.LastVieweds _
Group e By _
e.ApplicationId, _
e.SomeUserId _
Into g = Group _
Where DfaitEdsId = user _
Order By g.Max(Function(p) p.Id) Descending _
Select New With { _
ApplicationId _
}).Take(5)).Contains(New With {.ApplicationId = d.Id}) _
Select d
it currently crashes when I do this line.
query.ToList()
Thanks for your time.
Your 'problem' is in your very first line. The query you have specified returns an IOrderedQueryable(Of Inventory) that you are trying to assign to a variable of type IQueryable(Of Inventory). You aren't getting the error until query.ToList() thanks to deferred execution. You can let the compiler infer the type if you have 'option infer' set to on in your project and your query would be something like:
Dim query =
From d In ctx.Inventories
Where
(From e In ctx.LastVieweds _
Group e By _
e.ApplicationId, _
e.SomeUserId _
Into g = Group _
Where DfaitEdsId = user _
Order By g.Max(Function(p) p.Id) Descending _
Select ApplicationId Take 5 _
).Contains(d.Id) _
Select d
Or you can just change the type of query to be IOrderedQueryable(Of Inventory)
Note: What you have works fine in Linq to SQL, but Entities is much stricter when it comes to casting.
Edit:
Alright, let's try to dig a little deeper into this. Tell me which line blows up with the following:
Dim innerList = (From e In ctx.LastVieweds _
Group e By _
e.ApplicationId, _
e.SomeUserId _
Into g = Group _
Where DfaitEdsId = user _
Order By g.Max(Function(p) p.Id) Descending _
Select ApplicationId Take 5).ToList()
Dim query = (From d In ctx.Inventories
Where innerList.Contains(d.Id) _
Select d).ToList()
Don't forget to pare the results down a bit if enumerating the queries with 'ToList()' is too much. The types of both variables are now List(Of Inventory)

Resources