Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
skatermel
Starting Member
3 Posts |
Posted - 2005-05-31 : 15:34:23
|
| I am wondering if there is a way to get the query results I need in 1 SQL stmt. I have the following query:SELECT CASE WHEN GROUPING(pur.[Description]) = 1 THEN 'Total' ELSE pur.[Description] END AS 'Description' , COUNT(InspectionID) AS 'Total' , ServiceIDTextFROM tblInspection ins JOIN lkupInspectionPurpose pur ON ins.Purpose = pur.[ID]WHERE BeginDate BETWEEN '1/1/2005' AND '1/15/2005' -- paramsGROUP BY pur.[Description], ins.ServiceIDText WITH ROLLUPwhich produces the following results:Description Count ServiceComplaint Investigation 8 01Complaint Investigation 1 03Complaint Investigation 3 04Complaint Investigation 6 14Complaint Investigation 2 16Complaint Investigation 20 NULLConsultation 3 14Consultation 3 NULLIn-Office Review 10 01In-Office Review 6 02In-Office Review 1 03In-Office Review 3 05In-Office Review 1 07In-Office Review 1 08In-Office Review 4 14In-Office Review 1 16In-Office Review 27 NULLScheduled Review 1 NULLScheduled Review 1 02Scheduled Review 2 14Scheduled Review 4 NULLUnannounced Visit 1 NULLUnannounced Visit 11 01Unannounced Visit 7 02Unannounced Visit 1 03Unannounced Visit 3 04Unannounced Visit 2 05Unannounced Visit 3 07Unannounced Visit 1 08Unannounced Visit 4 14Unannounced Visit 33 NULLTotal 87 NULLWhat I need is the SQL to produce the following:Service 01 then the listing of the description categories with totals.Service 02 then the listing ...Is this possible?Thanks,Melissa |
|
|
SamC
White Water Yakist
3467 Posts |
Posted - 2005-05-31 : 17:46:05
|
| Sample data would help me understand this. Could you expand the wanted result recordset? I don't understand what "then the listing of the description categories with totals" is exactly. I could make some inferences, but a sample output, with 3 or 4 rows would help. |
 |
|
|
MichaelP
Jedi Yak
2489 Posts |
Posted - 2005-05-31 : 18:21:30
|
| Do some searching on CrossTab or Dynamic CrossTab.I belive that's what you are looking for here.Michael<Yoda>Use the Search page you must. Find the answer you will.</Yoda> |
 |
|
|
skatermel
Starting Member
3 Posts |
Posted - 2005-06-01 : 09:00:17
|
| Thanks. Got it from BOL. |
 |
|
|
skatermel
Starting Member
3 Posts |
Posted - 2005-06-01 : 09:07:57
|
| Actually, what I had originally was correct. I just needed to switch my cols in the GROUP BY clause. No case stmt needed and I can using the GROUPING for a label on my totals. |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2005-06-01 : 09:16:54
|
| Remember -- don't try to do presentation things in SQL Server. SQL is a database server, it's only job is to return rows of raw data. It is the presentations layer's job to format, subtotal and group the results into a presentable format. Keep things simple and don't try to use a screw driver to hammer nails. use the right tool for the job it was intended.- Jeff |
 |
|
|
|
|
|