sql

char column converted to varchar still inserting padding

I don't really know how to explain this one. I've taken a table, created a copy of it with a specific column as varchar instead of char. Then I've copied the data from one table into the new one. However, when I then programmatically add a new value to the table, the column that was previously char(200) is still being padded with space u...

Merge virtually two mysql tables

Hi ist there a way to merge two tables in MySQL virtually so I could query and insert data? (which is basicly splitted) exp. table id active foo table_meta table_id language text what I like to do is using something like this instead of sql-join INSERT INTO table_join SET active = 1, language = 'en'; regards jim ...

A SQL story in 2 parts - Are SQL views always good and how can I solve this example?

Hiya, I'm building a reporting app, and so I'm crunching an awful lot of data. Part of my approach to creating the app in an agile way is to use SQL views to take the strain off the DB if multiple users are all bashing away. One example is: mysql_query("CREATE VIEW view_silverpop_clicks_baby_$email AS SELECT view_email_baby_posit...

Myslq php update function problem

Hello i would like to ask how to create a php code for mysql to do the fallowing thing if active = 1 to do current amount + 2000 id, eid, amount, apply 1, apply 2, apply 3, active 1 1788 500 NULL NULL NULL 1 2 1956 1000 NULL NULL NULL 1 3 2035 ...

sql server complex query - fetch data and search on multiple tables

I have a two tables as follows: product category(t1): id name product master(t2): id name category Now I have the following query to fetch the products and the associated category name in one query only: select *, (select name from t1 where t1.id=t2.category) as 'category' from t2 which is working perfectly. Now what ...

SQL count(*) and distinct

Why can't we use count(distinct *) in SQL? As in to count all distinct rows? ...

Cross Database Unions

My warehouse datamart data is split across 3 databases on the same server. This is Proof-Of-Concept project with three extracts that were loaded into individual databases. While pulling into a cube, I am essentially doing this: SELECT * FROM DB1.dbo.Fact_Pres UNION SELECT * FROM DB2.dbo.Fact_Pres UNION SELECT * FROM DB3.dbo.Fact_Pres ...

How can I expand a PostgreSQL row into multiple rows for updating "custom fields"?

I have a table used by one piece of 3rd party software (say user_fields) that has for each row a user_id, a field_id and a field_value. Crappy I know. I also have another user table of my own, users, that contains the values needed to populate this, except the field_id which is "magic" and corresponds to the custom field mapping. How c...

If input name found in SQL then do else do… with an SQL query

Im trying to write an SQL query that will check in the table ‘persons’ in the column ‘rName’ for say a name “jack” and if it exists then UPDATE that row else INSERT a new row with the new rName. I’v been trying out IF/ELSE statements but haven’t really seen how they work. Or is there a better way to do what I want without If/ELSE? (I ha...

SELECT STATMENT

Hello, I have two table: EMP emp_id | Name | Surname | 1 | Bob | Park | 2 | Annie| South | 3 | Eric | P. | PROJECT proj_id | Tester_1 | Tester_2 | Tester_3 | 1 | 2 | 3 | 1 | Now I am trying to make a view, so that I have the following proj_id | Tester_1_Name | Tester_2_Na...

SQL (Server) Update performance

My limited SQL knowledge prompted this post! I have a stored procedure that runs a series of updates. There are 6 update statements, that could very well be mashed into one large update statement, but I dont know what that will mean for performance. Table basics: 6 tables are hit. 3 of which will never have more than about 5000 rec...

MySQL Order by multiple column combined (not order by field1 asc, field2 asc)

Hi all, it seems like a typical question but it's different. I have a table with an id and 3 timestamp fields (to simply). Initially all 3 fields are null, and they get filled with values. Examples of rows are: id time1 time2 time3 1 1259625661 1259643563 null 2 null 1259621231 null 3 1259625889 null 1259644511...

closing connection with sql server using a DBHelper file to make connection and XmlReader to point to it

Hello all, Well, as i mentioned, i am using an app_code file to handle my connections to the DB, using two public static functions. one for the SqlDataReader when using SQL and the other is XmlReader when using XML. The thing is, in ExecuteReader i can write: return cmd.ExecuteReader (CommandBehavior.CloseConnection); and that will cl...

Query driving a view and actual view not returning same results

I have a view that is returning four columns of data to be pushed to an external program. When I simply query the view ("Select * from schema.view_name") I get 10353 rows. When I run the actual SQL that created the view (I literally copied and pasted what Oracle had stored, minus the "Create or Replace" statement), I get 238745 rows. An...

MySQL error: key specification without a key length

I have a table with a primary key that is a varchar(255). Some cases have arisen where 255 characters isn't enough. I tried changing the field to a text, but I get the following error: BLOB/TEXT column 'message_id' used in key specification without a key length how can I fix this? edit: I should also point out this table has multiple...

Converting an Integer to Enum in PostgreSQL

I have created a custom data type enum like so: create type "bnfunctionstype" as enum ( 'normal', 'library', 'import', 'thunk', 'adjustor_thunk' ); From an external data source I get integers in the range [0,4]. I'd like to convert these integers to their corresponding enum values. How can I do this? I'm using PostgreSQL 8.4. ...

SQL select statement to find ID's that occur the most

Basic SQL statement question - I have a table (myUsers) that contains the column "UserID." The same UserID can appear one to many times in these rows. I'm looking for a query that will give me back the specific userIDs that appear the most in this table, and also with their count. Any thoughts? Thanks in advance! ...

Divide does nothing

I'm attempting to create percentile scores. My query generates the ranks correctly, but the divide does nothing(the ranks are displayed in the columns rather than scores) "/"(RANK() OVER(ORDER BY "Disk IO"),Count(*)) "Disk IO Score" I've also tried generating the rank then selecting that and dividing, but it has the same result. SELE...

a Rollup query with some logical netting using Oracle SQL

Hi, I have a table "AuctionResults" like below Auction Action Shares ProfitperShare ------------------------------------------- Round1 BUY 6 200 Round2 BUY 5 100 Round2 SELL -2 50 Round3 SELL -5 80 Now I need to aggregate results by every auction with BUYS after ne...

SQL query to find users that don't have any subscription to a specified list (many-to-many).

Having two tables, "users" and "lists", and a many-to-many "subscriptions" table relating users to lists (thus having foreign keys user_id and list_id), what would be a single SQL query to find all the users that don't have any subscription with a specific list_id (naturally including the users that have no subscriptions at all)? ...