current value of a column for every id according to the newest date - sql

This question is taking my previous problem further: (most recent (max) date for every id)
Suppose i have a table
which has
a = id
b = date
c = NewestDate
d = someValues -- ex 0.3
e = currentValue --this is what i need to create
For every a (id) i have b,c,d
I want to create 'e' with for every 'a' checks the date in 'c' then in 'e' it's being inserted the corresponding value from 'd'.
the column c was created like this:
SELECT a,
b,
max(b) OVER (PARTITION BY a) AS c
FROM myTable
ORDER BY a,b
example:
a b c d e
1 2009.02.15 2015.03.20 0.432 0.122 --e taken from the row below
1 2015.03.20 2015.03.20 0.122 0.122 --the value of e
1 2014.04.02 2015.03.20 0.98 0.122 --e taken from the row above
2 2010.04.12 2014.07.01 0.467 0.578
2 2014.07.01 2014.07.01 0.578 0.578
.
.
Anyone has a solution for this?
tried like this:
select *
into #myTable
from myTable
select
t1.a,
t1.b,
t1.c,
t1.d,
t.d as e
from #myTable t
left join myTable t1 on t.c = t1.b and t.a = t1.a
order by a, b

I think you can just use conditional aggregation with a window function:
SELECT t.*,
MAX(CASE WHEN b = c THEN d END) OVER (PARTITION BY a) as d
FROM (SELECT a, b, max(b) OVER (PARTITION BY a) AS c
FROM myTable
) t
ORDER BY a, b

Related

Consolidate information (time serie) from two tables

MS SQL Server
I have two tables with different accounts from the same customer:
Table1:
ID
ACCOUNT
FROM
TO
1
A
01.10.2019
01.12.2019
1
A
01.02.2020
09.09.9999
and table2:
ID
ACCOUNT
FROM
TO
1
B
01.12.2019
01.01.2020
As result I want a table that summarize the story of this costumer and shows when he had an active account and when he doesn't.
Result:
ID
FROM
TO
ACTIV Y/N
1
01.10.2019
01.01.2020
Y
1
02.01.2020
31.01.2020
N
1
01.02.2020
09.09.9999
Y
Can someone help me with some ideas how to proceed?
This is the typical gaps and island problem, and it's not usually easy to solve.
You can achieve your goal using this query, I will explain it a little bit.
You can test on this db<>fiddle.
First of all... I have unified your two tables into one to simplify the query.
-- ##table1
select 1 as ID, 'A' as ACCOUNT, convert(date,'2019-10-01') as F, convert(date,'2019-12-01') as T into ##table1
union all
select 1 as ID, 'A' as ACCOUNT, convert(date,'2020-02-01') as F, convert(date,'9999-09-09') as T
-- ##table2
select 1 as ID, 'B' as ACCOUNT, convert(date,'2019-12-01') as F, convert(date,'2020-01-01') as T into ##table2
-- ##table3
select * into ##table3 from ##table1 union all select * from ##table2
You can then get your gaps and island using, for example, a query like this.
It combines recursive cte to generate a calendar (cte_cal) and lag and lead operations to get the previous/next record information to build the gaps.
with
cte_cal as (
select min(F) as D from ##table3
union all
select dateadd(day,1,D) from cte_cal where d < = '2021-01-01'
),
table4 as (
select t1.ID, t1.ACCOUNT, t1.F, isnull(t2.T, t1.T) as T, lag(t2.F, 1,null) over (order by t1.F) as SUP
from ##table3 t1
left join ##table3 t2
on t1.T=t2.F
)
select
ID,
case when T = D then F else D end as "FROM",
isnull(dateadd(day,-1,lead(D,1,null) over (order by D)),'9999-09-09') as "TO",
case when case when T = D then F else D end = F then 'Y' else 'N' end as "ACTIV Y/N"
from (
select *
from cte_cal c
cross apply (
select t.*
from table4 t
where t.SUP is null
and (
c.D = t or
c.D = dateadd(day,1,t.T)
)
) t
union all
select F, * from table4 where T = '9999-09-09'
) p
order by 1
option (maxrecursion 0)
Dates like '9999-09-09' must be treated like exceptions, otherwise I would have to create a calendar until that date, so the query would take long time to resolve.

SQL query to get the count by applying group by

I want to get the below result:
source table :
Cnt A B
4 ABC YU/FGH
5 ABC YU/DFE
5 ABC KL
2 LKP BN/ER
4 JK RE
Result:
Cnt A B
9 ABC YU
5 ABC KL
2 LKP BN
4 JK RE
Here I want the count by grouping 'B' and want to display the 'B' record only till the special character (/)
Basically, you will have to filter out the all the characters after the "/" symbol and then apply a SUM and a GROUP BY. You can see this below. The inner query filters out the unwanted string and the outer query does the SUM and the GROUP BY :
SELECT SUM(t.Cnt), t.A, t.B
FROM (
SELECT Cnt,
A,
CASE
WHEN CHARINDEX('/', B) > 0 THEN SUBSTRING(B, 0, CHARINDEX('/', B))
ELSE B
END AS B
FROM #Tab
) t
GROUP BY t.A, t.B
ORDER BY t.A
You can see this working here -> http://rextester.com/IQJ79191
Hope this helps!!!
You can get your string till '/' by using SUBSTRING.
select
count(SUBSTRING(reverse(B),0,charindex('/',reverse(B)))),
A,
SUBSTRING(reverse(B),0,charindex('/',reverse(B)))
from source_table group by B;
Solution for Oracle - substr(B,0,instr(B,'/',1)-1) B
Put this both in select and groupby
I can suggest you to use a query like this:
select
sum(Cnt) Cnt,
A,
left(B, charindex('/',B+'/',0)-1) B -- Using `+'\'` will do the trick
from
t
group by
A,
left(B, charindex('/',B+'/',0)-1);
By using String and CharIndex Functions.
;WITH SourceTable(Cnt,A,B) AS
(
SELECT 4,'ABC','YU/FGH'UNION ALL
SELECT 5,'ABC','YU/DFE'UNION ALL
SELECT 5,'ABC','KL' UNION ALL
SELECT 2,'LKP','BN/ER' UNION ALL
SELECT 4,'JK','RE'
)
SELECT SUM(Cnt) AS Cnt,A,CASE WHEN CHARINDEX('/',B) = 0 THEN B
ELSE SUBSTRING(B,0,CHARINDEX('/',B)) END AS [B] FROM SourceTable
GROUP BY A,CASE WHEN CHARINDEX('/',B) = 0 THEN B
ELSE SUBSTRING(B,0,CHARINDEX('/',B)) END
ORDER BY Cnt DESC
Try this query --
SELECT SUM(Cnt) AS [COUNT]
,A
,CASE
WHEN CHARINDEX('/', B) > 0
THEN SUBSTRING(B, 1, (CHARINDEX('/', B) - 1))
ELSE B
END
FROM tblSample
GROUP BY A, B
ORDER BY A, B

Can I add multiple columns to Totals

Using MS SQL 2012
I want to do something like
select a, b, c, a+b+c d
However a, b, c are complex computed columns, lets take a simple example
select case when x > 4 then 4 else x end a,
( select count(*) somethingElse) b,
a + b c
order by c
I hope that makes sense
You can use a nested query or a common table expression (CTE) for that. The CTE syntax is slightly cleaner - here it is:
WITH CTE (a, b)
AS
(
select
case when x > 4 then 4 else x end a,
count(*) somethingElse b
from my_table
)
SELECT
a, b, (a+b) as c
FROM CTE
ORDER BY c
I would probably do this:
SELECT
sub.a,
sub.b,
(sub.a + sub.b) as c,
FROM
(
select
case when x > 4 then 4 else x end a,
(select count(*) somethingElse) b
FROM MyTable
) sub
ORDER BY c
The easiest way is to do this:
select a,b,c,a+b+c d
from (select <whatever your calcs are for a,b,c>) x
order by c
That just creates a derived table consisting of your calculations for a, b, and c, and allows you to easily reference and sum them up!

How can I Pivot a table in DB2? [duplicate]

This question already has answers here:
Pivoting in DB2
(3 answers)
Closed 5 years ago.
I have table A, below, where for each unique id, there are three codes with some value.
ID Code Value
---------------------
11 1 x
11 2 y
11 3 z
12 1 p
12 2 q
12 3 r
13 1 l
13 2 m
13 3 n
I have a second table B with format as below:
Id Code1_Val Code2_Val Code3_Val
Here there is just one row for each unique id. I want to populate this second table B from first table A for each id from the first table.
For the first table A above, the second table B should come out as:
Id Code1_Val Code2_Val Code3_Val
---------------------------------------------
11 x y z
12 p q r
13 l m n
How can I achieve this in a single SQL query?
select Id,
max(case when Code = '1' then Value end) as Code1_Val,
max(case when Code = '2' then Value end) as Code2_Val,
max(case when Code = '3' then Value end) as Code3_Val
from TABLEA
group by Id
SELECT Id,
max(DECODE(Code, 1, Value)) AS Code1_Val,
max(DECODE(Code, 2, Value)) AS Code2_Val,
max(DECODE(Code, 3, Value)) AS Code3_Val
FROM A
group by Id
If your version doesn't have DECODE(), you can also use this:
INSERT INTO B (id, code1_val, code2_val, code3_val)
WITH Ids (id) as (SELECT DISTINCT id
FROM A) -- Only to construct list of ids
SELECT Ids.id, a1.value, a2.value, a3.value
FROM Ids -- or substitute the actual id table
JOIN A a1
ON a1.id = ids.id
AND a1.code = 1
JOIN A a2
ON a2.id = ids.id
AND a2.code = 2
JOIN A a3
ON a3.id = ids.id
AND a3.code = 3
(Works on my V6R1 DB2 instance, and have an SQL Fiddle Example).
Here is a SQLFiddle example
insert into B (ID,Code1_Val,Code2_Val,Code3_Val)
select Id, max(V1),max(V2),max(V3) from
(
select ID,Value V1,'' V2,'' V3 from A where Code=1
union all
select ID,'' V1, Value V2,'' V3 from A where Code=2
union all
select ID,'' V1, '' V2,Value V3 from A where Code=3
) AG
group by ID
Here is the SQL Query:
insert into pivot_insert_table(id,code1_val,code2_val, code3_val)
select * from (select id,code,value from pivot_table)
pivot(max(value) for code in (1,2,3)) order by id ;
WITH Ids (id) as
(
SELECT DISTINCT id FROM A
)
SELECT Ids.id,
(select sub.value from A sub where Ids.id=sub.id and sub.code=1 fetch first rows only) Code1_Val,
(select sub.value from A sub where Ids.id=sub.id and sub.code=2 fetch first rows only) Code2_Val,
(select sub.value from A sub where Ids.id=sub.id and sub.code=3 fetch first rows only) Code3_Val
FROM Ids
You want to pivot your data. Since DB2 has no pivot function, yo can use Decode (basically a case statement.)
The syntax should be:
SELECT Id,
DECODE(Code, 1, Value) AS Code1_Val,
DECODE(Code, 2, Value) AS Code2_Val,
DECODE(Code, 3, Value) AS Code3_Val
FROM A

Performance issue in SQL

Hi friends I am in big trouble.
I have query which is taking around 20 mins in execution.(4.5 crores records).
This is the query.
SELECT
a.cmddefinitionid,
b.cmdinstanceid,
b.mobileid,
d.phonenumber,
d.hardwareid,
d.smsemail,
a.cmdid,
c.cmdname,
c.cmdxmldesc,
a.eventflag,
a.recurrenceflag,
a.paramflag,
a.filename,
a.paramname,
a.VALUE,
a.meterflag,
a.gosilentflag,
a.regurl,
b.scheduleddate,
-- e.TxnTypeID, -- Added
e.TxnID,-- Added
e.StatusMsg,-- Added
b.LastModified as TimeCreated,-- Added newly
d.PanelistID -- Added newly
FROM
( select CmdInstanceID, TxnTypeID, TxnID, StatusMsg
from ODM_TDCS.dbo.CMD_TXN
where TxnTypeID < 3
and TxnID IN (
select max(TxnID)
from ODM_TDCS.dbo.CMD_TXN
group by CmdInstanceID)
) AS e,
dbo.cmd_definition AS a,
dbo.cmd_instance AS b,
dbo.lu_cmd AS c,
dbo.lu_mobile AS d
WHERE
a.cmddefinitionid = b.cmddefinitionid
and
a.cmdid = c.cmdid and b.mobileid = d.mobileid and
b.cmdtypeid = 2 AND
b.scheduleddate > Getdate() - 2 AND
b.CmdInstanceID = e.CmdInstanceID
Now out of this:
select CmdInstanceID, TxnTypeID, TxnID, StatusMsg
from ODM_TDCS.dbo.CMD_TXN
where TxnTypeID < 3
and TxnID IN (
select max(TxnID)
from ODM_TDCS.dbo.CMD_TXN
group by CmdInstanceID)
this is taking above 5 mins , but if i remove this condition the query gets executed in
0.17 sec.
Any help or suggestion??
Try with this...
SELECT
a.cmddefinitionid,
b.cmdinstanceid,
b.mobileid,
d.phonenumber,
d.hardwareid,
d.smsemail,
a.cmdid,
c.cmdname,
c.cmdxmldesc,
a.eventflag,
a.recurrenceflag,
a.paramflag,
a.filename,
a.paramname,
a.VALUE,
a.meterflag,
a.gosilentflag,
a.regurl,
b.scheduleddate,
-- e.TxnTypeID, -- Added
e.TxnID,-- Added
e.StatusMsg,-- Added
b.LastModified as TimeCreated,-- Added newly
d.PanelistID -- Added newly
FROM
(Select * from (
select CmdInstanceID, TxnTypeID, TxnID, StatusMsg,
ROW_NUMBER() over (partition by CmdInstanceID order by TxnID desc ) as Row
from ODM_TDCS.dbo.CMD_TXN
where TxnTypeID < 3
)as t where e.Row = 1
) AS e,
dbo.cmd_definition AS a,
dbo.cmd_instance AS b,
dbo.lu_cmd AS c,
dbo.lu_mobile AS d
WHERE
a.cmddefinitionid = b.cmddefinitionid
and
a.cmdid = c.cmdid and b.mobileid = d.mobileid and
b.cmdtypeid = 2 AND
b.scheduleddate > Getdate() - 2 AND
b.CmdInstanceID = e.CmdInstanceID
Just used Row_number function and this will avoid travelling ODM_TDCS.dbo.CMD_TXN
repeatedly.
Good luck.