sql

optimising aggregates with and without a condition in ms sql

Hi, I would like to get a sum from a column, with and without a condition. The code I have now is SELECT regular.id, regular.sum as regularsum, special.sum as specialsum FROM (SELECT Sum(stuff) as sum, id FROM table WHERE commonCondition = true GROUP BY id) as regular INNER JOIN (SELECT Sum(stuff) as sum, id FROM table Where commo...

How to query hierarchical information with SQL-Server 2000?

I've got a table Folders with hierarchical information about folders: FolderID FolderName ParentID 1 Folder1 0 2 Folder2 1 3 Folder3 2 4 Folder4 3 For Folder4 I would like to get the parent folders in the following format: Folder1\Folder2\Folder3\ Not...

how can we get data from xml file in sql server 2005 as described?

--- This is the content of the XML file or consider as XML variable. From above xml file , i want the data like .. id name desc parentid 1 serach Search NULL 2 Search By Name SearchName 1 3 Search By ID SearchID 1 How can we get with single SQL statement in SQL serv...

SQL-Server common problem - Retrieve ID just inserted

I've looked at some other questions, they seem to be unrelated to SQL-Server. Given the simple example query: INSERT INTO tblComments (comment, user) VALUES ('lol', 'Guest') Now suppose after the new user is created I need to insert some more data in other tables that require the last entered ID, I would do: SELECT TOP 1 ID FROM tbl...

SQL report with Matrix which grows horizontal, need to break to next line instaed of moving to the next page.

I have a Sqlreport (SSRS 2005) which is using a Matrix, to print pivot table format data. and prints horizontically.Thats fine but it does not break to the next line of the same page, instead move to the next page and grows horizonticaly on the next page. I dont want to go another page instaed need to go to the next line. Please help m...

Should I Use a Self-Join

If I have a table... ID Name Manager 0 Joe Sue 1 Jake Tom 0 Joe Tom 2 Larry Red 2 Larry Paul 1 Jake Paul I want the output to be.... ID Name Manager1 Manager2 0 Joe Sue Tom 1 Jake Tom Paul 2 Larry Red Paul Thanks... ...

FK on same table

Hi all, I have table with ID who is PK and I have in same table column ParentID who is in some cases ID's.(I say some cases because for example ParentID can be 0 but this ID not have in ID's column). I try to create Query: ID ParentID Value 1 0 Test 2 0 Test01 3 2 Test02 4 3 ...

SSIS (DT_Decimal) to (DT_Str) removes trailing zeros for decimals

Hi there, When converting from DT_Decimal to DT_Str, the derived column stips off decimals from my result. ex.: -0.1250 returns -0.125 -2.0000 returns -2 Is there a way to keep the zeros to have a final string with always four decimals ? Here is my derived column for the decimal value calculation: (DT_DECIMAL,4)ROUND((DT_Decimal,4)[...

Data Flattening With SQL

This one seems so simple but I cannot figure it out. I have data stored like this... ManagerID EmployeeID MangerName 0 0 Debbie 1 0 Mark 2 2 Chris 3 2 Leo 4 1 Mar 5 2 Steve...

SQL - Is this possible?

I have a table that looks like this: -------------------------------------------- | Date | House# | Subscription | -------------------------------------------- | 3/02/10 | x | Monthly | -------------------------------------------- | 3/03/10 | y | Weekly | -------------------------------...

MySQL Query sometime OK, other times takes 5 min!

Hi, I have a table I use for high scores of an iPhone game. As of now the query is disabled so gamers are not able to see scores :-( Table has the following fields: Type Collation Attributes Null Default Extra Action id int(11) No auto_increment date date No ...

Need approach for working with small subsets of a large dataset

I am facing a conceptual problem that I am having a hard time overcoming. I am hoping the SO folks can help me overcome it with a nudge in the right direction. I am in the process of doing some ETL work with the source data being very similar and very large. I am loading it into a table that is intended for replication and I only want...

What drawbacks are there to maintaining a table of calculated records with triggers and FK constraints?

I have a table containing roles. Many users can be assigned to a role. Permissions can be assigned to a role, granting all users to that role with the permission. When I assign a permission to a role that has 500 hundred people, I've effectively created 500 permission assignments. With each permission that is assigned to a role I spe...

How do I get rows for last 24 hours by unixstamp

I have this; $long = "86400"; $query = "SELECT * FROM users WHERE unixdate = UNIX_TIMESTAMP()-$long ORDER BY unixdate DESC"; But it doesn't work. I would like to show all new users within 24 hours ...

PostgreSql + btrim function is not working properly

Here, I have probleming in trim the value in postgresql. Please have a look. Ruby code :: if(this.modelName=="ClientOffice") { this.params="model_name="+this.modelName+"&action_name="+this.actionName+"& find_condition=btrim(clients_corporate_billings.id,' ') %3D btrim('"+validString('populateValue0','text')+"',' ') & ob...

More effecient SQL for retreiving thousands of records on a vew

I am using Linq to Sql as my ORM and I have a list of Ids (up to a few thousand) passed into my retriever method, and with that list I want to grab all User records that correspond to those unique Ids. To clarify, imagine I have something like this: List<IUser> GetUsersForListOfIds(List<int> ids) { using (var db = new UserDataCo...

Oracle Bulk Collect issue

Hi Oracle experts. I'm having a little issue with a piece of bulk collect sql that I was hoping you could help out with. With the following code: declare cursor c1 is select customer,product from products; type type_cust is table of products.customer%type; type type_prod is table of products.product%type; ...

SQL Server SQL JOIN Assistance

I'm doing joins incorrectly, and haven't figured out the proper way to get the data I need given the 3 tables below (Customer, SalesHeader, SalesDetail) I'm trying to get line items with 1 Customer per line and the sum of all Sales Details that are in GL Acct 4000 and all Sales Deails in GL Acct 5000. There are many more columns, and se...

SQL select where matching record exists and no matching record

I need to cross reference 2 tables. within tb1 is booking_ref, investor within tb2 is booking_ref, investor, cost The trouble is if there in no cost, no record is created in table 2 So I have the following query... SELECT tb1.booking_ref, tb1.investor, tb2.cost FROM tb1, tb2 WHERE tb1.booking_ref = tb2.booking_ref AND ...

SQL - Left Join problem...

SELECT tb1.booking_ref, tb1.investor, tb2.cost, tb3.product FROM tb1, tb3 LEFT JOIN tb2 ON tb1.booking_ref = tb2.booking_ref AND tb1.investor = tb2.investor AND tb1.investor = '12345' WHERE tb1.location = tb3.location The above query errors because of the references to tb3 - it works great without them. Does anyone...