| Author |
Topic |
|
Zath
Constraint Violating Yak Guru
298 Posts |
Posted - 2009-07-16 : 13:53:35
|
I know there must be a way to achieve this, so, here we are again :DNeed 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 aINNER JOIN ImportType b ON a.ImportTypeID = b.ImportTypeIDINNER JOIN Client c ON a.ClientID = c.pk_idINNER JOIN (SELECT ImportAlertID, COUNT(*) AS numCustomers FROM customer GROUP BY ImportAlertID) cust ON a.ImportAlertID = cust.ImportAlertIDINNER 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 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-07-16 : 14:03:58
|
| [code]SELECT a.ImportAlertID, a.ClientID, CASE @type WHEN 1 THEN tmp1.cnt1 WHEN 2 THEN tmp2.cnt2 END AS numNA0, a.CompleteStatus, a.CompletedBy FROM ImportAlert aINNER JOIN ImportType b ON a.ImportTypeID = b.ImportTypeIDINNER JOIN Client c ON a.ClientID = c.pk_idINNER JOIN (SELECT ImportAlertID, COUNT(*) AS numCustomers FROM customer GROUP BY ImportAlertID) cust ON a.ImportAlertID = cust.ImportAlertIDINNER JOIN (SELECT ImportAlertID, COUNT(*) AS numInvoices, SUM(invorigamt) AS sumInvoices FROM dbo.invoicenotadvanced GROUP BY ImportAlertID) inv ON a.ImportAlertID = inv.ImportAlertIDOUTER APPLY --IF @type = 1 then use this select (SELECT COUNT(*) AS cnt1 FROM invoicenotadvanced WHERE NAStatus = 0 AND a.ImportAlertID = ImportAlertID)tmp1OUTER APPLY (SELECT COUNT(*) AS cnt2 FROM customer WHERE NAStatus = 'Not Sent' AND a.ImportAlertID = ImportAlertID)tmp2[/code] |
 |
|
|
Zath
Constraint Violating Yak Guru
298 Posts |
Posted - 2009-07-16 : 14:08:25
|
| Very nice!Never used an outer apply before.Thanks!!! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-07-16 : 14:13:34
|
| welcome |
 |
|
|
Zath
Constraint Violating Yak Guru
298 Posts |
Posted - 2009-07-16 : 14:38:00
|
| Forgot this is the legacy site and am using sql server 2000.I use 2000/2005/2008 all in the same place and wasworking in the 2005 window.OUTER APPLY didn't come out till 2005.I'll repost this in the 2000 forum... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-07-16 : 14:44:19
|
did you try this?SELECT a.ImportAlertID, a.ClientID, CASE @type WHEN 1 THEN (SELECT COUNT(*) FROM invoicenotadvanced WHERE NAStatus = 0 AND a.ImportAlertID = ImportAlertID) WHEN 2 THEN (SELECT COUNT(*) FROM customer WHERE NAStatus = 'Not Sent' AND a.ImportAlertID = ImportAlertID) END AS numNA0, a.CompleteStatus, a.CompletedBy FROM ImportAlert aINNER JOIN ImportType b ON a.ImportTypeID = b.ImportTypeIDINNER JOIN Client c ON a.ClientID = c.pk_idINNER JOIN (SELECT ImportAlertID, COUNT(*) AS numCustomers FROM customer GROUP BY ImportAlertID) cust ON a.ImportAlertID = cust.ImportAlertIDINNER JOIN (SELECT ImportAlertID, COUNT(*) AS numInvoices, SUM(invorigamt) AS sumInvoices FROM dbo.invoicenotadvanced GROUP BY ImportAlertID) inv ON a.ImportAlertID = inv.ImportAlertID |
 |
|
|
Zath
Constraint Violating Yak Guru
298 Posts |
Posted - 2009-07-16 : 14:49:34
|
| That was the first thing I tried.Didn't work. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-07-16 : 14:50:54
|
| why what was the error? |
 |
|
|
Zath
Constraint Violating Yak Guru
298 Posts |
Posted - 2009-07-16 : 14:51:26
|
| I take that back.I tried it but differently.The way you did it seems to work.I'll let you know.Thanks. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-07-16 : 14:54:46
|
| welcome |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
|