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 «««12345

problem in select command Expand / Collapse
Author
Message
Posted Monday, March 18, 2013 9:02 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 6:19 PM
Points: 21,624, Visits: 27,464
Sergiy (3/18/2013)
mister.magoo (3/18/2013)
I realise this is a bit off-topic for the original question,

I believe the whole thread by now is off-topic for the original question.

but just in case anyone who is unsure about the use of CTEs reads this thread, I feel it important to point out a situation where CTE is both more compact and more readable.

Only if you're not aware of this method:

 SELECT TOP 10000 N
FROM dbo.Tally AS T
ORDER BY N



Unless you work for an employer or with a system where you have been told you can't use a fixed tally table. We have seen numerous posts by OPs that have been hamstrung by such requirements, so knowing how to build a dynamic tally table is a worthwhile skill to have in ones back pocket.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1432448
Posted Monday, March 18, 2013 9:04 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 2:33 PM
Points: 8,620, Visits: 8,261
Sergiy (3/18/2013)

Declare styles (objects in OO languages), fill them up and then refer in the following code.
That's how you code CSS, that's how CTE code is formed.

Why it's not in line with SQL best practices?
Because SQL operates with databases.
Where all the objects are already declared (with various CREATE sratements) and populated (with INSERT/UPDATE/DELETE) statements.

Even the keyword used for CTE.
Type "WITH" in BOL and see what articles it will show to you.
The CTE use of "WITH" is totally foreign to SQL, it was mindlessly brought across from other languages, with the single purpose - give front end developers a construction they are familiar with to make them more comfortable while cutting cr..py SQL code.


What are you talking about here? Comparing styles and objects as being the same makes no sense. This is like saying that the color on a report in SSRS and a sql table are the same thing. I fail to understand why you think that CTEs were created so that front end developers can suddenly start writing sql. You seem to have an issue with CTEs because of the syntax surrounding them. I don't understand your logic or your dislike of the construct.


_______________________________________________________________

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
Post #1432449
Posted Monday, March 18, 2013 9:50 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, April 15, 2013 6:06 AM
Points: 246, Visits: 44
This can be achived by CTE.
1. First, declare CTE which will run same query with top 2 in decending order.
2. Select data from CTE with ID column in ascending order, from lowest value to highest value (i.e. 32, 33)
-------------------------------------------------------------------------------------------------------
;WITH MyData AS (
SELECT TOP 2 *
FROM DBO.NFFeeds
order by ID desc
)
SELECT * FROM MyData
ORDER BY ID
-------------------------------------------------------------------------------------------------------
Post #1432460
Posted Tuesday, March 19, 2013 4:03 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 11:00 AM
Points: 2,543, Visits: 4,384
Sergiy (3/18/2013)
Eugene Elutin (3/18/2013)
I did. Checked. He did not state that CTE are easier to write. For a mater of fact, he said that writing queries using CTE is easier than using derived tables.


OK. It's definitely enough for you for today.
Go to sleep.


Thank you very much! I did sleep well, indeed.
So, you think that there is no much difference in the above sentences?
May be! But not for myself. English is quite precise language, so a small variations sometimes make a difference.
I cannot say that CTE are easier to write than derived tables, as they are actually almost identical.
However, if you take the query as a whole thing, using CTE makes it easier to write as it makes it more readable.
You may not agree on the above, but you should know:
opposite is stupid



_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1432544
Posted Tuesday, March 19, 2013 6:32 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 3:04 PM
Points: 4,551, Visits: 8,202
Oh, I see.

"To write" is "to read.
Aha.
And "black" is "white'.
And "lie" is "truth".
Of course.

I'm recalling I read something like that before...
Or - did I write it?
Oh, I cannot be so sure anymore...
Post #1432960
Posted Tuesday, March 19, 2013 6:57 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 8:59 AM
Points: 342, Visits: 1,074
First one who writes after this post will be raped by a mad goat!


_____________________________________________________
XDetails Addin - for SQL Developers and DBA
blog.sqlxdetails.com - Transaction log myths - debunked!
Post #1432966
Posted Tuesday, March 19, 2013 10:01 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, April 15, 2013 6:06 AM
Points: 246, Visits: 44
You may use below query to achieve the same result (if the query is running on SQL Server 2000 or order version)
---------------------------------
SELECT * FROM
(SELECT TOP 2 *
FROM DBO.NFFeeds
ORDER BY ID DESC) x
ORDER BY ID ASC
---------------------------------
Post #1432980
Posted Wednesday, March 20, 2013 1:38 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 8:59 AM
Points: 342, Visits: 1,074
Moderator, please close this thread as nonconstructive.
I tried with mad goat method, but it turns out it did not work


_____________________________________________________
XDetails Addin - for SQL Developers and DBA
blog.sqlxdetails.com - Transaction log myths - debunked!
Post #1433028
Posted Wednesday, March 20, 2013 3:50 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 11:00 AM
Points: 2,543, Visits: 4,384
Vedran Kesegic (3/20/2013)
Moderator, please close this thread as nonconstructive.
I tried with mad goat method, but it turns out it did not work


More than that! It not only didn't work, it also managed to offend innocent fellow


_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1433098
Posted Wednesday, March 20, 2013 4:13 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 8:59 AM
Points: 342, Visits: 1,074
More than that! It not only didn't work, it also managed to offend innocent fellow

Did you noticed a smiley next to the message? But I don't know who would take a mad goat as something serious, even without a smiley
Non-constructive thread with too many messages, not very useful IMHO.


_____________________________________________________
XDetails Addin - for SQL Developers and DBA
blog.sqlxdetails.com - Transaction log myths - debunked!
Post #1433112
« Prev Topic | Next Topic »

Add to briefcase «««12345

Permissions Expand / Collapse