sql

optimizing the sql query by not repeating the math..

Here is the query in Oracle I was trying to remove the redundant math operation from: SELECT name, CASE when nvl(num1,0) + nvl(num2,0) - nvl(num3,0) > 0 THEN nvl(num1,0) + nvl(num2,0) - nvl(num3,0) ELSE 0 END as result, .... from .... How do I not repeat the summation above? The 'result...

Is there a way to split the results of a select query into two equal halfs?

I need a solution for a select query in Sql Server 2005. I'd like to have a query returning two ResultSets each of which holding exactly half of all records matching a certain criteria. I tried using TOP 50 PERCENT in conjunction with an Order By but if the number of records in the table is odd, one record will show up in both resultset...

@@TRANCOUNT and a current connection

Assume I connect to SQL server 2008 via SQL Server Management Studio ( SSMS ) and open new window W1 by clicking on New Query tab and write the following inside W1: BEGIN TRANSACTION; If I execute this statement 5 times, and then write (inside W1) SELECT @@TRANCOUNT; , then the value returned will be 5. But if I open another window...

What is the SQL statement that removes duplicates but keep additional column's data?

Hello: I'd like to know what should be the SQL statement (for ORACLE DBMS) that would get back unique (by CUSTOMER_ID, VEHICLE_ID, DEALER_ID and EVENT_TYPE_ID) rows BUT it will return the latest date (EVENT_INITIATED_DATE) for that row too. I've tried DISTINCT and GROUP BY, but wasn't able to figure out how to handle EVENT_INITIATED_DA...

PL/SQL time segment calculation

Hi, I need to figure how much time passed between 2 times. For example: (14:00:00 - 13:15:00) * 24 = .75 I need this to later on convert KW to KWh, but that's not the point. I can not find out how to do this in PL/SQL. My date/time fields look like this in the DB: 1/23/2010 21:00:00 I would appreciate any suggestions. Steve ...

SQL select statement from 2 tables

Hi, I have a small sql question. I have 2 tables Members and Managers Members has: memberID, Name, Address Managers has: memberID, EditRights, DeleteRights EditRights and DeleteRights are of type bit. Mangers have a relationship with Members, because they are members themselves. I want to select all members id's, name and adress a...

Ordering by a max or a min from another table

I have a table that consists of a unique id, and a few other attributes. It holds "schedules". Then I have another table that holds a list of all the times each schedule has or will "fire". This isn't the exact schema, but it's close: create table schedule ( id varchar(40) primary key, attr1 int, attr2 varchar(20) ); creat...

How to execute big SQL files on SQL Server?

I have about 50 T-SQL files, some of them are 30MB but some of them are 700MB. I thought on executing them manually, but if the file is bigger than 10MB it throws an out of memory exception on the SQL Server Management Studio. Any ideas? ...

SQL help for retrieving some records using the adjacency list model

I'm struggling with writing an SQL query to display the leaf node (last node) along with their parents names without having to use a procedural function in php (I need to do this in the SQL) and I'm wondering if someone can help me. Im using MySQL as the db. I'm using the adjacency list model for storing the data. What I want returned...

How to return result set based on other rows

I've 2 tables - packages and items. Items table contains all items belonging to the packages along with location information. Like following sample tables: Packages table id, type(enum{general,special}) 1, general 2, special Items table id, package_id, location 1, 1, America 2, 1, Europe 3, 2, Europe Question: I want to find all 'sp...

Why do I need to explicitly specify all columns in a SQL "GROUP BY" clause - why not "GROUP BY *"?

This has always bothered me - why does the GROUP BY clause in a SQL statement require that I include all non-aggregate columns? These columns should be included by default - a kind of "GROUP BY *" - since I can't even run the query unless they're all included. Every column has to either be an aggregate or be specified in the "GROUP BY", ...

In SQL Azure how what script can I use to create a read only user

I would like to create an Sql Azure user and grant her readonly access on a handful of DBs, what script can I use to achieve this? ...

(N)Hibernate: deleting orphaned ternary association rows when either associated row is deleted.

I have a ternary association table created using the following mapping: <map name="Associations" table="FooToBar"> <key column="Foo_id"/> <index-many-to-many class="Bar" column="Bar_id"/> <element column="AssociationValue" /> </map> I have 3 tables, Foo, Bar, and FooToBar. When I delete a row from the Foo table, the assoc...

Numeric Order By In Transact SQL (Ordering As String Instead Of Int)

I have an issue where I am trying to order a result set by what I believe to be a numberic column in my database. However when I get the result set, It has sorted the column as if it was a string (So alphabetically), instead of sorting it as an int. As an example. I have these numbers, 1 , 2, 3, 4, 5, 10, 11 When I order by in Trans...

SQL conditional Pivot

Yes, this is another Pivot question... I've read through nearly all the previous questions and I can't seem to hack together a query that does what I need. Here is what my table looks like: FirmName Account Balance Pmt Revolving Installment Mortgage Amex 12345 10000 2000 1 0 0 Discover 54321 20000 4000 ...

sql - datetime variable versus string representation of datetime variable

I have a query that takes too long to respond when the search parameter happens to be a varchar datatype with date. However, if i convert varchar to datetime variable, the query runs fine. For ex: This takes too long. select count(id) from names where updateddate > '1/5/2010' This runs fine. declare @dateparam datetime set @...

Interesting SQL Sorting Issue

It's crunch time, deadline for my most recent contract is coming in two days and almost everything is complete and working fine (knock on wood) except for one issue. In one of my stored procedures, I'm needing to return a result set as follows. group_id name -------- -------- A101 Craig A102 Craig Z101 ...

How to select a random record from a MySQL database?

I am using the following query to select 1 random record - SELECT name FROM table WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM table ) ORDER BY id LIMIT 1 but it gives me the same set of records every time I call it. How do I get better random record? ...

Avoiding Nested Queries

How Important is it to avoid nested queries. I have always learnt to avoid them like a plague. But they are the most natural thing to me. When I am designing a query, the first thing I write is a nested query. Then I convert it to joins, which sometimes takes a lot of time to get right. And rarely gives a big performance improvement (s...

Result Set Problem

Hello Friends, It is possible to Use two query with in single ResultSet ...