Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello, I have error when I try order by, I am using union

* ( i try to use order by date mm and YYYY to sort date from oldest to earlest)

What I have tried:

SELECT
[Spare6]
,[Spare5]
,[Spare4]
,[Spare3]
,[Spare2]
,[Spare1]
,[details]
,[note]
,[documents]
,[termsofpaymneten]
,[termsofpaymnet]
,[aprtmentstatusen]
,[aprtmentstatus]
,convert(varchar,amountdateen,103) as amountdateen
,convert(varchar,amountdate,103) as amountdate
,[amountleft]
,[amountpaid]
,[rent]
,[noofpayemtsen]
,[noofpayemts]
,[sourceen]
,[source]
,[iddateen]
,[iddate]
,[idnumber]
,convert(varchar, contractenen, 103) as contractenen
,convert(varchar, contracten, 103) as contracten
,convert (varchar,contractstarten ,103)as contractstarten
,convert (varchar,contractstart ,103)as contractstart
,[contractnoen]
,[contractno]
,[nationalityen]
,[nationality]
,[secondpartyen]
,[secondparty]
,[firstpartyen]
,[firstparty]
,[apartemetnnoen]
,[apartemetnno]
,[Bulidingnoen]
,[Bulidingno]
,[No] FROM [All_Vech].[dbo].[Bulding_Safa]
where CONVERT(date,amountdateen,103) < CONVERT(date ,GETDATE(),103)



UNION

SELECT
[Spare6]
,[Spare5]
,[Spare4]
,[Spare3]
,[Spare2]
,[Spare1]
,[details]
,[note]
,[documents]
,[termsofpaymneten]
,[termsofpaymnet]
,[aprtmentstatusen]
,[aprtmentstatus]
,convert(varchar,amountdateen,103) as amountdateen
,convert(varchar,amountdate,103) as amountdate
,[amountleft]
,[amountpaid]
,[rent]
,[noofpayemtsen]
,[noofpayemts]
,[sourceen]
,[source]
,[iddateen]
,[iddate]
,[idnumber]
,convert(varchar, contractenen, 103) as contractenen
,convert(varchar, contracten, 103) as contracten
,convert (varchar,contractstarten ,103)as contractstarten
,convert (varchar,contractstart ,103)as contractstart
,[contractnoen]
,[contractno]
,[nationalityen]
,[nationality]
,[secondpartyen]
,[secondparty]
,[firstpartyen]
,[firstparty]
,[apartemetnnoen]
,[apartemetnno]
,[Bulidingnoen]
,[Bulidingno]
,[No] FROM [All_Vech].[dbo].[Bulding_Sharfya]
where CONVERT(date,amountdateen,103) < CONVERT(date ,GETDATE(),103)

UNION

SELECT
[Spare6]
,[Spare5]
,[Spare4]
,[Spare3]
,[Spare2]
,[Spare1]
,[details]
,[note]
,[documents]
,[termsofpaymneten]
,[termsofpaymnet]
,[aprtmentstatusen]
,[aprtmentstatus]
,convert(varchar,amountdateen,103) as amountdateen
,convert(varchar,amountdate,103) as amountdate
,[amountleft]
,[amountpaid]
,[rent]
,[noofpayemtsen]
,[noofpayemts]
,[sourceen]
,[source]
,[iddateen]
,[iddate]
,[idnumber]
,convert(varchar, contractenen, 103) as contractenen
,convert(varchar, contracten, 103) as contracten
,convert (varchar,contractstarten ,103)as contractstarten
,convert (varchar,contractstart ,103)as contractstart
,[contractnoen]
,[contractno]
,[nationalityen]
,[nationality]
,[secondpartyen]
,[secondparty]
,[firstpartyen]
,[firstparty]
,[apartemetnnoen]
,[apartemetnno]
,[Bulidingnoen]
,[Bulidingno]
,[No] FROM [All_Vech].[dbo].[Commercial_Building]
where CONVERT(date,amountdateen,103) < CONVERT(date ,GETDATE(),103)

UNION

SELECT
[Spare6]
,[Spare5]
,[Spare4]
,[Spare3]
,[Spare2]
,[Spare1]
,[details]
,[note]
,[documents]
,[termsofpaymneten]
,[termsofpaymnet]
,[aprtmentstatusen]
,[aprtmentstatus]
,convert(varchar,amountdateen,103) as amountdateen
,convert(varchar,amountdate,103) as amountdate
,[amountleft]
,[amountpaid]
,[rent]
,[noofpayemtsen]
,[noofpayemts]
,[sourceen]
,[source]
,[iddateen]
,[iddate]
,[idnumber]
,convert(varchar, contractenen, 103) as contractenen
,convert(varchar, contracten, 103) as contracten
,convert (varchar,contractstarten ,103)as contractstarten
,convert (varchar,contractstart ,103)as contractstart
,[contractnoen]
,[contractno]
,[nationalityen]
,[nationality]
,[secondpartyen]
,[secondparty]
,[firstpartyen]
,[firstparty]
,[apartemetnnoen]
,[apartemetnno]
,[Bulidingnoen]
,[Bulidingno]
,[No] FROM [All_Vech].[dbo].[Mall_Reem]
where CONVERT(date,amountdateen,103) < CONVERT(date ,GETDATE(),103)


UNION
SELECT
[Spare6]
,[Spare5]
,[Spare4]
,[Spare3]
,[Spare2]
,[Spare1]
,[details]
,[note]
,[documents]
,[termsofpaymneten]
,[termsofpaymnet]
,[aprtmentstatusen]
,[aprtmentstatus]
,convert(varchar,amountdateen,103) as amountdateen
,convert(varchar,amountdate,103) as amountdate
,[amountleft]
,[amountpaid]
,[rent]
,[noofpayemtsen]
,[noofpayemts]
,[sourceen]
,[source]
,[iddateen]
,[iddate]
,[idnumber]
,convert(varchar, contractenen, 103) as contractenen
,convert(varchar, contracten, 103) as contracten
,convert (varchar,contractstarten ,103)as contractstarten
,convert (varchar,contractstart ,103)as contractstart
,[contractnoen]
,[contractno]
,[nationalityen]
,[nationality]
,[secondpartyen]
,[secondparty]
,[firstpartyen]
,[firstparty]
,[apartemetnnoen]
,[apartemetnno]
,[Bulidingnoen]
,[Bulidingno]
,[No] FROM [All_Vech].[dbo].[MALL_Residential ]
where CONVERT(date,amountdateen,103) < CONVERT(date ,GETDATE(),103)
UNION

SELECT
[Spare6]
,[Spare5]
,[Spare4]
,[Spare3]
,[Spare2]
,[Spare1]
,[details]
,[note]
,[documents]
,[termsofpaymneten]
,[termsofpaymnet]
,[aprtmentstatusen]
,[aprtmentstatus]
,convert(varchar,amountdateen,103) as amountdateen
,convert(varchar,amountdate,103) as amountdate
,[amountleft]
,[amountpaid]
,[rent]
,[noofpayemtsen]
,[noofpayemts]
,[sourceen]
,[source]
,[iddateen]
,[iddate]
,[idnumber]
,convert(varchar, contractenen, 103) as contractenen
,convert(varchar, contracten, 103) as contracten
,convert (varchar,contractstarten ,103)as contractstarten
,convert (varchar,contractstart ,103)as contractstart
,[contractnoen]
,[contractno]
,[nationalityen]
,[nationality]
,[secondpartyen]
,[secondparty]
,[firstpartyen]
,[firstparty]
,[apartemetnnoen]
,[apartemetnno]
,[Bulidingnoen]
,[Bulidingno]
,[No] FROM [All_Vech].[dbo].[Shop_Reem]
where CONVERT(date,amountdateen,103) < CONVERT(date ,GETDATE(),103)
ORDER BY Month(amountdateen), YEAR(amountdateen) -------------------------------------> error is here
END
Posted
Updated 14-Aug-17 2:43am
Comments
CHill60 14-Aug-17 8:31am    
The error seems quite clear ... you don't have Month(amountdateen) nor YEAR(amountdateen) in your select list.
On another point you should not be converting the dates.

1 solution

You appear to have at least 6 tables with exactly the same schema. This is usually a sure sign that your database design needs some attention.

From what I can see all of this information could be (and should be) stored in a single table. Add another column that indications the "BuildingType" - it will contain values such as "Safa", "Sharfya", "Commercial_Building" - or even better be a foreign key to a table containing details of the building types.

Secondly you should not use
where CONVERT(date,amountdateen,103) < CONVERT(date ,GETDATE(),103) 
Use DATEDIFF[^]. You should also not be converting dates before passing information back to the calling program - allow the GUI layer to interpret a DATE type instead of a string that can be ambiguous.

To get over your problem you could just include Month(amountdateen) and YEAR(amountdateen) in your SELECT lists but it would be far better to address the issues with your database design. ORDER by amountdateen DESC
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900