I have an SQL query to retrieve a few columns of a table and export them on a CSV file. However, one column use an ID (integer number). Is there any way to include something in SQL query and replace the IDs with specific strings?
For example:
If ID = 1 then replace it with "Dog"
If ID = 2 then replace it with "Cat"
If ID = 3 then replace it with "Apple"
I have only four IDs so, it will not be huge.
Here is what I have done until now to retrieve the columns I want
SELECT col_id, check_in, start_hour, price_total, email FROM reservations LIMIT 10
SELECT case when col_id = 1 then 'Dog'
when col_id = 2 then 'Cat'
when col_id = 3 then 'Apple'
end as id,
check_in, start_hour, price_total, email
FROM reservations
LIMIT 10
You could use case/when/then like this:
select case when ID = 1 then 'one' else 'two' end from reservations
Related
Background
I want to rename my case statement in sql select statement dynamically.
Eg:
SELECT (case when id= x.id then x.sums end) x.id
as (select id,count(*) sums from table
group by id) x
what i want the output is list of columns created ,with Labels as distinct id's from "id" column.
However,this variable x.id is not dynamically outputing values,rather i get output a single column x.id.
Eg:
Columns in table...
id---c1----c2
1----x1---x2
2----x2----x3
3----x4----x5
columns expected after running query...
1-----2----3
but actual o/p column is::
x.id
Query
Any ideas,how to generate columns dynamically using select query,please correct me ,if i am wrong.
Below is for BigQuery!
Please note: your expectations about output column names are not correct!
Column name cannot start with digit - so in below example - i will be using id_1, id_2 and id_3 instead of 1, 2 and 3
SELECT
SUM(CASE WHEN id = 1 THEN 1 END) AS id_1,
SUM(CASE WHEN id = 2 THEN 1 END) AS id_2,
SUM(CASE WHEN id = 3 THEN 1 END) AS id_3
FROM YourTable
Above example assumes you know in advance your IDs and there are very few of them so it is not a big deal to write manually few numbers of lines with SUM(...) for each id
If this is not a case - you can first generate above query programmatically by running below query
SELECT 'SELECT ' +
GROUP_CONCAT_UNQUOTED(
'SUM(CASE WHEN id = ' + STRING(id) + ' THEN 1 END) AS id_' + STRING(id)
)
+ ' FROM YourTable'
FROM (
SELECT id FROM (
SELECT * FROM YourTable GROUP BY id ORDER BY id
)
as a result - you will get string like below
SELECT SUM(CASE WHEN id = 1 THEN 1 END) AS id_1,SUM(CASE WHEN id = 2 THEN 1 END) AS id_2,SUM(CASE WHEN id = 3 THEN 1 END) AS id_3 FROM YourTable
So, now just copy it and paste into Query Editor and run it
you can see similar example here - https://stackoverflow.com/a/36623258/5221944
I have a list of items in MySQL table each one has a group number & unique item number.
I'm Trying to select records that mach 2 criterias.
First I need to select record that satisfies group & item numbers. If none found i want records selected that just belongs to a group.
Only one record has to be selected either way.
SELECT *
FROM YourTable
WHERE GroupID = :group
ORDER BY ItemID = :item DESC
LIMIT 1
LIMIT 1 makes it return just one record. The ORDER BY clause makes it prefer a record that matches the item criteria if there is one.
Try this
this won't work
Select * From MyTable
Where (GroupID = 1 and ItemID = 2) or (GroupID = 1)
this works
IF EXISTS (SELECT 1 FROM MyTable WHERE GroupID = 1 AND ItemID = 2)
BEGIN
SELECT TOP 1 * FROM MyTable WHERE GroupID = 1 AND ItemID = 2
END
ELSE
BEGIN
SELECT TOP 1 * FROM MyTable WHERE GroupID = 1
END
replace MyTable with the name of your table
replace GroupID and ItemID with the name of your columns
replace number 1 and 2 with whichever value associate with columns you wish to filter
In mysql - table like this:
ID Name Value
1 Color Blue
1 Weight 50
1 Type Fruit
2 Color Red
2 Weight 40
3 Color Yellow
I want to get a count of distinct ID's that have a name/characteristic of 'Type' and a distinct count of ID's that don't. The first one (those that do) is easy since it's defined, but if I do a select count(distinct ID) where name <> 'type' - ID 1 will still be part of that count as it has other rows/attributes that <> 'type'.
In this example - the desired result for distinct count = 'type' would be 1 (ID 1) and for distinct count <> 'type' would be 2 (ID's 2 & 3).
Thanks in advance.
This is similar to SQL Query User has one record in the table but not another one
You can select where the id is not in a subquery looking for ids that have type
select count(id) from table where id not in (select id from table where name = 'type')
group by id
for this particular task you can use:
select count(distinct ID) from table
where ID in (select ID from table where name='type') --- this will give you count of IDs where type exists
select count(distinct ID) from table
where ID not in (select ID from table where name='type') -- this will give you count of IDs without type
SELECT id FROM test GROUP BY id HAVING
CONCAT(",",CONCAT(GROUP_CONCAT(name), ","))
NOT LIKE '%,Type,%'
will give you all ids without Type: http://sqlfiddle.com/#!2/30837/1
(Concat with , ensures, that you are not matching XType by accident.)
while
SELECT COUNT(id) AS count, GROUP_CONCAT(id) AS ids
FROM(SELECT id, count(name) as count FROM test
GROUP BY id HAVING CONCAT(",",CONCAT(GROUP_CONCAT(name), ","))
NOT LIKE '%,Type,%') as temp;
will give you the desired count: http://sqlfiddle.com/#!2/30837/9
SELECT CASE
WHEN Name='Type' THEN 'Type'
ELSE 'Non-Type'
END Name
,ID
,COUNT(ID)
FROM Stuff
GROUP BY
CASE
WHEN Name='Type' THEN 'Type'
ELSE 'Non-Type'
END
,ID
See SQLFiddle
I would like to select multiple values from a single column in a database table that equal to a number of values. I want all these values to match otherwise it should return no rows. I do not want to use "IN" as that is equal to "OR".
The following is a basic mockup of what it should do but it needs to be dynamic as I wish to use it with a PDO statement. If the database only contains id's 1 and 2 it should fail ie return no rows.
SELECT
id
FROM
reports
WHERE
id=1 AND id=2 AND id=3
I have the current code as follow which is incorrectly returning zero rows:
SELECT id,title
FROM reports
WHERE id IN (1,2)
GROUP BY title
HAVING COUNT(DISTINCT id) = 2
My current table structure is as follows:
http://www.sqlfiddle.com/#!2/ce4aa/1
You have to use HAVING COUNT(id) = 3 to ensure that the selected rows have all the three id's. Something like:
SELECT *
FROM reports
WHERE id = 1 OR id = 2 OR id = 3 -- Or id IN(1, 2, 3)
GROUP BY SomeOtherField
HAVING COUNT(DISTINCT id) = 3;
Or:
SELECT *
FROM reports
WHERE SomeOtherField IN (SELECT SomeOtherField
FROM reports
WHERE id = 1 or id = 2 -- Or id IN(1, 2, 3)
GROUP BY SomeOtherField
HAVING COUNT(DISTINCT id) = 3
);
Note that: You have to GROUP BY SomeOtherField where SomeOtherField is other field than id because if you GROUP BY id with HAVING COUNT(id) you won't get any records, since COUNT(id) will be always = 1.
Edit: fixed WHERE clause, OR's instead of AND's.
SQL Fiddle Demo
I'm trying to update a database table within MySQL to combine rows. Here's what part of the table looks like:
NID SID CID NO DATA
5297 32002 5 0 10
5297 32002 5 1 17
5297 32002 5 2 1976
The 'DATA' column contains the month, day, and year that make up a date. So I would like to combine those records into the following:
5297 32002 5 0 10-17-1976
For each SID, it contains a CID with 3 different rows, indicated by the 'No' column and containing the three parts of the date.
I can get the output desired with this select statement:
SELECT GROUP_CONCAT( d.data ORDER BY `no` SEPARATOR '-') AS data FROM
webform_submitted_data_copy d WHERE cid = "5" GROUP BY d.sid
This returns what is desired to be in the 'data' column following an UPDATE..but I can't figure out how to form the correct UPDATE statement... Something like:
UPDATE webform_submitted_data_copy
SET webform_submitted_data_copy.data = (GROUP_CONCAT( d.data ORDER BY `no` SEPARATOR '-'))
WHERE webform_submitted_data_copy.cid = "5" GROUP BY webform_submitted_data_copy.sid
But this affects 0 rows, and has no failures...tried many other possible statements with no joy.
???
Anyone know what I need to do in order to make this work?
It sounds like you don't really want an UPDATE statement. After you run an UPDATE, your table still has the same number of records as before; but it sounds like you want it to end up with one-third as many records as it started with. (Right?)
So I think your best bet is to create a temporary table; something like this:
CREATE TABLE webform_submitted_data_copy_temp
SELECT d.nid, d.sid, d.cid, GROUP_CONCAT( d.data ORDER BY `no` SEPARATOR '-') AS data FROM
webform_submitted_data_copy d WHERE cid = "5" GROUP BY d.sid;
UPDATE webform_submitted_data_copy
SET data =
( SELECT data
FROM webform_submitted_data_copy_temp
WHERE sid = webform_submitted_data_copy.sid
)
WHERE cid = '5'
AND no = 0
;
DELETE webform_submitted_data_copy
WHERE cid = '5'
AND no > 0
;
DROP TABLE webform_submitted_data_copy_temp;