473,394 Members | 1,811 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

how to consolidate or merge two rows of data into one row in query result

After i run the query below i have two rows of data. The two rows arise because [CATEGORY DESCRIPTION] has many different values depending on TYPE_DESCRIPTION and other fields. A portion of the output is

Expand|Select|Wrap|Line Numbers
  1. Account    Category Description     ADJ_CD 
  2. 123        Average                   .82
  3. 123        B+                       1.41
I NEED
Expand|Select|Wrap|Line Numbers
  1. Account    Category Description    ADJ_CD 
  2. 123        B+                        .82

I also tried other queries and TRANSFORM with PIVOT.

The SQL View for the query is:

Expand|Select|Wrap|Line Numbers
  1. SELECT real_acct.ACCOUNT, structural_elem1.[CATEGORY_DESCRIPTION] AS Expr1, real_acct.SITE_ADDR_1
  2.    , [NEIGHBORHOOD_CODE] & "/ " & [NEIGHBORHOOD_GROUP] AS [NEIGHBORHOOD CODE/GR], structural_elem1.STATE_CLASS_CODE, real_acct.YR_IMPR, real_acct.TOTAL_BUILDING_AREA, structural_elem1.CATEGORY_DESCRIPTION, structural_elem1.ADJ_CD, real_acct.LAND_VALUE, real_acct.IMPROVEMENT_VALUE, real_acct.EXTRA_FEATURES_VALUE, real_acct.TOTAL_MARKET_VALUE
  3. FROM real_acct INNER JOIN structural_elem1 ON real_acct.ACCOUNT = structural_elem1.ACCOUNT
  4. WHERE (([structural_elem1]![ACCOUNT]=[real_acct]![ACCOUNT] And [TYPE_DESCRIPTION]="Grade Adjustment") AND ((real_acct.YR_IMPR)>"1997" And (real_acct.YR_IMPR)<"2007") AND ((real_acct.TOTAL_BUILDING_AREA)>"2100" And (real_acct.TOTAL_BUILDING_AREA)<"2300") AND ((structural_elem1.STRUCTURE_TYPE)="GRD" Or (structural_elem1.STRUCTURE_TYPE)="CDU") AND ((structural_elem1.TYPE_DESCRIPTION)="Grade Adjustment" Or (structural_elem1.TYPE_DESCRIPTION)="Cond / Desir / Util") AND ((structural_elem1.CATEGORY_DESCRIPTION)="B" Or (structural_elem1.CATEGORY_DESCRIPTION)="A-" Or (structural_elem1.CATEGORY_DESCRIPTION)="B+" Or (structural_elem1.CATEGORY_DESCRIPTION)="Very Poor" Or (structural_elem1.CATEGORY_DESCRIPTION)="Poor" Or (structural_elem1.CATEGORY_DESCRIPTION)="Average" Or (structural_elem1.CATEGORY_DESCRIPTION)="Fair" Or (structural_elem1.CATEGORY_DESCRIPTION)="Very Good" Or (structural_elem1.CATEGORY_DESCRIPTION)="Excellent" And [STRUCTURE_TYPE]="CDU" Or (structural_elem1.CATEGORY_DESCRIPTION)="GRD" And "«Expr» [structural_elem1]![TYPE_DESCRIPTION]"="Cond / Desir / Util" Or (structural_elem1.CATEGORY_DESCRIPTION)="Grade Adjustment")) OR (((real_acct.YR_IMPR)>"1997" And (real_acct.YR_IMPR)<"2007") AND ((real_acct.TOTAL_BUILDING_AREA)>"2100" And (real_acct.TOTAL_BUILDING_AREA)<"2300") AND ((structural_elem1.STRUCTURE_TYPE)="GRD" Or (structural_elem1.STRUCTURE_TYPE)="CDU") AND ((structural_elem1.TYPE_DESCRIPTION)="Grade Adjustment" Or (structural_elem1.TYPE_DESCRIPTION)="Cond / Desir / Util") AND ((structural_elem1.CATEGORY_DESCRIPTION)="B" Or (structural_elem1.CATEGORY_DESCRIPTION)="A-" Or (structural_elem1.CATEGORY_DESCRIPTION)="B+" Or (structural_elem1.CATEGORY_DESCRIPTION)="Very Poor" Or (structural_elem1.CATEGORY_DESCRIPTION)="Poor" Or (structural_elem1.CATEGORY_DESCRIPTION)="Average" Or (structural_elem1.CATEGORY_DESCRIPTION)="Fair" Or (structural_elem1.CATEGORY_DESCRIPTION)="Very Good" Or (structural_elem1.CATEGORY_DESCRIPTION)="Excellent" And [STRUCTURE_TYPE]="CDU" Or (structural_elem1.CATEGORY_DESCRIPTION)="GRD" And "«Expr» [structural_elem1]![TYPE_DESCRIPTION]"="Cond / Desir / Util" Or (structural_elem1.CATEGORY_DESCRIPTION)="Grade Adjustment"));
Jul 13 '16 #1
4 925
jforbes
1,107 Expert 1GB
If you are getting multiple rows for each real_acct.ACCOUNT, then it means that there are multiple rows available in structural_elem1 that have an ACCOUNT that match against real_acct.ACCOUNT

If there is a One-to-Many relationship for FROM real_acct INNER JOIN structural_elem1 ON real_acct.ACCOUNT = structural_elem1.ACCOUNT and you are expecting a One-to-One, there is probably a problem with the Data/Data Structure and not the SQL Statement.

If there is really a One-to-Many and you are attempting to only select a single record from structural_elem1 for each ACCOUNT, then you'll need to re-evaluate what it is that you are trying to accomplish. Mainly because, how is SQL supposed to know which record to return of the many available?
Jul 13 '16 #2
Thank you. You are correct the structural_elem1 table contains several records for each account. I would have used a different structure, but the table was created by a tax appraisal district and it contains about 2 million accounts.

The query returns two rows for each account that include the data or values that I need.

I just need to get the values to one row. Maybe I can concatenate the two fields to a new table.
Jul 15 '16 #3
zmbd
5,501 Expert Mod 4TB
What is it that you really need from the field [CATEGORY_DESCRIPTION] ?

In the example You have [CATEGORY_DESCRIPTION]="Average" which is in
WHERE ... Or (structural_elem1.CATEGORY_DESCRIPTION)="Average"
Yet you say you only want [CATEGORY_DESCRIPTION]="B-"
WHERE ... Or (structural_elem1.CATEGORY_DESCRIPTION)="B+

Brain storming here - nothing tried nor even closly evaluated for feasibility:

1st thought, in your very long WHERE clause eliminate the conditions returning the undesired [CATEGORY_DESCRIPTION] values. However, from what I am reading here, it appears that there are two separate paradigms being used for the [CATEGORY_DESCRIPTION] values.

2nd thought, build a second query based on the first query that returns only the desired values
Expand|Select|Wrap|Line Numbers
  1. SELECT (fields here) 
  2. FROM (your Query) 
  3. WHERE ([CATEGORY_DESCRIPTION] 
  4.    IN (list your values); 
You could also use the OR and build a series of
Expand|Select|Wrap|Line Numbers
  1. WHERE(([CATEGORY_DESCRIPTION]="A+") OR
  2.    [CATEGORY_DESCRIPTION]="B-" OR ...
Not sure which would be better for speed against that many records...

3rd thought, "two separate paradigms being used for the [CATEGORY_DESCRIPTION] values"
Use a cross reference table relating the values against a standard value then returning the standard value. Not sure if this is any better than the 2nd thought.
Jul 16 '16 #4
The query returns two rows for each account. Only one of values for ADJ_CD and CATEGORY_DESCRIPTION valid for what I need. I don't know or understand how to combine the sub-queries or create a new table with the correct results in one row.


For example, this subquery will return the correct ADJ_CDU, but the wrong CATEGORY_DESCRIPTION.

Expand|Select|Wrap|Line Numbers
  1. SELECT RA_SUB.[ACCOUNT /ADDRESS], RA_SUB.YR_IMPR, RA_SUB.ADJ_CD AS [Cdu], RA_SUB.CATEGORY_DESCRIPTION AS [Grade Adjustment]
  2. FROM RA_SUB
  3. WHERE RA_SUB.TYPE_DESCRIPTION IN ('Cond / Desir / Util');
For example, this subquery will return the correct CATEGORY_DESCRIPTION, but the wrong ADJ_CDU.
Expand|Select|Wrap|Line Numbers
  1. SELECT RA_SUB.[ACCOUNT /ADDRESS], RA_SUB.YR_IMPR, RA_SUB.ADJ_CD AS [Cdu], RA_SUB.CATEGORY_DESCRIPTION AS [Grade Adjustment]
  2. FROM RA_SUB
  3. WHERE RA_SUB.TYPE_DESCRIPTION IN ('Grade Adjustment');
I need the correct value of ADJ_CD and CATEGORY_DESCRIPTION in the same row.

I also shortened the query to:

Expand|Select|Wrap|Line Numbers
  1. SELECT [real_acct]![ACCOUNT] & " / " & [SITE_ADDR_1] AS [ACCOUNT /ADDRESS], [NEIGHBORHOOD_CODE] & " / " & [NEIGHBORHOOD_GROUP] & " / " & [STATE_CLASS_CODE] AS [NEIGHBORHOOD CODE/GR/CLASS], real_acct.YR_IMPR, real_acct.TOTAL_BUILDING_AREA, structural_elem1.ADJ_CD, structural_elem1.CATEGORY_DESCRIPTION, real_acct.LAND_VALUE, real_acct.IMPROVEMENT_VALUE, real_acct.EXTRA_FEATURES_VALUE, real_acct.TOTAL_MARKET_VALUE, structural_elem1.TYPE_DESCRIPTION, structural_elem1.STRUCTURE_TYPE
  2. FROM real_acct INNER JOIN structural_elem1 ON real_acct.ACCOUNT = structural_elem1.ACCOUNT
  3. WHERE (((real_acct.YR_IMPR)>"1997" And (real_acct.YR_IMPR)<"2007") AND ((real_acct.TOTAL_BUILDING_AREA)>"2100" And (real_acct.TOTAL_BUILDING_AREA)<"2300") AND ((structural_elem1.CATEGORY_DESCRIPTION)="A-" Or (structural_elem1.CATEGORY_DESCRIPTION)="B+" Or (structural_elem1.CATEGORY_DESCRIPTION)="B") AND ((structural_elem1.TYPE_DESCRIPTION)="Grade Adjustment" Or (structural_elem1.TYPE_DESCRIPTION)="Cond / Desir / Util") AND ((structural_elem1.STRUCTURE_TYPE)="GRD" Or (structural_elem1.STRUCTURE_TYPE)="CDU")) OR (((real_acct.YR_IMPR)>"1997" And (real_acct.YR_IMPR)<"2007") AND ((real_acct.TOTAL_BUILDING_AREA)>"2100" And (real_acct.TOTAL_BUILDING_AREA)<"2300") AND ((structural_elem1.CATEGORY_DESCRIPTION)="Very Poor" Or (structural_elem1.CATEGORY_DESCRIPTION)="Poor" Or (structural_elem1.CATEGORY_DESCRIPTION)="Fair" Or (structural_elem1.CATEGORY_DESCRIPTION)="Average" Or (structural_elem1.CATEGORY_DESCRIPTION)="Good" Or (structural_elem1.CATEGORY_DESCRIPTION)="Very Good" Or (structural_elem1.CATEGORY_DESCRIPTION)="Excellent") AND ((structural_elem1.TYPE_DESCRIPTION)="Grade Adjustment" Or (structural_elem1.TYPE_DESCRIPTION)="Cond / Desir / Util") AND ((structural_elem1.STRUCTURE_TYPE)="GRD" Or (structural_elem1.STRUCTURE_TYPE)="CDU"));
Jul 17 '16 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: 'bonehead | last post by:
Greetings, I'd like to figure out some syntax for retrieving the data from a table when I don't know all the of field names. What I do know are, the name of the table, the names of the primary...
2
by: Lin Ma | last post by:
Greetings, In my search application, user can type a number to search. I use LIKE in my query. If a query result generates over 10,000 recordsets, it may several minutes to run. Is there a...
11
by: Surajit Laha | last post by:
I am firing a query like: SELECT TaskName, StartDate FROMTasks WHERE StartDate >= '01-Aug-2003' Now the result comes as: TaskName StartDate -------------------------- Task1 ...
3
by: vivian | last post by:
Hi Is there any limitation in query result in postgresql? When I run the query , I only can see maximum 1000 records(rows). Actually the results should be more than 1000. I appreciate the...
1
by: RookieDan | last post by:
Greetings fellow Accessers! Im new but in Access, but I have some background in different coding. I have a programme loading customer data into Access belonging to BMW dealers in Europe. ...
2
by: hrgilley | last post by:
I hope someone here can help me. I am trying to merge rows of data within a table. Here's what I have Guest id: Row: Section: Seat: 567 A CC 102 567 ...
2
by: reeba | last post by:
I want to store the query result, in an servlet, into an xml file and display the contents of the xml file on the browser...... my code is as follows: public void doPost(HttpServletRequest...
25
by: NDayave | last post by:
How do, I have a form that outputs addresses in a format that can be printed on to 3x7 label paper for envelopes. What I want is a way to enter blank (or " ") rows to the query result where the...
1
by: sesh | last post by:
The following is the sample result of a large SQL SELECT Statement, from a single table. I have used --- to show the X values under each column CODE------XX-------------YY-----------------ZZ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.