Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
declare @Company_Code int=5
declare @i int =1
--while @i<5991
--begin
--insert into test values('test')
SELECT distinct @Company_Code,
       --CustomerID,
       'bc'+ cast(row_number()over(order by customerID) as varchar)as BillCode,
       AddressLine1,
       AddressLine2,
       City_Suburb,
       StateAbbreviation,
       Zip_Postcode,
       CC.Country_Code,       
       Phone ,
       Phone as phone_ext,	   
       Email,
       '' as Fax,
       GETDATE() as create_date_time,
       'SYSTEM' as create_user_id,
       GETDATE() as last_update_date_time,
       'SYSTEM' as last_update_user_id
from MTrackDataImport..rawCustomerSISU C
	LEFT OUTER JOIN CD_country CC
	ON (CC.Company_Code = @Company_Code
	AND C.Country = CC.Country_Name)


This is my query. I am getting results but it not get sorted. So plz help me. Only sorting is not working. So how to sort please tell me.
Posted
Updated 15-Jun-14 21:29pm
v2
Comments
Murali Vijay 16-Jun-14 2:57am    
You didn't mention whether you want asc or desc in row_number
Member 10685464 16-Jun-14 2:59am    
I tried that one also but it didn't work
Murali Vijay 16-Jun-14 3:06am    
post the code you have tried
Member 10685464 16-Jun-14 3:08am    
declare @Company_Code int=5
SELECT distinct @Company_Code,
'bc'+ cast(row_number()over(order by customerID asc) as nvarchar(100))as BillCode,
isnull(AddressLine1,'') as AddressLine1,
isnull(AddressLine2,'') as AddressLine1,
isnull(City_Suburb,'') as City_Suburb,
isnull(StateAbbreviation,'') as StateAbbreviation,
Zip_Postcode,
CC.Country_Code,
Phone ,
Phone as phone_ext,
Email,
'' as Fax,
GETDATE() as create_date_time,
'SYSTEM' as create_user_id,
GETDATE() as last_update_date_time,
'SYSTEM' as last_update_user_id

from MTrackDataImport..rawCustomerSISU C

LEFT OUTER JOIN CD_country CC
ON (CC.Company_Code = @Company_Code
AND C.Country = CC.Country_Name)

this is my query
Murali Vijay 16-Jun-14 3:28am    
try this

declare @Company_Code int=5

with CTE
AS( SELECT distinct @Company_Code,
'bc'+ cast(row_number()over(order by customerID asc) as nvarchar(100))as BillCode,
isnull(AddressLine1,'') as AddressLine1,
isnull(AddressLine2,'') as AddressLine1,
isnull(City_Suburb,'') as City_Suburb,
isnull(StateAbbreviation,'') as StateAbbreviation,
Zip_Postcode,
CC.Country_Code,
Phone ,
Phone as phone_ext,
Email,
'' as Fax,
GETDATE() as create_date_time,
'SYSTEM' as create_user_id,
GETDATE() as last_update_date_time,
'SYSTEM' as last_update_user_id

from MTrackDataImport..rawCustomerSISU C

LEFT OUTER JOIN CD_country CC
ON (CC.Company_Code = @Company_Code
AND C.Country = CC.Country_Name))

select BillCode,StateAbbreviation from cte

1 solution

-- method 1 : you can do this by customerID instead of BillCode (because there is no diff in prefix)

SELECT distinct @Company_Code,
--CustomerID,
'bc'+ cast(row_number()over(order by customerID) as varchar)as BillCode,
AddressLine1,
AddressLine2,
City_Suburb,
StateAbbreviation,
Zip_Postcode,
CC.Country_Code,
Phone ,
Phone as phone_ext,
Email,
'' as Fax,
GETDATE() as create_date_time,
'SYSTEM' as create_user_id,
GETDATE() as last_update_date_time,
'SYSTEM' as last_update_user_id
from MTrackDataImport..rawCustomerSISU C
LEFT OUTER JOIN CD_country CC
ON (CC.Company_Code = @Company_Code
AND C.Country = CC.Country_Name)

order by customerID

-- method 2 : you can do this by common table expression CTE


;with CTE as (SELECT distinct @Company_Code,
--CustomerID,
'bc'+ cast(row_number()over(order by customerID) as varchar)as BillCode,
AddressLine1,
AddressLine2,
City_Suburb,
StateAbbreviation,
Zip_Postcode,
CC.Country_Code,
Phone ,
Phone as phone_ext,
Email,
'' as Fax,
GETDATE() as create_date_time,
'SYSTEM' as create_user_id,
GETDATE() as last_update_date_time,
'SYSTEM' as last_update_user_id
from MTrackDataImport..rawCustomerSISU C
LEFT OUTER JOIN CD_country CC
ON (CC.Company_Code = @Company_Code
AND C.Country = CC.Country_Name)

)
select * from cte order by BillCode
 
Share this answer
 

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