Solved

TSQL query

Posted on 2014-12-11
4
149 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 143

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 53

Assisted Solution

by:Huseyin KAHRAMAN
Huseyin KAHRAMAN 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with Progress 4gl Rounding Function 6 60
Error in sql query statment. 21 70
SQL join ...want to return one row 4 14
Importing from CSV to SSMS 2 12
Creating and Managing Databases with phpMyAdmin in cPanel.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

738 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