I have a SQL statement that is combining two tables, but I've recently been asked to add case conditions. The conditions are working but the problem I'm running into is that each condition creates a duplicate column.
case when s.Department = 'Aero' then '(OA)' else '' end as Blah,
case when s.Department = 'Terrent' then '(OT)' else '' end as Blah,
case when s.Department = 'Vertigo' then '(OMG)' else '' end as Blah
This causes me to end up with
a| b | c | d | Blah | Blah | Blah|
| | | | (OT) | (OA) | (OT)|
| | | | (OT) | | |
| | | | (OT) | (OA) | |
| | | | (OT) | (OA) | (OT)|
| | | | | | (OT)|
How can I use the "case" cmd and have all the results if applicable show up under 1 column?
a| b | c | d | Blah |
| | | | (OT) |
| | | | (OT) |
| | | | (OT) |
| | | | (OT) |
| | | | (OA) |
| | | | (OA) |
| | | | (OA) |
| | | | (OT) |
| | | | (OT) |
| | | | (OT) |
You would use one case statement instead of three:
(case when s.Department = 'Aero' then '(OA)'
when s.Department = 'Terrent' then '(OT)'
when s.Department = 'Vertigo' then '(OMG)'
else ''
end) as Blah
Related
I have 3 records in pdatatest1:
| ID1 | Name1 | Class1 |
|------|-----------|--------|
| 1 | Methushal | 11 |
| 2 | Bush | 11 |
| 3 | Paul | 11 |
Datelist table is ptable:
| iDate | INAM |
|------------|------|
| 01-06-2022 | AM |
| 02-06-2022 | AM |
| 03-06-2022 | AM |
| 04-06-2022 | AM |
| 05-06-2022 | AM |
| 06-06-2022 | AM |
I inserted values are record1:
| ID | Name | Class | Indate | Intime | INAM1 |
|------|-----------|-------|------------|----------|-------|
| 1 | Methushal | 11 | 01-06-2022 | 08:00:00 | P |
| 1 | Methushal | 11 | 02-06-2022 | 08:00:00 | A |
| 1 | Methushal | 11 | 03-06-2022 | 08:00:00 | A |
| 1 | Methushal | 11 | 04-06-2022 | 08:00:00 | P |
| 1 | Methushal | 11 | 05-06-2022 | 08:00:00 | A |
My SQL Code giving result as below:
| dates12 | Name1 | INAM | INAM1 |
|------------|-----------|------|-------|
| 01-06-2022 | Methushal | AM | P |
| 01-06-2022 | Bush | AM | P |
| 01-06-2022 | Paul | AM | P |
| 02-06-2022 | Methushal | AM | A |
| 02-06-2022 | Bush | AM | A |
| 02-06-2022 | Paul | AM | A |
| 03-06-2022 | Methushal | AM | A |
| 03-06-2022 | Bush | AM | A |
| 03-06-2022 | Paul | AM | A |
| 04-06-2022 | Methushal | AM | P |
| 04-06-2022 | Bush | AM | P |
| 04-06-2022 | Paul | AM | P |
| 05-06-2022 | Methushal | AM | A |
| 05-06-2022 | Bush | AM | A |
| 05-06-2022 | Paul | AM | A |
| 06-06-2022 | Methushal | AM | NULL |
| 06-06-2022 | Bush | AM | NULL |
| 06-06-2022 | Paul | AM | NULL |
Looking for the result like below:
| dates12 | Name1 | INAM | INAM1 |
|------------|-----------|------|-------|
| 01-06-2022 | Methushal | AM | P |
| 01-06-2022 | Bush | AM | NULL |
| 01-06-2022 | Paul | AM | NULL |
| 02-06-2022 | Methushal | AM | A |
| 02-06-2022 | Bush | AM | NULL |
| 02-06-2022 | Paul | AM | NULL |
| 03-06-2022 | Methushal | AM | A |
| 03-06-2022 | Bush | AM | NULL |
| 03-06-2022 | Paul | AM | NULL |
| 04-06-2022 | Methushal | AM | P |
| 04-06-2022 | Bush | AM | NULL |
| 04-06-2022 | Paul | AM | NULL |
| 05-06-2022 | Methushal | AM | A |
| 05-06-2022 | Bush | AM | NULL |
| 05-06-2022 | Paul | AM | NULL |
| 06-06-2022 | Methushal | AM | NULL |
| 06-06-2022 | Bush | AM | NULL |
| 06-06-2022 | Paul | AM | NULL |
My sql code is:
Select Case when ptable.iDate Is null
then record1.Indate else ptable.idate end as dates12,
pdatatest1.Name1, ptable.INAM, record1.INAM1
into project1
From ptable full Join record1
on ptable.iDate=record1.Indate,pdatatest1
Select * From project1
And I have another code getting error in sql query I didn't get any result.
SELECT iDate dates12,
Name1,
INAM,
(SELECT INAM1
FROM record1
WHERE record1.ID = pdatatest1.ID1
) INAM1
FROM pdatatest1
JOIN ptable
ORDER BY iDate
I'm working this in vb.net table SQL Query. Please someone have a look once to correct my code to get correct result which I'm looking for.
You can use a cross join and a left join. For example:
select a.idate, b.name, a.inam, c.inam1
from ptable a
cross join pdatatest1 b
left join record1 c on c.id = b.id and c.indate = a.idate
I am trying to find out all the member ids who have more than 2 records on a different dates in the same class.
+----------+------------+-------+-----------+
| MemberId | Date | Class | |
+----------+------------+-------+-----------+
| 118111 | 2/18/2020 | A | Valid |
| 118111 | 10/15/2020 | A | Valid |
| 118216 | 1/31/2020 | B | Valid |
| 118216 | 5/16/1981 | B | Valid |
| 118291 | 6/9/2020 | A | Valid |
| 118291 | 12/5/2020 | A | Valid |
| 118533 | 4/9/2020 | A | Not valid |
| 118533 | 11/11/2020 | B | Not valid |
| 118533 | 7/22/2020 | C | Valid |
| 118533 | 10/25/2020 | C | Valid |
| 118293 | 3/30/2020 | A | Not valid |
| 118293 | 3/30/2020 | A | Not valid |
| 118499 | 4/16/2020 | B | Valid |
| 118499 | 7/26/2020 | B | Valid |
| 118499 | 3/25/2020 | A | Not valid |
+----------+------------+-------+-----------+
I have made a query which checks only 2 records but unable to find a solution for checking more than 2 records.
select mc.*
FROM table1 AS mc
JOIN table1 AS ma ON ma.memberid = mc.memberid
AND ma.date != mc.date
AND ma.class = mc.class
Assuming you just want the memberid you can us a HAVING:
SELECT memberid
FROM dbo.YourTable
GROUP BY memberid
HAVING COUNT(DISTINCT [Date]) > 2;
I am using following query to count how many Bill_date each BAN have
select replace(c.usertoken, '-', '') as BAN
, to_char(to_date(bi.name,'YYYY-MM-DD'),'dd-mm-yy') as Billdate_dmy
, (replace(c.usertoken, '-', '') ||':'|| to_char(to_date(bi.name,'YYYY-MM-DD'),'dd-mm-yy')) as BAN_Billdate_dmy
, count(c.usertoken) as Number_Of_Bills
from customer c
, service s
, document d
, bill bi
, batch ba
, billrun br
where c.ID = s.CUSTOMER_SERVICE_ID
and s.ID = d.SERVICE_DOCUMENT_ID
and bi.ID = d.BILL_DOCUMENT_ID
and d.BATCH = ba.ID
and ba.BILLRUN = br.ID
and br.STATUS = 'APPROVED'
and c.brand='rogers'
and d.VERSIONEDCONTENTFOLDER='cbu'
group by c.usertoken, bi.name
order by c.usertoken
Output of the above query
+-----------+----------+--------------------+--------------+--+-------+
| BAN | Bill_date | BAN_Billdate | Count |
+-----------+----------+--------------------+--------------+--+-------+
| 100001247 | 25-09-19 | 100001247:25-09-19 | 1 | | |
| 100001247 | 25-10-19 | 100001247:25-10-19 | 1 | | |
| 100002583 | 15-10-19 | 100002583:15-10-19 | 1 | | |
| 100004753 | 25-09-19 | 100004753:25-09-19 | 1 | | |
| 100004753 | 25-10-19 | 100004753:25-10-19 | 1 | | |
| 100005719 | 25-09-19 | 100005719:25-09-19 | 1 | | |
| 100005719 | 25-10-19 | 100005719:25-10-19 | 1 | | |
| 100006311 | 06-09-19 | 100006311:06-09-19 | 1 | | |
| 100009596 | 25-09-19 | 100009596:25-09-19 | 1 | | |
| 100009596 | 25-10-19 | 100009596:25-10-19 | 1 | | |
+-----------+----------+--------------------+--------------+--+-------+
However I was expecting the following output
+-----------+----------+--------------------+--------------+--+-------+
| BAN | Billdate | BAN_Billdate | | Count |
+-----------+----------+--------------------+--------------+--+-------+
| 100001247 | 25-09-19 | 100001247:25-09-19 | 2 | | |
| 100001247 | 25-10-19 | 100001247:25-10-19 | 2 | | |
| 100002583 | 15-10-19 | 100002583:15-10-19 | 3 | | |
| 100004753 | 25-09-19 | 100004753:25-09-19 | 3 | | |
| 100004753 | 25-10-19 | 100004753:25-10-19 | 3 | | |
| 100005719 | 25-09-19 | 100005719:25-09-19 | 2 | | |
| 100005719 | 25-10-19 | 100005719:25-10-19 | 2 | | |
| 100006311 | 06-09-19 | 100006311:06-09-19 | 1 | | |
| 100009596 | 25-09-19 | 100009596:25-09-19 | 2 | | |
| 100009596 | 25-10-19 | 100009596:25-10-19 | 2 | | |
+-----------+----------+--------------------+--------------+--+-------+
Please advise what changes should I do in the query to have the count column reflecting the expected values.
I don't want to touch your query and the archaic join syntax. Please learn proper SQL grammar with JOIN and ON clauses for joins.
That said, you seem to want a window function to sum the counts:
select sum(count(*)) over (partition by ban, to_date(bi.name, 'YYYY-MM-DD'))
I'm not sure that aggregation is really useful, if you are only getting one row per group. In that case, you might want to remove the group by and use:
select count(*) over (partition by ban, to_date(bi.name, 'YYYY-MM-DD'))
I am new to this forum. Hope fully I will be able to contribute and get my queries resolved too.
I am stuck at this that I do not know where to start off.
I have below data set.
| Start Step| End 1 | End 2 |
| 1001866 | 1001867 | NULL |
| 1001866 | 1001868 | NULL |
| 1001868 | 1001873 | NULL |
| 1001873 | 1001868 | NULL |
| 1001868 | 1005206 | NULL |
| 1001873 | 1001867 | NULL |
| 1005206 | 1001873 | NULL |
| 1005206 | 1005385 | 1005386 |
| 1005206 | 1005377 | 1005378 |
| 1005378 | 1005376 | 1005206 |
| 1005379 | 1005376 | 1005206 |
| 1005379 | 1005380 | 1005381 |
| 1005381 | 1005382 | 1001869 |
| 1005381 | 1005383 | NULL |
| 1005381 | 1005384 | 1001872 |
| 1005378 | 1005379 | NULL |
| 1005383 | 1001872 | NULL |
| 1005383 | 1005376 | 1005206 |
| 1005383 | 1005381 | NULL |
| 1001869 | 1001871 | NULL |
| 1005386 | 1005376 | 1005206 |
I want each step to be in single row with their corresponding end1 and end2 and ordered by step and ranked. I want the output to be as in the image:
| Rank | Start | End Step 1 | End Step 2 |
| 1 | 1001866 | 1001867 | NULL |
| 1 | 1001866 | 1001868 | NULL |
| 2 | 1001867 | NULL | NULL |
| 3 | 1001868 | 1001873 | NULL |
| 3 | 1001868 | 1005206 | NULL |
| 4 | 1001869 | NULL | NULL |
| 4 | 1001869 | 1001871 | NULL |
| 5 | 1001871 | NULL | NULL |
| 6 | 1001872 | NULL | NULL |
| 7 | 1001873 | 1001868 | NULL |
| 7 | 1001873 | 1001867 | NULL |
| 8 | 1005206 | 1001873 | NULL |
| 8 | 1005206 | 1005385 | 1005386 |
| 8 | 1005206 | 1005377 | 1005378 |
| 9 | 1005376 | NULL | NULL |
| 10 | 1005377 | NULL | NULL |
| 11 | 1005378 | 1005379 | NULL |
| 11 | 1005378 | 1005376 | 1005206 |
| 12 | 1005379 | 1005376 | 1005206 |
| 12 | 1005379 | 1005380 | 1005381 |
| 13 | 1005380 | NULL | NULL |
| 14 | 1005381 | 1005382 | 1001869 |
| 14 | 1005381 | 1005383 | NULL |
| 14 | 1005381 | 1005384 | 1001872 |
| 15 | 1005382 | NULL | NULL |
| 16 | 1005383 | 1001872 | NULL |
| 16 | 1005383 | 1005376 | 1005206 |
| 16 | 1005383 | 1005381 | NULL |
| 17 | 1005384 | NULL | NULL |
| 18 | 1005385 | NULL | NULL |
| 19 | 1005386 | 1005376 | 1005206 |
| 19 | 1005386 | 1005387 | NULL |
| 20 | 1005387 | NULL | NULL |
Just highlighted few values for better understanding.
Is it possible ?
Can any one please help ?
select dense_rank() over(order by [start step]) [rank], * from
(select * from yourtable
union
select distinct [end 1], null, null from yourtable where [end 1] is not null
union
select distinct [end 2], null, null from yourtable where [end 2] is not null
)a order by [start step]
I have a table test
+----+--+------+--+--+--------------+--+--------------+
| ID | | Name1 | | | Name2 |
+----+--+------+--+--+--------------+--+--------------+
| 1 | | Andy | | | NULL |
| 2 | | Kevin | | | NULL |
| 3 | | Phil | | | NULL |
| 4 | | Maria | | | NULL |
| 5 | | Jackson | | | NULL |
+----+--+------+--+--+----------+--+--
I am expecting output like
+----+--+------+--+--+----------+--
| ID | | Name1 | | | Name2 |
+----+--+------+--+--+----------+--
| 1 | | NULL | | | Andy |
| 2 | | NULL | | | Kevin |
| 3 | | NULL | | | Phil |
| 4 | | NULL | | | Maria |
| 5 | | NULL | | | Jackson |
+----+--+------+--+--+----------+--
I unfortunately inserted data in wrong column and now I want to shift the data to the next column.
You can use an UPDATE statement with no WHERE condition, to cover the entire table.
UPDATE test
SET Name2 = Name1,
Name1 = NULL