Jump to content

Sql query help


Darling999

Recommended Posts

I have a situation where I need to Insert 2 rows(little info from second row) into single row.

 

Data looks like this:

 

Date                 Service      CurrMonth  CURR_COUNTS

 

2016-03-01      Internet     March                    10

 

2016-04-01      Internet      April                      10

 

I need a result set in this way: have to show the latest month date and put the Curr month as prev month in single row with corresponding counts.

 

DATE                  Service                 Current Month     PrevMonth    CURR_M_COUNTS       PREV_M _COUNTS

 

2016-04-01       Internet                April                     March                       10                              10

 

 

Please help.

 

Link to comment
Share on other sites

  • Replies 30
  • Created
  • Last Reply

Top Posters In This Topic

  • loveindia

    13

  • mettastar

    5

  • mtkr

    2

  • former

    2

Select t1.cols, t2.cols from table1 t1 inner join table1 t2 on t1.service = t2.service

 

Join condition can be more proper based on your fields 

Link to comment
Share on other sites

1 hour ago, rrc_2015 said:

Select t1.cols, t2.cols from table1 t1 inner join table1 t2 on t1.service = t2.service

 

Join condition can be more proper based on your fields 

if this ever worked let me know man... this will definitely not work...

Link to comment
Share on other sites

use this man...

;WITH CTE AS (
 SELECT '2016-03-01' AS [Date], 'Internet' AS [Service], 'March' AS CurrMonth, 10 AS CURR_COUNTS
 UNION ALL
 SELECT '2016-04-01', 'Internet', 'April', 10
 )
SELECT *
FROM CTE a 
INNER JOIN CTE b ON a.[Service] = b.[Service] AND a.[Date] > b.[Date]
WHERE a.[Date] = '2016-04-01'

Link to comment
Share on other sites

you don't have to do the date = filter using where clause, I just used it for my purpose... even if you remove it you get the same result man... good luck..

Link to comment
Share on other sites

1 minute ago, loveindia said:

use this man...

;WITH CTE AS (
 SELECT '2016-03-01' AS [Date], 'Internet' AS [Service], 'March' AS CurrMonth, 10 AS CURR_COUNTS
 UNION ALL
 SELECT '2016-04-01', 'Internet', 'April', 10
 )
SELECT *
FROM CTE a 
INNER JOIN CTE b ON a.[Service] = b.[Service] AND a.[Date] > b.[Date]
WHERE a.[Date] = '2016-04-01'

how can you hardcode date there.. it is better to use lead lag functions

Link to comment
Share on other sites

1 hour ago, mettastar said:

how can you hardcode date there.. it is better to use lead lag functions

in the other comment i posted if you see man, you don't need the date actually... i posted with date because I just wrote it that way to see it.. Lead or Lag will not work in this case.. please prove me wrong and I would like to see it too...

Link to comment
Share on other sites

Deniki preeceedig. Function vaadali I got the result using that. Just required rows ki ah preceding function used.

Link to comment
Share on other sites

31 minutes ago, nenuVedava said:

Deniki preeceedig. Function vaadali I got the result using that. Just required rows ki ah preceding function used.

code paste chey.. maakkooda help avuddi ga

Link to comment
Share on other sites

On 5/27/2016 at 3:11 PM, loveindia said:

in the other comment i posted if you see man, you don't need the date actually... i posted with date because I just wrote it that way to see it.. Lead or Lag will not work in this case.. please prove me wrong and I would like to see it too...

nuvvu ichina query lo what happens if you have 12 months of data? in your self join for december it will give 11 output records.. @loveindia

 

Run below and tell me how many records u r getting for April

WITH CTE AS (
 SELECT '2016-03-01' AS [Date], 'Internet' AS [Service], 'March' AS CurrMonth, 10 AS CURR_COUNTS
 UNION ALL
 SELECT '2016-04-01', 'Internet', 'April', 10
 UNION ALL
 SELECT '2016-02-01', 'Internet', 'Feb', 11
  UNION ALL
 SELECT '2016-01-01', 'Internet', 'Jan', 11
 )
SELECT *
FROM CTE a 
INNER JOIN CTE b ON a.[Service] = b.[Service] AND a.[Date] > b.[Date]
WHERE a.[Date] = '2016-04-01';

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...