I am trying to make one table value function in Sql Server. Here I have 3 columns and i want to merge them into one column lets say "new". This new column should show the name of 3 columns if they have value as 1.For example for row one it should display Isprod, for row 2 it should display IsCompetProd and so on.
IsProd IsCompetProd IsOther
1 0 0
0 1 0
0 0 1
1 0 0
Is there any way to do that?
Try like this
SELECT CASE WHEN IsProd=1 THEN 'IsProd'
WHEN IsCompetProd=1 THEN 'IsCompetProd'
WHEN IsOther=1 THEN 'IsOther'
END [New]
FROM table1
If I am getting what you want. Use a case statement. Try this:
SELECT
tbl.*,
(
CASE
WHEN IsProd=1
THEN 'IsProd'
WHEN IsCompetProd=1
THEN 'IsCompetProd'
WHEN IsOther=1
THEN 'IsOther'
ELSE 'None'
END
)AS newColumn
FROM
tbl
Related
I've got some troubles about SQL request :
I have a table like this table data image
I would like to create a view from this table to get :
Time_A : SUM of a column (total_time_taken) WHERE column (is_radiant)=1
Time_B : SUM of the same column (total_time_taken) WHERE column (is_radiant)=0
Time_AB : SUM of the column (total_time_taken) WHERE column (is_radiant)=0 OR (is_radiant)=1
SELECT
SUM(`matchpickban_radiant`.`total_time_taken`) AS `draft_time_radiant`,
SUM(`matchpickban_dire`.`total_time_taken`) AS `draft_time_radiant`
FROM
(`matchpickban` AS `matchpickban_radiant`
JOIN `matchpickban` AS `matchpickban_dire` ON ((`matchpickban_dire`.`idmatchpickban` = `matchpickban_radiant`.`idmatchpickban`)))
WHERE
`matchpickban_radiant`.`is_radiant` = 1
AND `matchpickban_dire`.`is_radiant` = 0
Actually I can run this request without syntax error but the result is NULL cause no data can be equal to 0 AND equal to 1 in the same time, obviously...
Also, I don't know if it's possible to make a JOIN the table to itself as I did (matchpickban JOIN matchpickban).
If syntax is correct I need to place my WHERE CONDITION away but don't know how, is it possible to replace it with 2 IF statement (IF is_radiant=0 SUM(...))
Thx for reading and helping me about this issue I got !
If you need more info about table or request I will give you all you need !
No need for a self-join or complex logic, you can just use conditional aggregation, which consists in using conditional expression within aggregate functions.
In MySQL, you could go:
select
sum(is_radiant * total_time_taken) time_a,
sum((1 - is_radiant) * total_time_taken) time_b,
sum(total_time_taken) time_ab
from matchpickban
where is_radiant in (0, 1)
This works because is_radiant is made of 0/1 values only - so this simplifies the logic. A more canonical way to phrase the conditional sums would be:
sum(case when is_radiant = 1 then total_time_taken else 0 end) time_a,
sum(case when is_radiant = 0 then total_time_taken else 0 end) time_b,
How can I multiply all numbers in one column?
for example:
Status(finished or not finished)
1
1
1
1
0
I know how to use sum.
sum(Status)=4
I need some thing like sum for multiply
mul(status)=0
do we have something like mul(status)?
I don't know of a multiply aggregate function. However, in the case of a column containing only zeroes and ones the product will be one only if every value be one, otherwise it will be zero:
SELECT
CASE WHEN SUM(status) = COUNT(status) THEN 1 ELSE 0 END AS product
FROM yourTable
CASE WHEN SUM(status) = COUNT(status) THEN 1 ELSE 0 END AS product
What SQLite statement do I need to get the column name WHERE there is a value?
COLUMN NAME: ALPHA BRAVO CHARLIE DELTA ECHO
ROW VALUE: 0 1 0 1 1
All I want in my return is: Bravo, Delta, Echo.
Your request is not entirely clear, but you appear to be asking for a SELECT statement that will return not data but rather columns names, and not a predictable number of values but rather a number values that depend on the data in the table.
For instance,
A B C D E
0 1 0 1 1
would return (B,D,E) whereas
A B C D E
1 0 1 0 0
would return (A, C).
If that's what you're asking, this is not something that SQL does. SQL retrieves data from the table and an SQL result set always has the same number of columns per row.
To accomplish your goal, you would have to retrieve all columns that might have a value in the table and then, in your program code, check for the value in each column and accrue a list of column names that had values.
Also, consider what happens when there is more than one row to examine and the distribution of values differ. In other words, what's the expected result if the data looks like this:
A B C D E
- - - - -
0 1 0 1 1
1 0 1 0 0
[Also, note that all the columns in your example have values, some 0, some 1. What you really want is a list of column names where the column contains a value of 1.]
Finally, consider that your inability to easily get the results you need from your data might indicate a flaw in the data model you're using. For instance, if you were to structure your data like this:
TagName TagValue
------- --------
Alpha 0
Bravo 1
Charlie 0
Delta 1
Echo 1
you could then obtain your results with SELECT TagName FROM Tags WHERE TagValue = 1.
Furthermore, if 0 and 1 are really the only two possible values (indicating boolean "presence" or "absence" of the tag) then you could remove the TagValue column and the rows for Alpha and Charlie entirely (you'd INSERT a row into the table to add tag and DELETE a row to remove it).
A design along these lines seems to model your data more accurately and allows you to entire new tags to the system without having to issue an ALTER TABLE command.
http://sqlfiddle.com/#!9/1407e/1
SELECT CONCAT(IF(ALPHA,'ALPHA,',''),
IF(BRAVO,'BRAVO,',''),
IF(CHARLIE,'CHARLIE,',''),
IF(DELTA,'DELTA,',''),
IF(ECHO,'ECHO',''))
FROM table1
I have 3 rows in sql:
sno ids
1. 5,6,7
2. 6,8
3. 5,7,8
I would like to know how many users have max match of (5,6,7) in them for eg: ,Sno. 2 has 1 and Sno 3 has 2 and so on.Is there a query/way to do that or its not possible?
While I'd highly recommend normalizing your database (i.e. not storing comma delimited strings in a colum), here's one option using case and find_in_set:
select sno,
case when find_in_set(5, ids) then 1 else 0 end +
case when find_in_set(6, ids) then 1 else 0 end +
case when find_in_set(7, ids) then 1 else 0 end cnt
from yourtable
group by sno
SQL Fiddle Demo
SELECT a,b from <table_name> GROUP BY a,b,c.
Is the above a valid sql statement?
Not without a table name it is not. If it had a table name it would be valid, but probably not very useful.
Typically one would use GROUP BY clauses in conjunction with some aggregate function (SUM, COUNT, MAX, MIN, etc.) to derive some values related to the grouped fields.
Yes, that query is legal SQL. Whether it's useful SQL is another matter entirely.
The query
select a , b
from foo
group by a,b,c
Does the following:
groups the rows from the source table into distinct groups, 1 for each unique combination of columns a, b and c.
Each such group is then collapsed into a single row containing the grouping columns and the values of any required aggregate functions required by the query.
The resulting result set is then returned to the caller, tossing any unwanted columns (in this case, column c).
Since one of the grouping columns is discarded, the specified query is not guaranteed to be a set of unique rows. It might well contain duplicates. For instance, if group by came up with these groups to be returned:
A B C
- - -
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
The results set returned by the query would be
A B
- -
0 0
0 0
0 1
0 1
1 0
1 0
1 1
1 1
And so, not necessarily useful.
If you include a table name I guess you're asking if you can group by an unselected element c. Yes you can.
No - You need a table.
SELECT a,b FROM myTable GROUP BY a,b,c
where myTable is the table you are selecting from (which must have columns a,b and c).
No- because you didn't specify a table name. If you had, then yes, it's certainly valid for mysql, not sure about Oracle (which validates GROUPing very differently). But it doesn't make any sense...
a b c
0 0 0
0 0 1
0 1 0
0 1 1
applying the query to the table above will give:
0 0
0 0
0 1
0 1
And I don't see how that would be a meaningful result - if you don't want an aggregate value, then DISTINCT makes more sense - but gives a different result:
SELECT DISTINCT a, FROM `atable`
0 0
0 1
Maybe if you explained the real reason youwere asking the question we could make a more sensible attempt at answering it.