Tom Moreau
3/12/2007 3:30:00 PM
Thanx for the DDL, the data, the spec and your attempted query. It made it
easy to produce a solution for you:
select
*
from
(
select
x =
case
when left(ba,2) in ('10','11','12') then
'FireExtenguishers '
when left(ba,2) in ('13','14') then
'Sealed FirePrevention '
when left(ba,2) in ('21','22') then
'Automated '
when left(ba,2) in ('31') then
'Accessories '
else
'Other '
end+ substring(ba,4,10),
sum(WHA) as 'WHA',
sum(WHA_ar) as 'WHA_ar',
sum(DeptStores) as 'DeptStores',
sum(DeptStores_ar) as 'DeptStores_ar',
sum(Asia) as 'Asia',
sum(Asia_ar) as 'Asia_ar',
sum(Dealers) as 'Dealers',
sum(Dealers_ar) as 'Dealers_ar',
sum(GovLaw) as 'GovLaw',
sum(GovLaw_ar) as 'GovLaw_ar',
sum(Other) as 'Other',
sum(Other_ar) as 'Other_ar',
sum(SouthAmerica) as 'SouthAmerica',
sum(SouthAmerica_ar) as 'SouthAmerica_ar'
from monthly_ships
group by
case
when left(ba,2) in ('10','11','12') then
'FireExtenguishers '
when left(ba,2) in ('13','14') then
'Sealed FirePrevention '
when left(ba,2) in ('21','22') then
'Automated '
when left(ba,2) in ('31') then
'Accessories '
else
'Other '
end+ substring(ba,4,10)
with rollup
) as z
order by
case x
when 'FireExtenguishers 01-2007' then 1
when 'Sealed FirePrevention 01-2007' then 2
when 'Automated 01-2007' then 3
when 'Accessories 01-2007' then 4
when 'Other 01-2007' then 5
else 6
end
--
Tom
----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
.
"Blasting Cap" <goober@christian.net> wrote in message
news:OLO0AlLZHHA.688@TK2MSFTNGP03.phx.gbl...
Tom:
After we finally decided on the sources for the data, they created a
table that actually has data in it we're wanting to use, instead of just
thinking of how I was anticipating the data to look.
Here's the ddl:
if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[monthly_ships]') and OBJECTPROPERTY(id,
N'IsUserTable') = 1)
drop table [dbo].[monthly_ships]
GO
CREATE TABLE [dbo].[monthly_ships] (
[BA] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[org] [char] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[WHA] [int] NULL ,
[WHA_ar] [int] NULL ,
[DeptStores] [int] NULL ,
[DeptStores_ar] [int] NULL ,
[Asia] [int] NULL ,
[Asia_ar] [int] NULL ,
[Dealers] [int] NULL ,
[Dealers_ar] [int] NULL ,
[GovLaw] [int] NULL ,
[GovLaw_ar] [int] NULL ,
[Other] [int] NULL ,
[Other_ar] [int] NULL ,
[SouthAmerica] [int] NULL ,
[SouthAmerica_ar] [int] NULL
) ON [PRIMARY]
GO
INSERT INTO [monthly_ships]
([BA],[org],[WHA],[WHA_ar],[DeptStores],[DeptStores_ar],[Asia],[Asia_ar],[Dealers],[Dealers_ar],[GovLaw],[GovLaw_ar],[Other],[Other_ar],[SouthAmerica],[SouthAmerica_ar])VALUES('10
01-2007','ORG1',213214,213370,32554,33778,0,0,4411,5996,0,0,25138,2281,0,0)
INSERT INTO [monthly_ships]
([BA],[org],[WHA],[WHA_ar],[DeptStores],[DeptStores_ar],[Asia],[Asia_ar],[Dealers],[Dealers_ar],[GovLaw],[GovLaw_ar],[Other],[Other_ar],[SouthAmerica],[SouthAmerica_ar])VALUES('11
01-2007','ORG1',2298452,2292340,1427145,1426464,0,0,123295,117531,223055,220192,450086,241724,0,0)
INSERT INTO [monthly_ships]
([BA],[org],[WHA],[WHA_ar],[DeptStores],[DeptStores_ar],[Asia],[Asia_ar],[Dealers],[Dealers_ar],[GovLaw],[GovLaw_ar],[Other],[Other_ar],[SouthAmerica],[SouthAmerica_ar])VALUES('12
01-2007','ORG1',0,-7087,0,0,0,0,0,-1584,0,-345,506007,386284,0,0)
INSERT INTO [monthly_ships]
([BA],[org],[WHA],[WHA_ar],[DeptStores],[DeptStores_ar],[Asia],[Asia_ar],[Dealers],[Dealers_ar],[GovLaw],[GovLaw_ar],[Other],[Other_ar],[SouthAmerica],[SouthAmerica_ar])VALUES('14
01-2007','ORG1',274401,274401,323137,323137,0,0,4049,4781,0,0,2323,3894,0,0)
INSERT INTO [monthly_ships]
([BA],[org],[WHA],[WHA_ar],[DeptStores],[DeptStores_ar],[Asia],[Asia_ar],[Dealers],[Dealers_ar],[GovLaw],[GovLaw_ar],[Other],[Other_ar],[SouthAmerica],[SouthAmerica_ar])VALUES('21
01-2007','ORG1',1633450,1635968,3656214,3660268,0,0,5560,5562,306386,282301,989948,848059,0,0)
INSERT INTO [monthly_ships]
([BA],[org],[WHA],[WHA_ar],[DeptStores],[DeptStores_ar],[Asia],[Asia_ar],[Dealers],[Dealers_ar],[GovLaw],[GovLaw_ar],[Other],[Other_ar],[SouthAmerica],[SouthAmerica_ar])VALUES('22
01-2007','ORG1',0,0,48660,48660,0,0,0,0,0,0,0,0,0,0)
INSERT INTO [monthly_ships]
([BA],[org],[WHA],[WHA_ar],[DeptStores],[DeptStores_ar],[Asia],[Asia_ar],[Dealers],[Dealers_ar],[GovLaw],[GovLaw_ar],[Other],[Other_ar],[SouthAmerica],[SouthAmerica_ar])VALUES('31
01-2007','ORG1',109216,108809,149904,152029,0,0,2870,2876,2217,1213,32089,5463,0,0)
INSERT INTO [monthly_ships]
([BA],[org],[WHA],[WHA_ar],[DeptStores],[DeptStores_ar],[Asia],[Asia_ar],[Dealers],[Dealers_ar],[GovLaw],[GovLaw_ar],[Other],[Other_ar],[SouthAmerica],[SouthAmerica_ar])VALUES('33
01-2007','ORG1',0,0,0,0,0,0,0,0,55033,51751,0,0,0,0)
INSERT INTO [monthly_ships]
([BA],[org],[WHA],[WHA_ar],[DeptStores],[DeptStores_ar],[Asia],[Asia_ar],[Dealers],[Dealers_ar],[GovLaw],[GovLaw_ar],[Other],[Other_ar],[SouthAmerica],[SouthAmerica_ar])VALUES('51
01-2007','ORG1',0,0,0,0,0,0,0,0,0,0,231111,231282,0,0)
INSERT INTO [monthly_ships]
([BA],[org],[WHA],[WHA_ar],[DeptStores],[DeptStores_ar],[Asia],[Asia_ar],[Dealers],[Dealers_ar],[GovLaw],[GovLaw_ar],[Other],[Other_ar],[SouthAmerica],[SouthAmerica_ar])VALUES('10
01-2007','ORG2',0,0,0,0,118307,62220,0,0,0,0,0,0,0,0)
INSERT INTO [monthly_ships]
([BA],[org],[WHA],[WHA_ar],[DeptStores],[DeptStores_ar],[Asia],[Asia_ar],[Dealers],[Dealers_ar],[GovLaw],[GovLaw_ar],[Other],[Other_ar],[SouthAmerica],[SouthAmerica_ar])VALUES('11
01-2007','ORG2',0,0,0,0,338472,96918,0,0,0,0,0,0,0,0)
INSERT INTO [monthly_ships]
([BA],[org],[WHA],[WHA_ar],[DeptStores],[DeptStores_ar],[Asia],[Asia_ar],[Dealers],[Dealers_ar],[GovLaw],[GovLaw_ar],[Other],[Other_ar],[SouthAmerica],[SouthAmerica_ar])VALUES('12
01-2007','ORG2',0,0,0,0,199,43,0,0,0,0,0,0,0,0)
INSERT INTO [monthly_ships]
([BA],[org],[WHA],[WHA_ar],[DeptStores],[DeptStores_ar],[Asia],[Asia_ar],[Dealers],[Dealers_ar],[GovLaw],[GovLaw_ar],[Other],[Other_ar],[SouthAmerica],[SouthAmerica_ar])VALUES('14
01-2007','ORG2',0,0,0,0,22532,10622,0,0,0,0,0,0,0,0)
INSERT INTO [monthly_ships]
([BA],[org],[WHA],[WHA_ar],[DeptStores],[DeptStores_ar],[Asia],[Asia_ar],[Dealers],[Dealers_ar],[GovLaw],[GovLaw_ar],[Other],[Other_ar],[SouthAmerica],[SouthAmerica_ar])VALUES('21
01-2007','ORG2',0,0,0,0,1320419,1320852,0,0,0,0,0,0,0,0)
INSERT INTO [monthly_ships]
([BA],[org],[WHA],[WHA_ar],[DeptStores],[DeptStores_ar],[Asia],[Asia_ar],[Dealers],[Dealers_ar],[GovLaw],[GovLaw_ar],[Other],[Other_ar],[SouthAmerica],[SouthAmerica_ar])VALUES('22
01-2007','ORG2',0,0,0,0,48476,48476,0,0,0,0,0,0,0,0)
INSERT INTO [monthly_ships]
([BA],[org],[WHA],[WHA_ar],[DeptStores],[DeptStores_ar],[Asia],[Asia_ar],[Dealers],[Dealers_ar],[GovLaw],[GovLaw_ar],[Other],[Other_ar],[SouthAmerica],[SouthAmerica_ar])VALUES('31
01-2007','ORG2',0,0,0,0,66082,66158,0,0,0,0,0,0,0,0)
INSERT INTO [monthly_ships]
([BA],[org],[WHA],[WHA_ar],[DeptStores],[DeptStores_ar],[Asia],[Asia_ar],[Dealers],[Dealers_ar],[GovLaw],[GovLaw_ar],[Other],[Other_ar],[SouthAmerica],[SouthAmerica_ar])VALUES('10
01-2007','ORG3',0,0,0,0,0,0,0,0,0,0,0,0,51000,50184)
INSERT INTO [monthly_ships]
([BA],[org],[WHA],[WHA_ar],[DeptStores],[DeptStores_ar],[Asia],[Asia_ar],[Dealers],[Dealers_ar],[GovLaw],[GovLaw_ar],[Other],[Other_ar],[SouthAmerica],[SouthAmerica_ar])VALUES('11
01-2007','ORG3',0,0,0,0,0,0,0,0,0,0,0,0,1035136,952032)
INSERT INTO [monthly_ships]
([BA],[org],[WHA],[WHA_ar],[DeptStores],[DeptStores_ar],[Asia],[Asia_ar],[Dealers],[Dealers_ar],[GovLaw],[GovLaw_ar],[Other],[Other_ar],[SouthAmerica],[SouthAmerica_ar])VALUES('12
01-2007','ORG3',0,0,0,0,0,0,0,0,0,0,0,0,12274,10200)
INSERT INTO [monthly_ships]
([BA],[org],[WHA],[WHA_ar],[DeptStores],[DeptStores_ar],[Asia],[Asia_ar],[Dealers],[Dealers_ar],[GovLaw],[GovLaw_ar],[Other],[Other_ar],[SouthAmerica],[SouthAmerica_ar])VALUES('14
01-2007','ORG3',0,0,0,0,0,0,0,0,0,0,0,0,32922,29278)
INSERT INTO [monthly_ships]
([BA],[org],[WHA],[WHA_ar],[DeptStores],[DeptStores_ar],[Asia],[Asia_ar],[Dealers],[Dealers_ar],[GovLaw],[GovLaw_ar],[Other],[Other_ar],[SouthAmerica],[SouthAmerica_ar])VALUES('21
01-2007','ORG3',0,0,0,0,0,0,0,0,0,0,0,0,858612,715973)
INSERT INTO [monthly_ships]
([BA],[org],[WHA],[WHA_ar],[DeptStores],[DeptStores_ar],[Asia],[Asia_ar],[Dealers],[Dealers_ar],[GovLaw],[GovLaw_ar],[Other],[Other_ar],[SouthAmerica],[SouthAmerica_ar])VALUES('31
01-2007','ORG3',0,0,0,0,0,0,0,0,0,0,0,0,69763,68817)
Here's the query:
select
x =
case
when left(ba,2) in ('10','11','12') then
'FireExtenguishers '
when left(ba,2) in ('13','14') then
'Sealed FirePrevention '
when left(ba,2) in ('21','22') then
'Automated '
when left(ba,2) in ('31') then
'Accessories '
else
'Other '
end+ substring(ba,4,10),
sum(WHA) as 'WHA',
sum(WHA_ar) as 'WHA_ar',
sum(DeptStores) as 'DeptStores',
sum(DeptStores_ar) as 'DeptStores_ar',
sum(Asia) as 'Asia',
sum(Asia_ar) as 'Asia_ar',
sum(Dealers) as 'Dealers',
sum(Dealers_ar) as 'Dealers_ar',
sum(GovLaw) as 'GovLaw',
sum(GovLaw_ar) as 'GovLaw_ar',
sum(Other) as 'Other',
sum(Other_ar) as 'Other_ar',
sum(SouthAmerica) as 'SouthAmerica',
sum(SouthAmerica_ar) as 'SouthAmerica_ar'
from monthly_ships
group by
case
when left(ba,2) in ('10','11','12') then
'FireExtenguishers '
when left(ba,2) in ('13','14') then
'Sealed FirePrevention '
when left(ba,2) in ('21','22') then
'Automated '
when left(ba,2) in ('31') then
'Accessories '
else
'Other '
end+ substring(ba,4,10)
How it comes out is:
Accessories 01-2007
Automated 01-2007
FireExtenguishers 01-2007
Other 01-2007
Sealed FirePrevention 01-2007
How I want it to be listed is:
FireExtenguishers 01-2007
Sealed FirePrevention 01-2007
Automated 01-2007
Accessories 01-2007
Other 01-2007
In addition to that, on the columns with the numbers in them - WHA,
WHA_ar, DeptStores, Deptstores_ar and so on - I want to provide a line
with a total on it. I know that'll be done with rollup on the grouping,
but I am not sure of the syntax yet.
Tom Moreau wrote:
> The data you provided were numeric, so you'd have to post the actual data
> you are using for me to figure out what you're doing. That said, you can
> use the CASE expression to roll your own sort order:
>
> ORDER BY
> CASE Col1
> WHEN 'F' then 1
> WHEN 'A' then 2
> WHEN 'S' then 3
> WHEN 'P' then 4
> END
>