Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 388
  • Last Modified:

SQL Group By

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
crmcIT
Asked:
crmcIT
  • 2
  • 2
4 Solutions
 
Kyle AbrahamsSenior .Net DeveloperCommented:
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
 
crmcITAuthor Commented:
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
 
Kyle AbrahamsSenior .Net DeveloperCommented:
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
 
Anthony PerkinsCommented:
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
 
crmcITAuthor Commented:
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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now