Rank based on conditional Partitions in Snowflake - sql

I have a table1 which has a d_date column and table_2 which has start and end_dates columns and a code column , after joining this matches with the first table on at start and end date values :
So I got below result after joining these two tables as:
`
with cte as (select table_a.d_date, table_2.start_date, table_2.end_date,
table_2.code from
table_a left join table_2 on table_a.d_date = table_2.start_date order by d_date
asc)
select * from cte where year(d_date) =2023
D_DATE
START_DATE
END_DATE
Code
2023-01-01
NULL
NULL
NULL
2023-01-02
NULL
NULL
NULL
2023-01-03
NULL
NULL
NULL
2023-01-04
2023-01-04
2023-01-31
C
2023-01-04
2023-01-04
2023-01-31
D
2023-01-04
2023-01-04
2023-01-31
E
2023-01-04
2023-01-04
2023-01-31
M
2023-01-05
NULL
NULL
NULL
2023-01-06
NULL
NULL
NULL
2023-01-07
NULL
NULL
NULL
2023-01-08
NULL
NULL
NULL
2023-01-09
NULL
NULL
NULL
2023-01-10
NULL
NULL
NULL
2023-01-11
NULL
NULL
NULL
2023-01-12
NULL
NULL
NULL
2023-01-13
NULL
NULL
NULL
2023-01-14
NULL
NULL
NULL
2023-01-15
NULL
NULL
NULL
2023-01-16
NULL
NULL
NULL
2023-01-17
NULL
NULL
NULL
2023-01-18
NULL
NULL
NULL
2023-01-19
NULL
NULL
NULL
2023-01-20
NULL
NULL
NULL
2023-01-21
NULL
NULL
NULL
2023-01-22
NULL
NULL
NULL
2023-01-23
NULL
NULL
NULL
2023-01-24
NULL
NULL
NULL
2023-01-25
NULL
NULL
NULL
2023-01-26
NULL
NULL
NULL
2023-01-27
NULL
NULL
NULL
2023-01-28
NULL
NULL
NULL
2023-01-29
NULL
NULL
NULL
2023-01-30
NULL
NULL
NULL
2023-01-31
NULL
NULL
NULL
2023-02-01
2023-02-01
2023-02-28
M
2023-02-01
2023-02-01
2023-02-28
E
2023-02-01
2023-02-01
2023-02-28
D
2023-02-01
2023-02-01
2023-02-28
C
2023-02-02
NULL
NULL
NULL
I need to generate sequence over the d_date column of table_a as shown below, here the start of a sequnce should begin when start_date is matching with d_date and it should continue once it has reached end_date .
so sequence of numbers ranging between start_date and end_date
pls note 32,33 , 34 sequnce is coming from previous month
D_DATE
START_DATE
END_DATE
Code
RES_SEQ
2023-01-01
NULL
NULL
NULL
32
2023-01-02
NULL
NULL
NULL
33
2023-01-03
NULL
NULL
NULL
34
2023-01-04
2023-01-04
2023-01-31
C
NULL
2023-01-04
2023-01-04
2023-01-31
D
1
2023-01-04
2023-01-04
2023-01-31
E
Null
2023-01-04
2023-01-04
2023-01-31
M
Null
2023-01-05
NULL
NULL
NULL
2
2023-01-06
NULL
NULL
NULL
3
2023-01-07
NULL
NULL
NULL
4
2023-01-08
NULL
NULL
NULL
5
2023-01-09
NULL
NULL
NULL
6
2023-01-10
NULL
NULL
NULL
7
2023-01-11
NULL
NULL
NULL
8
2023-01-12
NULL
NULL
NULL
9
2023-01-13
NULL
NULL
NULL
10
2023-01-14
NULL
NULL
NULL
11
2023-01-15
NULL
NULL
NULL
12
2023-01-16
NULL
NULL
NULL
13
2023-01-17
NULL
NULL
NULL
14
2023-01-18
NULL
NULL
NULL
15
2023-01-19
NULL
NULL
NULL
16
2023-01-20
NULL
NULL
NULL
17
2023-01-21
NULL
NULL
NULL
18
2023-01-22
NULL
NULL
NULL
19
2023-01-23
NULL
NULL
NULL
20
2023-01-24
NULL
NULL
NULL
21
2023-01-25
NULL
NULL
NULL
22
2023-01-26
NULL
NULL
NULL
23
2023-01-27
NULL
NULL
NULL
24
2023-01-28
NULL
NULL
NULL
25
2023-01-29
NULL
NULL
NULL
26
2023-01-30
NULL
NULL
NULL
27
2023-01-31
NULL
NULL
NULL
28
2023-02-01
2023-02-01
2023-02-28
M
Null
2023-02-01
2023-02-01
2023-02-28
E
Null
2023-02-01
2023-02-01
2023-02-28
D
1
2023-02-01
2023-02-01
2023-02-28
C
Null
2023-02-02
NULL
NULL
NULL
2

Related

How can I put the column values as column headers in Big Query

I have a bigquery table that looks like this:
date name grade
2021-02-01 John 7.5
2021-02-01 Martha 8.6
2021-02-01 Peter 5.5
2021-02-02 John 8.0
2021-02-03 John 7.0
2021-02-04 Peter 4.0
2021-02-04 Martha 9.1
2021-02-04 Paul 6.3
2021-02-05 John 7.1
2021-02-05 Mary 1.5
2021-02-06 John 3.3
2021-02-07 Martha 1.8
I would like to create a new table, but that has as columns the names of each person that column names has.
Like this:
date John Martha Peter Paul Mary
2021-02-01 7.5 8.6 5.5 null null
2021-02-02 8.0 null null null null
2021-02-03 7.0 null null null null
2021-02-04 null 9.1 4.0 6.3 null
2021-02-05 7.1 null null null 1.5
2021-02-06 3.3 null null null null
2021-02-07 null 1.8 null null null
What I have tried is this:
select date,
case when name = 'John' then grade end as John,
case when name = 'Martha' then grade end as Martha,
case when name = 'Peter' then grade end as Peter,
case when name = 'Paul' then grade end as Paul,
case when name = 'Mary' then grade end as Mary
from mytable
order by date
But what I get is:
date John Martha Peter Paul Mary
2021-02-01 7.5 null null null null
2021-02-01 null 8.6 null null null
2021-02-01 null null 5.5 null null
2021-02-02 8.0 null null null null
2021-02-03 7.0 null null null null
2021-02-04 null null 4.0 null null
2021-02-04 null 9.1 null null null
2021-02-04 null null null 6.3 null
2021-02-05 7.1 null null null null
2021-02-05 null null null null 1.5
2021-02-06 3.3 null null null null
2021-02-07 null 1.8 null null null
Use aggregation:
select date,
max(case when name = 'John' then grade end) as John,
max(case when name = 'Martha' then grade end) as Martha,
max(case when name = 'Peter' then grade end) as Peter,
max(case when name = 'Paul' then grade end) as Paul,
max(case when name = 'Mary' then grade end) as Mary
from mytable
group by date
order by date

SQL-SERVER Filter By ROW_NUMBER and Another Condition

I have this SQL-SERVER Query:
SELECT t.Id,
t.ClientTemplateFieldId,
t.ClientTemplateFieldOptionId,
t.TextData,
t.NumberData,
t.DateTimeData,
t.TreatmentDate,
t.rn FROM
(
SELECT tfd.Id,
tfd.ClientTemplateFieldId,
tfd.ClientTemplateFieldOptionId,
tfd.TextData,
tfd.NumberData,
tfd.DateTimeData,
t.TreatmentDate,
ROW_NUMBER() OVER (PARTITION BY tfd.ClientTemplateFieldId ORDER BY t.TreatmentDate DESC) as rn
FROM TemplateFieldData tfd
INNER JOIN
Treatment t ON tfd.TreatmentId = t.Id
WHERE tfd.IsDeleted = 0 AND
t.IsDeleted = 0 AND
tfd.ClientId = 2 AND
t.ClientId = 2 AND
t.PatientId = 6003720 AND
tfd.ClientTemplateFieldId IN
(1873,1874,1875,1876)
) AS t
--WHERE CLAUSE...
And this is the table result of the above query:
Id ClientTemplateFieldId ClientTemplateFieldOptionId TextData NumberData DateTimeData TreatmentDate rn
4324 1873 1627 NULL NULL NULL 2021-01-14 09:00:00.000 1
4325 1873 1628 NULL NULL NULL 2021-01-14 09:00:00.000 2
4326 1873 1631 NULL NULL NULL 2021-01-14 09:00:00.000 3
4322 1873 1627 NULL NULL NULL 2021-01-13 09:00:00.000 4
4323 1873 1629 NULL NULL NULL 2021-01-13 09:00:00.000 5
4320 1873 1626 NULL NULL NULL 2021-01-12 22:00:00.000 6
4321 1873 1630 NULL NULL NULL 2021-01-12 22:00:00.000 7
4338 1874 NULL SomeTxt NULL NULL 2021-01-14 09:00:00.000 1
4330 1874 NULL SomeTxt NULL NULL 2021-01-13 09:00:00.000 2
4327 1874 NULL SomeTxt NULL NULL 2021-01-12 22:00:00.000 3
4328 1875 NULL NULL 10.00 NULL 2021-01-12 22:00:00.000 1
4331 1876 1634 NULL NULL NULL 2021-01-13 09:00:00.000 1
4329 1876 1632 NULL NULL NULL 2021-01-12 22:00:00.000 2
What should I put in the Where Clause in order to retrieve all rows with row_number = 1 and
also if there is a row with the same TreatmentDate and ClientTemplateFieldId as of one of the rows where
row_number = 1, I need that row to.
Meaning I need this result:
Id ClientTemplateFieldId ClientTemplateFieldOptionId TextData NumberData DateTimeData TreatmentDate rn
4324 1873 1627 NULL NULL NULL 2021-01-14 09:00:00.000 1
4325 1873 1628 NULL NULL NULL 2021-01-14 09:00:00.000 2
4326 1873 1631 NULL NULL NULL 2021-01-14 09:00:00.000 3
4338 1874 NULL SomeTxt NULL NULL 2021-01-14 09:00:00.000 1
4328 1875 NULL NULL 10.00 NULL 2021-01-12 22:00:00.000 1
4331 1876 1634 NULL NULL NULL 2021-01-13 09:00:00.000 1
You could use MAX() window function instead of ROW_NUMBER() to get all the rows with the latest TreatmentDate for each ClientTemplateFieldId:
SELECT t.Id,
t.ClientTemplateFieldId,
t.ClientTemplateFieldOptionId,
t.TextData,
t.NumberData,
t.DateTimeData,
t.TreatmentDate,
t.rn
FROM (
SELECT tfd.Id,
tfd.ClientTemplateFieldId,
tfd.ClientTemplateFieldOptionId,
tfd.TextData,
tfd.NumberData,
tfd.DateTimeData,
t.TreatmentDate,
ROW_NUMBER() OVER (PARTITION BY tfd.ClientTemplateFieldId ORDER BY t.TreatmentDate DESC) AS rn,
MAX(t.TreatmentDate) OVER (PARTITION BY tfd.ClientTemplateFieldId) AS max_TreatmentDate
FROM TemplateFieldData tfd
INNER JOIN
Treatment t ON tfd.TreatmentId = t.Id
WHERE tfd.IsDeleted = 0 AND
t.IsDeleted = 0 AND
tfd.ClientId = 2 AND
t.ClientId = 2 AND
t.PatientId = 6003720 AND
tfd.ClientTemplateFieldId IN
(1873,1874,1875,1876)
) AS t
WHERE t.TreatmentDate = t.max_TreatmentDate
ROW_NUMBER() is not actually needed but I kept it in my query just in case you want it in the results.
If I assume that this entire query result (without the where condition) is created as a View called T for example, you can do the following:
SELECT *
FROM T
WHERE T.rn = 1
OR EXISTS
(SELECT 1 FROM T as T2
WHERE T2.TreatmentDate = T.TreatmentDate
AND T2.ClientTemplateFieldId = T.ClientTemplateFieldId
AND T.rn != 1
AND T2.rn = 1)

A problem with a simple join taking too long to finish

I´m having trouble identifying who to fix this simple join.
SELECT A.CUSER,
COUNT(*) AS QTD
FROM BO_PRODUCTCONFIG A WITH (NOLOCK)
INNER JOIN BO_PRICER001 B WITH (NOLOCK)
ON (
A.CIDBRANCH = B.CIDBRANCH
OR B.CIDBRANCH IS NULL
)
AND (
A.CIDPRODUCT = B.CIDPRODUCT
OR B.CIDPRODUCT IS NULL
)
My problem is, after adding the column B.CIDPRODUCT IS NULL this query takes hours to finish.
table BO_PRICER001 rows 286537
table BO_PRODUCTCONFIG rows 7934844
on the table BO_PRICER001 exists 15329 rows with null
with B.CIDPRODUCT IS NULL the live query show this plan:
and without this column the plan is:
I already try to force an index and switch to a subquery but the problem continues.
if possible, I really appreciate a help.
Sample of data from PRICER001
CIDPRICE CIDCOMPANY CIDBRANCH CIDTABLEPRICE CIDCOMMISSIONGROUP CIDPRODUCT CIDPERIODPAYMENT DENDDATEVALIDITY DSTARTDATEVALIDITY NINCREASEPERCENT LACTIVE DLASTUPDATE NINCREASEPERCENTORIG
XPTO0319 XPTO NULL NULL NULL NULL 319 9999-12-31 00:00:00.000 2018-08-27 00:00:00.000 2.100 1 2020-10-07 05:07:37.917 21.000
XPTO0321 XPTO NULL NULL NULL NULL 321 9999-12-31 00:00:00.000 2014-08-06 00:00:00.000 2.100 1 2020-10-07 05:07:37.917 21.000
XPTO0326 XPTO NULL NULL NULL NULL 326 9999-12-31 00:00:00.000 2018-08-27 00:00:00.000 3.150 1 2020-10-07 05:07:37.917 31.500
XPTO0328 XPTO NULL NULL NULL NULL 328 9999-12-31 00:00:00.000 2014-08-06 00:00:00.000 3.150 1 2020-10-07 05:07:37.917 31.500
XPTO0345 XPTO NULL NULL NULL NULL 345 9999-12-31 00:00:00.000 2020-05-29 00:00:00.000 4.530 1 2020-10-07 05:07:37.917 45.300
XPTO0390 XPTO NULL NULL NULL NULL 390 9999-12-31 00:00:00.000 2020-05-29 00:00:00.000 9.130 1 2020-10-07 05:07:37.917 91.300
XPTO0412 XPTO NULL NULL NULL NULL 412 9999-12-31 00:00:00.000 2020-05-29 00:00:00.000 11.500 1 2020-10-07 05:07:37.917 115.000
XPTO0428 XPTO NULL NULL NULL NULL 428 9999-12-31 00:00:00.000 2020-05-29 00:00:00.000 2.640 1 2020-10-07 05:07:37.917 26.400
XPTO0460 XPTO NULL NULL NULL NULL 460 9999-12-31 00:00:00.000 2020-05-29 00:00:00.000 5.690 1 2020-10-07 05:07:37.917 56.900
XPTO0515 XPTO NULL NULL NULL NULL 515 9999-12-31 00:00:00.000 2020-05-29 00:00:00.000 13.900 1 2020-10-07 05:07:37.917 139.000
Sample of data from PRODUCTCONFIG
CIDPRODUCTCONFIG CIDPRODUCT CIDPRODUCTMARKET CIDPRODUCTBUSINESS CIDPRODUCTLINE CIDPRODUCTGROUP CIDPRODUCTSUBGROUP CIDREGION CIDBRANCH CIDTEAM CIDTERRITORY LACTIVE LFLAG DLASTUPDATE CUSER CTPDCA
5840992036 584099 XPTO XPTO XPTO ACM FFK NULL 1685 NULL 2036 1 NULL 2020-10-07 00:49:15.837 2036 1
5840992037 584099 XPTO XPTO XPTO ACM FFK NULL 1685 NULL 2037 1 NULL 2020-10-07 00:49:15.837 2037 1
5840992038 584099 XPTO XPTO XPTO ACM FFK NULL 1685 NULL 2038 1 NULL 2020-10-07 00:49:15.837 2038 1
5840992039 584099 XPTO XPTO XPTO ACM FFK NULL 1685 NULL 2039 1 NULL 2020-10-07 00:49:15.837 2039 1
5840992040 584099 XPTO XPTO XPTO ACM FFK NULL 1685 NULL 2040 1 NULL 2020-10-07 00:49:15.837 2040 1
5840992041 584099 XPTO XPTO XPTO ACM FFK NULL 1685 NULL 2041 1 NULL 2020-10-07 00:49:15.837 2041 1
5840992042 584099 XPTO XPTO XPTO ACM FFK NULL 1402 NULL 2042 1 NULL 2020-10-07 00:18:20.783 2042 1
5840992044 584099 XPTO XPTO XPTO ACM FFK NULL 1685 NULL 2044 1 NULL 2020-10-07 00:49:15.837 2044 1
5840992045 584099 XPTO XPTO XPTO ACM FFK NULL 1685 NULL 2045 1 NULL 2020-10-07 00:49:15.837 2045 1
5840992048 584099 XPTO XPTO XPTO ACM FFK NULL 1686 NULL 2048 1 NULL 2020-10-07 00:50:20.223 2048 1
OR kills performance. A typical workaround are multiple LEFT JOIN. Perhaps:
I´m having trouble identifying who to fix this simple join.
SELECT pc.CUSER,
COUNT(*) AS QTD
FROM BO_PRODUCTCONFIG pc LEFT JOIN
BO_PRICER001 prbp
ON pc.CIDBRANCH = prbp.CIDBRANCH AND
pc.CIDPRODUCT = prbp.CIDPRODUCT LEFT JOIN
BO_PRICER001 prb
ON pc.CIDBRANCH = prb.CIDBRANCH AND
prbp.CIDPRODUCT IS NULL LEFT JOIN
BO_PRICER001 prp
ON prp.CIDBRANCH IS NULL AND
prp.CIDPRODUCT = pc.CIDPRODUCT LEFT JOIN
BO_PRICER001 pr
ON prp.CIDBRANCH IS NULL AND
prp.CIDPRODUCT IS NULL
WHERE prbp.CIDBRANCH is not null OR prb.CIDBRANCH is not null OR pr.CIDPRODUCT is not null or pr.<some other column> is not null;

Case when Statement Not Working with my data

While Working case statement i got stuck
For Eg i have below scenario
Amt StartDate EndDate Port Trade
10.00 9/21/2018 9/21/2020 NULL NULL
54,523.00 11/14/2018 11/15/2018 NULL NULL
756.00 11/14/2018 11/15/2018 NULL NULL
456.00 11/14/2018 11/15/2018 NULL NULL
86.00 11/14/2018 11/15/2018 NULL NULL
86.00 11/14/2018 11/15/2018 NULL NULL
453.00 11/14/2018 11/15/2018 NULL NULL
786.00 11/14/2018 11/15/2018 NULL NULL
86.00 11/14/2018 11/15/2018 NULL NULL
568.00 11/14/2018 11/15/2018 NULL NULL
12,358.00 11/14/2018 11/15/2018 NULL NULL
45,388.00 11/5/2018 12/5/2018 NULL NULL
75,368.00 8/9/2018 12/20/2018 call collateral
783,678.00 7/13/2018 1/14/2019 NULL NULL
1)what i am looking for is when startdate and enddate difference = 1
or Port like '%call%' and Trade='collateral' then amt
2) when startdate and enddate difference > 7
and Port not like '%call%' and Trade <> 'collateral' then amt
My First Condition Works
select CASE WHEN DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE))=1
OR (Port like 'Call' and [Trade]='collateral')
THEN amt ELSE 0 END AS money1
from tablename
But Second Condition not Working
select CASE WHEN (DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE))> 7 AND
DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE)) <= 9999)
and [Trade] <> 'collateral' and Portfolio not like '%Call%'
THEN amt ELSE 0 END AS money2 from tablename
excepted o/p
Amt StartDate EndDate money1 money2
10.00 9/21/2018 9/21/2020 10.00
54,523.00 11/14/2018 11/15/2018 54,523.00
756.00 11/14/2018 11/15/2018 756.00
456.00 11/14/2018 11/15/2018 456.00
86.00 11/14/2018 11/15/2018 86.00
86.00 11/14/2018 11/15/2018 86.00
453.00 11/14/2018 11/15/2018 453.00
786.00 11/14/2018 11/15/2018 786.00
86.00 11/14/2018 11/15/2018 86.00
568.00 11/14/2018 11/15/2018 568.00
12,358.00 11/14/2018 11/15/2018 12,358.00
45,388.00 11/5/2018 12/5/2018 45,388.00
75,368.00 8/9/2018 12/20/2018 75,368.00
783,678.00 7/13/2018 1/14/2019 783,678.00
Need Help i am using Sql server 2012.
Check whether PortFolio and Trade are NULL.
So:
SELECT CASE WHEN DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE))> 7 AND
DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE)) <= 9999 AND
([Trade] IS NULL OR [Trade] <> 'collateral') AND
(Portfolio IS NULL OR Portfolio NOT LIKE '%call%')
THEN amt ELSE 0 END AS money2
FROM tablename;
Your like operator should be '%Call%'
select CASE WHEN (DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE))> 7 AND
DATEDIFF(DAY,CAST(Startdate AS DATE),CAST(Enddate AS DATE)) <= 9999)
and [Trade] <> 'collateral' and Portfolio not like '%Call%'
THEN amt ELSE 0 END AS money2 from tablename

Comparing and Merging two tables in SQL Server 2005

I have two scripts which produce the following two tables(t1 and t2).
Is there a way of merging t1 and t2 to make t3?
The first 5 rows of each table have the same value in the first column (Number).
However in t1 the columns PreOpOrg and PreOpModality are populated but in t2 they are NULL, and vice versa for columns PostOpOrg and PostOpModality.
So the merge would populate the columns with the non-NULL values from each table (t1 and t2). For the rows in t1 and t2 that only appear in one table then they should appear in t3 as if a normal UNION were being attempted.
Unfortunately while I am working in SQL2005 the script needs to be compatible with SQL2000.
Many thanks.
--t1
Number DateOfBirth PreOpOrg PreOpModality PostOpOrg PostOpModality DiagDate
8643576344 01/04/1935 00:00:00 RBA11 2 NULL NULL 04/09/2012 00:00
2345680016 01/01/1960 00:00:00 RBA11 4 NULL NULL 11/11/2011 00:00
6955934252 20/01/1980 00:00:00 RBA11 4 NULL NULL 22/08/2012 00:00
2234582369 28/12/1940 00:00:00 RBA11 5 NULL NULL 02/11/2011 00:00
9477684368 01/02/1959 00:00:00 RBA11 5 NULL NULL 02/01/2014 00:00
--t2
Number DateOfBirth PreOpOrg PreOpModality PostOpOrg PostOpModality DiagDate
8643576344 01/04/1935 00:00:00 NULL NULL RBA11 2 04/09/2012 00:00
2345680016 01/01/1960 00:00:00 NULL NULL RBA11 2 11/11/2011 00:00
6955934252 20/01/1980 00:00:00 NULL NULL RN325 5 22/08/2012 00:00
2234582369 28/12/1940 00:00:00 NULL NULL RBA11 5 02/11/2011 00:00
9477684368 01/02/1959 00:00:00 NULL NULL RBA11 5 02/01/2014 00:00
0000001564 01/01/1970 00:00:00 NULL NULL RBA11 2 08/08/2012 00:00
4225127842 11/07/1948 00:00:00 NULL NULL RBA11 2 10/09/2011 00:00
0909093842 01/07/1957 00:00:00 NULL NULL RN325 5 26/08/2009 00:00
8528528855 05/12/1969 00:00:00 NULL NULL RBA11 5 25/10/2010 00:00
1000001367 05/05/1950 00:00:00 NULL NULL RBA11 5 08/04/2011 00:00
2234573513 30/11/1946 00:00:00 NULL NULL RN325 5 20/04/2011 00:00
1000001566 18/03/1951 00:00:00 NULL NULL RBA11 5 20/03/2012 00:00
0000001600 01/01/1970 00:00:00 NULL NULL RBA11 5 28/03/2013 00:00
1000001621 12/12/1950 00:00:00 NULL NULL RBA11 5 06/12/2013 00:00
--t3
Number DateOfBirth PreOpOrg PreOpModality PostOpOrg PostOpModality DiagDate
8643576344 01/04/1935 00:00:00 RBA11 2 RBA11 2 04/09/2012 00:00
2345680016 01/01/1960 00:00:00 RBA11 4 RBA11 2 11/11/2011 00:00
6955934252 20/01/1980 00:00:00 RBA11 4 RN325 5 22/08/2012 00:00
2234582369 28/12/1940 00:00:00 RBA11 5 RBA11 5 02/11/2011 00:00
9477684368 01/02/1959 00:00:00 RBA11 5 RBA11 5 02/01/2014 00:00
0000001564 01/01/1970 00:00:00 NULL NULL RBA11 2 08/08/2012 00:00
4225127842 11/07/1948 00:00:00 NULL NULL RBA11 2 10/09/2011 00:00
0909093842 01/07/1957 00:00:00 NULL NULL RN325 5 26/08/2009 00:00
8528528855 05/12/1969 00:00:00 NULL NULL RBA11 5 25/10/2010 00:00
1000001367 05/05/1950 00:00:00 NULL NULL RBA11 5 08/04/2011 00:00
2234573513 30/11/1946 00:00:00 NULL NULL RN325 5 20/04/2011 00:00
1000001566 18/03/1951 00:00:00 NULL NULL RBA11 5 20/03/2012 00:00
0000001600 01/01/1970 00:00:00 NULL NULL RBA11 5 28/03/2013 00:00
1000001621 12/12/1950 00:00:00 NULL NULL RBA11 5 06/12/2013 00:00
Considering to your data and requirement to run on SQL 2000 I am providing you below solution.
Please let me know in case you face any issue. For my convenience I have added only few records from your sample. If you are using SQL Server 2008 or higher version you can use of COALESCE function as explained here.
create table t1
(
Number VARCHAR(10),
DateOfBirth DATETIME,
PreOpOrg VARCHAR(10),
PreOpModality INT,
PostOpOrg VARCHAR(10),
PostOpModality INT,
DiagDate DATETIME
)
create table t2
(
Number VARCHAR(10),
DateOfBirth DATETIME,
PreOpOrg VARCHAR(10),
PreOpModality INT,
PostOpOrg VARCHAR(10),
PostOpModality INT,
DiagDate DATETIME
)
INSERT INTO T1 values (8643576344,'01/04/1935 00:00:00','RBA11',2,NULL,NULL,'04/09/2012 00:00'),
(2345680016,'01/01/1960 00:00:00','RBA11',4,NULL,NULL,'11/11/2011 00:00')
INSERT INTO T2 values ('8643576344','01/04/1935 00:00:00',NULL,NULL,'RBA11',2,'04/09/2012 00:00'),
('0000001564','01/01/1970 00:00:00',NULL,NULL,'RBA11',2,'08/08/2012 00:00'),
('2345680016','01/01/1960 00:00:00',NULL,NULL,'RBA11',2,'11/11/2011 00:00' )
SELECT * FROM T1
SELECT * FROM T2
SELECT CASE WHEN T1.Number IS NOT NULL THEN T1.Number ELSE T2.NUMBER END AS Number,
CASE WHEN T1.DateOfBirth IS NOT NULL THEN T1.DateOfBirth ELSE T2.DateOfBirth END AS DateOfBirth,
CASE WHEN T1.PreOpOrg IS NOT NULL THEN T1.PreOpOrg ELSE T2.PreOpOrg END AS PreOpOrg,
CASE WHEN T1.PreOpModality IS NOT NULL THEN T1.PreOpModality ELSE T2.PreOpModality END AS PreOpModality,
CASE WHEN T1.PostOpOrg IS NOT NULL THEN T1.PostOpOrg ELSE T2.PostOpOrg END AS PostOpOrg,
CASE WHEN T1.PostOpModality IS NOT NULL THEN T1.PostOpModality ELSE T2.PostOpModality END AS PostOpModality,
CASE WHEN T1.DiagDate IS NOT NULL THEN T1.DiagDate ELSE T2.DiagDate END AS DiagDate
FROM T1
RIGHT JOIN T2 ON T1.Number = T2.Number
Another solution can be
SELECT
ISNULL(T1.Number, T2.NUMBER) AS Number
,ISNULL(T1.DateOfBirth, T2.DateOfBirth) AS DateOfBirth
,ISNULL(T1.PreOpOrg, T2.PreOpOrg) AS PreOpOrg
,ISNULL(T1.PreOpModality, T2.PreOpModality) AS PreOpModality
,ISNULL(T1.PostOpOrg, T2.PostOpOrg) AS PostOpOrg
,ISNULL(T1.PostOpModality, T2.PostOpModality) AS PostOpModality
,ISNULL(T1.DiagDate, T2.DiagDate) AS DiagDate
FROM T1
RIGHT JOIN T2
ON T1.Number = T2.Number