Separate column based on condition in same table in SQL - sql

I'm having a result set from a table in which I have to split a specific value and display in another column(user defined) based on a condition.
//Query
SELECT wtcCommentId, wtcTransactionId, wtcTaskId, wtcUserLogin,
wtcCommentDateTime, wtcCommentText
FROM dbo.tblWorkflowTransactionComments (NOLOCK)
WHERE wtcTransactionId = 141248
The output for the above query will be some 10 rows.
In the above query I need to separate the wtcCommentText column if the wtcTaskId for that result is 0. So if the wtcTaskId is 0 the wtcCommenttext value should be displayed in a separate column(transcommenttext) and for others it should be displaying in the same column(wtcCommentText). So that I can make a split between the comments based on the taskId.
How can I achieve this?

I'm not sure it really makes sense, but okay, I'm game for a laugh:
SELECT wtcCommentId, wtcTransactionId, wtcTaskId, wtcUserLogin,
wtcCommentDateTime,
CASE WHEN wtcTaskId!=0 THEN wtcCommentText END as wtcCommentText,
CASE WHEN wtcTaskId=0 THEN wtcCommentText END as transcommenttext
FROM dbo.tblWorkflowTransactionComments (NOLOCK)
WHERE wtcTransactionId = 141248
It seems like the kind of trivial reformatting that would be better done in a front end or report generator though.

Related

Combining multiple Case statements into single output column

I need to re-categorise a column marketing_channel with 10 unique values into 15 distinct groups by matching certain criteria.
I've done this via case statements but then the output is in 15 new columns.
Is there a more elegant way to re-class the marketing_channel by simply adding 1 extra column like "marketing_sub_channel" that contains all new 15 classes?
Is there better way to do the classification than by creating 15 case statements? Was thinking a with clause, but that would also be quite lengthy
Output looks like this but ultimately just a single added column would be great:
Yes you just have to change the format a bit. Remove the "case" statement at the beginning of each line and just put the "End" at the end of the statement, like so :
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE null
END as marketing_sub_channel
or in your case:
CASE
WHEN medium like ('%affiliate%') or marketing_cannel ='Affiliates' then 'Affiliate'
WHEN campaign like ('%_Display brand_global Progromatic Display%') then 'Dispay'
WHEN campaign like ('%display%') and campaign not like ('progrommatic') then 'Dispay'
....
else null
END as marketing_sub_channel
Also I would like to note that in your case statement since you have '%display%' and '%_Display brand_global Progromatic Display%' that you place the longer more specific one on top so it can trigger if it needs to. If '%display%' is on top then it will always trigger first since it contains a substring of the other one.

How to run a sql query multiple times and combine the results into single output?

I have a list of 2500 obj numbers stored in Excel for which I need to run the below SQL:
SELECT
a.objno,
a.table_comment,
b.queue_comment
FROM
aq$_queue_tables a
JOIN
AQ$_QUEUES b ON a.objno = b.table_objno
WHERE
a.objno = 19551;
Is there any way I can write a loop on above SQL with objno feeding from a list or from a different table? I also want to store/produce all the results from each loop run as a single output.
I considered the option to upload the numbers into a new table and add a where condition:
a.objno=(SELECT newtab.objectno FROM newtab);
However, the logic I'll be writing in the query would exclude certain objectno results. Let's say that the associated objectno has certain queue_comment as of certain date associated with that objectno. I do not want to pull that record. This condition would match with some objectno and wouldn't match with others. Having that condition and running the query against all the objectno is returning 0 results. I couldn't share the original logic as it would reveal certain business rules and it'll be a violation of some policy.
So, I need to run the query on each objectno separately and combine the results.
I'm totally new to SQL and got this task assigned. I'm aware of the regular loop, for in SQL, but I don't think I can apply them in this situation.
Any guidance or reference links to helpful topics is much appreciated as well.
Thanks in advance for the help.
One option is to upload the object numbers from Excel sheet to a table in the database and run the query as following. Assuming newtab is the table where the objectno are uploaded.
SELECT
a.objno,
a.table_comment,
b.queue_comment
FROM
aq$_queue_tables a JOIN AQ$_QUEUES b on a.objno = b.table_objno
WHERE
a.objno IN (SELECT newtab.objectno FROM newtab);
I have used a subquery here, join to the aq$ can work as well.
Reading the comments and all I think you need to enhance your Excel with 2 additional columns and load to a new table.
IN can be used in the following way too:
SELECT
a.objno,
a.table_comment,
b.queue_comment
FROM
aq$_queue_tables a
JOIN
AQ$_QUEUES b ON a.objno = b.table_objno
WHERE
(a.objno,a.table_comment,b.queue_comment) IN (19551,'something','something');
so with the new table will be:
WHERE
(a.objno,a.table_comment,b.queue_comment) IN
(select n.objno, n.table_comment, n.queue_comment from new_table n)

SQL - LEFT JOIN and WHERE statement to show just first row

I read many threads but didn't get the right solution to my problem. It's comparable to this Thread
I have a query, which gathers data and writes it per shell script into a csv file:
SELECT
'"Dose History ID"' = d.dhs_id,
'"TxFieldPoint ID"' = tp.tfp_id,
'"TxFieldPointHistory ID"' = tph.tph_id,
...
FROM txfield t
LEFT JOIN txfielpoint tp ON t.fld_id = tp.fld_id
LEFT JOIN txfieldpoint_hst tph ON fh.fhs_id = tph.fhs_id
...
WHERE d.dhs_id NOT IN ('1000', '10000')
AND ...
ORDER BY d.datetime,...;
This is based on an very big database with lots of tables and machine values. I picked my columns of interest and linked them by their built-in table IDs. Now I have to reduce my result where I get many rows with same values and just the IDs are changed. I just need one(first) row of "tph.tph_id" with the mechanics like
WHERE "Rownumber" is 1
or something like this. So far i couldn't implement a proper subquery or use the ROW_NUMBER() SQL function. Your help would be very appreciated. The Result looks like this and, based on the last ID, I just need one row for every og this numbers (all IDs are not strictly consecutive).
A01";261511;2843119;714255;3634457;
A01";261511;2843113;714256;3634457;
A01";261511;2843113;714257;3634457;
A02";261512;2843120;714258;3634464;
A02";261512;2843114;714259;3634464;
....
I think "GROUP BY" may suit your needs.
You can group rows with the same values for a set of columns into a single row

How to use aggregate function to filter a dataset in ssrs 2008

I have a matrix in ssrs2008 like below:
GroupName Zone CompletedVolume
Cancer 1 7
Tunnel 1 10
Surgery 1 64
ComplatedVolume value is coming by a specific expression <<expr>>, which is equal to: [Max(CVolume)]
This matrix is filled by a stored procedure that I am not supposed to change if possible. What I need to do is that not to show the data whose CompletedVolume is <= 50. I tried to go to tablix properties and add a filter like [Max(Q9Volume)] >= 50, but when I try to run the report it says that aggregate functions cannot be used in dataset filters or data region filters. How can I fix this as easy as possible?
Note that adding a where clause in sql query would not solve this issue since there are many other tables use the same SP and they need the data where CompletedVolume <= 50. Any help would be appreciated.
EDIT: I am trying to have the max(Q9Volume) value on SP, but something happening I have never seen before. The query is like:
Select r.* from (select * from results1 union select * from results2) r
left outer join procedures p on r.pid = p.id
The interesting this is there are some columns I see that does not included by neither results1/results2 nor procedures tables when I run the query. For example, there is no column like Q9Volume in the tables (result1, result2 and procedures), however when I run the query I see the columns on the output! How is that possible?
You can set the Row hidden property to True when [Max(CVolume)] is less or equal than 50.
Select the row and go to Row Visibility
Select Show or Hide based on an expression option and use this expression:
=IIF(
Max(Fields!Q9Volume.Value)<=50,
True,False
)
It will show something like this:
Note maximum value for Cancer and Tunnel are 7 and 10 respectively, so
they will be hidden if you apply the above expression.
Let me know if this helps.

SQL case statement in Teradata

I'm trying to write a simple IF/ELSE statement in Teradata. As far as I understand you have to use a CASE. What I want to do is write a statement that will check if a column IS NOT NULL and display something. ELSE if it IS NULL, display something else.
Every example I found simply replaces a single value with a hard coded string or int. I'm looking for something more along the lines of this which uses the THEN statement to make another SELECT:
SELECT CARS.VIN_NUM, CARS.DRIVER_NAMES
CASE
WHEN CARS.FUEL IS NOT NULL THEN SELECT CARS.DESTINATIONS
WHEN CARS.FUEL IS NULL THEN SELECT CARS.GAS_STATIONS
END
FROM AUTOMOBILES CARS
WHERE CARS.VIN_NUM IN
('345353',
'354632',
'535231')
ORDER BY CARS.VIN_NUM
The end result should be a table displaying the VIN_NUM, DRIVER_NAMES, DESTINATIONS OR GAS_STATIONS based on the CASE. Is something like this possible or am I going about it the wrong way?
If DESTINATIONS and GAS_STATIONS are columns in the table AUTOMOBILES (aliased CARS) then the following should work:
SELECT CARS.VIN_NUM,
CARS.DRIVER_NAMES,
CASE
WHEN CARS.FUEL IS NOT NULL THEN CARS.DESTINATIONS
ELSE CARS.GAS_STATIONS
END
FROM AUTOMOBILES CARS
WHERE CARS.VIN_NUM IN
('345353',
'354632',
'535231')
ORDER BY CARS.VIN_NUM;