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.
Author |
Topic |
mxfrail
Yak Posting Veteran
84 Posts |
Posted - 2008-08-27 : 09:12:33
|
Below is the stored procedure -On the last union query I have the order by [date], [transaction] but when it orders it is sorting by XX/XX and not XX/XX/XX. So basically randomally I will see this -1/10/20081/11/20081/12/20081/13/2007etc.Can someone help me here?CREATE PROCEDURE ASP_All @ClientID int ASSELECT convert(varchar,tblInvoiceHdr.InvoiceDate,101) AS [Date], tblInvoiceHdr.ClientID, cast([TotalDue] as dec(12,2)) AS Amount, [Transaction] = case when tblInvoiceHdr.Invoicetype = 'ADJ' then 'REF#' + convert(varchar(10), tblInvoiceHdr.InvoiceNO) + ' - ' + 'Account Adjustment-' else 'REF#' + convert(varchar(10), tblInvoiceHdr.InvoiceNO) + ' - ' + 'Advertising Services Due: for Mailing: ' + [Description] end, 'INV' as Type, invoicenoFROM tblInvoiceHdrWHERE tblInvoiceHdr.Status<>'VOID' and clientid=@clientidunion all SELECT convert(varchar,tblPayments.DatePaid ,101) AS [Date],tblPayments.ClientNo AS ClientID,Abs([AmtPaid]) AS Amount,[transaction] = case when tblpayments.PaymentType='NSF' then'NSF Check - ck#: ' + isnull(tblpayments.checkno, 'Not Available') else 'Payment Refund'end,'NSF' as Type, '' as invoicenoFROM tblPaymentsWHERE Abs(tblpayments.AmtPaid)<> 0 AND tblPayments.PaymentType='NSF' Or tblPayments.PaymentType='Refund' and clientno=@clientidunion all SELECT convert(varchar,tblPayments.DatePaid ,101) AS [Date], tblPayments.ClientNo AS ClientID, Sum(-CAST([AmtPaid] AS dec(12,2))) AS Amount, [Transaction]= case when tblpayments.paymenttype = 'Credit' then 'Account Credit-' + tblpayments.DisplayDescription else'Payment Received-' + case when tblpayments.paymenttype = 'Check' then'Check # ' + tblpayments.checkno else 'CC Payment' end end, 'PMT' as Type, '' as invoicenoFROM tblPaymentsWHERE (((tblPayments.PaymentType)<>'NSF' And (tblPayments.PaymentType)<>'Refund')) and clientno=@clientidGROUP BY tblPayments.DatePaid, tblPayments.ClientNo, case when tblpayments.paymenttype = 'Credit' then 'Account Credit-' + tblpayments.DisplayDescription else'Payment Received-' + case when tblpayments.paymenttype = 'Check' then'Check # ' + tblpayments.checkno else 'CC Payment' end end, tblPayments.CheckNoHAVING (((Sum(-CAST([AmtPaid] AS dec(12,2))))<>0))union all SELECT convert(varchar,tblClients.DateEntered ,101) AS [Date], tblClients.ClientID, cast([BeginningBalance] as dec(12,2)) AS Amount, 'Beginning Balance --' AS [Transaction], 'BB' as Type,'' as invoicenoFROM tblClientsWHERE clientid=@clientidorder by [Date] , [transaction]GO |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-27 : 10:24:29
|
quote: Originally posted by mxfrail Below is the stored procedure -On the last union query I have the order by [date], [transaction] but when it orders it is sorting by XX/XX and not XX/XX/XX. So basically randomally I will see this -1/10/20081/11/20081/12/20081/13/2007etc.Can someone help me here?CREATE PROCEDURE ASP_All @ClientID int ASSELECT convert(varchar,tblInvoiceHdr.InvoiceDate,101) AS [Date], tblInvoiceHdr.ClientID, cast([TotalDue] as dec(12,2)) AS Amount, [Transaction] = case when tblInvoiceHdr.Invoicetype = 'ADJ' then 'REF#' + convert(varchar(10), tblInvoiceHdr.InvoiceNO) + ' - ' + 'Account Adjustment-' else 'REF#' + convert(varchar(10), tblInvoiceHdr.InvoiceNO) + ' - ' + 'Advertising Services Due: for Mailing: ' + [Description] end, 'INV' as Type, invoicenoFROM tblInvoiceHdrWHERE tblInvoiceHdr.Status<>'VOID' and clientid=@clientidunion all SELECT convert(varchar,tblPayments.DatePaid ,101) AS [Date],tblPayments.ClientNo AS ClientID,Abs([AmtPaid]) AS Amount,[transaction] = case when tblpayments.PaymentType='NSF' then'NSF Check - ck#: ' + isnull(tblpayments.checkno, 'Not Available') else 'Payment Refund'end,'NSF' as Type, '' as invoicenoFROM tblPaymentsWHERE Abs(tblpayments.AmtPaid)<> 0 AND tblPayments.PaymentType='NSF' Or tblPayments.PaymentType='Refund' and clientno=@clientidunion all SELECT convert(varchar,tblPayments.DatePaid ,101) AS [Date], tblPayments.ClientNo AS ClientID, Sum(-CAST([AmtPaid] AS dec(12,2))) AS Amount, [Transaction]= case when tblpayments.paymenttype = 'Credit' then 'Account Credit-' + tblpayments.DisplayDescription else'Payment Received-' + case when tblpayments.paymenttype = 'Check' then'Check # ' + tblpayments.checkno else 'CC Payment' end end, 'PMT' as Type, '' as invoicenoFROM tblPaymentsWHERE (((tblPayments.PaymentType)<>'NSF' And (tblPayments.PaymentType)<>'Refund')) and clientno=@clientidGROUP BY tblPayments.DatePaid, tblPayments.ClientNo, case when tblpayments.paymenttype = 'Credit' then 'Account Credit-' + tblpayments.DisplayDescription else'Payment Received-' + case when tblpayments.paymenttype = 'Check' then'Check # ' + tblpayments.checkno else 'CC Payment' end end, tblPayments.CheckNoHAVING (((Sum(-CAST([AmtPaid] AS dec(12,2))))<>0))union all SELECT convert(varchar,tblClients.DateEntered ,101) AS [Date], tblClients.ClientID, cast([BeginningBalance] as dec(12,2)) AS Amount, 'Beginning Balance --' AS [Transaction], 'BB' as Type,'' as invoicenoFROM tblClientsWHERE clientid=@clientidorder by [Date] tblInvoiceHdr.InvoiceDate, [transaction]GO
thats because you're converting it to varchar. use the original date field itself for ordering |
 |
|
mxfrail
Yak Posting Veteran
84 Posts |
Posted - 2008-08-27 : 10:47:36
|
I tried that before and it does not work.I get the following error -Error 104: Order By items must appear in the select list if the statement contains a union operator.I also tried sorting by each unique date field I converted. I tried order by within each select and that does not seem to be work as I get errors unless I just put one at the end. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-27 : 10:52:38
|
quote: Originally posted by mxfrail I tried that before and it does not work.I get the following error -Error 104: Order By items must appear in the select list if the statement contains a union operator.I also tried sorting by each unique date field I converted. I tried order by within each select and that does not seem to be work as I get errors unless I just put one at the end.
you could simply use field InvoiceDate itself in select list and do the formatting at your front end application. |
 |
|
mxfrail
Yak Posting Veteran
84 Posts |
Posted - 2008-08-27 : 10:57:03
|
Could I do the convert differently?I am displaying the data in ASP.net after and do not want to enable sorting. I am not sure of another way to have the data appear already sorted for me. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-27 : 10:58:24
|
otherwise use this:-CREATE PROCEDURE ASP_All @ClientID intASSELECT [Date],Amount,[Transaction],Type,invoiceno FROM(SELECT tblInvoiceHdr.InvoiceDate AS [Original],convert(varchar,tblInvoiceHdr.InvoiceDate,101) AS [Date], tblInvoiceHdr.ClientID, cast([TotalDue] as dec(12,2)) AS Amount, [Transaction] = case when tblInvoiceHdr.Invoicetype = 'ADJ' then 'REF#' + convert(varchar(10), tblInvoiceHdr.InvoiceNO) + ' - ' + 'Account Adjustment-' else'REF#' + convert(varchar(10), tblInvoiceHdr.InvoiceNO) + ' - ' + 'Advertising Services Due: for Mailing: ' + [Description] end, 'INV' as Type, invoicenoFROM tblInvoiceHdrWHERE tblInvoiceHdr.Status<>'VOID' and clientid=@clientidunion all SELECT tblPayments.DatePaid,convert(varchar,tblPayments.DatePaid ,101) AS [Date],tblPayments.ClientNo AS ClientID,Abs([AmtPaid]) AS Amount,[transaction] = case when tblpayments.PaymentType='NSF' then'NSF Check - ck#: ' + isnull(tblpayments.checkno, 'Not Available') else 'Payment Refund'end,'NSF' as Type, '' as invoicenoFROM tblPaymentsWHERE Abs(tblpayments.AmtPaid)<> 0 AND tblPayments.PaymentType='NSF' Or tblPayments.PaymentType='Refund' and clientno=@clientidunion all SELECT tblPayments.DatePaid,convert(varchar,tblPayments.DatePaid ,101) AS [Date], tblPayments.ClientNo AS ClientID, Sum(-CAST([AmtPaid] AS dec(12,2))) AS Amount, [Transaction]= case when tblpayments.paymenttype = 'Credit' then 'Account Credit-' + tblpayments.DisplayDescription else'Payment Received-' + case when tblpayments.paymenttype = 'Check' then'Check # ' + tblpayments.checkno else 'CC Payment' end end, 'PMT' as Type, '' as invoicenoFROM tblPaymentsWHERE (((tblPayments.PaymentType)<>'NSF' And (tblPayments.PaymentType)<>'Refund')) and clientno=@clientidGROUP BY tblPayments.DatePaid, tblPayments.ClientNo, case when tblpayments.paymenttype = 'Credit' then 'Account Credit-' + tblpayments.DisplayDescription else'Payment Received-' + case when tblpayments.paymenttype = 'Check' then'Check # ' + tblpayments.checkno else 'CC Payment' end end, tblPayments.CheckNoHAVING (((Sum(-CAST([AmtPaid] AS dec(12,2))))<>0))union all SELECT tblClients.DateEntered,convert(varchar,tblClients.DateEntered ,101) AS [Date], tblClients.ClientID, cast([BeginningBalance] as dec(12,2)) AS Amount, 'Beginning Balance --' AS [Transaction], 'BB' as Type,'' as invoicenoFROM tblClientsWHERE clientid=@clientid)rorder by r.[Original], r.[transaction]GO |
 |
|
mxfrail
Yak Posting Veteran
84 Posts |
Posted - 2008-08-27 : 11:04:54
|
Sweet thanks! |
 |
|
|
|
|
|
|