sql

T-SQL: Sorting by date, then grouping?

Let's say I have a database table that looks like this: ID name salary start_date city region ----------- ---------- ----------- ----------------------- ---------- ------ 1 Jason 40420 1994-02-01 00:00:00.000 New York W 2 Robert 14420 1995-01-02 00:00:00.0...

Can I add an entry to mysql and return a column if it's not present in one query?

Can I have one query to check if an entry is in a mysql table, and if it is present, return a column, but if it's not present, insert it and return a value? I have a users table with the following layout: userID - int, auto-increment userName - varchar(100) active - tinyint(1) I want to do something like select userID from users wh...

Crazy SQL question: How to do a sort of cross apply with a pivot?

Hello. Customer has shortcuts in their data where they have a quantity of records in a field. When I parse them, I need to manufacture records, one for each quantity, incrementing the "identifier" by 7 days (because the number represents a date.Example: a single product that is on sale for four weeks and I need four records, one product...

getting the index of set bits from an int column in mysql

Anyone have any SQL-fu (can be MySQL-specific) that will give me the indexes of set bits in an int, without using a procedure? How about with a procedure? For example, given: create table example (val int); insert into example values (1), (2), (3), (4), (256); I can see the set bits: select conv(val, 10, 2) from example; +---------...

Keeping track of web application usage in an SQL Database (Need Suggestions)

I have a system that will allow users to send messages to thier clients. I want to limit the user to x amount of messages per day/week/month (depending on subscription). What is a good way to tally for the time period and reset the tally once the time period restarts? I would like to incorporate some sort of SQL job to reset the tally. ...

Mysql - modify result dataset

I have a program (flex) that queries a database and gets back a dataset (value, timestamp). In the program I then put each value in the set through an algorithm to get a new value resulting in all the values being transformed. Instead of having to do this transformation of data in my program I would like mysql to do it and send the res...

How to use SQL from asp.net?

Building a relatively simple website, and need to store some data in the database (mainly authentication, but there's some per-user stuff). I've worked on a couple of websites previously, and used database there too, but never liked the way I accessed the database. The way I usually did this was by having a SqlMethods.cs, which basicall...

Displaying each of the 24hrs of the day...

Hi I was trying to display just the hours in 24hr format like: select to_char(trunc(sysdate+(1/24)),'HH24:mi') from dual But this only always returns 00:00. How can I show 01:00 to 23:00? Thanks and Regards ...

Django: Ordering objects by their children's attributes

Consider the models: class Author(models.Model): name = models.CharField(max_length=200, unique=True) class Book(models.Model): pub_date = models.DateTimeField() author = models.ForeignKey(Author) Now suppose I want to order all the books by, say, their pub_date. I would use order_by('pub_date'). But what if I want a list of all a...

How do I retrieve every Nth record from a table?

The query remains constant, but the offset varies. SELECT NAME from tbl WHERE alphabet_index='A' limit 880,1; SELECT NAME from tbl WHERE alphabet_index='A' limit 1760,1; SELECT NAME from tbl WHERE alphabet_index='A' limit 2640,1; .... SELECT NAME from tbl WHERE alphabet_index='A' limit 34320,1; Is there a better way to do this withou...

sql lite replicating the records by using triggers based on current date

Hi, Im using sqllite in my palm-pre web os application, i have the table with the following structure. Spending(id,note,amount,date,recurringtype,type,isprocessed); the value for the recurring type will be like daily,weekly monthly etc.(1,2,3..etc) i need to replicate a record by checking the date and the recurring type. Can it be ac...

SQL database, multiple tables sharing primary index

I am developing a site that will save data into an SQL database that describes events. One of the events being stored in the database has several variations. Some of these variations are fairly uncommon and i am trying to limit the number of blank fields in the records. I am debating between these two styles: option A: report_primar...

does ordering by lot of columns hit the performance badly?

I just ran into a SQL query with about 5 different columns in the ORDER statement, is it a good practice and how does it play with performance? ...

Oracle SQL Developer How To Default To Other Users Tables?

Hi Guys, In order to see all of the tables in our companies DB I have to go find the main sys account. Is there a way to default my connection so that it shows the other users tables? ...

SQL select at least X rows that are in a given time

hello, i am looking for an SQL query, that selects exactly rows ordered by date from a table. for this there is a column that contains a timestamp. and here comes the tricky part: it should not select rows that are older than a certain time, but it should still select at least X rows. so if in the given time there are not more than X ...

Get data from table, Using Rows as Columns.

I have following data in my table. I want to extract ProductId which has this criteria, FieldValue = 1.0 and FieldValue = 'Y' and FieldValue = 'N' This is not possible using following query select * from MyTable WHERE (FieldId = 50 AND (FieldValue BETWEEN '1.0' AND '1.0')) AND (FieldId = 55 AND FieldValue = 'Y') AND (FieldId = ...

Caching of Map applications in Hadoop MapReduce?

Looking at the combination of MapReduce and HBase from a data-flow perspective, my problem seems to fit. I have a large set of documents which I want to Map, Combine and Reduce. My previous SQL implementation was to split the task into batch operations, cumulatively storing what would be the result of the Map into table and then performi...

Exclude Statement in SQL

Hello StackOverflow Community, I had a quick question on how to exclude data from an SQL database using an SQL statement. My situation is I have a user login to their profile page where they will be able to friend people. I want to display all users except themselves that are found in the SQL database. Thanks for all the answers! Roha...

Help understanding SQL Server 2005 execution plan

One of my stored procedure has long execution time (average around 4 to 7 minutes). Now I trying to tweak it an make it run faster. I am looking at execution plan and two things I see that using most of percentage. First is 68% of "Clustered Index Scan" of one main tables for reporting, This table has primary key of two columns and 200...

Attempt at database localization using table-valued functions

Hi ! I'm looking for opinions on the following localization technique: We start with 2 tables: tblProducts : ProductID, Name,Description,SomeAttribute tblProductsLocalization : ProductID,Language,Name,Description and a table-valued function: CREATE FUNCTION [dbo].[LocalizedProducts](@locale nvarchar(50)) RETURNS TABLE AS (SELECT a.P...