Select column with highest value

 Hi,

How do I get the desired result for the following sample Table1 data:

  Source Table:  Table1
  Columns:         Mukey          State    L11     L12    L21    L22
                         m657753      AL       8.00    0.00   5.00   1.00
                         m657753      TN      0.00     1.00   3.00   0.00
                         m657754      AL       1.00     6.00  2.00   27.00
                         M657754      TN       0.00     4.00  1.00   5.00

  Desired result:
  Target Table:  Table2
  Columns:         Mukey         State         LandUse 
                        m657753     AL             L11
                        m657753     TN             L21
                        m657754     AL              L22
                        m657754     TN             L22

Thanks in advance.
0
Utf
5/24/2010 8:53:01 PM
sqlserver.programming 1873 articles. 0 followers. Follow

4 Replies
619 Views

Similar Articles

[PageSpeed] 51

You didn't specify what to do in the case of ties, and whether or not the 
Lxx columns can be NULL.  But the following is one possibility as long as 
you don't have NULLs.

Insert Table2(Mukey, State, LandUse)
Select Mukey, State,
  Case When L11>= L12 And L11>=L21 And L11>=L22 Then 'L11'
    When L12>=L21 And L12>=L22 Then 'L12'
    When L21>=L22 Then 'L21'
    Else 'L22' End As LandUse
From Table1

You could also do it with an UNPIVOT and then using the ROW_NUMBER() 
function on the UNPIVOTed result to find the largest value.  But if you 
don't have NULLs the above will probably be more efficient than an UNPIVOT 
solution.

Tom

"Marilyn" <Marilyn@discussions.microsoft.com> wrote in message 
news:AD5B208A-8FBA-4C0D-87E0-3B33BF7CA806@microsoft.com...
> Hi,
>
> How do I get the desired result for the following sample Table1 data:
>
>  Source Table:  Table1
>  Columns:         Mukey          State    L11     L12    L21    L22
>                         m657753      AL       8.00    0.00   5.00   1.00
>                         m657753      TN      0.00     1.00   3.00   0.00
>                         m657754      AL       1.00     6.00  2.00   27.00
>                         M657754      TN       0.00     4.00  1.00   5.00
>
>  Desired result:
>  Target Table:  Table2
>  Columns:         Mukey         State         LandUse
>                        m657753     AL             L11
>                        m657753     TN             L21
>                        m657754     AL              L22
>                        m657754     TN             L22
>
> Thanks in advance. 

0
Tom
5/24/2010 9:23:05 PM
Here is example with UNPIVOT and another with XQuery:
http://pratchev.blogspot.com/2009/08/column-aggregates.html

-- 
Plamen Ratchev
http://www.SQLStudio.com
0
Plamen
5/24/2010 10:29:24 PM
It would really help if you would post DDL instead of your personal
narratives.
0
CELKO
5/25/2010 5:29:11 AM
Hi,

I followed your unpivot sample in your blog and it worked in my case.  Thank 
you.

"Plamen Ratchev" wrote:

> Here is example with UNPIVOT and another with XQuery:
> http://pratchev.blogspot.com/2009/08/column-aggregates.html
> 
> -- 
> Plamen Ratchev
> http://www.SQLStudio.com
> .
> 
0
Utf
5/25/2010 7:02:01 PM
Reply:

Similar Artilces:

Combo box list selection based on earlier selection by user
HI, I have a form that has a combo box 1 (Product) that reads a the Product table and has a unique id attached to each product (RO#). There is a second combo box 2 that is RO#. Now what I would like to do is when a user selects a Product in combo box 1, the system should lookup the Product selected from combo box 1 and upload the corresponding RO# in the combo box 2 list. Now I am using both combo boxes as Unbound and in the combo box 2 I have the two columns Product and RO# - in the Criteria row of RO# I have the following: Like IIf([Forms]![xa_f_lookup]![RO#]=" ",[Forms]![x...

Run report on multi selected records.
I try to make a report for a custom entity that works like the Account Summary: let's say that it's Customer's Inquiry Summary for entity Customer's Inquiry I made the report so that I'm able to run the report on Customer's Inquiry Form. But when I select multi records in the list of Customer's Inquiry and run report, only one record is show in one page of the the report. I would like to have the selected records appear in the report with each record on each page. How can I do this? ...

Help with grouping columns
Maybe I'm overlooking the obvious...but I'm having a hard time groupin columns. Here's the goal: Columns A - L are the 12 months... Jan-Dec. I want to group 3 columns (3 months) at a time. So I'd have 4 colum groups side by side. I select the entire first 3 columns...data...group/outline...an group...creates the group fine. I then select the next 3 columns (next 3 months)...to be in their ow group. When I try to group them...it just ads them to the firs group...making one large group. The first 3 columns are not selected when I do this. I've also tried playing with the s...

move row data to columns
We had a problem with some test data and instead of test results being recorded in multiple rows 5 columns wide, all of the data was recorded in one row multiple columns wide. The data consists of 5 readings taken every second for a period of 2 hours. Each reading is in it's own cell (no cell contains more than one reading). Do you know of a way that I can move each set of readings (5 test results) into it's own 5 column wide row? i.e. currently shows in single row as: 0, 0, 0, 0, 0, 128, 128, 128, 128, 128, 234, 234, 234, 234, 234, etc. and I need in 5 column wide rows: 0, 0, 0, ...

applying operation to column
I am trying to divide a column that just contains numbers by 100, but read online that I should use some paste special technique, but m version of excel doesn't have that. I think I have '97. Is ther another method to divide every cell in a column by 100 -- chrismicle ----------------------------------------------------------------------- chrismiclei's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=2505 View this thread: http://www.excelforum.com/showthread.php?threadid=38577 Yes, you have the paste special, but you first have to copy a cell that con...

Fast way to search many cells by column for text strings
Am looking for quick way to search for and list, a specified, given text string in all cells in a column, e.g. look for text string " E/G " Assume the target col is col A, data from row2 Put in B2: =ISNUMBER(FIND("E/G",A2)) Put in C2: =ISNUMBER(SEARCH("E/G",A2)) (FIND is case sensitive, SEARCH is not case sensitive) Select B2:C2 and fill down Put a label into B1:C1, then do an autofilter in B1 (or in C1) to filter out the rows with: TRUE, depending on whether search is to be case sensitive or not. -- Rgds Max xl 97 --- GMT+8, 1� 22' N 103� 45' E xd...

Can't select objects in Excel 2007
I'm trying to create a form in Excel 2007, and although I can manipulate, copy & paste controls (check boxes, etc) I can't select multiple objects to align them, etc. I've checked on-line help (and have followed the instructions), and other websites, but no one seems to have indicated a problem. It is extremely cumbersome and I need a solution. Any ideas? Thanks in advance, Jim Berglund I know this is about a year too late, but it may help others who have stumbled across this page looking for similar help. On the Home tab, in the Editing group, click Find & Select....

Lot Selection not Showing when I select PO on Receiving Entry
Usually, the Lot selection screen would pop up when I select a PO at the Line Detail of the Receivings Entry, now it does not! And GP will let items post without a lot number! It seems as if a setting has changed. Any ideas anyone? Any help is appreciated. Thanks, R Are you sure the item is marked to track lots? This wiould be set item by item on the Item Maintenance Options window. -- Richard L. Whaley Author / Consultant / MVP 2006-2008 Documentation for Software Users Get our Free Tips and Tricks Newsletter and check out our books at http://www.AccoladePublications.com &q...

How do I have stacked columns and a reg col. on same chart?
I have three variables - payroll expenses, vendor expenses, and management fees. I want payroll and vendor to be on top of eachother, and management to be next to them. Can anyone help? Please? "Kat" wrote: > I have three variables - payroll expenses, vendor expenses, and management > fees. I want payroll and vendor to be on top of eachother, and management to > be next to them. Can anyone help? Hi, Have a look at Jon's page which has a collection of links to examples of how to produce clustered stacked charts. http://peltiertech.com/Excel/ChartsHowTo/Clus...

Select contact then hangs
Hi, please help. I have a user that when she selects a contact using To: in a new email, after selecting 2, it closes outlook??. Spec is W2kSP4\office2k with all windows updates and office updates installed. Profile has been deleted and re-created, plus i have set up personal folders and moved contacts into there, and set that folder as a default, but makes no difference. Please help. ...

CListCtrl: Changing the Column Header Layout
Hi all, I'm using a CListCtrl and I need another layout of the column header, especially - 2 lines of header text - changed background and forground colours - changeable y-Size I that possible? I cannot find any information about. Thanks in advance Richard You should be able to get the header control and do anything with it you want. Go to the MSDN, look under the topic "ClistCtrl class", and about 17 subitems into it there is a topic called "header controls" which explains all this. GetHeaderCtrl is what you are looking for. joe On Thu, 26 Aug 2004 13:58...

Selecting rows from various sheets
Hi all, I have several sheets with a few columns in each. One colum in each sheet has a unique value. Most of these unique values are th same in each sheet but not all. Is there any way that I can set up new sheet that will have the unique value in column 1 and th corresponding column values frommeach sheet beside it. For example Sheet 1 aa 1 1 1 bb 2 2 2 cc 3 3 3 Sheet 2 aa 4 4 cc 5 5 Result aa 1 1 1 4 4 bb 2 2 2 cc 3 3 3 5 5 Thanks alot, Sha -- sha ----------------------------------------------------------------------- shav's Profile: http...

select text in Calendar
I'm working on a calendar in Publisher. I'm adding text into a cell by spacing down 1 point, then typing in 8 points. Doing fine until April. At that time, I can only select cells (whole days). It will not let me select the text. Help is no help. Any hints out there. Did I accidentally select something to do this? Bonnie Have you added a picture to the cell or a shape? If you have send it to the back, make the cell no fill and try again. -- Mary Sauer MSFT MVP http://office.microsoft.com/ http://msauer.mvps.org/ news://msnews.microsoft.com "GoBonnieGo" <GoB...

macro to filter data based on selected data
Hi, I am looking for a macro that requests input of a reference code and then a date and then filters the data in my spreadsheet based on this criteria. I am using excel 2007. Any help with this will be greatly appreciated. You can probably record a macro while doing it manually and then clean it up and add input boxes to ask the question or input your variable in a cell and use that. If all else fails, If desired, send your file to my address below. I will only look if: 1. You send a copy of this message on an inserted sheet 2. You give me the newsgroup and the ...

How to stop empty columns from printing
Hello, My spreadsheet uses columns A-G, but when I print, column H also prints even though there is nothing in any of its cells. There is no print area set. Why is this empty column printing? And how do I stop it from printing? TIA. Mark On Mon, 27 Nov 2006 10:04:37 -0500, "Mark Christensen" <markc@ssbrakes.com> wrote: >My spreadsheet uses columns A-G, but when I print, column H also prints even >though there is nothing in any of its cells. There is no print area set. >Why is this empty column printing? And how do I stop it from printing? TIA. Hide column...

How do I add different ranges in a column together?
I have a large column of numbers and need to select random numbers that I need to total. Rather than adidng one cell at a time. How do I add multiple ranges within the column together? Thank you If they're ranges (as in groups of consecutive cells) you can use SUM, as in =sum(a1:a5,a20:a43, ... ). If they're cells that are selected based on the value of some other cell in the same row, you can use sumif: =sumif(b:b,"Selection",a:a) would add the values in column A of the rows where column B is equal to "Selection". If it's really pretty random, I'...

Link select with a blast
I have Win 7 Home Premium, x64. Quite often, when I select a link, a mouse pointer changes to a palm with a blast. I understand, the palm is the standard mouse pointer for link select, but what is the blast? What it indicates? -- Ilya Zeldes Fort Myers, Florida Hi Ilya, I suspect that this is being caused by a tracking program. Run something like Ad-Aware and see if it indicates a tracking company. If so, you can delete it, however, it might prevent you from accessing that particular website. Dave "Ilya Zeldes" wrote in message news:7A6FF9B1-5512-44...

How do I convert a column from USD to GBP
I am trying to convert a column of USD figures in excel into GBP then I want to add 92% - is there a formula I can use? with your value in A1, in B1 enter: =A1+(A1*.92) format as Currency and select the GBP currency Symbol HTH Bruce -- swatsp0p ------------------------------------------------------------------------ swatsp0p's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=15101 View this thread: http://www.excelforum.com/showthread.php?threadid=383116 Hi Lisa, Image you have the exchange rage (i.e. the number of dollars in a pound) in cell B2 and ...

Select Case in a Change Event
Hi, I am trying to use a worksheet Change Event to trap changes to specific columns. depending on which column is chnaged by the user, the value in another ciolumn will be altered; so, changes in either column O or P will update column Q, changes in either column T or U will update column V and changes in column Z or AA will update column AB. What I can't figure out is the Select Case bit (where I've got the shouted question below). Here's what I've put together, any comments would be much appreciated. Private Sub Worksheet_Change(ByVal Target As Range) ...

Select Records based another table...
This may be very elementary, but i can't wrap my brain around it. I have tried to dummy it down as much as i can for myself, with plans of adapting it into something working. I have three tables that look like the following: Tbl 1: ProdID A B C 1 $1 $2 $3 2 $4 $5 $6 3 $7 $8 $9 Tbl 2: RmNum ProdID 1 3 1 2 2 1 2 2 Tbl 3: RmNum Style 1 A 2 C Relationships are: [tbl1 ProdID] ------ [tbl2 ProdID] [tbl2 RmNum] ...

Two-Column problem
Using Arrange, Layout Guides, I set up Publisher 2002 with two columns per page and with two master pages with mirrored guides. For the first 11 pages, text flowed as expected. That is, each new pair of pages had two textboxes per page. But now, when I add a pair of pages, Publisher gives me guides for two columns per page but gives me only one page-wide textbox per page, not two. How do I force Publisher to give me two textboxes per page, as it did for the first 11 pages? (I might be missing something obvious. I haven't used Publisher in several years.) Thanks. Charley After mana...

Value Limits In A chart
Is there any way possible to change the color of a data series within a chart when it exceeds a value of another data series? Thanks!! Hi see: http://peltiertech.com/Excel/Charts/format.html#CondChart and http://peltiertech.com/Excel/Charts/format.html#CondChart2 -- Regards Frank Kabel Frankfurt, Germany "Robby" <Robby@discussions.microsoft.com> schrieb im Newsbeitrag news:03E95BC9-B246-41E9-A941-177C2C22CB4F@microsoft.com... > Is there any way possible to change the color of a data series within a chart > when it exceeds a value of another data series? > > Tha...

Test for a value on a subform
I have a form with a subform. The user is suppose to complete a response for all of the Result fields in the subform. Each subform will hav a variable number of Result records. I want to test for the existence of a result and, if null, prompt the user to go back and complete the form. the code below is close, but not quite there. Any help? Thanks 'Test for values in all Result field on open subform 'For N = 1 to MaxQuestionNumber 'CurrentResult = ("[Result]", "tblAuditQuestionResults", "[QuestionNumber] = N") ' If IsNull CurrentR...

column heads
How do I designate an entry as the column head/label/name vs. data entry? In what context? -- Cordially, Chip Pearson Microsoft MVP - Excel Pearson Software Consulting, LLC www.cpearson.com "4most" <4most@discussions.microsoft.com> wrote in message news:B5B96664-1D46-4CD8-8564-5ABDBF36DA64@microsoft.com... > How do I designate an entry as the column head/label/name vs. > data entry? I am creating an elementary spreadsheet, and I want to place headings at the top of each column. "Chip Pearson" wrote: > In what context? > > > -- > ...

Multi-Select List Box Does Not Retain Selected Items
I created an Outlook Form in 2003. But when I select items in the list box they do not remain select when I select another control on the form. Any suggestions? Did you bind the list box to a keywords field? -- Sue Mosher, Outlook MVP Author of Microsoft Outlook Programming - Jumpstart for Administrators, Power Users, and Developers http://www.outlookcode.com/jumpstart.aspx "kaykay" <anonymous@discussions.microsoft.com> wrote in message news:276dd01c4638d$bd5af590$a501280a@phx.gbl... > I created an Outlook Form in 2003. But when I select > items i...