We help IT Professionals succeed at work.

SELECT unique value on column - MS SQL (urgent)

jr_bautista
jr_bautista asked
on
3,474 Views
Last Modified: 2012-05-06
How can I get a unique TM column on this query, I need to get the unique time(tm). At this query I cannot use it by distinct.  

Sorry, this is urgent.

SELECT DISTINCT RIGHT(CONVERT(VARCHAR(19), CONVERT(SMALLDATETIME, TIME), 0), 7) AS tm, TIME AS tme, DATE AS dte
FROM         tbl1
WHERE     (GETDATE() + 2 < DATE)
GROUP BY TIME, DATE
ORDER BY tme

  TM                   tme                         DTE
 8:15AM            1900-01-01 08:15:00.000            2009-06-09 00:00:00.000            
 9:30AM            1900-01-01 09:30:00.000              2009-06-16 00:00:00.000      
10:30AM            1900-01-01 10:30:00.000              2009-06-25 00:00:00.000      
11:00AM            1900-01-01 11:00:00.000              2009-04-29 00:00:00.000      
11:15AM            1900-01-01 11:15:00.000             2009-05-12 00:00:00.000      
12:15PM            1900-01-01 12:15:00.000             2009-04-16 00:00:00.000      
12:15PM            1900-01-01 12:15:00.000            2009-05-08 00:00:00.000      
12:15PM            1900-01-01 12:15:00.000            2009-06-02 00:00:00.000      
12:30PM            1900-01-01 12:30:00.000            2009-04-20 00:00:00.000      
12:30PM            1900-01-01 12:30:00.000            2009-05-15 00:00:00.000      
12:30PM            1900-01-01 12:30:00.000            2009-06-12 00:00:00.000      
Comment
Watch Question

This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Senior Database Architect
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
We cannot drop the date column, as it needs to be there....

Sharath SData Engineer
CERTIFIED EXPERT

Commented:
With your query result, what is your expected result?

Author

Commented:
This is the actual data:

DATE                        TIME
2006-10-09 00:00:00.000            1899-12-30 22:00:00.000
2006-10-09 00:00:00.000            1899-12-30 21:00:00.000
2006-10-09 00:00:00.000            1900-01-01 20:00:00.000
2006-10-10 00:00:00.000            1899-12-30 22:00:00.000
2006-10-10 00:00:00.000            1899-12-30 20:00:00.000
2006-10-10 00:00:00.000            1899-12-30 21:00:00.000
2007-06-11 00:00:00.000            1900-01-01 21:00:00.000
2007-06-11 00:00:00.000            1900-01-01 22:00:00.000


I should output this: (TM should be unique, and date should be included, never mind the tme)

  TM                   tme                         DTE
 8:15AM            1900-01-01 08:15:00.000            2009-06-09 00:00:00.000            
 9:30AM            1900-01-01 09:30:00.000              2009-06-16 00:00:00.000      
10:30AM            1900-01-01 10:30:00.000              2009-06-25 00:00:00.000      
11:00AM            1900-01-01 11:00:00.000              2009-04-29 00:00:00.000      
11:15AM            1900-01-01 11:15:00.000             2009-05-12 00:00:00.000      
12:15PM            1900-01-01 12:15:00.000             2009-04-16 00:00:00.000      
12:15PM            1900-01-01 12:15:00.000            2009-05-08 00:00:00.000      
12:15PM            1900-01-01 12:15:00.000            2009-06-02 00:00:00.000      
12:30PM            1900-01-01 12:30:00.000            2009-04-20 00:00:00.000      
12:30PM            1900-01-01 12:30:00.000            2009-05-15 00:00:00.000      
12:30PM            1900-01-01 12:30:00.000            2009-06-12 00:00:00.000      
Sharath SData Engineer
CERTIFIED EXPERT

Commented:
I observed that there is no differecne between your query result (in original question) and the output you want (post id: 24023876).
Also, in your actual data, you don't have data for year 2009. How do you get that?
Your actual data does not support any means for extracting the following line:
 8:15AM            1900-01-01 08:15:00.000            2009-06-09 00:00:00.000            
 Where in your actual data (reproduced below) does any time exist that is not a whole hour?
DATE                        TIME
2006-10-09 00:00:00.000            1899-12-30 22:00:00.000
2006-10-09 00:00:00.000            1899-12-30 21:00:00.000
2006-10-09 00:00:00.000            1900-01-01 20:00:00.000
2006-10-10 00:00:00.000            1899-12-30 22:00:00.000
2006-10-10 00:00:00.000            1899-12-30 20:00:00.000
2006-10-10 00:00:00.000            1899-12-30 21:00:00.000
2007-06-11 00:00:00.000            1900-01-01 21:00:00.000
2007-06-11 00:00:00.000            1900-01-01 22:00:00.000

Your original data had some times that were like 8:15 but the newest "actual data" that you provided does not.
Did you try the code snippet I provided?

Author

Commented:
8080_Diver I got error with your code, I just drop the date (sorry I messed up, it can be eliminated) .

Thank you guys, sorry it's kinda urgent and I can't entertain your question well...

Author

Commented:
8080_Diver I got error with your code, I just drop the date (sorry I messed up, it can be eliminated) .

Thank you guys, sorry it's kinda urgent and I can't entertain your question well...

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.