coldfusion - SQL multiple aliases alongside query -


i'm working on query pulls out items trolley user has added. below code demonstrates trying achieve, take trolley items , total quantity up, aswell multiplying cost + quantity of each item against eachother , summing too. want able call out bog standard column names query. i'm not sure how can other create 3 queries, 1 trolley itself, 1 total amount of items user , 1 total cost of user, surely can done in 1 query right?

<cfquery datasoure="#application.datasource#" name="trolley">     select *, isnull(sum(trolley_amount), 0) trolly_items, isnull(sum(trolley_cost * trolley_amount), 0) trolley_totalcost     trolley </cfquery> 

i'll give coldfusion answer. can in 1 query db , 1 or 2 query of query queries. "look like" 2 or 3 queries in code in reality 1 query (trip db) , 2 "array filtering or aggreegating" operations. in short this:

<cfquery name="mytrolley" datasource="mydsn"> select  item, quantity, cost, quantity * cost totalitemcost    trolley   userid = <cfqueryparam cfsqltype="cf_sql_integer" value="#userid#"/> 

<cfquery name="itemtotals" dbtype="query">     select sum(totalitemcost) grandtotal     fromy   mytrolley </cfquery> 

of course actual query differ, if goal reduce traffic db (a laudable goal can reap dividends sometimes) might way go. q of q pretty lean , efficient sort of thing - though of course break down when try overly complex it.


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -