Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Dynamic SQL in creating columns and summary Expand / Collapse
Author
Message
Posted Thursday, February 11, 2010 1:40 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum 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.
Post #864300
Posted Thursday, February 11, 2010 4:34 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 9:18 AM
Points: 6,829, Visits: 13,290
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
Post #864398
Posted Thursday, February 11, 2010 4:43 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 3:53 PM
Points: 1,780, Visits: 5,750
[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


  • MMGrid Addin
  • MMNose Addin


  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • Post #864403
    Posted Thursday, February 11, 2010 4:58 PM


    SSCertifiable

    SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

    Group: General Forum Members
    Last Login: 2 days ago @ 9:18 AM
    Points: 6,829, Visits: 13,290
    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
    Post #864407
    Posted Thursday, February 11, 2010 5:17 PM


    SSCommitted

    SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

    Group: General Forum Members
    Last Login: Yesterday @ 3:53 PM
    Points: 1,780, Visits: 5,750

    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


  • MMGrid Addin
  • MMNose Addin


  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • Post #864412
    « Prev Topic | Next Topic »

    Add to briefcase

    Permissions Expand / Collapse