Solved

TSQL query

Posted on 2014-12-11
4
141 Views
Last Modified: 2014-12-12
Base on the TSQL below, what's the purpose of adding "MAX"  ? Can anyone think of any possible reason ? Does it mean it may return such records on PIC3, PIC2 and PIC1 and we use 'MAX' to restrict to one record ?

select inventbatchid,itemid,a2,a3,a4,
Tent,
max(case typeid when 'ImgS' then pic else '' end) as 'PIC3',
max(case typeid when 'sketchA' then pic else '' end) as 'PIC2',
max(case typeid when 'sketchB' then pic else '' end) as 'PIC1'  
into #tmp3 from #tmp2
0
Comment
Question by:AXISHK
  • 2
4 Comments
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 100 total points
ID: 40493284
Is there a GROUP BY in the source code?

One suggestion is that MAX() is being used to ensure only one value is returned, but without sample data there's a lot of guesswork involved.
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 300 total points
ID: 40493286
yes. in MySQL, the GROUP BY is not mandatory to get the MAX to work...
but I would recommend to add it up to the statement to make it clearer.

you may want to read up this article around that concept:
http://www.experts-exchange.com/Database/Miscellaneous/A_3203-DISTINCT-vs-GROUP-BY-and-why-does-it-not-work-for-my-query.html
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40493309
I would go further about MySQL's default slackness on GROUP BY!
By default there is an "extension" which in my opinion leads to lazy code that can produce poor results.

see:
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_28500137.html#a40270222
and
http://dev.mysql.com/doc/refman/5.0/en/sql-mode.html#sqlmode_only_full_group_by

However turning off the "extension" may affect existing scripts.
0
 
LVL 51

Assisted Solution

by:HainKurt
HainKurt earned 100 total points
ID: 40493537
it is just to get the not null value

Max(pic,'','','') >>> pic

case returns null for non 'ImgS' records, and returns pic for 'ImgS' record (s)

max(case typeid when 'ImgS' then pic else '' end) as 'PIC3',

maybe it is really getting max of those records like

Max(123,234,102,'','','',327,'') >>>327, just dealing with 'ImgS' records...
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now