| 
                
                    | 
                            
                                | Author | Topic |  
                                    | ianottStarting Member
 
 
                                        6 Posts | 
                                            
                                            |  Posted - 2014-11-14 : 12:48:37 
 |  
                                            | I am new to SQL and am trying to use the Select Distinct statement and am having some issues getting it to work.  When I use it on only 1 column it is working fine, but I want to use it on 2 columns it does not do what I expect it to.  Can someone help me out, here is my code: SELECT *INTO #NEWTEMPFROM DBO.REPORTDATA;ALTER TABLE #NEWTEMPALTER COLUMN UTCTime DATE Select Distinct UTCTime, SignalID, Count(1) as SignalIDCountINTO #SignalIDCountFROM #NEWTEMP (NOLOCK)Group by UTCTime, SignalIDOrder by UTCTime, SignalIDSelect A.SignalID, A.fValue,A.UTCTime, B.SignalIDCountFROM(Select SignalID, UTCTime,fValue,ROW_NUMBER() OVER(PARTITION BY SignalID ORDER BY fValue DESC) AS 'Sub_Rank'FROM #NEWTEMP (NOLOCK) ) AINNER JOIN #SignalIDCount B ONA.SignalID = B.SignalIDWhere A.Sub_rank = 1Group by A.SignalID, A.fValue,A.UTCTime, A.Sub_Rank, B.SignalIDCountRESULTS  In the results table I have 3 entries for each signalid with the same UTCTime, I only want to have 1 |  |  
                                    | tkizerAlmighty SQL Goddess
 
 
                                    38200 Posts | 
                                        
                                          |  Posted - 2014-11-14 : 12:54:11 
 |  
                                          | Which one do you want?Here it is with MAX:Select A.SignalID, A.fValue,A.UTCTime, MAX(B.SignalIDCount) AS SignalIDCountFROM(Select SignalID, UTCTime,fValue,ROW_NUMBER() OVER(PARTITION BY SignalID ORDER BY fValue DESC) AS 'Sub_Rank'FROM #NEWTEMP (NOLOCK) ) AINNER JOIN #SignalIDCount B ONA.SignalID = B.SignalIDWhere A.Sub_rank = 1Group by A.SignalID, A.fValue,A.UTCTime, A.Sub_RankTara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |  
                                          |  |  |  
                                    | ianottStarting Member
 
 
                                    6 Posts | 
                                        
                                          |  Posted - 2014-11-14 : 13:18:21 
 |  
                                          | Thanks for responding, but I do not think that will help me.  The problem is that only 1 of those results is correct.  For SignalID 1100 the result with a SignalIDCount of 40 is correct, but for 1100 the one with SignalIDCount of 1 is correct.  Somehow I am making extras show up in my results.What I want is to:1 - Take the data from dbo.ReportData, copy it into #NEWTEMP2 - Change the UTCTime column from datatype DATETIME to datatype DATE3 - Count how many times each SignalId was recorded each day4 - Take the maximum value for fValue for each signal each dayDoes that make sense? |  
                                          |  |  |  
                                    | tkizerAlmighty SQL Goddess
 
 
                                    38200 Posts | 
                                        
                                          |  Posted - 2014-11-14 : 13:21:14 
 |  
                                          | You'll need to post sample data at this point (before and after).Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |  
                                          |  |  |  
                                    | tkizerAlmighty SQL Goddess
 
 
                                    38200 Posts | 
                                        
                                          |  Posted - 2014-11-14 : 13:21:42 
 |  
                                          | Here's an article that shows how to post it: http://www.sqlservercentral.com/articles/Best+Practices/61537/Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |  
                                          |  |  |  
                                    | ianottStarting Member
 
 
                                    6 Posts | 
                                        
                                          |  Posted - 2014-11-14 : 14:20:36 
 |  
                                          | FIRST TABLE--===== If the test table already exists, drop it     IF OBJECT_ID('TempDB..#mytable','U') IS NOT NULL         DROP TABLE #mytable--===== Create the test table with  CREATE TABLE #mytable 	(	[SignalID] [float] NULL,	[UTCTime] [datetime] NULL,	[fValue] [float] NULL,	)	 SELECT 'SELECT '      + QUOTENAME(SignalID,'''')+','      + QUOTENAME(UTCTime,'''')+','      + QUOTENAME(fValue,'''')+','      + ' UNION ALL' FROM ReportData --===== All Inserts into the IDENTITY column    SET IDENTITY_INSERT #mytable ON--===== Insert the test data into the test table INSERT INTO #mytable        (SignalID, UTCTime, fValue)SELECT '1100','Oct 1 2014 10:30PM','1', UNION ALLSELECT '1100','Oct 1 2014 10:30PM','5', UNION ALLSELECT '1101','Oct 1 2014 10:30PM','6', UNION ALLSELECT '1100','Oct 1 2014 10:30PM','14', UNION ALLSELECT '1101','Oct 1 2014 10:30PM','13', UNION ALLSELECT '1102','Oct 1 2014 10:30PM','12', UNION ALLSELECT '1100','Oct 1 2014 10:30PM','10', UNION ALLSELECT '1101','Oct 1 2014 10:30PM','9', UNION ALLSELECT '1102','Oct 1 2014 10:30PM','8', UNION ALLSELECT '1103','Oct 1 2014 10:30PM','7', UNION ALLSELECT '1100','Oct 2 2014 10:30PM','11', UNION ALLSELECT '1101','Oct 2 2014 10:30PM','21', UNION ALLSELECT '1102','Oct 2 2014 10:30PM','31', UNION ALLSELECT '1103','Oct 2 2014 10:30PM','41', UNION ALLSELECT '1101','Oct 2 2014 10:30PM','61', UNION ALLSELECT '1102','Oct 2 2014 10:30PM','161', UNION ALLSELECT '1103','Oct 2 2014 10:30PM','151', UNION ALLSELECT '1102','Oct 2 2014 10:30PM','121', UNION ALLSELECT '1103','Oct 2 2014 10:30PM','111', UNION ALLSELECT '1103','Oct 2 2014 10:30PM','71'--===== Set the identity insert back to normal    SET IDENTITY_INSERT #mytable ONDESIRED RESULTING TABLE--===== Create the test table with  CREATE TABLE #mytable2 	(	[SignalID] [float] NULL,	[UTCTime] [datetime] NULL,	[fValue] [float] NULL,	)	 SELECT 'SELECT '      + QUOTENAME(SignalID,'''')+','      + QUOTENAME(UTCTime,'''')+','      + QUOTENAME(fValue,'''')+','	  + QUOTENAME(SignalIDCount,'''')+','      + ' UNION ALL' FROM #mytable --===== All Inserts into the IDENTITY column    SET IDENTITY_INSERT #mytable2 ON--===== Insert the test data into the test table INSERT INTO #mytable2        (SignalID, UTCTime, fValue, SignalIDCount)SELECT '1100','Oct 1 2014 10:30PM','14','4', UNION ALLSELECT '1101','Oct 1 2014 10:30PM','13','3', UNION ALLSELECT '1102','Oct 1 2014 10:30PM','12','2', UNION ALLSELECT '1103','Oct 1 2014 10:30PM','7','1', UNION ALLSELECT '1100','Oct 2 2014 10:30PM','11','1', UNION ALLSELECT '1101','Oct 2 2014 10:30PM','61','2', UNION ALLSELECT '1102','Oct 2 2014 10:30PM','151','3', UNION ALLSELECT '1103','Oct 2 2014 10:30PM','151''4'--===== Set the identity insert back to normal    SET IDENTITY_INSERT #mytable2 ON |  
                                          |  |  |  
                                    | ianottStarting Member
 
 
                                    6 Posts | 
                                        
                                          |  Posted - 2014-11-14 : 15:38:34 
 |  
                                          | I figured it out.... I had to change my join statement in the last block of code....Select A.SignalID, A.fValue, B.UTCTime, B.SignalIDCountFROM(Select SignalID, UTCTime, fValue,ROW_NUMBER() OVER(PARTITION BY SignalID, UTCTime ORDER BY fValue DESC) AS 'Sub_Rank'FROM #NEWTEMP (NOLOCK) ) Aright JOIN #SignalIDCount B ONA.SignalID = B.SignalID AND A.UTCTime = B.UTCTimeWhere A.Sub_rank = 1Group by A.SignalID, A.fValue, B.UTCTime, A.Sub_Rank, B.SignalIDCount |  
                                          |  |  |  
                                |  |  |  |