Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Very SIMPLE query working in Access not working on Sql Server Expand / Collapse
Author
Message
Posted Friday, January 17, 2014 12:05 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:37 PM
Points: 11,965, Visits: 10,992
dquirion78 (1/17/2014)
Yean but I want to avoid to repeat PrdCmd.Prix * PrdCmd.Qte so many times. This expression is simple but if it become pretty long and I need to change base(prix*qte) expression later I can forget to change a column.

example :
(PrdCmd.Prix * PrdCmd.Qte *100) * 1000 AS Column2
,(PrdCmd.Prix * PrdCmd.Qte *100) * 3 / 4 AS Column3
,(PrdCmd.Prix * PrdCmd.Qte) * 0.01 AS Column4 ' I forgot to change this one but I the query became so complex that I didn't see this one.

But If I've a common expression I can't forget it.



If you need to reuse this calculation repeatedly maybe you should look into computed columns. http://technet.microsoft.com/en-us/library/ms191250.aspx

Also, I would not use select * anywhere in production code.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1532217
Posted Friday, January 17, 2014 2:18 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, February 17, 2014 3:17 PM
Points: 22, Visits: 47
wow thanks I just tried some test on computed columns... Sql server is so strong :)

thanks again
Post #1532281
Posted Friday, January 17, 2014 3:17 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 10:59 AM
Points: 35,951, Visits: 30,239
dquirion78 (1/17/2014)
wow thanks I just tried some test on computed columns... Sql server is so strong :)

thanks again


If you can't add PERSISTED Computed Columns to your table for some reason, one reasonable way to condense code is to use CROSS APPLY like the following...

 SELECT  PrdCmd.NoCommande         AS NoCmd
,PrdCmd.DATE
,PrdCmd.Qte AS q
,PrdCmd.Prix AS p
,ca.PrixQteProduct AS Column1
,ca.PrixQteProduct * 1000 AS Column2
,ca.PrixQteProduct * 3 / 4 AS Column3
,ca.PrixQteProduct * 0.01 AS Column4
FROM dbo.ProduitCommande AS PrdCmd
CROSS APPLY (SELECT PrdCmd.Prix * PrdCmd.Qte) ca (PrixQteProduct)
;




--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1532308
Posted Friday, January 17, 2014 3:24 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, February 17, 2014 3:17 PM
Points: 22, Visits: 47
thanks exactly what I wanted but now I've 2 good solutions, computed and cross apply solutions

you are the best(all) !
Post #1532311
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse