Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7398095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:49:45+00:00 2026-05-29T03:49:45+00:00

i have a pretty messy query, i would like to know if there is

  • 0

i have a pretty messy query, i would like to know if there is any way to separate/simplify it subqueries or what ever it takes. it looks like i am a suspect of this!! http://weblogs.sqlteam.com/jeffs/archive/2005/12/14/8546.aspx

;with
cte_biggie ( [Full Date],     [Year Entered],   [Month Entered],  [Day Entered],      
[DOW],      [Week Ending] ,[CountAccns],[Sales Rep],   [MLNPI], [IMSNPI],     [Physician],      
[Practice Code],  [MLIS Code],      [Practice Name],  
[Date Established],     [Address],  [Address2], [City],     [State],    [Status]
) as (
select CONVERT(VARCHAR(8), [DATE entered], 1),DATEPART(yy, [DATE entered]) ,  
            LEFT(DATENAME(MONTH, GETDATE()), 3)
           ,DATEPART(dd, [DATE entered]),

           case when DATEPART(WEEKDAY, [DATE entered])=1 THEN 'Sun'
           when DATEPART(WEEKDAY, [DATE entered])=2 THEN 'Mon'
           when DATEPART(WEEKDAY, [DATE entered])=3 THEN 'Tus'
           when DATEPART(WEEKDAY, [DATE entered])=4 THEN 'Wed'
           when DATEPART(WEEKDAY, [DATE entered])=5 THEN 'Thu'
           when DATEPART(WEEKDAY, [DATE entered])=6 THEN 'Fri'
           when DATEPART(WEEKDAY, [DATE entered])=7 THEN 'Sat'
           end,
           CONVERT(VARCHAR(8), DATEADD (D, -1 * DatePart (dw,[date entered]) + 6, [date entered]), 1),
           count(a.[specimen id]) ,c.salesrep,c.npi,e.npib,[Requesting Physician] ,
           a.[practice code],b.[mlis practice id],[practice name],

   c.dateestablished , c.practiceaddress1, c.practiceaddress2,c.practicecity,c.practicestate,
    b.[Active Inactive]
from quicklabdump a
    left outer join qlmlismapping b
    on (b.[practice code] = a.[practice code])
    left outer join PracticeandPhysician c
    on (a.[Requesting Physician]=c.doctorfirstname+' '+c.DOCTORLASTNAME
    and a.[practice code]=c.practicecode)
    left outer join TestResults d 
    on a.QuickLabDumpID = d.QuickLabDumpID
    left outer join IMSData e
    on c.NPI=e.npib
where    [Date Entered] <= '20111231'
and [Date Entered] >= '20111201'


group by [DATE entered],DATEPART(yy, [DATE entered]), DATEPART(mm, [DATE entered]),DATEPART(dd, [DATE entered]), a.[practice name],b.[mlis practice id],a.[practice code],
    a.[Requesting Physician],c.salesrep,c.dateestablished, c.practicecity,c.practicestate,c.npi,e.npib,c.practiceaddress1 ,c.practiceaddress2,
    b.[Active Inactive]


)

select * from cte_biggie
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T03:49:45+00:00Added an answer on May 29, 2026 at 3:49 am

    I would generally disagree with the link that you provided regarding the use of sub-selects to make group by’s clearer. I’ve been writing TSQL in SQLServer ever since it came out of the Sybase oven. For me, it is quite clear that whatever columns being returned in the result set that are not aggregates are iterated in the GROUP BY. In fact, once they’re coded in the GROUP BY section I don’t even pay attention to them. For me it’s actually distracting to see a sub-select that is not necessary. I tend to only use sub-selects for when they’re an absolute necessity, which does happen from time to time. But, then when I see a sub-select that’s my cue to pay special attention to the logic because I know I’m only using them when something special is happening. I have no problem seeing a few extra columns at the end in my GROUP BY and I think the SQL engine is smart enough that having the full list of columns is not a performance loss.

    Here’s how I would format your SQL. I’m not a fan of using column names that have spaces so I would probably remove spaces from column names and get rid of all the brackets but since you have them I just followed your lead.

    select 
        [Full Date]=CONVERT(VARCHAR(8), [DATE entered], 1),
        [Year Entered]=DATEPART(yy, [DATE entered]) ,  
        [Month Entered]=LEFT(DATENAME(MONTH, GETDATE()), 3),
        [Day Entered]=DATEPART(dd, [DATE entered]),
        [DOW]=
           case when DATEPART(WEEKDAY, [DATE entered])=1 THEN 'Sun'
           when DATEPART(WEEKDAY, [DATE entered])=2 THEN 'Mon'
           when DATEPART(WEEKDAY, [DATE entered])=3 THEN 'Tus'
           when DATEPART(WEEKDAY, [DATE entered])=4 THEN 'Wed'
           when DATEPART(WEEKDAY, [DATE entered])=5 THEN 'Thu'
           when DATEPART(WEEKDAY, [DATE entered])=6 THEN 'Fri'
           when DATEPART(WEEKDAY, [DATE entered])=7 THEN 'Sat'
           end,
        [Week Ending]=CONVERT(VARCHAR(8),
            DATEADD (D, -1 * DatePart (dw,[date entered]) + 6, [date entered]), 1),
        [CountAccns]=count(a.[specimen id]),
        [Sales Rep]=c.salesrep,
        [MLNPI]=c.npi,
        [IMSNPI]=e.npib,
        [Physician]=[Requesting Physician],
        [Practice Code]=a.[practice code],
        [MLIS Code]=b.[mlis practice id],
        [practice name],
        [Date Established]=c.dateestablished , 
        [Address]=c.practiceaddress1, 
        [Address2]=c.practiceaddress2,
        [City]=c.practicecity,
        [State]=c.practicestate,
        [Status]=b.[Active Inactive]
    from 
        quicklabdump a
        left outer join qlmlismapping b on (b.[practice code] = a.[practice code])
        left outer join PracticeandPhysician c on 
            a.[Requesting Physician]=c.doctorfirstname+' '+c.DOCTORLASTNAME 
            and a.[practice code]=c.practicecode
        left outer join TestResults d on a.QuickLabDumpID = d.QuickLabDumpID
        left outer join IMSData e    on c.NPI=e.npib
    where    
        [Date Entered] <= '20111231'
        and [Date Entered] >= '20111201'
    group by 
        c.salesrep,
        c.npi,
        e.npib,
        [Requesting Physician],
        a.[practice code],
        b.[mlis practice id],
        [practice name],
        c.dateestablished , 
        c.practiceaddress1, 
        c.practiceaddress2,
        c.practicecity,
        c.practicestate,
        b.[Active Inactive]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have pretty URLs for my tagging system along with all
Right so I know my code/structure is pretty messy, I've not done MVC before
Have a quick question about what would be the best way to implement iterators
I have pretty much finished my first working Symbian application, but in my hastened
I have pretty big background of .net, and I've decided that i want to
I have pretty good skills in PHP , Mysql and Javascript for a junior
i have pretty simple simple question (i hope so). How do i change the
I have pretty standard Qmail toaster installation. I'm using the dot files to set
I might have pretty basic question about regex. I have the following regex, which
I have built a PHP calendar system and have pretty much everything done but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.