W.E.B
asked on
SQL 2008 UNION ALL
Hello,
Can you please help.
I need to union the headers with the rest of the statement.
Please see attached
thank you,
UNION-ALL.txt
Can you please help.
I need to union the headers with the rest of the statement.
Please see attached
thank you,
UNION-ALL.txt
ASKER
Hi Scott,
I don't get any results.
thanks,
I don't get any results.
thanks,
all the columns would have to be varchar to include those headings
------------
deducting 3 milliseconds from a datetime value is NOT a reliable method for your date range.
DECLARE @Pieces varchar(2000)
, @OrderNumber int
DECLARE @FinalizedOrdersPieces TABLE
(
InvoiceNumber int -- change all columns to varchar
... more
)
INSERT
INTO @FinalizedOrdersPieces
SELECT (
SELECT TOP 1
convert(varchar,InvoiceNumber)
FROM FinalizedOrdersInvoices
WHERE FinalizedOrdersInvoices.OrderNo = Finalizedorders.OrderNo
)
AS [InvoiceNumber]
... more
FROM finalizedorders
WHERE orderdate BETWEEN (
SELECT
DATEADD(mm, DATEDIFF(mm, 0, DATEADD(mm, -1, GETDATE())), 0)
)
AND (
SELECT
DATEADD(ms, -3, DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0))
)
AND Accountnumber IN (9, 10, 74, 554, 845, 1713, 2310, 2313, 4556, 7640, 10046, 10065, 10066, 10067, 10068, 10069, 10076, 10135, 10145, 11149)
ORDER BY
FinalizedOrders.OrderNo
SELECT
*
FROM @FinalizedOrdersPieces
UNION ALL
SELECT
'InvoiceNumber' AS [InvoiceNumber]
, 'OrderNo' AS [OrderNo]
, 'OrderDate' AS [OrderDate]
, 'AccountNumber' AS [AccountNumber]
, 'Caller' AS [Caller]
, 'Reference' AS [Reference]
, 'PickupCompanyName' AS [PickupCompanyName]
, 'PickupStreet' AS [PickupStreet]
, 'PickupUnit' AS [PickupUnit]
, 'PickupPostalCode' AS [PickupPostalCode]
, 'PickupZoneID' AS [PickupZoneID]
, 'PickupCity' AS [PickupCity]
, 'PickupProvince' AS [PickupProvince]
, 'pickupContact' AS [pickupContact]
, 'pickupPhone' AS [pickupPhone]
, 'DeliveryCompanyName' AS [DeliveryCompanyName]
, 'DeliveryStreet' AS [DeliveryStreet]
, 'DeliveryUnit' AS [DeliveryUnit]
, 'DeliveryPostalcode' AS [DeliveryPostalcode]
, 'DeliveryZoneID' AS [DeliveryZoneID]
, 'DeliveryCity' AS [DeliveryCity]
, 'DeliveryProvince' AS [DeliveryProvince]
, 'DeliveryContact' AS [DeliveryContact]
, 'DeliveryPhone' AS [DeliveryPhone]
, 'Vehicle' AS [Vehicle]
, 'PieceCount' AS [PieceCount]
, 'Weight' AS [Weight]
, 'ServiceType' AS [ServiceType]
, 'DeliveredTime' AS [DeliveredTime]
, 'POD' AS [POD]
, 'BaseAmount' AS [BaseAmount]
, 'FuelAmount' AS [FuelAmount]
, 'Subtotalamount' AS [Subtotalamount]
, 'Totalamount' AS [Totalamount]
, 'Pieces' AS [Pieces]
, 'NULL' AS [NULL]
------------
deducting 3 milliseconds from a datetime value is NOT a reliable method for your date range.
WHERE orderdate BETWEEN (
SELECT
DATEADD(mm, DATEDIFF(mm, 0, DATEADD(mm, -1, GETDATE())), 0)
)
AND (
SELECT
DATEADD(ms, -3, DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0))
)
Instead just ignore 'between', use >= with < instead:
WHERE (
orderdate >= DATEADD(mm, DATEDIFF(mm, 0, DATEADD(mm, -1, GETDATE())), 0)
AND
orderdate < DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)
)
for more on this topic please see: "Beware of Between"
ASKER
thank you Paul,
how do I get it to sort with headers on top?
the headers are coming as the last row now.
thank you,
how do I get it to sort with headers on top?
the headers are coming as the last row now.
thank you,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you very much.
No problem, thanks for the question. Cheers, Paul
Open in new window