I have the following two tables
I need to create a table that would summarize points for each date
How can I do this . I have updraded to Teradata 14 . And I am not quite familiar with all the new functions
If table1 is actually only a few rows you don't need any fancy query.
Assuming table1.caseid is a byteint this will result in a product join:
Select t2.datex, t2.caseid, sum(t1.points)
from table1 as t1 join table2 a t2
on position(trim(t1.caseid) in t2.caseid) > 0
group by 1,2
Of course if this was a properly normalized table you could simple use a t1.caseid = t2.caseid join instead.
Related
I'm trying to join two tables but not getting the desired results. Im new to SQL and will need help to joining these two tables.
The data in Table 2 is transposed and the table will only be populated only when the "value" field is populated with random values.
Details of Table 1 and Table 2 and expected output
You could use CTEs to split up the impact from outcome. Also, your screenshot had an error for task_id 6.
with t_impact as (
select task_id, value
from table2
where name = 'task_impact'
),
t_outcome as (
select task_id, value
from table2
where name = 'task_outcome'
)
select distinct t1.id,
t1.title,
i.value as task_impact,
o.value as task_outcome
from table1 t1
left join t_impact i
on t1.id = i.task_id
left join t_outcome o
on t1.id = o.task_id
order by t1.id
DB-fiddle found here.
So I want to use datediff for two tables that I'm doing a join on. The problem is if I filter by a unique value, it returns two rows of result. For example:
select *
from [internalaudit]..ReprocessTracker with (nolock)
where packageID = '1983446'
It returns two rows, because it was repackaged twice, by two different workers.
User RepackageTime
KimVilder 2021-06-10
DanielaS 2021-06-05
I want to use the latest repackagetime of that unique packageID and then do a datediff with another time record when I do a join with a different table.
Is there way to filer so I can get the latest time entry of Repackagetime?
There are numerous ways you can accomplish this, if I understand your goal - proper example data and tables would be a help here.
One way is using apply and selecting the max date for each packageId
select DateDiff(datepart, t.datecolumn, r.RepackageTime)...
from othertable t
cross apply (
select Max(RepackageTime)RepackageTime
from internalaudit.dbo.ReprocessTracker r
where r.packageId=t.packageId
)r
select *
from Othertable t1
join (
select *
from [internalaudit]..ReprocessTracker t2
where packageID = '1983446'
limit 1
) t2
on t1.id = t2.id
if you are using sql server instead of limit 1 you should use top 1
also otherwise you solid reason to use nolock hint, avoid using it.
also to generalize the query above:
select *
from Othertable t1
cross join (
select *
from [internalaudit]..ReprocessTracker t2
where t1.packageID = t2.packageID
limit 1
) t2
I need to produce a synthetic table from 2 tables but one of them is tricky.
First table (sample):
Second table (sample also) :
I want to use it to find the 'store' of each 'pers'
What i want at the end :
(and i want to keep the 'zero' if sum_of_duration is null)
My first code :
SELECT
QRT,
STORE,
sum(DURATION) as SUM_of_DURATION
FROM T1
LEFT JOIN T2
on T1.PERS=T2.PERS
group by QRT, STORE
But my 'Sum_of_durations' are out of the roof because of multiple joins induced by table 2 i guess.
Any help ?
Thanks
Remove duplicates before joining:
SELECT T1.QRT, T2.STORE,
SUM(T1.DURATION) as SUM_of_DURATION
FROM T1 LEFT JOIN
(SELECT DISTINCT QRT, PERS
FROM t2
) t2
ON T1.PERS = T2.PERS
GROUP BY T1.QRT, T2.STORE;
I have two tables:
and
I'm trying to create the following output:
How can I do this considering there's nothing to join on? Both of these initial tables are coming from SQL queries and are not a sequence. Using MSSQL.
You can use cross join by 2 ways
SELECT Number,Letter FROM Table1,Table2
Or
SELECT Number,Letter FROM Table1 CROSS JOIN Table2
Demo
http://sqlfiddle.com/#!9/157e3/3
I am working on an SSIS job that contains a complex query.
It has some thing like :
some sql statements
left outer join
(
select query joining two more tables )
table1
here, i am unable to understand what that table1 mean ? Is it a kind of temporary view
created . This table1 is used in the other parts of query . But, actually the table1 does
exists in the database.
Is it like , the results of the select query in the parenthesis is created as table1
Please clarify me on this..
I am not able to put down my code because of Security Policies
Here is SQL Fiddel example
Below is the sample query
Select Temp1.id,Table1.id Table1_id
from Temp1
left Outer join
(
Select Temp2.id
from Temp2
join Temp3
On Temp2.id = Temp3.id
) Table1
on Temp1.id = Table1.Id
In above example table1 is the Alias for data coming from joinsof two tables (temp2 and temp3)
table1 is an alisas your subquery. It's the name of subquery you can use with columns for example table1.col1
It is an alias for the query in the parenthesis.
If you would remove that you would get an error.
Aliases are also good when you have the same column in more than on joined tables, so you can distinquish them.
For instance if colX is both in Table1 and Table2 you would have a query like:
SELECT T1.colX,T2.colX
FROM Table1 T1
JOIN Table2 T2
ON T1.id = T2.id