2013-03-20 13 views
6

Utilizzo di SQL Server 2008, sto cercando di ottenere il ritorno di alcune di queste colonne di tornare da $ xxx, xxx.xxTSQL Gettate la somma di denaro

Questa è la query che sto usando (questo interrogare poi alcuni aggiornamenti solo per calcolare i numeri e selezionare ##tempshow alla fine)

SELECT 
    CASE GROUPING(s.StoreID) WHEN 1 THEN '' ELSE s.StoreID END [StoreID], 
    CASE GROUPING(p.VendorID) WHEN 1 THEN '' ELSE p.VendorID END [VendorID], 
    SUM(d.Quantity) AS [UnitSold], 
    CAST(SUM(d.amount * d.quantity) AS DECIMAL(18,2)) AS Amount, 
    CAST(SUM(d.Discount) AS DECIMAL(18,2)) AS Discount, 
    CAST(SUM((d.Amount * d.Quantity - d.Discount)) AS DECIMAL(18,2)) AS ExtSold, 
    CAST(SUM(d.Cost * d.Quantity) AS DECIMAL(18,2)) AS Cost, 
    CAST(0 AS DECIMAL(18,2)) AS Profit, 
    CAST(0 AS DECIMAL(18,2)) AS OnHand, 
    CAST(0 AS DECIMAL(18,2)) AS OnHandRetail, 
    CAST(0 AS DECIMAL(18,2)) AS OnHandCost, 
    CAST(0 AS DECIMAL(18,2)) AS ReceivedCost, 
    CAST(0 AS DECIMAL(18,2)) AS ReceivedRetail, 
    CAST(0 AS DECIMAL(18,2)) AS ReceivedQty, 
    CAST(0 AS DECIMAL(18,2)) AS Margin, 
    CAST(0 AS DECIMAL(12,2)) AS TurnOver, 
    CAST(0 AS INTEGER) AS OnOrder 
INTO 
    ##tempshow 
FROM 
    RPTrs s, 
    RPTrsd d, 
    RPIv i, 
    RPProducts p 
WHERE 
    s.ReceiptNO = d.ReceiptNO and 
    s.StoreID = d.StoreID and 
    i.UPC = d.UPC and 
    i.StoreID = d.StoreID and 
    p.ProductID = i.IVProduct and 
    s.StoreID = '01' and 
    s.TRSDate > GETDATE()-20 and 
    p.Service = 0 
GROUP BY 
    GROUPING SETS((s.StoreID,p.VendorID),()) 

che restituisce: enter image description here

ho cercato

CAST(SUM(d.amount * d.quantity) AS MONEY) AS Amount,

e

SUM(CAST((d.amount * d.quantity) AS MONEY)) AS Amount,

output atteso (oltre alle altre colonne stesse come questa colonna Amount):

|StoreID | VendorID | UnitSold | Amount 
--------------------------------------------- 
1 | 01  | 0000  | 0  | $0.00 
2 | 01  | am  | 62  | $6,275.00 
3 | 01  | AO  | 58  | $18,964.00 
4 | 01  | payless | 6  | $1,383.36 
5 |  |   | 126  | $26,622.36 

Ho bisogno del Amount, Discount, ExtSold, Cost, Profit, OnHandRetail, OnHandCost, ReceivedCost, ReceivedRetail essere nel formato di denaro

+2

Forse si sta confondendo _data TYPE_ e _format_. Formattazione, ad es. aggiungere un simbolo di valuta, migliaia di separatori e un punto di radix, è meglio lasciare alla vostra applicazione. – HABO

+0

Purtroppo non posso farlo perché è solo una semplice funzione di reporting all'interno del software e prende solo la query e gli output che cosa è all'interno della query – JohnZ

+0

@JohnZ: Potrebbe quella funzione essere sostituita con una leggermente più complessa allora? In realtà, anche se è possibile ottenere ciò che si desidera in T-SQL, è il livello di presentazione che dovrebbe interessare la formattazione. Il database dovrebbe preoccuparsi solo di fornire dati in questo caso. –

risposta

16

Questo è qualcosa che dovrebbe essere fatto sul livello di presentazione, ma se hai bisogno di fare questo in SQL è possibile utilizzare:

'$'+convert(varchar(50), CAST(amount as money), -1) amount 

Ecco un esempio:

;with cte (amount) 
as 
(
    select 123254578.00 union all 
    select 99966.00 union all 
    select 0.00 union all 
    select 6275.00 union all 
    select 18964.00 union all 
    select 1383.36 union all 
    select 26622.36 
) 
select '$'+convert(varchar(50), CAST(amount as money), -1) amount 
from cte 

Vedi SQL Fiddle with Demo. Ciò restituisce:

|   AMOUNT | 
------------------- 
| $123,254,578.00 | 
|  $99,966.00 | 
|   $0.00 | 
|  $6,275.00 | 
|  $18,964.00 | 
|  $1,383.36 | 
|  $26,622.36 | 

Nota: Questo sarà molto più facile in SQL Server 2012, perché è possibile utilizzare FORMAT()

;with cte (amount) 
as 
(
    select 123254578.00 union all 
    select 99966.00 union all 
    select 0.00 union all 
    select 6275.00 union all 
    select 18964.00 union all 
    select 1383.36 union all 
    select 26622.36 
) 
select '$'+FORMAT(amount,'#,0.0000') amount 
from cte 

Vedi SQL Fiddle with Demo

+0

bluefeet in soccorso ancora una volta.Grazie mille, la tua somma "$" + (varchar (50), importo CAST (importo in denaro), -1) ha funzionato come un incantesimo – JohnZ

+0

Cosa fa il -1? – bonh

+0

@bonh Aggiunge le virgole al valore numerico. – Taryn

3

aggiunta alla risposta di bluefeet, la funzione di formattazione disponibili in SQL 2012 potrebbe essere fatto in questo modo:

SELECT FORMAT(12345.6789, 'C', 'en-us') 

Th e C significa valuta, e l'ultimo argomento è cultura. La cultura è importante se vuoi che la tua applicazione sia multilingue, perché si prende cura di cose come i segni del dollaro (o dell'euro) e il separatore delle migliaia. Per esempio:

SELECT 
FORMAT(12345.6789, 'C', 'en-us') as "USA", 
FORMAT(12345.6789, 'C', 'fr-ca') as "French Canada", 
FORMAT(12345.6789, 'C', 'fr-fr') as "French France", 
FORMAT(12345.6789, 'C', 'hi-in') as "Hindi India" 

darò questo risultato:

USA   French Canada  French France  Hindi India 
----------- -------------  -------------- -------------- 
$12,345.68  12 345,68 $   12 345,68 €  ₹ 12,345.68