February 11, 2010 at 1:40 pm
Hi...I am currently taking a new DBA role, and need some help:
A manufacture company has many machines that are maintained by
different contractors in their factories:
Machine M1,M2,M3,M4… locate in different Factory F1, F2,F3,F4…, and
they are maintained by different Contractors: C1,C2,C3,C4….
The main Machine table structure is like:
MachineID, Factory, Contractor
M1, F3, C4
M2, F2, C5
…
There is a request to get a summary of each contractor, how many
machines they are maintaining in all the factories, the result report should
be like:
Contractor, Factory1, Factory2, Factory3, Factory4….
C1, 50, 41, 32, 12,…
That is: Contractor C1 maintains: 50 machines in Facotry1; 41 machines
in Factory2; 32 machines in factory3 and 12 machines in Factory4…
C2, 98, 120,35,65,…and so on...
Search around and found the following query does do the work:
declare @sqltmp varchar (3000)
set @sqltmp = ' select contractor'
select @sqltmp = @sqltmp + ', sum (case factory when '''+factory+''' then 1
else 0 end) ['+factory+']' from (select distinct factory from tbl_machines
order by factory) as tt
select @sqltmp = @sqltmp + 'from tbl_machines group by contractor order by
contractor'
exec(@sqltmp)
But I do not understand how are the '''+factory+''' and ['+factory+']' really work in this
query, and the puzzle many '''s.
Could somebody explain this magic, so that I can completely understand and really re-use the same technics in the future.
Thanks.
February 11, 2010 at 4:34 pm
The concept itself is described in the last link in my signature.
The reason for all the ''' is simple:
If you want to get the dynamic sql to look like "...CASE factory WHEN 'F1' ..." you have to provide the quotation marks around F1, since those are not part of the value stored in your table.
If you want to have a single quotation mark in your dynamic statement you need to surround it by separate quotation marks.
It's basically the same concept as if you'd like to use the value O'Connor in an select statement:
SELECT 'O'Connor' (invalid) vs.
SELECT 'O''Connor' (valid) vs.
SELECT 'O''Connor'+'''' (valid)
February 11, 2010 at 4:43 pm
[previous post appeared while I was writing this one - this can be taken as an addendum to that]
If you really want that sort of information, just extract the data using a reporting tool (Excel, Crystal, SSRS etc) and ask it to cross tabulate it for you - it is easier and for the amount of data I imagine you have there it will work fine.
If you just want to understand the TSQL then:
''' <- the single quote is used to delimit strings, so if you want to include a single quote in a string you have to use two quotes together. There are three here because you have two quotes inside the string immediately followed by a single quote to mark the end of the string.
In this case:
...case factory when '''+factory+''' then 1 else 0 end
is building a string that checks for a specific "factory" value (e.g. F1) in the column factory and if it is found returns 1, otherwise it returns 0
e.g. if factory = F1 you get
...case factory when 'F1' then 1 else 0 end
and ['+factory+'] would give you : [F1] and putting all that together you get
...sum(case factory when 'F1' then 1 else 0 end) [F1]
which is generating the sql to query the data table and assign a value of 1 to that column whenever the factory is = F1 and sum up all the 1s, returning the value (which is actually a count of the number of rows with factory='F1' for each contractor) as column [F1]
declare @sqltmp varchar (3000)
set @sqltmp = 'select contractor'
select @sqltmp = @sqltmp + ', sum (case factory when '''+factory+''' then 1
else 0 end) ['+factory+']' from (select distinct factory from tbl_machines
order by factory) as tt
select @sqltmp = @sqltmp + 'from tbl_machines group by contractor order by
contractor'
The code above should generate a sql command something like this (reformatted for clarity):
select contractor
, sum(case factory when 'F1' then 1 else 0 end) [F1]
, sum(case factory when 'F2' then 1 else 0 end) [F2]
, sum(case factory when 'F3' then 1 else 0 end) [F3]
, sum(case factory when 'F4' then 1 else 0 end) [F4]
from tbl_machines
group by contractor
order by contractor
Personally, I would recommend letting Excel/Crystal/SSRS do the hard work for you - they are all very competent at it and won't care if you add factories/machines/contractors.
This kind of dynamic sql works up to a point, but it's not what SQL is good at - let your favoured reporting tool do the formatting...
MM
select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);
February 11, 2010 at 4:58 pm
mister.magoo (2/11/2010)
...This kind of dynamic sql works up to a point, but it's not what SQL is good at - let your favoured reporting tool do the formatting...
Would you mind giving a more detailed explanation including a sample scenario to show the point where dynamic SQL start not to work anymore?
I'm scared about your statement which implies that EXCEL is a better tool than SQL regarding PIVOT (or dynamic sql)....
How about using the "fantastic Excel 2003" to build a PIVOT with a data source set of more than 66 thousand rows? Care to show how to do that including performace comparison (Let's assume you have 64000 rows to process to give Excel a chance to hold the data...)?
February 11, 2010 at 5:17 pm
Would you mind giving a more detailed explanation including a sample scenario to show the point where dynamic SQL start not to work anymore?
I meant "up to a point" as in - don't go writing whole reporting packages in dynamic sql, not as in "your query will fail", it's just an opinion...
I'm scared about your statement which implies that EXCEL is a better tool than SQL regarding PIVOT (or dynamic sql)....
Don't be, again it's an opinion, but let's just compare how long it takes the OP to write a select statement that pivots an unknown number of columns and then output that to somewhere to show the requester the information they wanted against how long it takes them to do a straight select into Excel or SSRS and say "there you go , pivot that".
How about using the "fantastic Excel 2003" to build a PIVOT with a data source set of more than 66 thousand rows? Care to show how to do that including performace comparison (Let's assume you have 64000 rows to process to give Excel a chance to hold the data...)?
Ok, if they have 64000 contractors, then Excel 2003 would be a problem - granted - maybe we should also go back to SQL 2000 if you want to use old products for your reporting?
No, but seriously, the number of people wanting to "present" data in a certain way in SQL seems to be growing and AFAIK it is not a reporting tool and never will be, so why not accept that and choose a reporting tool that is designed to do the job - if excel can handle the dataset, let it - especially in the newer releases - they can actually handle quite large sets of data (not massive, but quite large) these days, and other reporting tools exist that can do even more. SSRS combined with Analysis services weren't created just for laughs - they are designed to take the reporting load away from SQL server.
Anyway, I hope you didn't get the impression I was trying to undermine your good advice for the OP, as I said in my edit, your post appeared while I was writing mine and I certainly did not want to detract from yours...
Kind regards
M
MM
select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply