Solved

SQL Group By

Posted on 2011-03-25
5
380 Views
Last Modified: 2012-05-11
I have a select statment I am trying to group by. If I leave the date out from printing then it gives me the right total, If I leave it in, it gives me more records than I need. Any Ideas.

Here's my Select Statment:

SELECT     TOP (100) PERCENT dbo.AdmVisits.UnitNumber, dbo.AdmVisits.LocationID, dbo.AdmVisits.Status, dbo.OeOrders2.OrdSourceID
FROM         dbo.OeOrders INNER JOIN
                      dbo.OeOrders2 ON dbo.OeOrders.VisitID = dbo.OeOrders2.VisitID AND dbo.OeOrders.OrderID = dbo.OeOrders2.OrderID LEFT OUTER JOIN
                      dbo.AdmVisits ON dbo.OeOrders.VisitID = dbo.AdmVisits.VisitID LEFT OUTER JOIN
                      dbo.DMisUsers ON dbo.OeOrders.EnteredUserID = dbo.DMisUsers.UserID AND dbo.OeOrders.ProviderDeaNumber = dbo.DMisUsers.ProviderDeaNumber
WHERE     (dbo.OeOrders.OrderDateTime > CONVERT(DATETIME, '2011-03-07 00:00:00', 102)) AND (dbo.OeOrders.OrderDateTime < CONVERT(DATETIME, '2011-03-14 00:00:00',
                      102))
GROUP BY dbo.AdmVisits.UnitNumber, dbo.AdmVisits.Status, dbo.AdmVisits.LocationID, dbo.OeOrders2.OrdSourceID
HAVING      (dbo.AdmVisits.Status = 'DEP ER') AND (dbo.OeOrders2.OrdSourceID = 'PROVIDER') OR
                      (dbo.AdmVisits.Status = 'REG ER') OR
                      (dbo.AdmVisits.Status = 'PRE ER')
0
Comment
Question by:crmcIT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 400 total points
ID: 35216554
the group by is also grouping by the date.  (breaking up the record set further by the date).

What date are you looking for?  Note you can use a max(date) or min(date) to get one of the date times (no need for it in the group by)
0
 

Assisted Solution

by:crmcIT
crmcIT earned 0 total points
ID: 35216986
Sorry that didn't work. I got double the records. Here's the revised query

SELECT DISTINCT
                      TOP (100) PERCENT AdmVisits_1.UnitNumber, AdmVisits_1.LocationID, AdmVisits_1.Status, dbo.OeOrders2.OrdSourceID, MIN(OeOrders_1.OrderDateTime)
                      AS EXPR1
FROM         dbo.OeOrders AS OeOrders_1 LEFT OUTER JOIN
                      dbo.OeOrders2 ON OeOrders_1.VisitID = dbo.OeOrders2.VisitID AND OeOrders_1.OrderID = dbo.OeOrders2.OrderID LEFT OUTER JOIN
                      dbo.AdmVisits AS AdmVisits_1 ON OeOrders_1.VisitID = AdmVisits_1.VisitID
GROUP BY AdmVisits_1.UnitNumber, AdmVisits_1.Status, AdmVisits_1.LocationID, dbo.OeOrders2.OrdSourceID
HAVING      (AdmVisits_1.UnitNumber IS NOT NULL) AND (dbo.OeOrders2.OrdSourceID = 'PROVIDER') AND (AdmVisits_1.Status = 'DEP ER') AND (MIN(OeOrders_1.OrderDateTime)
                      > CONVERT(DATETIME, '2011-03-07 00:00:00', 102)) AND (MAX(OeOrders_1.OrderDateTime) < CONVERT(DATETIME, '2011-03-14 00:00:00', 102)) OR
                      (AdmVisits_1.Status = 'REG ER') OR
                      (AdmVisits_1.Status = 'PRE ER')
ORDER BY AdmVisits_1.UnitNumber
0
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 400 total points
ID: 35217116
What exactly are you trying to do, I thought you wanted a date time to print?  The minimum?

SELECT     dbo.AdmVisits.UnitNumber, dbo.AdmVisits.LocationID, dbo.AdmVisits.Status, dbo.OeOrders2.OrdSourceID, MIN(OeOrders_1.OrderDateTime)
FROM         dbo.OeOrders INNER JOIN
                      dbo.OeOrders2 ON dbo.OeOrders.VisitID = dbo.OeOrders2.VisitID AND dbo.OeOrders.OrderID = dbo.OeOrders2.OrderID LEFT OUTER JOIN
                      dbo.AdmVisits ON dbo.OeOrders.VisitID = dbo.AdmVisits.VisitID LEFT OUTER JOIN
                      dbo.DMisUsers ON dbo.OeOrders.EnteredUserID = dbo.DMisUsers.UserID AND dbo.OeOrders.ProviderDeaNumber = dbo.DMisUsers.ProviderDeaNumber
WHERE     (dbo.OeOrders.OrderDateTime > CONVERT(DATETIME, '2011-03-07 00:00:00', 102)) AND (dbo.OeOrders.OrderDateTime < CONVERT(DATETIME, '2011-03-14 00:00:00',
                      102))
GROUP BY dbo.AdmVisits.UnitNumber, dbo.AdmVisits.Status, dbo.AdmVisits.LocationID, dbo.OeOrders2.OrdSourceID
HAVING      (dbo.AdmVisits.Status = 'DEP ER') AND (dbo.OeOrders2.OrdSourceID = 'PROVIDER') OR
                      (dbo.AdmVisits.Status = 'REG ER') OR
                      (dbo.AdmVisits.Status = 'PRE ER')
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 100 total points
ID: 35217357
You have a number of problems:
1. You are using DISTINCT.  Lose it, it pointless
2. You are using TOP (100) PERCENT.  Lose it, it pointless
3. You have conditions in your HAVING clause that do not belong there, they should be in your WHERE clause.  As in:
WHERE	AdmVisits_1.UnitNumber IS NOT NULL
	AND dbo.OeOrders2.OrdSourceID = 'PROVIDER'
        AND AdmVisits_1.Status IN ('DEP ER', 'REG ER', 'PRE ER')
GROUP BY AdmVisits_1.UnitNumber,
        AdmVisits_1.Status,
        AdmVisits_1.LocationID,
        dbo.OeOrders2.OrdSourceID
HAVING  MIN(OeOrders_1.OrderDateTime) > CONVERT(DATETIME, '2011-03-07 00:00:00', 102)
        AND MAX(OeOrders_1.OrderDateTime) < CONVERT(DATETIME, '2011-03-14 00:00:00', 102)
ORDER BY AdmVisits_1.UnitNumber

Open in new window

0
 

Author Closing Comment

by:crmcIT
ID: 35252797
Here's what I eneded up with:
SELECT     dbo.AdmVisits.UnitNumber, dbo.AdmVisits.LocationID, dbo.AdmVisits.Status, dbo.OeOrders2.OrdSourceID, MAX(DISTINCT CONVERT(varchar(10),
                      dbo.OeOrders.OrderDateTime, 111)) AS OrderDate, dbo.AdmVisits.Name AS Patient, MAX(dbo.OeOrders.EnteredUserName) AS EnteredBy,
                      MAX(dbo.OeOrders.EnteredUserID) AS EnteredBYUserID
FROM         dbo.OeOrders RIGHT OUTER JOIN
                      dbo.AdmVisits ON dbo.OeOrders.VisitID = dbo.AdmVisits.VisitID LEFT OUTER JOIN
                      dbo.OeOrders2 ON dbo.OeOrders.VisitID = dbo.OeOrders2.VisitID AND dbo.OeOrders.OrderID = dbo.OeOrders2.OrderID
WHERE     (dbo.OeOrders.OrderDateTime > CONVERT(DATETIME, '2011-03-07 00:00:00', 102)) AND (dbo.OeOrders.OrderDateTime < CONVERT(DATETIME, '2011-03-14 00:00:00',
                      102))
GROUP BY dbo.AdmVisits.UnitNumber, dbo.AdmVisits.Status, dbo.AdmVisits.LocationID, dbo.OeOrders2.OrdSourceID, dbo.AdmVisits.Name
HAVING      (dbo.AdmVisits.Status = 'DEP ER') AND (dbo.OeOrders2.OrdSourceID = 'PROVIDER') AND (dbo.AdmVisits.LocationID = 'ED') OR
                      (dbo.AdmVisits.Status = 'REG ER') OR
                      (dbo.AdmVisits.Status = 'PRE ER')
ORDER BY dbo.AdmVisits.UnitNumber
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question