|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, February 11, 2010 5:00 PM
Points: 5,
Visits: 39
|
|
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.
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Wednesday, April 24, 2013 3:17 PM
Points: 6,731,
Visits: 12,131
|
|
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)
Lutz A pessimist is an optimist with experience.
How to get fast answers to your question How to post performance related questions Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 5:50 PM
Points: 1,308,
Visits: 3,899
|
|
[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
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Wednesday, April 24, 2013 3:17 PM
Points: 6,731,
Visits: 12,131
|
|
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...)?
Lutz A pessimist is an optimist with experience.
How to get fast answers to your question How to post performance related questions Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 5:50 PM
Points: 1,308,
Visits: 3,899
|
|
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
|
|
|
|