I have a column , says name Student_name and its values lets say, A, B, C, D, E, F and so on..
Now i have to convert this column into row with each alias.
select A.counts from (
select count(b.ATTND_FLAG) as counts , b.ATTND_FLAG as ATTND_FLAG
from hr_emp_notifications a, v_emp_attendance b
where a.emp_id=b.emp_id
and a.emp_id=90327
and b.ATTND_FLAG is not null
group by b.ATTND_FLAG )A
my query showing one column which has multiple values in rows.
i have to convert these values into row.
I would use conditional aggregation:
select sum(case when ea.attnd_flag = 'A' then 1 else 0 end) as num_a,
sum(case when ea.attnd_flag = 'B' then 1 else 0 end) as num_b,
sum(case when ea.attnd_flag = 'C' then 1 else 0 end) as num_c,
sum(case when ea.attnd_flag = 'D' then 1 else 0 end) as num_d,
sum(case when ea.attnd_flag = 'E' then 1 else 0 end) as num_e,
sum(case when ea.attnd_flag = 'F' then 1 else 0 end) as num_f
from v_emp_attendance ea
where ea.emp_id = 90327;
If you want multiple employees, use group byea.emp_id`.
Notice that the join is not needed.
It seems you need a pivot clause here -
SELECT *
FROM (SELECT NAME, ATTND_FLAG
FROM v_emp_attendance)
PIVOT (COUNT(ATTND_FLAG)
FOR NAME IN ('A' AS A, 'B' AS B, 'C' AS C /* AND SO ON */)
)
Related
I need help How can I merge the column into a single column, here is my code, is this method is correct. I want to get the count of the selected row in the table for the columns.
SELECT
CAT_MGR,
SUM ( case when CAT_MGR = 'A' THEN 1 else 0 end ) AS DESIGN,
sum (case when CAT_MGR = 'b' THEN 1 else 0 END) AS DESIGN,
sum (case when CAT_MGR = 'c' THEN 1 else 0 END) AS DESIGN
from Table_A
GROUP BY
CAT_MGR
Can you guys help me I'm a beginner at SQL.
Thank you in advance
If you want just one row in the resultset, then remove the group by clause. Then, if you want to count the three cat mgr together, you can use in:
select
sum(case when cat_mgr = 'a' then 1 else 0 end ) as design_a,
sum(case when cat_mgr = 'b' then 1 else 0 end ) as design_b,
sum(case when cat_mgr = 'c' then 1 else 0 end ) as design_c,
sum(case when cat_mgr in ('a', 'b', 'c') then 1 else 0 end ) as design
from Table_a
You just need to make addion like below in order to get one column "Design"
SELECT
CAT_MGR,
SUM (case when CAT_MGR = 'A' THEN 1 else 0 end )
+ sum (case when CAT_MGR = 'b' THEN 1 else 0 END)
+ sum (case when CAT_MGR = 'c' THEN 1 else 0 END)
AS DESIGN
from TJD_CORE_CATPB_TB
GROUP BY
CAT_MGR
have column that has values of total and hours inside it but I need to split the group into two columns
for example
select
case
when A.TYPE = 'H'
then A.Value
end as "Hours",
Case
when A.TYPE != 'H'
then A.VALUE
end as "Total"
from a
what this is returning is 2 columns but doubling it not a lining values.
Hours Total
2 null
null 20
Your table would appear to have two rows. If you want one row in the result set you need aggregation:
select max(case when A.TYPE = 'H' then A.Value end) as Hours,
max(case when A.TYPE <> 'H' then A.VALUE end) as Total
from a;
Lets say I have the following table:
FKEY A B C D E F
'A' 1 0 1 0 1 0
'A' 0 1 1 1 0 0
Now i want to make a group by FKEY but I just want to know if the A-F columns has 1 in one, all or none of the grouped rows.. The resulton the above table would be:
FKEY A B C D E F
'A' S S A S S N
..where S is "some", A is "all" and N is "none".
What would be the best approach to make this query. I could so some nested queries, but isnt there a smarter way?
In my real life data, the 1's and 0's are actually DATETIME and NULL's
You can use case and aggregation:
select fkey,
(case when sum(a) = 0 then 'N'
when sum(a) = count(*) then 'A'
else 'S'
end) as a,
(case when sum(b) = 0 then 'N'
when sum(b) = count(*) then 'A'
else 'S'
end) as b,
. . .
from t
group by fkey;
The above assumes that the values are only 0 and 1. If that is the case, you can actually phrase this as:
(case when max(a) = 0 then 'N'
when min(a) = 1 then 'A'
else 'S'
end) as a,
You mentioned that your 0 and 1 are actually null or non null dates. Here's a modified version of Gordon's query that caters for that:
select fkey,
(case when count(datecol) = 0 then 'all dates are null'
when count(datecol) = count(*) then 'all dates are filled'
else 'some are null, some filled'
end) as a,
...
from t
group by fkey;
COUNT(null) is 0, COUNT('2001-01-01') is 1, COUNT(*) is the row count independent of any variable. Hence, if our count of the dates was 0, all must be null. If the count of the dates was equal to the count of the rows, then all must be filled with some value, otherwise it's a mix
I am trying to get a count on values that might appear in 3 different columns but only require the count of unique values. Microsoft SQL.
Eg. value X might appear in column A, B, or C or all 3 but need to make sure I only get a unique count of value X no matter what columns it comes under.
Thanks!
If you want to count each individual occurrence of X in any column A, B, or C, then the following should work:
SELECT
SUM(CASE WHEN A = 'X' THEN 1 ELSE 0 END) +
SUM(CASE WHEN B = 'X' THEN 1 ELSE 0 END) +
SUM(CASE WHEN C = 'X' THEN 1 ELSE 0 END)
FROM yourTable
Is this what you are looking for? This will Count only 1 occurrence per row regardless of how many columns it is found in.
SELECT
ID
,SUM(CASE WHEN ColA = 'X' OR ColB = 'X' OR ColC = 'X' THEN 1 ELSE 0 END) AS ValueCount
FROM
TABLENAME
GROUP BY
ID
I guess I should show it without the group by too because you don't specify a grouping.
SELECT
,SUM(CASE WHEN ColA = 'X' OR ColB = 'X' OR ColC = 'X' THEN 1 ELSE 0 END) AS ValueCount
FROM
TABLENAME
i stuck with this problem, please help!
Here is the problem:
I have a catalog and line detail table. Such as;
Line detail table: TRANSACTIONS_LINE_DETAIL catalog table: CATALOG
In TRANSACTIONS_LINE_DETAIL table: SQ_TRANSACTION_LINE_DETAIL_ID, RF_TRANSACTION_ID, CH_ITEM_CODE,.. columns are included.
In CATALOG table: CH_ITEM_CODE, CH_ITEM_NAME,.. columns are included. (CH_ITEM_CODE is unique)
TRANSACTIONS_LINE_DETAIL table and CATALOG table relates each other by their CH_ITEM_CODE columns.
So my problem is;
I wanna write a query that fetches me transaction ids (RF_TRANSACTION_ID) which has X,Y and Z item names together. (CH_ITEM_NAME).
Below code couldn't help me;
SELECT RF_TRANSACTION_ID
FROM TRANSACTIONS_LINE_DETAIL TLD, CATALOG CAT
WHERE TLD.CH_ITEM_CODE= CAT.CH_ITEM_CODE
AND CAT.CH_ITEM_NAME IN ('X', 'Y', 'Z')
GROUP BY RF_WO_ID
HAVING COUNT(1) = 3
The query should fetch me transaction ids that can have;
X, Y, Z
or
A, B, C, X, Y, Z
or
X, Y, Z, P
but NOT
X
or
X, Y
or
Z, Y, A, B
You need to move the condition to the having clause:
SELECT RF_TRANSACTION_ID
FROM TRANSACTIONS_LINE_DETAIL TLD join
CATALOG CAT
on TLD.CH_ITEM_CODE = CAT.CH_ITEM_CODE
where cat.ch_item_name in ('X', 'Y', 'Z')
GROUP BY RF_TRANSACTION_ID
HAVING sum(case when CAT.CH_ITEM_NAME = 'X' then 1 else 0 end) > 0 and
sum(case when CAT.CH_ITEM_NAME = 'Y' then 1 else 0 end) > 0 and
sum(case when CAT.CH_ITEM_NAME = 'Z' then 1 else 0 end) > 0;
This is an example of a "set-within-sets" query. I like doing these with the logic in the having clause, because it makes it more general.
This version includes a where clause, which (in your case) makes the query more efficient.
Each condition in the having clause is counting the row that match a particular condition.
To add Q, for instance, is quite easy (this condition is in your query but not in the sample results):
SELECT RF_TRANSACTION_ID
FROM TRANSACTIONS_LINE_DETAIL TLD join
CATALOG CAT
on TLD.CH_ITEM_CODE = CAT.CH_ITEM_CODE
GROUP BY RF_TRANSACTION_ID
HAVING sum(case when CAT.CH_ITEM_NAME = 'X' then 1 else 0 end) > 0 and
sum(case when CAT.CH_ITEM_NAME = 'Y' then 1 else 0 end) > 0 and
sum(case when CAT.CH_ITEM_NAME = 'Z' then 1 else 0 end) > 0 and
sum(case when CAT.CH_ITEM_NAME = 'Q' then 1 else 0 end) > 0;
If you wanted to find transactions with X, Y, Z, but not Q, the last condition would be = 0.