all groups > sql server programming > october 2003 >
You're in the

sql server programming

group:

Complicated Query


Complicated Query Abhi
10/14/2003 11:21:42 PM
sql server programming:
I am using SQL server 2K.
I have a table
ID,School,Program,Credits,Amount

ID School Program Credits Amount

1 Eng BS.EE 12 100
2 Arch BS.Arch 14 200
3 FineArts BS.Arts 8 300
4 Eng MS.Comp 18 1000
5 Med BS.DO 12 4000
6 Eng BS.EE 14 100
7 Eng MS.Comp 12 1000
8 Arch BS.Arch 8 5000
9 Arch BS.Arch 14 1200


Now my problem is I need to create a report which will have sum of all the
credits students are regristered as FullTime(credits>=12) and also PartTime
(credits<12) and there respective Amount

Eg.

Report should look like this.

School Program FullTime PartTime
FT_Amount PT_Amount
Eng BS.EE 26 0
200 0
Eng MS.Comp 30 0
2000 0
Arch BS.Arch 24 8
1400 5000

so and so forth

The idea is I want the sum of all the fullTime credtis and part time credits
and there respective amount in one query.

IS THIS POSSIBLE? IF NOT WHAT IS THE BEST WAY TO ACHIEVE THIS.

Abhi

Re: Complicated Query Abhi
10/15/2003 1:51:19 AM
No luck.
It didn't work
Somwhere I feel this can be done, if it can sum the FullTime why it cannot
sum up the PartTime into another column.

Abhi

[quoted text, click to view]

Re: Complicated Query Abhi
10/15/2003 1:58:42 AM
What I did was I created two views one for Full Time and another for Part
Time
And then pulled them together into my stored proc. But no luck.

Abhi


[quoted text, click to view]

Re: Complicated Query Louis Davidson
10/15/2003 9:59:58 AM
Can you please post DDL an to your table so we can run queries against it.
Then at the very least when the reply comes, the query will have been tested
to at least handle your minimal query (if not every possible case.) I would
have figured his solution worked. In any case:

[quoted text, click to view]

This is not moving us in a direction that gets you closer to a solution. At
least post the results of you executing his query and show where it fails.

--
----------------------------------------------------------------------------
-----------
Louis Davidson (drsql@hotmail.com)
Compass Technology Management

Pro SQL Server 2000 Database Design
http://www.apress.com/book/bookDisplay.html?bID=266

Note: Please reply to the newsgroups only unless you are
interested in consulting services. All other replies will be ignored :)

[quoted text, click to view]

Re: Complicated Query Pavel S.Vorontsov
10/15/2003 11:48:59 AM
Hi!

try this:

select school, program, sum(case when Credits>=12 then 1 else 0 end) as
FullTime,
sum(case when Credits >= 12 then 0 else 1 end) as PartTime,
sum(case when Credits >= 12 then Amount else 0 end) as FT_Amount,
sum(case when Credits >= 12 then 0 else Amount end) as PT_Amount
from Table
group by school, program


[quoted text, click to view]

Re: Complicated Query Pavel S.Vorontsov
10/15/2003 1:52:07 PM
Can you be please more distinguish? What is wrong with my solution?
[quoted text, click to view]

Re: Complicated Query Abhi
10/15/2003 4:51:11 PM
Here you go the complete package.

DDL
===============

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[EN]')
and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[EN]
GO

CREATE TABLE [dbo].[EN] (
[STU_ID] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[TERM] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[CREDITS] [float] NULL ,
[ACTIVE_PROG] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[ACAD_LEVEL] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[STATUS] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[SCHOOL] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[LOCATION] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[AMOUNT] [money] NULL
) ON [PRIMARY]
GO
=========================
SAMPLE DATA
HERE IS THE SAMPLE DATA FOR THIS TABLE

1,2003RF,18,BFA.CMART.TVRAD,UG,R,ASCM,CI,9417.00
2,2003RS,18,BFA.CMART.TVRAD,UG,R,ASCM,CI,8740.00
3,2003RX,12,BFA.CMART.TVRAD,UG,T,ASCM,CI,6425.00
4,2003RF,17,BARCH.ARCH,UG,R,ARCH,OW,8763.00
5,2003RS,16,BARCH.ARCH,UG,R,ARCH,OW,8000.00
6,2003RX,9,BARCH.ARCH,UG,T,ARCH,OW,4825.00
7,2003RS,15,ND.UNDERGRADUATE,UG,R,,MA,7975.00
8,2003RX,5,ND.UNDERGRADUATE,UG,R,,MA,2725.00
9,2003RS,6,MS.INSTR,GR,R,EDPS,TEACH,1200.00
10,2003RX,3,MS.INSTR,GR,R,EDPS,TEACH,600.00
11,2003RF,13,BS.NURS,UG,R,AHLS,MA,8613.00
12,2003RS,17,BFA.FNART.CPGRP,UG,R,ASCM,OW,7975.00
13,2003RF,14,BS.LFSCI.OT,UG,R,AHLS,OW,8763.00
14,2003RS,15,BS.LFSCI.OT,UG,R,AHLS,OW,8000.00
15,2003RS,20,BFA.FNART.CPGRP,UG,R,ASCM,MA,9550.00
16,2003RS,20,BFA.FNART.CPGRP,UG,R,ASCM,MA,9550.00
17,2003RX,9,BFA.FNART.CPGRP,UG,R,ASCM,MA,4825.00
18,2003RS,3,MS.CMPSC,GR,R,ENGT,OW,2015.00
19,2003RF,15,BFA.DSGGP,UG,R,ASCM,MA,8613.00
20,2003RS,15,BFA.DSGGP,UG,R,ASCM,MA,7975.00
21,2003RF,11,MS.PT,GR,R,AHLS,OW,6878.00
22,2003RS,19,MS.PT,GR,R,AHLS,OW,11215.00
23,2003RF,18,BS.CMPSC,UG,R,ENGT,MA,9327.00
24,2003RS,15,BS.CMPSC,UG,R,ENGT,MA,8000.00
25,2003RX,9,BS.CMPSC,UG,R,ENGT,MA,4825.00
26,2003RF,17,BARCH.ARCH,UG,R,ARCH,MA,8763.00
27,2003RS,19,BARCH.ARCH,UG,R,ARCH,MA,9050.00
28,2003RX,8,BARCH.ARCH,UG,R,ARCH,MA,4300.00
29,2003RS,18,BPS.HSPMG,UG,R,EDPS,CI,8500.00
30,2003RS,15,BS.POLSC,UG,R,ASCM,OW,7975.00




[quoted text, click to view]

Re: Complicated Query Abhi
10/15/2003 4:53:39 PM
When I run your suggested SQL, it didn't give me the correct results.
It calculated the FULL time correctly and then the PartTimers were the same
as fulltimers.
For eg if there are students who are taking FT and if there credit total is
25, it will show 25 for part time also.

Abhi

[quoted text, click to view]

Re: Complicated Query John Gilson
10/16/2003 6:21:36 AM
[quoted text, click to view]

Assume table is named Credits.

SELECT school,
program,
SUM(CASE WHEN credits >= 12
THEN credits
ELSE 0
END) AS full_time,
SUM(CASE WHEN credits < 12
THEN credits
ELSE 0
END) AS part_time,
SUM(CASE WHEN credits >= 12
THEN amount
ELSE 0
END) AS full_time_amount,
SUM(CASE WHEN credits < 12
THEN amount
ELSE 0
END) AS part_time_amount
FROM Credits
GROUP BY school, program

Regards,
jag

Re: Complicated Query Abhi
10/16/2003 11:33:16 AM
Miraculously it worked.
Few days back somebody else suggested the same exact solution, for some
reason I thought it is giving me wrong results.
But today it is working fine.
Thanks you to you and also the person who suggested before.

Thanks you guys.

Abhi


[quoted text, click to view]

Re: Complicated Query (quick add in?) Abhi
10/16/2003 12:19:49 PM
Is there anyway I can add a subtotal clause. I mean is there anywhere in the
SQL I can say that when you group by the columns can you subtotal it by the
column?????
The reason I am asking this is, I would like to get this data out of my SQL
server and transfere it as an xml file, then I would like to write a xsl so
that I can show that report in a nice format.
So now my biggest problem is the reports demands subtotals on the different
groups.

Is there anyway I can squeeze the subtotals within my query???? I hightly
doubt it. But if somebody knows that will be simply great.

Abhi


[quoted text, click to view]

Re: Complicated Query (quick add in?) John Gilson
10/17/2003 3:53:47 AM
[quoted text, click to view]

This query aggregates by school and program, by school, and for all
schools and programs.

SELECT school,
program,
SUM(CASE WHEN credits >= 12
THEN credits
ELSE 0
END) AS full_time,
SUM(CASE WHEN credits < 12
THEN credits
ELSE 0
END) AS part_time,
SUM(CASE WHEN credits >= 12
THEN amount
ELSE 0
END) AS full_time_amount,
SUM(CASE WHEN credits < 12
THEN amount
ELSE 0
END) AS part_time_amount
FROM Credits
GROUP BY school, program
UNION ALL
SELECT school,
NULL AS program,
SUM(CASE WHEN credits >= 12
THEN credits
ELSE 0
END) AS full_time,
SUM(CASE WHEN credits < 12
THEN credits
ELSE 0
END) AS part_time,
SUM(CASE WHEN credits >= 12
THEN amount
ELSE 0
END) AS full_time_amount,
SUM(CASE WHEN credits < 12
THEN amount
ELSE 0
END) AS part_time_amount
FROM Credits
GROUP BY school
UNION ALL
SELECT NULL AS school,
NULL AS program,
SUM(CASE WHEN credits >= 12
THEN credits
ELSE 0
END) AS full_time,
SUM(CASE WHEN credits < 12
THEN credits
ELSE 0
END) AS part_time,
SUM(CASE WHEN credits >= 12
THEN amount
ELSE 0
END) AS full_time_amount,
SUM(CASE WHEN credits < 12
THEN amount
ELSE 0
END) AS part_time_amount
FROM Credits
ORDER BY school, program

-- Using your sample data
school program full_time part_time full_time_amount part_time_amount
NULL NULL 96 16 7600 5300
Arch NULL 28 8 1400 5000
Arch BS.Arch 28 8 1400 5000
Eng NULL 56 0 2200 0
Eng BS.EE 26 0 200 0
Eng MS.Comp 30 0 2000 0
FineArts NULL 0 8 0 300
FineArts BS.Arts 0 8 0 300
Med NULL 12 0 4000 0
Med BS.DO 12 0 4000 0

Regards,
jag

[quoted text, click to view]

Re: Complicated Query (quick add in?) TGarrod5 NO[at]SPAM hotmail.com
10/17/2003 1:42:22 PM
Check out BOL topic on Grouping, specifically WITH CUBE, WITH
ROLLUP expressions.


[quoted text, click to view]
AddThis Social Bookmark Button