 |

|
You are right.
The second one worked for me.
Thank you!
|
|
|
|

|
If this is still confusing, you can create a view with the expression and then do your join on the view.
|
|
|
|

|
I am sure I am overlooking something very simple but I can't see it.
I have a database of locations with latitudes and longitudes. I need to be able to query for locations that fall within a given radius from a lat/lon point
The code I am intending to use is: (Showing fixed centre 38,-118 in this version)
SELECT quakeid, (6371 * acos(cos(radians(38)) * cos(radians(latitude)) * cos(radians(longitude) - radians(-118)) + sin(radians(38)) * sin(radians(latitude)))) AS [dkm]
FROM tblUSGSData
(Derived from Creating a store locator[^])
This works fine and returns just over half a million entries.
My problem is that as soon as I add
HAVING [dkm] <= 50
or
WHERE [dkm] <= 50
I get the message
Msg 207, Level 16, State 1, Line 3
Invalid column name 'dkm'.
I obviously have not got my SQL glasses on today as I can't seem to resolve this. Can any one make (polite) suggestions?
|
|
|
|

|
Well, [dkm] is an Alias, not a columnname.
You would need to have the same expression in the having or where clause as you have in the select clause.
"The ones who care enough to do it right care too much to compromise."
Matthew Faithfull
|
|
|
|

|
Ah yes. My bad
SELECT quakeid, latitude, longitude, (6371 * acos(cos(radians(37)) * cos(radians(latitude)) * cos(radians(longitude) - radians(-122)) + sin(radians(37)) * sin(radians(latitude))))
FROM tblUSGSData
GROUP BY quakeid, latitude, longitude
HAVING (6371 * acos(cos(radians(37)) * cos(radians(latitude)) * cos(radians(longitude) - radians(-122)) + sin(radians(37)) * sin(radians(latitude)))) <= 50
works - at least does not produce an error. Not sure about the working bit but that is another story.
Many thanks.
|
|
|
|

|
Are you sure you need to use Haversines formula?
If you Radiuses are small enough the Cartesian distance formula would do, depending on what coordinate system you're using of course.
Ignore that question, I just remembered what USGS is, and realized it's a different sort of Quake you're working with.
"The ones who care enough to do it right care too much to compromise."
Matthew Faithfull
modified 13 Feb '13 - 6:55.
|
|
|
|

|
By the way, 50 km is about half a degree. Depending on the amount of rows in your table, it might be wise to filter for longitude between -122.5 and -121.5 and latitude between 36.5 and 37.5 first.
Also a simple transformation with 110 km/degree for latitude, and 110 * cos(latitude) for longitude, and then using simple pythagoras might speed up the query.
|
|
|
|

|
Thanks for your input. Yes I am adding some filtering now. I just needed to get the thing working first.
|
|
|
|

|
To add to Jorgen's answer, I've always wondered why SQL Server doesn't allow us to use alias in WHERE and HAVING clauses. The answer to that lies in the logical order in which the query is processed. The WHERE and HAVING clauses are processed before the SELECT clause and the alias do not exist at that stage.
However, technically it should be possible to introduce another stage earlier in the query processing pipeline where a mapping between expressions and their alias is made and WHERE and HAVING clauses can look up to these mappings and substitute the actual expression in place of the alias.
|
|
|
|

|
You are looking for CTE.
"The ones who care enough to do it right care too much to compromise."
Matthew Faithfull
|
|
|
|

|
Yes, or a simple
SELECT ... FROM
(
SELECT ... FROM table WHERE ...
) T
...
|
|
|
|

|
Yes, thats equivalent. I just prefer the readability of the CTE (which is merely an opinion) plus that you can refer to a CTE in more than one place.
"The ones who care enough to do it right care too much to compromise."
Matthew Faithfull
|
|
|
|

|
Ado.net is the Library in .net technology. this library help to you for connect your application to the database.
- Now Come in depth
C# is a language. and responsible to create the User Interface like Console, Web, and Window Application.
SQL is also language for create the database and database store the data.
Suppose i need to connect my Front-End to the Back-End.
or We say that C# Communicate to the SQL. now its not possible to two different language communicate to each other. Simple Example is
One Person is Russian and second is Indian now they want to talk to each other. Big Problem Occur when they want to communicate because Russian not known the Hindi language and similar Indian not know the Russian Language. now both person need to mediator or Converter. Converter convert the language means for Indian to Hindi and Russian to Russian Language.
-----Ado.Net Mediator between the Front-End to the Back-End-----
Ado.Net is provider to make the connection between the Front-End to the Back-End.
Thank you
Student of Dr. Sandeep Karan CAC Noida
Himanshu Sharma
|
|
|
|

|
And what is your question?
|
|
|
|

|
That's not a question, it's a Tip (or atleast he thinks so)
|
|
|
|

|
This is really a great tip for the beginners.
i thank you from all the Beginners those who need a Elaborated definition to understand the concepts..
|
|
|
|
|

|
What is the difference between truncating and deleting a table?
After the truncating or deleting do we need to re-index or shrink the table?
We are looking to do some clean-up on some rather large tables on our database. Once we do the clean-up what steps do we need to preform for overall database "health"?
Thanks!
|
|
|
|

|
mrfalk wrote: re-index
If there's no records left, there's little to reindex.
mrfalk wrote: What is the difference between truncating and deleting a table?
Google "MSDN Truncate", and from the manual we learn;
Removes all rows from a table without logging the individual row deletions. TRUNCATE TABLE is similar to the DELETE statement with no WHERE clause; however, TRUNCATE TABLE is faster and uses fewer system and transaction log resources.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|

|
So if we are using the Delete statement with the WHERE clause is it necessary to reindex? Is there a "standard" for when to reindex files?
After further research we will be using the delete clause because we want to retain a given number of days data. Within that given number of days data we will also be deleting specific rows based on a given parameter. We just want to make sure we are covering all bases.
|
|
|
|

|
mrfalk wrote: So if we are using the Delete statement with the WHERE clause is it necessary to reindex?
Yes.
mrfalk wrote: Is there a "standard" for when to reindex files?
No.
Start here[^].
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|

|
Truncate statement:
1. Deletion is very faster. Almost no (very minimal) transaction log is produced during this operation.
2. Data pages which are used by the table are de allocated for further use by SQL Server in other operations.
3. Identity column value is reset from its original default position. For example if a table has an identity column and identity value start form 1 then after running truncate command the value start incrementing back from 1.
4. As wiping out rows are very faster, so the number of locks are low. Although during TRUNCATE operation table and page lock happens but not each row.
5. TRUNCATE TABLE command does not support where clause, it also does not works if foreign key exists in the table. In addition, table participates in log shipping or replication also does not honor TRUNCATE table command.
6. Records removed by the TRUNCATE COMMAND cannot be restored even though the database recovery model is set to FULL.
7. TRUNCATE statement also does not fire triggers.
DELETE Statement:
1. The DELETE statement removes rows one at a time. For each deleted row the operation writes an entry in the transaction log.
2. DELETE operation is more resource intensive thus consumes more database resources and locks.
3. Where clause can be included with the statement to restrict the number of affected rows.
4. Internally the DELETE operation does not cleanup rows immediately if the table has any index on it, The operation marks the affected rows "to be deleted". The marked records are known as GHOST RECORDS. Although these records are de allocated quickly by a background cleanup process for better performance.
dsdf
|
|
|
|

|
Truncating will also reset the seed for any identity column, whereas delete will not.
-NP
Never underestimate the creativity of the end-user.
|
|
|
|

|
We have found that for integrated security through VB.NET the DELETE works with less permissions. The error you get back when using TRUNCATE gives no indication as to truncate being the problem.
|
|
|
|

|
Hi,
-- DELETE, TRUNCATE and DROP Statements
DELETE
/*
The DELETE command is used to remove rows from a table.
A WHERE clause can be used to only remove some rows.
If no WHERE condition is specified, all rows will be removed.
After performing a DELETE operation you need to COMMIT or ROLLBACK the transaction to make the change permanent or to undo it.
Note that this operation will cause all DELETE triggers on the table to fire.
*/
TRUNCATE
/*
TRUNCATE removes all rows from a table.
The operation cannot be rolled back and no triggers will be fired.
As such, TRUCATE is faster and doesn't use as much undo space as a DELETE.
*/
DROP
/*
The DROP command removes a table from the database.
All the tables' rows, indexes and privileges will also be removed.
No DML triggers will be fired. The operation cannot be rolled back.
*/
--Difference between TRUNCATE and DELETE commands
/*
1) TRUNCATE is a DDL command whereas DELETE is a DML command.
2) TRUNCATE is much faster than DELETE.
Reason:When you type DELETE.all the data get copied into the Rollback Tablespace first.then delete operation get performed.Thatswhy when you type ROLLBACK after deleting a table ,you can get back the data(The system get it for you from the Rollback Tablespace).All this process take time.But when you type TRUNCATE,it removes data directly without copying it into the Rollback Tablespace.
Thatswhy TRUNCATE is faster.Once you Truncate you cann't get back the data.
3) You cann't rollback in TRUNCATE but in DELETE you can rollback.
TRUNCATE removes the record permanently.
4) In case of TRUNCATE ,Trigger doesn't get fired.
But in DML commands like DELETE .Trigger get fired.
5) You cann't use conditions(WHERE clause) in TRUNCATE.
But in DELETE you can write conditions using WHERE clause
*/
Regards,
GVPrabu
|
|
|
|

|
Hi Guys,
I want to know the table creation for the problem described below.
I have a 26 parameters for a customer feed back,
and i have a set of sites , some sites don't have some parameters in my 26 points, and some site want more then 26 points,
so i thought to create table like
id(int),parametename(Nvarchar(max)),site1(bit),site2(bit),site3(bit),site4(bit)
and i will allow in UI to check which point is applicable for which site,
this solution is for time being,
my concern is id the site name may add in future that time we have to change the parameter table and modify the insert and update methods as well as Store procedure,
How can avoid the re-coding by creating customized table.
Please suggest me the solution.
Thanks and regards
Vishwa
|
|
|
|

|
You should have a table that stores the list of sites identified by a SiteID column. And then you can store the values in your table with these columns
id(int),parametename(Nvarchar(max)),SiteID(int),value(bit)
Whenever there is a new site is to be added, just add it to the site table and insert a new row with that id in this table.
|
|
|
|

|
Table: site
- id
- Site_name
- Site_url
Table: site_parameter
- id
- Site_id (foreign key to above table)
- name
- value
For "26 parameters" you would have 26 rows in the site_parameter table.
|
|
|
|

|
Hi
I have an employee table with column gender F and M. Now i want to update the gender values of all the employee as F with M and M with F. But this should happen in a single query. How can i do this.
Thanks for your help.
Regards
Naina
Naina
|
|
|
|

|
You cannot do it in a single query, you need 3 updates, move F to X, move M to F, move X to M you can wrap the 3 queries in a transaction to insure integrity is maintained.
Never underestimate the power of human stupidity
RAH
|
|
|
|

|
Mycroft Holmes wrote: You cannot do it
You can actually do it. SQL is a set based language, changes made in a query are either fully committed or fully rolled back, there is no partial update. So, Eddy's solution will actually work.
|
|
|
|

|
Eddie got my 5, I didn't think of using case statement in the update clause.
Never underestimate the power of human stupidity
RAH
|
|
|
|

|
BEGIN TRANSACTION
CREATE TABLE Person
(
Name VARCHAR(MAX)
,Gender CHAR(1)
)
INSERT INTO Person VALUES
('Pete', 'M')
,('John', 'M')
,('Mary', 'F')
,('Dude', 'M')
,('Mary', 'F')
SELECT *
FROM Person
UPDATE Person
SET Gender = CASE Gender WHEN 'M' THEN 'F'
ELSE 'M'
END
SELECT *
FROM Person
ROLLBACK
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|

|
CREATE TABLE EmpDtls(Code VARCHAR(10), Name VARCHAR(100), DeptCode TINYINT)
CREATE TABLE DeptDtls(DeptCode TINYINT, DeptName VARCHAR(50), EmpCount INT)
INSERT INTO EmpDtls(Code, Name, DeptCode)
VALUES('Emp1','Venkat', 1), ('Emp2','Prabu', 2), ('Emp3','Kumar', 1), ('Emp4','Karthick', 3),
('Emp5','Amith', 2),('Emp6','HariKrishna', 2)
INSERT INTO DeptDtls(DeptCode, DeptName)
VALUES(1, 'IT'),(2, 'Sales'), (3,'HR'), (4, 'Accounts')
SELECT Code, Name, DeptCode FROM EmpDtls
SELECT DeptCode, DeptName,EmpCount FROM DeptDtls
UPDATE D SET D.EmpCount= E.EmpCount
FROM DeptDtls D
INNER JOIN (SELECT DeptCode, COUNT(Code) 'EmpCount' FROM EmpDtls GROUP BY DeptCode) E ON E.DeptCode=D.DeptCode
SELECT DeptCode, DeptName,EmpCount FROM DeptDtls
IF OBJECT_ID('EmpDtls') IS NOT NULL DROP TABLE EmpDtls
IF OBJECT_ID('DeptDtls') IS NOT NULL DROP TABLE DeptDtls
|
|
|
|

|
Hi Naina,
Check the Script, U can use CASE Statement in UPDATE.
CREATE TABLE #EmpDtls(ID INT, EmpGender CHAR(1))
INSERT INTO #EmpDtls (ID, EmpGender) VALUES (1,'M'),(2,'F'),(3,'M')
SELECT ID, EmpGender FROM #EmpDtls
UPDATE #EmpDtls SET EmpGender = (CASE WHEN EmpGender='M' THEN 'F' WHEN EmpGender='F' THEN 'M' END)
SELECT ID, EmpGender FROM #EmpDtls
Regards,
GVPrabu
|
|
|
|

|
I am adding my code below
create procedure dist_profit_pairs1(IN user_id
varchar(20),IN plan varchar(20))
begin
DECLARE left_bvs,right_bvs Float DEFAULT 0;
SELECT sum(businessvalues)as left_bvs from bv_history where userid=user_id and type='Distribution Profit' and position='Left';
SELECT sum(businessvalues)as right_bvs from bv_history where userid=user_id and type='Distribution Profit' and position='Right';
IF left_bvs < right_bvs THEN
SELECT left_bvs;
ELSE
SELECT right_bvs;
|
|
|
|

|
Hi
I assume you want to print the variable value when executing in sqlserver studio management. If so, you can print the variable value by print variablename
regards
Naina
Naina
|
|
|
|

|
The printed value has to be a varchar or char. So, please cast the value to a varchar before printing.
|
|
|
|

|
Can Visual Studio 2005 connect with an Access 2010 Database?
|
|
|
|

|
Yes
G to connectionstrings.com as a research beginning.
Never underestimate the power of human stupidity
RAH
|
|
|
|

|
Hello,
We're currently implementing a warehouse management system with some customizations. One task is to plan the work in the warehouse by releasing waves of picking activities. The release is handled by a stored procedure. This stored procedure generates data on the amount of work that needs to be done, number of containers that are required, etc.. The planner needs to know this information in order to release the appropriate waves.
So, in order to capture the required data, we have another stored procedure which releases all waves that are not yet released and rolls back the transaction once it's done. This roll-back is a guarantee. The problem is that during this 'simulation' other queries are blocked, which creates a performance problem.
Is there a way to run the 'simulation' without blocking all other queries?
Thanks in advance!
Kind regards,
Bob Stanneveld
|
|
|
|

|
Sounds to me like you are doing something dramatically wrong, running a simulation on production data, therefore requiring the roll back, or building a business process on cancelling a transaction.
A more normal method of simulating would be to restore production to another instance, run the process to completion and then restore again to repeat.
Never underestimate the power of human stupidity
RAH
|
|
|
|

|
Earlier this week I queried the possibility of Propagating a MySQL transaction with MSMQ on a concept level.
Thanks to Pete I crossed that bridge. But there is a stumbling block.
MySQL transactions themselves do not appear compatible with TransactionScope!
Short of migration to SQLServer what are my choices?
Ger
|
|
|
|

|
Good Day All
I imported data into a Ms Access 2013 from SQL and all the data is there. I created a Query and also created a report that will use the Query. So the Query returns all the data. There are around 590 000 records that needs to be displayed by the record and records that are returned by the Query. when i do a Print Preview it gives me
Page 32767 of 12224
So i am afraid to start printing because from this look it seems like it does not show all data, but my assumption is that Print Preview cant show all the data , even if i try to go to the last page, can anyone clear my assumption.
Thanks
Vuyiswa Maseko,
Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code.
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.com
vuyiswa[at]dotnetfunda.com
http://www.Dotnetfunda.com
|
|
|
|

|
Vuyiswa Maseko wrote: There are around 590 000 records...So i am afraid to start printing
You should be.
Why exactly do you think that you should print something that is probably going to be about 6000 pages (at 100 records per page).
Who has told you that they are going to read this? Have you suggested to them what 6000 pages represents? Perhaps if you put a box (10 x 500 packages) on their desk and ask them what they planned to do with that then you might start a conversation that would lead to reports that are actually useful.
Other than that I certainly wouldn't attempt to print that at one go. If it messes up in the middle you have to start the entire job over. So select 400 pages at a time and print.
Might tell them you need your own printer too as it is going to tie it up for quite a while.
|
|
|
|

|
Good Day Jschell
Thank you for your reply. i will like to reserve my harsh comment for the other reply that i received after you , but i will politely reply to your question.
Who has told you that they are going to read this?
This is a book called the valuation roll , everyone comes and read this book because it tells a person how much they are going to pay in taxes ,for sure more than a million people are going to read the book.
Have you suggested to them what 6000 pages represents? Perhaps if you put a box (10 x 500 packages) on their desk and ask them what they planned to do with that then you might start a conversation that would lead to reports that are actually useful.
I have now decided to split it to 30000 to be on the safe side and created temp tables with the data sequential and its fine now thanks , what i learned is that Access Report will never output more than "32767", so basically in a Page you would have 10 records and if you take 590 000 and Divide by 10 it gives more 30000 Pages, so that is when i realized where and how to fix the problem.
Your reply was still going to point me to the direction of Splitting the data.
Thanks
Vuyiswa Maseko,
Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code.
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.com
vuyiswa[at]dotnetfunda.com
http://www.Dotnetfunda.com
|
|
|
|

|
Vuyiswa Maseko wrote: for sure more than a million people are going to read the book.
Nope.
There is no way that you are going to get one million people to read 6000 pages (plural) much les 30,000.
What is going to happen instead is that they are going to search for what they already know they are looking for. And then read 1 or perhaps as many as 10 pages to find what they are looking for.
And that is where the solution lies. Provide a way for them to search for it.
Vuyiswa Maseko wrote: so that is when i realized where and how to fix the problem.
Presumably your design will have 30,000 static pages and then provide a way to figure out where something is in those pages and then go to the specific page.
|
|
|
|

|
I agree with jschell, you are asking access to do something inherently dumb, print 1000s of pages and you wonder why it is giving you wrong results!
Never underestimate the power of human stupidity
RAH
|
|
|
|

|
Requirements in Software Development or in any industry might sometimes overcome the basic understanding of what makes sense to you.
Sometimes before reply you must try to understand what is the poster's point of view and after that you can reply or decide to be rude.
For your information, you can read my reply on jschell's reply.
Vuyiswa Maseko,
Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code.
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.com
vuyiswa[at]dotnetfunda.com
http://www.Dotnetfunda.com
|
|
|
|

|
Your "harsh reply" may be justified. I should have been somewhat more diplomatic. However you say more than a million people are going to read this tome, astonishing, no really I will be absolutely astounded if a million people read that book.
Some may reference the book and here is where I should have been more sensitive, I automatically assure that referencing such a vast amount of data would only be done electronically, this may not be an option for some!
Still the viability of printing 1000s of pages via Access stikes me as using the wrong tool for the job. Anything lees than a professional printing solution would be the wrong tool!
I am mightily impressed that you did not resort to down voting the response
Never underestimate the power of human stupidity
RAH
|
|
|
|
 |