Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Derived Tables

Author  Topic 

alr37
Starting Member

2 Posts

Posted - 2003-08-21 : 02:46:48
I'm new to this and I'm trying to get a handle on Derived Tables. I'm trying to get all this info on one row in a datagrid. The tblClaim is one to many to the tblClaimDetail. Meaning there is one row in tblClaim to two rows in tblClaimDetail. So because of the two rows in tblClaimDetail, I get two rows on my datagrid. So I gave several attempts and I got no where. This select statement was my last attempt. I would truly be grateful for any help anyone might lend. TIA

Here is the Select statement:

DECLARE @GenDMLtr BIT
SET @GenDMLtr = 0

SELECT @GenDMLtr As [Generate DM Letter]
, dm.DebitMemoNumber as DebitMemoNumber
, ISNULL(vscc.SCCVendorName, '') AS SCCVendorName
, ISNULL(CONVERT(DECIMAL(20,2), tc.OriginalFindingsAmount), 0) AS OriginalFindingsAmount
, ISNULL(CONVERT(DECIMAL(20,2), tc.AmountRecovered), 0) AS AmountRecovered
, ISNULL(dm.ClaimPriorityID, '') AS ClaimPriority
, ISNULL(tc.ClaimTypeID, '') AS ClaimType
, ISNULL(tc.RootCauseID, '') AS RootCause
, ISNULL(cd.ClientVendorNumber, '') AS ClientVendorNumber
, ISNULL(vci.Address1, '') AS Address1
, ISNULL(vci.Address2, '') AS Address2
, ISNULL(vci.Address3, '') AS Address3
, ISNULL(vci.City, '') AS City
, ISNULL(vci.State, '') AS State
, ISNULL(vci.Zip, '') AS Zip
, ISNULL(vci.Country, '') AS Country
, ISNULL(cd.CompanyCode, '') AS CompanyCode
, ISNULL(tc.ClaimYear, '') AS ClaimYear
, cd.ActiveVendor AS ActiveVendor
, ISNULL(CONVERT(VARCHAR(12), dm.DebitMemoDate, 101), '') AS DebitMemoDate
, ISNULL(tc.ReportReference, '') AS ReportReference
, ISNULL(CONVERT(VARCHAR(12), tc.DateGivenToClient, 101), '') AS DateGivenToClient
, ISNULL(CONVERT(VARCHAR(12), tc.DateApprovedByClient, 101), '') AS DateApprovedByClient
, ISNULL(CONVERT(DECIMAL(20,2), tc.AmountApprovedByClient), 0) AS AmountApprovedByClient
, ISNULL(tc.RecoveryMethod, '') AS RecoveryMethod
, ISNULL(CONVERT(VARCHAR(12), tc.Date1DMMailed, 101), '') AS Date1DMMailed
, ISNULL(CONVERT(VARCHAR(12), tc.Date2DMMailed, 101), '') AS Date2DMMailed
, ISNULL(CONVERT(VARCHAR(12), tc.Date3DMMailed, 101), '') AS Date3DMMailed
, ISNULL(CONVERT(VARCHAR(12), tc.DateEnteredIntoClientsSystem, 101), '') AS DateEnteredIntoClientsSystem
, ISNULL(CONVERT(DECIMAL(20,2), tc.AmountRecoveredOnCheckRequest), 0) AS AmountRecoveredOnCheckRequest
, ISNULL(cd.InvoiceNumber, '') AS InvoiceNumber
, ISNULL(CONVERT(VARCHAR(12), cd.InvoiceDate, 101), '') AS InvoiceDate
, ISNULL(cd.CheckNumber, '') AS CheckNumber
, ISNULL(CONVERT(VARCHAR(12), cd.CheckDate, 101), '') AS CheckDate
, ISNULL(CONVERT(DECIMAL(20,2), cd.CheckAmt), 0) AS CheckAmt
, ISNULL(CONVERT(DECIMAL(20,2), tc.AmountRecoveredOnCheckRuns), 0) AS AmountRecoveredOnCheckRuns
, ISNULL(CONVERT(DECIMAL(20,2), tc.CurrentBalanceAmount), 0) AS CurrentBalanceAmount
, ISNULL(CONVERT(DECIMAL(20,2), tc.VoidedFindingAmount), 0) AS VoidedFindingAmount
, ISNULL(CONVERT(VARCHAR(12), tc.DateDebitMemoVoided, 101), '') AS DateDebitMemoVoided
, ISNULL(tc.VoidID, '') AS VoidCause
, ISNULL(tc.PrimaryClaimStatusID, '') AS ClaimStatus
, ISNULL(CONVERT(DECIMAL(20,2), tc.PrimaryStatusAmount), 0) AS PrimaryStatusAmount
, ISNULL(CONVERT(VARCHAR(12), tc.DateInvoiced, 101), '') AS DateInvoiced
, ISNULL(CONVERT(DECIMAL(20,2), tc.AmountInvoiced), 0) AS AmountInvoiced
, ISNULL(tc.SCCInvoiceNo, '') AS SCCInvoiceNo
, ISNULL(cd.Notes, '') AS Notes
, dm.CSGReviewFlag AS CSGReviewFlag
, ISNULL(dm.DocumentationReviewedBy, '') AS DocumentationReviewedBy
, ISNULL(dm.ClientVendorInfoID, 0) AS ClientVendorInfoID
, ISNULL(tc.ClaimID, 0) AS ClaimID

FROM dbo.tblDebitMemo dm INNER JOIN
dbo.tblVendorClientInfo vci ON dm.ClientVendorInfoID = vci.ClientVendorInfoID INNER JOIN
dbo.tblVendorSCC vscc ON dm.SCCVendorID = vscc.SCCVendorID INNER JOIN
dbo.tblClaim tc ON dm.DebitMemoNumber = tc.DebitMemoNumber INNER JOIN
(Select ClientVendorNumber, CompanyCode, ActiveVendor, InvoiceNumber,
InvoiceDate, CheckNumber, CheckDate, CheckAmt, Notes, ClaimID from dbo.tblClaimDetail) cd ON tc.ClaimID = cd.ClaimID
WHERE (dm.ClientNumber = @strClientNo)

---@strClientNo is a parm being passed in the stored procedure this select statement belongs to.



Thank You,
alr

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2003-08-21 : 08:00:28
Why not return multiple (normalized) rowsets? If you have one-to-many cardinality, a join will return duplicate date correlating to each row of your 'many' table. That's the way it works.

Jay White
{0}
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-08-21 : 11:08:23
Why all the ISNULL's? does your data really allow for NULLS in all of those columns? that is probably not a good design.

Also, why are you converting all of your dates to VARCHAR's ? For formatting purposes? Do that at the presentation layer, not within a query. Same with the conversions to decimals -- do that at the presentation layer as well. DO not use SQL for formatting output results; that is not its purpose.

For that matter, your main question has to do with formatting as well. SQL returns "square" result sets, meaning (as Jay mentions) that data is repeated for each row potentially. Do not focus on how the data LOOKS when you return it in Query Analyzer -- focus on returning the proper data. In your presnetation layer (an ASP page, a Crystal Report, Access, etc.) is where you can do things like formatting the columns and "hiding" repeating data by the use of group headers and footers and other techiniques.



- Jeff
Go to Top of Page

alr37
Starting Member

2 Posts

Posted - 2003-08-21 : 16:06:58
Well thank you both (Jay & Jeff) for the well given criticism. Both your points are correct as far as formatting. The reason for doing all the formatting in the SQL statement is because I'm using test data and it was just easier at this point to do it there. But I will be handling the formatting on the presentation side.

I also know that a one-to-many cardinality will give me a result set of duplicate data because of the 'many' table. The data that I am returning is correct. Unfortunately for me, my employer wants to see the data all in one row. I thought that using derived tables for this was the solution. I'm creating a .Net windows application and am using component one’s TrueDBGrid. Component one’s Hierarchical dataview for their datagrid has serious problems so for right now I'm fighting the battle of getting everything on one line until Component One gets a patch out for their product.

So, is a Derived table the answer? Is it possible to do what I need to do? If so, can you please show me how?
Like I stated earlier I'm new to this and I'm trying to learn as I go. So if you kind people can lend a hand I'd be truly grateful. TIA



Thank You,
alr
Go to Top of Page

mr_mist
Grunnio

1870 Posts

Posted - 2003-08-22 : 03:26:07
Hmm. If you can provide DDL and some sample data it will make answering your question easier.

If you must return everything in the same row then it would be something like this..



create table #moo (personname varchar(10))
create table #mooness (personname varchar(10), tran_date smalldatetime, transaction_amount int null)

insert into #moo values ('mr moo') -- One person
insert into #mooness values ('mr moo',getdate(),10)
insert into #mooness values ('mr moo',dateadd (dd, 1, getdate() ),20)

SELECT
#moo.personname, firstone.tran_date as 'first tran date', firstone.value, secondone.tran_date as 'second tran date', secondone.value
FROM
#moo
INNER JOIN
(
SELECT
#mooness.personname, tran_date, transaction_amount as value
FROM
#mooness
INNER JOIN
(
SELECT
personname, min (tran_date) as firstdate -- Get the first transaction only
FROM
#mooness
group by personname
) lowest
on
lowest.firstdate = #mooness.tran_date
) firstone
on #moo.personname = firstone.personname

INNER JOIN
(
SELECT
#mooness.personname, tran_date, transaction_amount as value
FROM
#mooness
INNER JOIN
(
SELECT
personname, max (tran_date) as lastdate
FROM
#mooness
group by personname
) highest
on
highest.lastdate = #mooness.tran_date
) secondone
on #moo.personname = secondone.personname


drop table #mooness
drop table #moo



But it is a heavily flawed concept because it will fall over if the two transactions were at the same time,or there were more than two transactions per person. You would be constricting your own design by trying to push the data out in this way, I believe.

-------
Moo. :)
Go to Top of Page

TSQLMan
Posting Yak Master

160 Posts

Posted - 2003-08-22 : 11:38:31
Cube the data is SQL Analysis Services before you call it from .Net, then it will already be on one line.(If you have SQL 2000)

On our SQL 7 database I sometime use an htm form with an Excel Pivot table. If you have the office xp references/Components this is an easy one.

quote:
Originally posted by alr37

Well thank you both (Jay & Jeff) for the well given criticism. Both your points are correct as far as formatting. The reason for doing all the formatting in the SQL statement is because I'm using test data and it was just easier at this point to do it there. But I will be handling the formatting on the presentation side.

I also know that a one-to-many cardinality will give me a result set of duplicate data because of the 'many' table. The data that I am returning is correct. Unfortunately for me, my employer wants to see the data all in one row. I thought that using derived tables for this was the solution. I'm creating a .Net windows application and am using component one’s TrueDBGrid. Component one’s Hierarchical dataview for their datagrid has serious problems so for right now I'm fighting the battle of getting everything on one line until Component One gets a patch out for their product.

So, is a Derived table the answer? Is it possible to do what I need to do? If so, can you please show me how?
Like I stated earlier I'm new to this and I'm trying to learn as I go. So if you kind people can lend a hand I'd be truly grateful. TIA



Thank You,
alr

Go to Top of Page
   

- Advertisement -