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
 SQL Server Development (2000)
 Select when parm is 1

Author  Topic 

Zath
Constraint Violating Yak Guru

298 Posts

Posted - 2009-07-16 : 14:39:00
I know there must be a way to achieve this, so, here we are again :D

Need to change the select according to what the param is...


SELECT a.ImportAlertID, a.ClientID,

--IF @type = 1 then use this select
(SELECT COUNT(*)
FROM invoicenotadvanced WHERE NAStatus = 0
AND a.ImportAlertID = ImportAlertID) AS numNA0,

--IF @type = 2 then use this select
(SELECT COUNT(*)
FROM customer WHERE NAStatus = 'Not Sent'
AND a.ImportAlertID = ImportAlertID) AS numNA0,

a.CompleteStatus, a.CompletedBy

FROM ImportAlert a


INNER JOIN ImportType b ON a.ImportTypeID = b.ImportTypeID
INNER JOIN Client c ON a.ClientID = c.pk_id

INNER JOIN (SELECT ImportAlertID, COUNT(*) AS numCustomers
FROM customer GROUP BY ImportAlertID) cust ON a.ImportAlertID = cust.ImportAlertID

INNER JOIN (SELECT ImportAlertID, COUNT(*) AS numInvoices, SUM(invorigamt) AS sumInvoices
FROM dbo.invoicenotadvanced
GROUP BY ImportAlertID) inv
ON a.ImportAlertID = inv.ImportAlertID





How can this be done?
A case statement won't work here.

Thanks,

Zath

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-07-16 : 15:09:01
quote:
Originally posted by Zath

How can this be done?
A case statement won't work here.
Yes.
SELECT		a.ImportAlertID,
a.ClientID,
CASE @type
WHEN 1 THEN inv.numNA0
WHEN 2 THEN cust.numNA0
END AS numNA0,
a.CompleteStatus,
a.CompletedBy
FROM ImportAlert AS a
INNER JOIN ImportType AS b ON a.ImportTypeID = b.ImportTypeID
INNER JOIN Client AS c ON a.ClientID = c.pk_id
INNER JOIN (
SELECT ImportAlertID,
COUNT(*) AS numCustomers,
SUM(CASE WHEN NAStatus = 0 THEN 1 ELSE 0 END) AS numNA0
FROM customer
GROUP BY ImportAlertID
) as cust ON a.ImportAlertID = cust.ImportAlertID
INNER JOIN (
SELECT ImportAlertID,
COUNT(*) AS numInvoices,
SUM(invorigamt) AS sumInvoices,
SUM(CASE WHEN NAStatus = 'Not Sent' THEN 1 ELSE 0 END) AS numNA0
FROM dbo.invoicenotadvanced
GROUP BY ImportAlertID
) as inv ON a.ImportAlertID = inv.ImportAlertID


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
   

- Advertisement -