Solved

SQL Group By

Posted on 2011-03-25
5
385 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

631 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