Solved

SQL Group By

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Parse field in SQL View 15 99
T-SQL--Two Different Totals Even with the Same Concept 6 46
recover sqlserver db 8 58
SQL View Syntax case etc 5 0
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

863 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

23 Experts available now in Live!

Get 1:1 Help Now