?
Solved

SQL query structure

Posted on 2011-05-03
4
Medium Priority
?
268 Views
Last Modified: 2012-05-11
I have two tables. Together both tables represent an acutal table to be displayed on the web with rows and columns.

TableA contains the table definition and three columns.

IDNo - Unique Identifier
NoOfRows - Total number of rows found in the table
NoOfCols - Total Number of columns found in the table

TableB contains the actual table data. Each record contains the data for a table cell. This is identified by the row and column number. For this query this table has just 4 rows

IDNo - Unique Identifier
TableAIDNo - The unique Identifier from TableA that relates this record in TableB to a specific table   definition in TableA.
RowNo - The Row number of this record
ColNo - The Column Number of this record.

Somewhere along the line, TableA and TableB have gone out of Sync. That is TableA's No of Rows and No of Cols fields may not reflect the Max Number of rows or the Max Number of Columns of a particular web table in TableB.

I want to identify the TableA IDNo's where the tables are out of Sync.

I tried

Select A.IDNo From TableA A
Left Outer Join TableB B
On A.IDNo = B.TableAIDNo
where A.NoOfRows <> Max(B.RowNo)

This failed because of the aggegate function. What would be the correct syntax to get this result?

I was thinking of using a similar query to identify the records whose Columns do not match and joining the two queries with a UNION.

The second part of the questions relates to fixing the problem. What would the update statement look like to update the NoOfRows and NoOfCols fields in TableA with the Max(RowNo) and Max(ColNo) from tableB. It does not matter if the UPDATE statement is one combined for both Rows and Cols or if there are two UPDATE statements, one for Rows and a second for Cols.

Thanks,
Pat

0
Comment
Question by:mpdillon
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
kaminda earned 2000 total points
ID: 35512242
You can find the mismatching records using this query

SELECT A.IDNo FROM TableA A
WHERE 
NoOfRows <> (SELECT MAX(RowNo) FROM TABLEB WHERE TableAIDNo  = A.IDNO) 
OR 
NoOfCols <> (SELECT MAX(ColNo) FROM TABLEB WHERE TableAIDNo  = A.IDNO)

Open in new window


This will update all rows regardless of they are correct or not. But as far as it solve your issue and dont break anything I think it is fine.

UPDATE A 
SET NoOfRows = (SELECT MAX(RowNo) FROM TABLEB WHERE TableAIDNo  = A.IDNO) ,
NoOfCols = (SELECT MAX(ColNo) FROM TABLEB WHERE TableAIDNo  = A.IDNO)
FROM 
TableA A

Open in new window



0
 
LVL 1

Expert Comment

by:Egg77
ID: 35512253
How about something like this

SELECT A.IDNo, A.NoOfRows as ANoOfRows, B.NoOfRows as BNoOfRows
FROM TABLEA A,
     (SELECT TableAIDNo, COUNT(RowNo) as NoOfRows
      FROM TABLEB) as B
WHERE A.IDNo = B.TableAIDNo
AND A.NoOfRows <> B.NoOfRows
0
 
LVL 9

Expert Comment

by:kaminda
ID: 35512264
Just adding to my solution. It would be much more accurate to use COUNT instead of MAX I think.
0
 

Author Closing Comment

by:mpdillon
ID: 35512566
Perfect. Thank you.
pat
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

864 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