Is it posible to use case in where in clause? Something like this: DECLARE @Status VARCHAR(50); SET @Status=’published’; SELECT * FROM Product P WHERE P.Status IN (CASE WHEN @Status=’…
Tag: select
SELECT DISTINCT values and INSERT INTO table
I want to take a column with values that repeat multiple times and get that value only once and store it for later use, but at the same time I would like to get another value in the same row as that distinct column. A, B, C represent three columns. Ignore C for now. My question is: How can I
How to create/add a column in an SQL select query based on another column’s values?
I want to dynamically add another column hook_name, via an SQL select query, based on a condition. For example if hook_type = 0, table hook_name should have a value of OFFER, and similarly for hook_type = 1, hook_name should show “ACCEPT”. Below is a screenshot of the result: The select query is t…
Count of non-null columns in each row
I have a table that contains 4 columns and in the 5th column I want to store the count of how many non-null columns there are out of the previous 4. For example: Where X is any value: Answer
Fastest way to determine if record exists
As the title suggests… I’m trying to figure out the fastest way with the least overhead to determine if a record exists in a table or not. Sample query: Say the ? is swapped with ‘TB100’… both the first and second queries will return the exact same result (say… 1 for this c…
MySQL INSERT INTO … VALUES and SELECT
Is there a way to insert pre-set values and values I get from a select-query? For example: I have the value of “A string” and the number 5, but I’ve to find the [int] value from a select like this: that gives me that id to put inside table1. How to merge this into one statement? Answer Use a…
select users have more than one distinct records in mysql
For a table that holds the records of user’s webpages visiting behavior, how can I select users that visit more than one webpages. The structure of this tables is: userId webpageId …
SELECT id HAVING maximum count of id
Have a products table with item_id and color_id. I’m trying to get the color_id with the most non-null instances. This fails: with This Returns I am looking for color_id 3, which has the most instances. Is there a quick and easy way of getting what I want without 2 queries? Answer This will give you the…
Rename single column in SELECT * in SQL, select all but a column
here is what I’m trying to do- I have a table with lots of columns and want to create a view with one of the column reassigned based on certain combination of values in other columns, e.g. Name, Age, Band, Alive ,,, <too many other fields) And i want a query that will reassign one of the fields, e.g.…
sql select query in same table
i have a newby sql related question. let’s say i have this simple table: i’d like to query for records which have “counterparts” only, i.e. i want to get a b only if there is b a in the table but i want to skip the “back links” (which is b ahere). to sum up i’d like t…