SELECT CASE
WHEN
CASE
WHEN `AA`>6
THEN `A`
ELSE NULL
END,
CASE
WHEN `BB`>6
THEN `B`
ELSE NULL
END,
CASE
WHEN `CC`>6
THEN `C`
ELSE NULL
END
THEN `NAME`
END
FROM TABLE;
The goal of this query is to show the columns A through C and Name. but if A-C is null to not show NAME.
I'm new to CASEs and 'advanced' queries in general, so if I have this set up like a fool, feel free to let me know.
Thanks for reading.
NOTE: doesn't compile
The logic I'm trying to get is so:
if(
if(AA>6) then 1 else 0
OR
if(BB>6) then 1 else 0
OR
if(CC>6) then 1 else 0
etc,etc
) then 1 else 0
Table:
Name A B C AA BB cc
--------------------------------------------------------
Name1 Data1 null Data3 3 n 15
Name2 null Data2 Data4 n 2 8
Name3 null Data2 Data4 n 2 2
Output wanted if it's only showing values gt 6:
Name A B C
-------------------------
Name1 Data3
Name2 Data4
One way to do it
SELECT Name,
CASE WHEN aa > 6 THEN a END a,
CASE WHEN bb > 6 THEN b END b,
CASE WHEN cc > 6 THEN c END c
FROM table1
WHERE aa > 6
OR bb > 6
OR cc > 6
or
SELECT Name,
CASE WHEN aa > 6 THEN a END a,
CASE WHEN bb > 6 THEN b END b,
CASE WHEN cc > 6 THEN c END c
FROM table1
HAVING COALESCE(a, b, c, -1) <> -1
In the second query -1 is just a constant that will be returned if all of your columns have NULL values. You can use any value that will never be a part of your result set. In this particular case since you're applying column > 6 condition to every column you can use any number that is less than 6.
Output:
| NAME | A | B | C |
-----------------------------------
| Name1 | (null) | (null) | Data3 |
| Name2 | (null) | (null) | Data4 |
Here is SQLFiddle demo
Related
I has Join table, and result is like this
IDA IDB QTY
A 3 1
A 4 1
A 5 1
B 3 1
B 4 1
C 3 2
D 3 2
E 3 1
F 4 1
G 3 1
G 4 1
G 5 1
H 3 3
H 4 3
H 5 3
i'm confused how to count the IDA who has same Condition of IDB and QTY together.
So what i want is:
Combination of IDB - QTY
(3-1, 4-1, 5-1) = 2 from IDA (A and G)
(3-1) = 1 from IDA (E)
(3-2) = 2 from IDA (C and D)
So basically i want count of IDA who has same IDB and QTY Combination. What Query should i do? i still don't get how making query for this logic, thank you.
Edit :
What i want is the result in one query, i will not making manually Where condition of IDB-QTY Combination. (so basically the result is not from Where condition)
You can use aggregating with using group_concat() function
select group_concat( ida order by ida ) as grouped_letters, count(*) as count
from
(
select ida, count( idb - qty ) as ct, sum( idb - qty ) as sm
from tab
group by ida
) q
group by ct, sm
order by ida;
+---------------+-----+
|grouped_letters|count|
+---------------+-----+
| B | 1 |
| C,D | 2 |
| E | 1 |
| F | 1 |
| A,G | 2 |
| H | 1 |
+---------------+-----+
Demo
I have to write an SQL query for the following:
Input:
Id | Name
-- | ----
1 | A
2 | B
3 | C
Output:
Id | Name | IdType
-- | ---- | -----
1 | A | O
2 | B | E
3 | C | O
I can extract odd or even rows by putting where condition Id % 2 = 0. But I'm not sure how to put 'O' or 'E' in the new column.
Like this, using the IF() function:
SELECT Id, Name, IF(Id % 2 = 0, 'E', 'O') AS IdType FROM table_name;
You make use of CASE and print E when Id%2=0 and O if Id%2 is not equal to 0
SELECT Id,Name,
CASE WHEN (Id%2)=0 THEN 'E'
ELSE 'O' END AS IdType
FROM [YourTable]
Use this query If you want to update your table
UPDATE table_name SET IdType = CASE
WHEN mod (Id, 2) = 0 THEN 'E'
WHEN mod (Id, 2) <> 0 THEN 'O'
ELSE IdType
END
If you just want to print,then Robby Cornelissen had a perfect answer for you
I have a table with the next structure
Table A:
| Id | 1 | 2 | 3 |
|-----|---------|----------|-----------|
| 1 | 00:05:00| 00:10:00 | (null) |
| 2 | 00:10:00| (null) | (null) |
Table B
| Id | col |Expected |
|-----|---------|----------|
| 1 | 1 | 00:06:00 |
| 2 | 2 | 00:12:00 |
| 3 | 3 | 00:22:00 |
I am trying to make a sum depending on the actual value on the rows of table A in comparison to the expected on table B
Select
Id, (Select ??????? From (Select TiempoStd from B)as Stime) as Time
From
A
Basically i want to make a comparison between the 2 tables to see which one is greater and add that to the next one.
I cant manage to understand how to call a specific value under my temp table Stime.
i am not that familiar with SQL so thats why i cant get this, the logic is something like this. in where the question marks are on the Query
ADDTIME(IF(A.1>(Stime.Expected where col = 1),A.1,(Stime.Expected where col = 1)),
ADDTIME(IF(A.2>(Stime.Expected where col = 2),A.2,(Stime.Expected where col = 2)),
IF(A.3>(Stime.Expected where col = 3),A.3,(Stime.Expected where col = 3))
Stime.Expected where col = 3 is a bad syntax right? but i hope you get the point of the logic im trying to make here.
so the output would be like this
| Id | Time |
|-----|---------|
| 1 | 00:40:00|
| 2 | 00:44:00|
Use a UNION to convert table A to a table with separate rows for each column, then join this with table B.
select *
FROM TableB AS b
JOIN (SELECT Id, 1 AS col, a.1 AS Time
FROM TableA as a
UNION
SELECT Id, 2 AS col, a.2 AS Time
FROM TableA AS a
UNION
SELECT Id, 3 AS col, a.3 AS Time
FROM TableA AS a) AS a
ON a.Id = b.Id AND a.col = b.col
This made the trick, i got help from some friends and got to it, gives the desired result.
SELECT
A.Id,
addtime(
addtime(
ifnull(Case when A.1 > E.Expected1 Then A.1 else E.expected1 end,'00:00:00') ,
ifnull(Case when A.2 > E.Expected2 Then A.2 else E.expected2 end,'00:00:00')
), ifnull(Case when A.3 > E.Expected3 Then A.3 else E.expected3 end,'00:00:00')
) as Time
From TableA A, (
SELECT
sum(case when col=1 Then Expected else 0 end) as Expected1,
sum(case when col=2 Then Expected else 0 end) as Expected2,
sum(case when col=3 Then Expected else 0 end) as Expected3
From TableB ) as E
Hi all i have one table which have two colums i want to show column 2 data in different column nothing is fix every thing is dynamic like column creation based on rows
table 1
id col1 col2
1 x aa
2 x bb
3 x cc
4 y ww
5 y ee
6 z hh
7 z tt
8 z uu
9 z pp
10 z oo
i want table1 data in this format
id val1 val2 val3 val4 val5 val6
1 x aa bb cc null null
2 y ww ee null null null
3 z hh tt uu pp oo
can any one please help me out
how can i do this in mysql
this result:
| ID | VAL1 | VAL2 | VAL3 | VAL4 | VAL5 | VAL6 |
|----|------|------|------|--------|--------|--------|
| 1 | x | aa | bb | cc | (null) | (null) |
| 4 | y | ww | ee | (null) | (null) | (null) |
| 6 | z | hh | tt | uu | pp | oo |
from this query:
SELECT
MIN(id) AS id
, col1 AS val1
, MAX(CASE WHEN colno = 1 THEN col2 END) AS val2
, MAX(CASE WHEN colno = 2 THEN col2 END) AS val3
, MAX(CASE WHEN colno = 3 THEN col2 END) AS val4
, MAX(CASE WHEN colno = 4 THEN col2 END) AS val5
, MAX(CASE WHEN colno = 5 THEN col2 END) AS val6
FROM (
SELECT
#row_num :=IF(#prev_value = t.col1, #row_num + 1,1) AS colno
, t.id
, t.col1
, t.col2
, #prev_value := t.col1 as pv
FROM Table1 t
CROSS JOIN(SELECT #row_num := 1 x, #prev_value :='' y) vars
ORDER BY t.col1, t.id ASC
) sq
GROUP BY
col1
ORDER BY
col1
;
But you should note that sites such as this are not places where we just cough up finished work for you for nothing. You are supposed to demonstrate what you have tried at the very least. Claiming urgency isn't helpful, those who provide answers also have work to do and this is entirely voluntary.
Please prepare your next question with some evidence of research and an attempted query.
Hi Friends i have small doubt in sql server how to handle target table unmatched records replace with NA.please tell mehow to solve this issue in sql server.
TableA:
id | Descr
1 |ab
2 |bc
3 |de
5 |
6 |jk
TableB:
id | Descr
1 |
2 |
4 |
5 |
Here i want update tableB table Descr From TableA Based on id columns
i tried like below
merge TableB dest
using TbleA stag
on dest.id=stag.id
when matched then
UPDATE
SET
dest.[id]= CASE WHEN coalesce(ltrim(rtrim(stag.[id])),'') = '' THEN 'NA' ELSE ltrim(rtrim(stag.[id])) END
,dest. [descr]= CASE WHEN coalesce(ltrim(rtrim(stag.[descr])),'') = '' THEN 'NA' ELSE ltrim(rtrim(stag.[descr])) END
;
i got out put like below
TableB:
id | Descr
1 | ab
2 | bc
4 |
5 | NA
But i want output like below
TableB:
id | Descr
1 | ab
2 | bc
4 | NA
5 | NA
Please tell me how to handle NA values in sql server
You could use a left join with the update statement and set descr to NA where it is NULL. Try this:
UPDATE dest
SET dest.descr =
CASE
WHEN coalesce(ltrim(rtrim(stag.[descr])),'') = '' THEN 'NA'
WHEN stag.[descr] IS NULL THEN 'NA'
ELSE ltrim(rtrim(stag.[descr]))
END
FROM TableB dest
LEFT JOIN TableA stag on dest.id = stag.id;
Here's an SQL Fiddle to show it.