sql

SQL Server + Select only two records for each masterID in a table.

I got a child table that contains 1 to n records linked to the master table via a MasterID column. How can I select from the child table only the first 5 records for each MasterID? ...

How to test my Firebird SQL queries

I'm in the process of learning SQL, and I need a way of verifying that my SQL queries are valid (i.e. no syntax errors). I also would like to check what results they yield on a test database of my choosing and structure. I'm using embedded firebird in my C# .NET application, so I don't really have any tools to work with. Anyone have any...

Tree structured resource Authorization

I have portfolio table with portoflio_id and parent_portfolio_id and I have user table now some users may have access to all portfolios, or selective portfolios or depending on group, everything under a portfolio tree. can someone suggest a good schema or any existing framework ...

Using PIVOT in SQL Server

How can i convert below using PIVOT as Thanks ...

Number query in sql

I have table in sql as User | Account -----+--------- 1 | 25 1 | 31 1 | 35 1 | 44 1 | 50 1 | 59 and output need in as three columns 1 | 25 | 31 1 | 35 | 44 1 | 50 | 59 ...

sort items based on their appears count

Hello Experts, I have data like this d b c a d c b a b c a c a d c if you analyse, you will find the appearance of each element as follows a: 4 b: 3 c: 5 d: 2 According to appearance my sorted elements would be c,a,b,d and final output should be c b d a d c b a b c a c a d c Any clue, how we can achieve th...

Transforming large XML for SQL Server insertion

Hi, Our state government has opened its transport timetable data. The data is in xml based TransXchange standard format. The problem is the data files are huge. The sample data file itself is 300 MB. The good thing is most of the data is redundant and I don't need it for my application. I am wondering what options do I have of insert...

Replace table of one database with other

I have 2 databases with same tables. DBProduction and DBLocal both have testTable. How can i replace testTable of DBProduction with of DBLocal? There are some new rows inserted and some updated rows in testTable of DBLocal. Using SQL Server 2008 ...

Copy a Table's data from a Stored Procedure

I am learning how to use SQL and Stored Procedures. I know the syntax is incorrect: Copy data from one table into another table on another Database with a Stored Procedure. The problem is I don't know what table or what database to copy to. I want it to use parameters and not specify the columns specifically. I have 2 Databases (Master...

C# SQL Data Adapter System.Data.StrongTypingException

I get my data from SQL to Dataset with Fill. It's just one table with two columns (CategoryId (int) and CategoryName (varchar)). When I look at my dataset after fill method, CategoryId Columns seems to be correct. But in the CategoryName I have a *System.Data.StrongTypingExceptio*n. What could that mean? Any Ideas? ...

Results from two queries at once in sqlite?

I'm currently trying to optimize the sluggish process of retrieving a page of log entries from the SQLite database. I noticed I almost always retrieve next entries along with count of available entries: SELECT time, level, type, text FROM Logs WHERE level IN (%s) ORDER BY time DESC, id DESC LIMIT LOG_...

Bulk Insert multiple records and get identity for all using ADO.NET

I want to batch insert multiple records in sql server database using ADO.NET command/sqldataadapter and get identity value generated for all the rows in a single database trip. What are my options? ...

count columns group by

I hava the sql as below: select a.dept, a.name from students a group by dept, name order by dept, name And get the result: dept name -----+--------- CS | Aarthi CS | Hansan EE | S.F EE | Nikke2 I want to summary the num of students for each dept as below: dept name count -----+-----------+------ CS | ...

Entity Framework VS LINQ to SQL VS ADO.NET with stored procedures?

How would you rate each of them in terms of: Performance Speed of development Neat, intuitive, maintainable code Flexibility Overall I like my SQL and so have always been a die-hard fan of ADO.NET and stored procedures but I recently had a play with Linq to SQL and was blown away by how quickly I was writing out my DataAccess layer a...

What are some good examples where SQL's OUTER JOIN is used?

I often get asked the questions in an interview that "what is an outer join in SQL"? While it can be answered, I wonder what might be some classic and good real life examples where a (LEFT) OUTER JOIN is used? ...

Faster to get one row from DB or count number of rows

Hi, I was wondering if its faster to retrieve the "count" of the number of rows or to retrieve just 1 row using limit. The purpose being to see whether theres any row when given certain Where conditions. ...

How to store MySQL query results in another Table?

How to store results from following query into another table. Considering there is an appropriate table already created. SELECT labels.label,shortabstracts.ShortAbstract,images.LinkToImage,types.Type FROM ner.images,ner.labels,ner.shortabstracts,ner.types WHERE labels.Resource=images.Resource AND labels.Resource=shortabstracts.Reso...

From varchar(36) to UNIQUEIDENTIFIER

I am trying to cast an AuctionId that is a UNIQUEIDENTIFIER to an varchar(36) and then back to an UNIQUEIDENTIFIER. Please help me. CAST((SUBSTRING(CAST([AuctionId] as VARCHAR(36)), 0, 35) + '1') AS UNIQUEIDENTIFIER) But I keep getting this error: Msg 8169, Level 16, State 2, Line 647 Conversion failed when converting from a c...

How to create a sql function using temp sequences and a SELECT on PostgreSQL8

Hello there, I have this issue in either postgreSQL 8.3 or 8.4: I've created a script which (re)creates three temp sequences and then uses them to transpose some tables in order to get a single row. Now I want to put it in a FUNCTION which returns a type already created (I've done a select into and then I took the fields from the creat...

How can we run .sql file from java without using JDBC?

I want to run a .sql file from java without using jdbc on windows 7. I am trying with java runtime class which executes the system commands. But my java is running with only user privileges and the command needs Administrator privileges. The command that am using to run is "sqlplus /nolog @sqlscript.sql". ...