SQL Query to conditionally display values in DB2 - db2

I need to conditionally display values by joining 3 different tables, below is the sample data,
TABLE A
LOCATION SITE LOCATIONID
100 DEMO 1234
10 DEMO 1050
TABLE B
LOCATION PARENT SITE
100 10 DEMO
TABLE C
LDKEY LDTEXT LDOWNERTABLE
1234 Hello A
1050 Welcome A
OR
TABLE C
LDKEY LDTEXT LDOWNERTABLE
1050 Welcome A
When Select query is executed on Table A then it should bring output as 100 if its locationid (1234) has a record in Table C .
Output 1
LOCATION SITE LDTEXT
100 DEMO Hello
If there is no record corresponding to locationid(1234) in table C then it should bring in its parent's LDTEXT
OUTPUT 2
LOCATION SITE LDTEXT
100 DEMO Welcome
Below is the query that I had tried and it brought me both the records 100 and 10 ideally it should bring me only 100 with LDTEXT as HELLO
select * from A where (location = '100' and site = 'DEMO'
and (locationsid in (select ldkey from C where ldownertable='A' )) or
location in ( select parent from B where location = '100' and site = 'DEMO' and
locationsid in (select ldkey from C where ldownertable='A' )))

Try this as is. You may uncomment the commented out line to check the difference.
WITH
A (LOCATION, SITE, LOCATIONID) AS
(
VALUES
(100, 'DEMO', 1234)
, ( 10, 'DEMO', 1050)
)
, B (LOCATION, PARENT, SITE) AS
(
VALUES
(100, 10, 'DEMO')
)
, C (LDKEY, LDTEXT, LDOWNERTABLE) AS
(
VALUES
(1050, 'Welcome', 'A')
--, (1234, 'Hello', 'A')
)
SELECT A.LOCATION, A.SITE, COALESCE(C1.LDTEXT, C2.LDTEXT) LDTEXT
FROM A
LEFT JOIN
(
B
JOIN A A2 ON A2.LOCATION = B.PARENT
) ON B.LOCATION = A.LOCATION
LEFT JOIN C C1 ON C1.LDKEY = A.LOCATIONID
LEFT JOIN C C2 ON C2.LDKEY = A2.LOCATIONID
WHERE A.LOCATION = 100
;

Related

Change column value after left join SQL

I have the following statement:
select distinct x_order.code , x_order.status , x_order.project , project.active , project.code as projectcode
from project
left join x_order on x_order.project = project.code
where status = 'B' and active = 0
Which gives me a nice table with all the records I want to change. I now need to change the values in the 'status' column from B to D in this table. How can I do that?
I tried UPDATE, but to no success.
Not sure of the exact table structures you're using, but would a CTE work for you like this...
;WITH CTE
AS (
select distinct x_order.code as ordercode , x_order.status , x_order.project , project.active , project.code as projectcode
from project
left join x_order on x_order.project = project.code
where status = 'B' and active = 0
)
UPDATE X
SET [status] = 'D'
FROM x_order X
JOIN CTE C
ON X.code = C.ordercode

SSRS Expression Split string in rows and column

I am working with SQL Server 2008 Report service. I have to try to split string values in different columns in same row in expression but I can't get the excepted output. I have provided input and output details. I have to split values by space (" ") and ("-").
Input :
Sample 1:
ASY-LOS,SLD,ME,A1,A5,J4A,J4B,J4O,J4P,J4S,J4T,J7,J10,J2A,J2,S2,S3,S3T,S3S,E2,E2F,E6,T6,8,SB1,E1S,OTH AS2-J4A,J4B,J4O,J4P,J4S,J4T,J7,J1O,J2A,S2,S3,J2,T6,T8,E2,E4,E6,SLD,SB1,OTH
Sample 2:
A1 A2 A3 A5 D2 D3 D6 E2 E4 E5 E6 EOW LH LL LOS OTH P8 PH PL PZ-1,2,T1,T2,T3 R2-C,E,A RH RL S1 S2-D S3
Output should be:
Thank you.
I wrote this before I saw your comment about having to do it in the report. If you can explain why you cannot do this in the dataset query then there may be a way around that.
Anyway, here's one way of doing this using SQL
DECLARE #t table (RowN int identity (1,1), sample varchar(500))
INSERT INTO #t (sample) SELECT 'ASY-LOS,SLD,ME,A1,A5,J4A,J4B,J4O,J4P,J4S,J4T,J7,J10,J2A,J2,S2,S3,S3T,S3S,E2,E2F,E6,T6,8,SB1,E1S,OTH AS2-J4A,J4B,J4O,J4P,J4S,J4T,J7,J1O,J2A,S2,S3,J2,T6,T8,E2,E4,E6,SLD,SB1,OTH'
INSERT INTO #t (sample) SELECT 'A1 A2 A3 A5 D2 D3 D6 E2 E4 E5 E6 EOW LH LL LOS OTH P8 PH PL PZ-1,2,T1,T2,T3 R2-C,E,A RH RL S1 S2-D S3'
drop table if exists #s1
SELECT RowN, sample, SampleIdx = idx, SampleValue = [Value]
into #s1
from #t t
CROSS APPLY
spring..fn_Split(sample, ' ') as x
drop table if exists #s2
SELECT
s1.*
, s2idx = Idx
, s2Value = [Value]
into #s2
FROM #s1 s1
CROSS APPLY spring..fn_Split(SampleValue, '-')
SELECT SampleKey = [1],
Output = [2] FROM #s2
PIVOT (
MAX(s2Value)
FOR s2Idx IN ([1],[2])
) p
This produced the following results
If you do not have a split function, here is the script to create the one I use
CREATE FUNCTION [dbo].[fn_Split]
/* Define I/O parameters WARNING! DO NOT USE MAX DATA-TYPES HERE! IT WILL KILL PERFORMANCE! */
(#pString VARCHAR(8000)
,#pDelimiter CHAR(1)
)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN
/*"Inline" CTE Driven "Tally Table" produces values from 1 up to 10,000: enough to cover VARCHAR(8000)*/
WITH E1(N) AS (
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
)--10E+1 or 10 rows
,E2(N) AS (SELECT 1 FROM E1 a,E1 b)--10E+2 or 100 rows
,E4(N) AS (SELECT 1 FROM E2 a,E2 b)--10E+4 or 10,000 rows max
/* This provides the "base" CTE and limits the number of rows right up front
for both a performance gain and prevention of accidental "overruns" */
,cteTally(N) AS (
SELECT TOP (ISNULL(DATALENGTH(#pString), 0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
FROM E4
)
/* This returns N+1 (starting position of each "element" just once for each delimiter) */
,cteStart(N1) AS (
SELECT 1 UNION ALL
SELECT t.N + 1 FROM cteTally t WHERE SUBSTRING(#pString, t.N, 1) = #pDelimiter
)
/* Return start and length (for use in SUBSTRING later) */
,cteLen(N1, L1) AS (
SELECT s.N1
,ISNULL(NULLIF(CHARINDEX(#pDelimiter, #pString, s.N1), 0) - s.N1, 8000)
FROM cteStart s
)
/* Do the actual split.
The ISNULL/NULLIF combo handles the length for the final element when no delimiter is found. */
SELECT
idx = ROW_NUMBER() OVER (ORDER BY l.N1)
,value = SUBSTRING(#pString, l.N1, l.L1)
FROM cteLen l

DB2: SQL to return all rows in a group having a particular value of a column in two latest records of this group

I have a DB2 table having one of the columns (A) which has either value PQR or XYZ.
I need output where the latest two records based on col C date have value A = PQR.
Sample Table
A B C
--- ----- ----------
PQR Mark 08/08/2019
PQR Mark 08/01/2019
XYZ Mark 07/01/2019
PQR Joe 10/11/2019
XYZ Joe 10/01/2019
PQR Craig 06/06/2019
PQR Craig 06/20/2019
In this sample table, my output would be Mark and Craig records
Since 11.1
You may use the nth_value OLAP function.
Refer to OLAP specification.
SELECT A, B, C
FROM
(
SELECT
A, B, C
, NTH_VALUE (A, 1) OVER (PARTITION BY B ORDER BY C DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) C1
, NTH_VALUE (A, 2) OVER (PARTITION BY B ORDER BY C DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) C2
FROM TAB
)
WHERE C1 = 'PQR' AND C2 = 'PQR'
dbfiddle link.
Older versions
SELECT T.*
FROM TAB T
JOIN
(
SELECT B
FROM
(
SELECT
A, B
, ROWNUMBER() OVER (PARTITION BY B ORDER BY C DESC) RN
FROM TAB
)
WHERE RN IN (1, 2)
GROUP BY B
HAVING MIN(A) = MAX(A) AND COUNT(1) = 2 AND MIN(A) = 'PQR'
) G ON G.B = T.B;
A simple solution could be
SELECT A,B,C
FROM tab
WHERE A = 'PQR'
ORDER BY C DESC FETCH FIRST 2 ROWS only

Select VISITID based on values from 2 different hit level data Bigquery

I have a FULLVISITORID in my table with 2 different visitid.
Each visitid has multiple hits. I want to select the visitid where one hits.page.pagepath = 'somepage'
and another hits.eventInfo.eventCategory = 'some event'. Both the conditions should be happening for the same visitid.
For Example:
Select * from my_table where FullVisitorid = '1'
FullVisitorid Visitid ........... hits.page.pagepath .....hits.event.eventCategory
1 123 A abc
B cde
c efg
1 147 somePage ggg
D fff
E SomeEvent
I want the result to be VistiID = 147 becuase the visitid has both pagepath = 'somepage' and eventcategory = 'someevent'
Thanks for your help!
Using CTEs, you can use simple join logic to get your results.
with unnested as (
-- Get the fields you care about
select FullVisitorid, Visitid, h.page.pagepath, h.event.eventCategory
from `dataset.table`
left join unnest(hits) h
),
somepage as (
-- Get somepage hit Visitids
select FullVisitorid, Visitid
from unnested
where pagepath = 'somepage'
group by 1,2
),
someevent as (
-- Get someevent hit Visitids
select FulVisitorid, Visitid
from unnested
where eventCategory = 'someevent'
group by 1,2
),
joined as (
-- Join the CTEs to get common Visitids
select FulVisitorid, Visitid
from somepage
inner join someevent using(FullVisitorid, Visitid)
)
select * from joined

t-sql outer join across three tables

I have three tables:
CREATE TABLE person
(id int,
name char(50))
CREATE TABLE eventtype
(id int,
description char(50))
CREATE TABLE event
(person_id int,
eventtype_id int,
duration int)
What I want is a single query which gives me a list of the total duration of each eventtype for each person, including all zero entries. E.g. if there are 10 people and 15 different eventtypes, there should be 150 rows returned, irrespective of the contents of the event table.
I can get an outer join to work between two tables (e.g. durations for all eventtypes), but not with a second outer join.
Thanks!
You'll have to add a CROSS APPLY to the mix to get the non-existing relations.
SELECT q.name, q.description, SUM(q.Duration)
FROM (
SELECT p.Name, et.description, Duration = 0
FROM person p
CROSS APPLY eventtype et
UNION ALL
SELECT p.Name, et.description, e.duration
FROM person p
INNER JOIN event e ON e.person_id = p.id
INNER JOIN eventtype et ON et.id = e.eventtypeid
) q
GROUP BY
q.Name, q.description
You can cross join person and eventtype, and then just join the result to the event table:
SELECT
p.Name,
et.Description,
COALESCE(e.duration,0)
FROM
person p
cross join
eventtype et
left join
event e
on
p.id = e.person_id and
et.id = e.eventtype_id
A cross join is one where, for each row in the left table, it's joined to every row in the right table.
If you want a row for every combination of person and eventtype, that suggets a CROSS JOIN. To get the duration we need to join to event, but this needs to be an OUTER join since there might not always be a row. Your use of "total" suggests there there could be more than one event for a given combination of person and event, so we'll need a SUM in there as well.
Sample data:
insert person values ( 1, 'Joe' )
insert person values ( 2, 'Bob' )
insert person values ( 3, 'Tim' )
insert eventtype values ( 1, 'Cake' )
insert eventtype values ( 2, 'Pie' )
insert eventtype values ( 3, 'Beer' )
insert event values ( 1, 1, 10 )
insert event values ( 1, 2, 10 )
insert event values ( 1, 2, 5 )
insert event values ( 2, 1, 10 )
insert event values ( 2, 2, 7 )
insert event values ( 3, 2, 8 )
insert event values ( 3, 3, 16 )
insert event values ( 1, 1, 10 )
The query:
SELECT
PET.person_id
, PET.person_name
, PET.eventtype_id
, PET.eventtype_description
, ISNULL(SUM(E.duration), 0) total_duration
FROM
(
SELECT
P.id person_id
, P.name person_name
, ET.id eventtype_id
, ET.description eventtype_description
FROM
person P
CROSS JOIN eventtype ET
) PET
LEFT JOIN event E ON PET.person_id = E.person_id
AND PET.eventtype_id = E.eventtype_id
GROUP BY
PET.person_id
, PET.person_name
, PET.eventtype_id
, PET.eventtype_description
Output:
person_id person_name eventtype_id eventtype_description total_duration
----------- ----------- ------------ --------------------- --------------
1 Joe 1 Cake 20
1 Joe 2 Pie 15
1 Joe 3 Beer 0
2 Bob 1 Cake 10
2 Bob 2 Pie 7
2 Bob 3 Beer 0
3 Tim 1 Cake 0
3 Tim 2 Pie 8
3 Tim 3 Beer 16
Warning: Null value is eliminated by an aggregate or other SET operation.
(9 row(s) affected)