1

Can someone help modify the following SQL script so that I'm able to do a GROUP BY and replace the character (null).

At the moment I have two tables:

Table 1

PK_LOYALTYACCOUNT     COUNTRY_ID     CDC_TYPE
int                   varchar(50)    varchar(50)
666                   DE             U
98                    DE             U
27975373              DE             U
666                   DE             N

Table 2

PK_LOYALTYACCOUNT     COUNTRY_ID     CDC_TYPE
int                   varchar(50)    varchar(50)
666                   DE             U
75                    DE             U
27975370              DE             U
578                   WE             N
54                    RT             N

The results from the sql script produces the following table:

is_deleted     PK_LOYALTYACCOUNT     COUNTRY_ID     CDC_TYPE
int            int                   varchar(50)    varchar(50)
1              666                   DE             U
0              666                   DE             U
(null)         769                   DE             U
(null)         578                   WE             N
(null)         54                    RT             N

However, I would like to group by the table (in any fashion) and remove or replace the null values, such that the tables appear something as follows:

COUNTRY_ID     CDC_TYPE     PK_LOYALTYACCOUNT    is_deleted

DE             U            666                  1
DE             U            666                  0
RT             N            54                   replace null
WE             N            578                  replace null
DE             N            769                  replace null

The sql script is as follows:

with cmn as 
  ( SELECT a.CDC_TYPE,
           a. PK_LOYALTYACCOUNT, --Add these also in CTE result set 
           a.COUNTRY_ID --Add these also in CTE result set 
    FROM  tabled  a  
    INNER JOIN tablee b  
    ON a.COUNTRY_ID = b.COUNTRY_ID 
    AND a.PK_LOYALTYACCOUNT = b.PK_LOYALTYACCOUNT  
    AND a.CDC_TYPE = 'U'
    )
 SELECT 1 AS is_deleted, 
        a.*  
 FROM  tabled  a  
 INNER JOIN cmn  
 ON a.CDC_TYPE = cmn.CDC_TYPE 
 and  a.COUNTRY_ID = cmn.COUNTRY_ID  
 AND a.PK_LOYALTYACCOUNT = cmn.PK_LOYALTYACCOUNT

 UNION ALL  
 SELECT 0 AS is_deleted, 
        b.*  
 FROM tablee  b  
 INNER JOIN cmn  
 ON b.CDC_TYPE = cmn.CDC_TYPE 
 and b.COUNTRY_ID = cmn.COUNTRY_ID 
 AND b.PK_LOYALTYACCOUNT = cmn.PK_LOYALTYACCOUNT
UNION ALL 
SELECT NULL AS CDC_TYPE,
       a.* 
FROM   tabled a 
WHERE  a.CDC_TYPE = 'N' 
UNION ALL 
SELECT NULL AS CDC_TYPE, 
       b.* 
FROM   tablee b 
WHERE  b.CDC_TYPE = 'N'

Thanks in advance

Nandu
  • 103
  • 9
Carltonp
  • 1,166
  • 5
  • 19
  • 39
  • Post the tables and data as text [READ THIS](http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking-a-question/285557#285557) – Juan Carlos Oropeza Dec 24 '18 at 08:09
  • Please explain the logic for the desire output. Please read [**How-to-Ask**](http://stackoverflow.com/help/how-to-ask) And here is a great place to [**START**](http://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/) to learn how improve your question quality and get better answers. – Juan Carlos Oropeza Dec 24 '18 at 08:18
  • please see tables as suggestd --Sql Server 2014 Express Edition --Batches are separated by 'go' select @@version as 'sql server version' create table a ( PK_LOYALTYACCOUNT int, COUNTRY_ID varchar(50), CDC_TYPE varchar(50)); insert into a values (666,'DE','U'),(98 ,'DE','U'),(27975373,'DE','U'); select * from a; create table b ( PK_LOYALTYACCOUNT int, COUNTRY_ID varchar(50), CDC_TYPE varchar(50)); insert into b values (666,'DE','U'),(75 ,'DE','U'),(27975370,'DE','U'); – Carltonp Dec 24 '18 at 09:13
  • the desired output is the third image.thanks – Carltonp Dec 24 '18 at 09:14
  • Dont add code in the comment section, just edit your question to include aditional information. The images without explanation doesnt help. :'( – Juan Carlos Oropeza Dec 24 '18 at 09:14
  • @JuanCarlosOropeza, thanks and duly noted for next time – Carltonp Dec 24 '18 at 09:59
  • That sql script doesn't produce that "results from the sql script" based on the sample data. F.e. there's no 769 in both tables, and why no 27975373 in the results? – LukStorms Dec 24 '18 at 12:19

1 Answers1

2

You could perhaps use a string for the "is_deleted" in all of those unioned queries.

But then maybe this query could also be simplified via a FULL JOIN

For example:

SELECT PK_LOYALTYACCOUNT, COUNTRY_ID, CDC_TYPE, IS_DELETED
FROM
(
    SELECT 
    COALESCE(d.PK_LOYALTYACCOUNT, e.PK_LOYALTYACCOUNT) AS PK_LOYALTYACCOUNT, 
    COALESCE(d.COUNTRY_ID, e.COUNTRY_ID) AS COUNTRY_ID, 
    COALESCE(d.CDC_TYPE, e.CDC_TYPE) AS CDC_TYPE,
    (CASE 
     WHEN d.CDC_TYPE = e.CDC_TYPE AND d.CDC_TYPE = 'U' THEN '1' 
     WHEN d.CDC_TYPE = e.CDC_TYPE AND d.CDC_TYPE = 'N' THEN '0' 
     ELSE '' 
     END) AS IS_DELETED
    FROM tabled d
    FULL JOIN tablee e 
      ON e.PK_LOYALTYACCOUNT = d.PK_LOYALTYACCOUNT 
     AND e.COUNTRY_ID = d.COUNTRY_ID 
     AND e.CDC_TYPE = d.CDC_TYPE
    WHERE d.CDC_TYPE = e.CDC_TYPE 
       OR d.CDC_TYPE = 'N'
       OR e.CDC_TYPE = 'N'
) AS cmn 
GROUP BY PK_LOYALTYACCOUNT, COUNTRY_ID, CDC_TYPE, IS_DELETED
ORDER BY IS_DELETED DESC, PK_LOYALTYACCOUNT, COUNTRY_ID, CDC_TYPE;
LukStorms
  • 28,916
  • 5
  • 31
  • 45
  • absolutely perfect. Really very much appreciated. Sorry for badly worded question.. However, you nailed it. – Carltonp Dec 24 '18 at 15:17
  • Just one last comment, I tried entering the commands on my spark.sql by enclosing your commands within """YOURCOMMANDSHERE""", but it returns an error saying ('EOF in multi-line string', (1, 73)). If you're not familiar with spark.sql don't worry, if you familiare with spark.sql your help will be greatly appreciated – Carltonp Dec 24 '18 at 16:00
  • @Carltonp Nope, never used that. But a bit of googling hinted that stuff like that could happen if there's missing apostrophes somewhere. F.e. [here](https://stackoverflow.com/questions/7885136/) – LukStorms Dec 24 '18 at 16:05
  • can someone help modify the original script in the question so that the Is_deleted column is removed? – Carltonp Dec 28 '18 at 18:34
  • @Carltonp Not sure what you mean. You created the question, so you can edit it. And this question was about what's supposed to show in the "is_deleted"? Do you mean that it should have another alias name? If then I don't really see the point, once you got something that works you can edit it for your personal scripts anyway, the question doesn't need to be adapted for that. – LukStorms Dec 28 '18 at 19:07
  • thanks for getting in touch. I created the question but I didn't create the script. It was kindly compiled for me, but I can't get in touch with person that original wrote the script – Carltonp Dec 28 '18 at 19:14
  • I tried editing by removing `SELECT 1 AS is_deleted, a.* FROM tabled a INNER JOIN cmn ON a.CDC_TYPE = cmn.CDC_TYPE and a.COUNTRY_ID = cmn.COUNTRY_ID AND a.PK_LOYALTYACCOUNT = cmn.PK_LOYALTYACCOUNT UNION ALL SELECT 0 AS is_deleted, b.* FROM tablee b INNER JOIN cmn ON b.CDC_TYPE = cmn.CDC_TYPE and b.COUNTRY_ID = cmn.COUNTRY_ID AND b.PK_LOYALTYACCOUNT = cmn.PK_LOYALTYACCOUNT UNION ALL ` but it kept on throwing errors – Carltonp Dec 28 '18 at 19:17
  • @Carltonp When you use `UNION` or `UNION ALL` then the column names or alias names from the top query will be shown for the result. So I assume it's because by removing those 2 that the first column is now named "CDC_TYPE". So change that alias name to "is_deleted"? And btw, using `*` has a disadvantage that if 1 of the tables get a new column, that the number of column in each query isn't the same anymore. So explicit listing the column names is normally safer. – LukStorms Dec 28 '18 at 20:44