Solved

SQL Group By

Posted on 2011-03-25
5
359 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
  • 2
  • 2
5 Comments
 
LVL 39

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 39

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

759 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now