Jump to content

SQL Experts


4Vikram

Recommended Posts

I have a table which has 4 columns in which fourth column is cumulative of first three and I am joining it with another table for referring. 

Example:

Table Alpha

ID     S1    S2    S3    STotal    DateID

1       1       2       0       3           June4

 

Table Beta has three columns

ID    MonthID           DateID

1     0                    Jan1

2     1                     Feb2

3     2                     March3

4     3                    April4

 

So I join this way, 

Select A*, B*

FROM Alpha A

Join Beta B on A.ID = B.ID and A.DateID = B.DateID

 

So what I want is a case statement where if I join I want to see the DateId wise rows with my S1, S2, S3 as 0 and my STotal should get cumulative of all S1 to S3. Like this

 

ID   SColumns    MonthID 

1   S1 as 0             Jan1

1   S2  as 0            Feb2

1   S3   as 0           March3

1   STotal as 3      April4

So the S1, S2, S3 are recurring, they will be same for all ID's and the MonthID is associated with S columns. Like S1 is for Jan1 MonthID like wise..

So what I am looking is if MonthID is Jan1 then S1 be 0, MonthID is Feb2 S2 be 0, MonthID is March3 then it be 0 as well but finally my STotal should be 3 (Like Cumulative)

I need a case statement but I donno how to write with two tables joined. Please help thanks

Link to comment
Share on other sites

14 minutes ago, 4Vikram said:

I have a table which has 4 columns in which fourth column is cumulative of first three and I am joining it with another table for referring. 

Example:

Table Alpha

ID     S1    S2    S3    STotal    DateID

1       1       2       0       3           June4

 

Table Beta has three columns

ID    MonthID           DateID

1     0                    Jan1

2     1                     Feb2

3     2                     March3

4     3                    April4

 

So I join this way, 

Select A*, B*

FROM Alpha A

Join Beta B on A.ID = B.ID and A.DateID = B.DateID

 

So what I want is a case statement where if I join I want to see the DateId wise rows with my S1, S2, S3 as 0 and my STotal should get cumulative of all S1 to S3. Like this

 

ID   SColumns    MonthID 

1   S1 as 0             Jan1

1   S2  as 0            Feb2

1   S3   as 0           March3

1   STotal as 3      April4

So the S1, S2, S3 are recurring, they will be same for all ID's and the MonthID is associated with S columns. Like S1 is for Jan1 MonthID like wise..

So what I am looking is if MonthID is Jan1 then S1 be 0, MonthID is Feb2 S2 be 0, MonthID is March3 then it be 0 as well but finally my STotal should be 3 (Like Cumulative)

I need a case statement but I donno how to write with two tables joined. Please help thanks

Use temp tables for simplicity or staging tables . Single query lone cheyyali ante performance debba 

  • Upvote 1
Link to comment
Share on other sites

3 minutes ago, sandie said:

Case lo 

if alpha.id= beta.id and alpha.dateid = beta.dateid then stotal else 0

let me try this

3 minutes ago, xano917 said:

Use temp tables for simplicity or staging tables . Single query lone cheyyali ante performance debba 

temp ante? do I need to put the output from both tables into temp first and then retrieve to modify?

Link to comment
Share on other sites

Just now, 4Vikram said:

let me try this

temp ante? do I need to put the output from both tables into temp first and then retrieve to modify?

Volatile table sample format ani check cheyye. Complex queries aithe tempory ga dhantlo load cheskuni. Condition through final select rastharu..

Link to comment
Share on other sites

select A.ID,

case when dateid = Jan1 then 'S1 as 0' 

          when dateid = Feb2 then 'S2 as 0'

           when dateid = Mar3 then 'S3 as 0'

            when dateid = Apr4 then 'STotal as '||A.STotal

end SColumns,

A.DateID from 

Alpha A

Join Beta B on A.ID = B.ID and A.DateID = B.DateID;

Link to comment
Share on other sites

8 minutes ago, sandie said:

Volatile table sample format ani check cheyye. Complex queries aithe tempory ga dhantlo load cheskuni. Condition through final select rastharu..

will check yourock

6 minutes ago, CheGuevara said:

select A.ID,

case when dateid = Jan1 then 'S1 as 0' 

          when dateid = Feb2 then 'S2 as 0'

           when dateid = Mar3 then 'S3 as 0'

            when dateid = Apr4 then 'STotal as '||A.STotal

end SColumns,

A.DateID from 

Alpha A

Join Beta B on A.ID = B.ID and A.DateID = B.DateID;

try this one

Link to comment
Share on other sites

4 hours ago, sandie said:

Volatile table sample format ani check cheyye. Complex queries aithe tempory ga dhantlo load cheskuni. Condition through final select rastharu..

If using volatile( assuming TD) the  please create stats on key columns

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...