stored procedure sql case or if else displaying all records using single query - sql

ok i have
ID TITLE CONTENT DATEPUBLISH DATEEXPIRED PUBLISH as my content table
i want to display all records in the table based upon these conditions using one query.
i have datepublish as a textbox ,dateexpired as a textbox and publish as a checkbox.
1) if i enter datepublish the content will be publish without expiry.
2) if i enter datexpire the content will start publishing straight away and will expire when it reaches the expiry date
3) if i enter both dates then the content will start on the datepublish and will expire when dateexpired
the other thing is that
4) if i checked publish then it will bypass the dates.
how can i display all records using a single query?.( i am able to do it with cursor)

SELECT *
FROM Table
WHERE ((#datePublish IS NULL OR Table.datePublish > #datePublish)
AND (#dateExpired IS NULL OR Table.dateExpired < #dateExpired))
OR #published = 1
If either of your date values is empty, then you pass NULL to your stored procedure for that parameter (thus that parameter won't restrict the result set), and #published = 1 if the checkbox is checked.

Related

SQL For Each 'event' return Open/Close date/times

I have a table of events, some date/times, and revision numbers for each event. The number of revisions vary. Most events open, then things happen to cause revisions, then they close. Sometimes events open, things happen, the event closes, then it re-opens using the same event number, things happen . . .
There is no available table of events that tells me "at this time the event opened", "at this time the event was closed", "at this time the event was re-opened", etc. That would be too easy.
The same event can open and close multiple times.
I want to create a report where a user can pick some date/time parameters and see what events are active (open) at that moment, or between two date/times. Here is an example of both an event that opens and closes three times, and an event that only opens and closes once, and what I desire for an output. The results below are stored into a table variable called #CEvents.
It looks to me like the English version of the question is something like "If all of an event's ClosedDateTime values are NULL:
The Created or reopened DT = the RevDateTime value of the MIN(rev_num);
The Closed DT = the RevDateTime value of the MAX(rev_num);
If an event has one or more non-NULL ClosedDateTime values:
A Created or reopened DT = the RevDateTime value of the MIN(rev_num);
A Closed DT = the RevDateTime value for the row previous to the row where the
ClosedDateTime is not NULL;
A Created or reopened DT = the RevDateTime value for the row right after the row where
the ClosedDateTime is not NULL;
Repeat for each instance where there is a ClosedDateTime value;
Finish up with a Closed DT = the RevDateTime value of the MAX(rev_num);
I cannot figure out how to write the SQL for this question. Or, perhaps you have a better idea? Thanks in advance.
I solved this to my satisfaction by building a temp table containing the cloumns I wanted and updating the table with a series of INSERT queries.
Each event's first rev_num was given an INT 'Status' value of 0, meaning open
Since there was a ClosedDT field a 'Status' value of -1 (closed) was added for each row that had one.
To get the "I'm back open again" values for those events with more than one open/close pair I used a query that joined the table upon itself by the rev_num to the copy of the table's rev_num + 1, thereby retrieving the timestamp value of the row after the CloseDT, which is the re-opened datetime:
INSERT INTO #CEvents2
SELECT a.ag_id, a.dgroup, a.num_1, a.RevDT TimeStamp, Status = 0, RowNum = NULL, a.eid
FROM #CEvents a
LEFT JOIN #CEvents b on a.num_1 = b.Num_1 and a.rev_num = b.rev_num + 1
WHERE b.ClosedDT is NOT NULL
To get a final closed date/time I used a summary table that contains the
earliest and latest date/time for the event, choosing that one field that contained the latest date/time to insert as the timestamp.
I added one more field, a RowNum partitioning by the event number, ordering by the timestamp which resulted in a table that shows the chronology of each event and for each row a 0 (I'm open) and -1 (I'm closed).
It just remains for the report user to pick a set of dates to see the chronology, or to pick a time and see which events are in a 0 status at that very moment.
I opted to forgo putting the results into a side-by-side table showing a column for Open and one for Close. Instead each row has the open/close INT values of 0 or -1.

Displaying different table values depending on the value of another table/column

I'm quite new to SQL and generally can find my way with basic stuff, but this time I'm kind of stuck...
I'm trying to retrieve (and display) the different configurations of an online app that are stored in a MSSQL DB.
I have to look at the value of a certain table and column (let's call it "configName.id"), and depending on the value returned (negative, positive, NULL), I have to match it in different tables and display one of 3 options in the same column.
I tried using CASE, but I don't want to display 3 different columns; I want to query different tables, and display the results in a single column. The displayed column name should be "Configuration Name".
In basic algorithm language, this would be:
if (configName.id > 0)
-> Display the corresponding value from table "a"
if (configName.id < 0)
-> Invert the value returned to be positive, display the corresponding value from table "b"
else
-> Display "NULL" as results.
My challenge is that regardless of the results returned from any table, I must display it under my report column "Configuration Name".
Any help would be really appreciated, thanks!
It would be something like this
select coalesce(a.value, b.value) as value
from configname left join
a
on a.id = configname.id and configname.id > 0 left join
b
on b.id = abs(configname.id) and configname.id < 0;

SQL Server Report Builder - How to grey out a parameter with multiple sub-reports?

I have a main report which has three sub-reports. I am trying to grey out one of the parameter option when a certain report is selected. The reason I want to do that is because one of the sub report does not use this parameter.
Here's the code I am using now. The parameter displaying the query result is #device.
--When user select report 2, the parameter displays the device list from table2.
IF #selectReport = 2
BEGIN
SELECT DISTINCT type
FROM table2
END
--When user select report 3, the parameter displays the device list from table3.
IF #selectReport = 3
BEGIN
SELECT DISTINCT type
FROM table3
END
--When user select report 1. I want to grey out the parameter, but I could not do it.
--So I created the table contains NULL value.
--So, when the user select the report 1, the parameter will show only null value.
IF #selectReport = 1
BEGIN
SELECT DISTINCT type
FROM nullValueTable1
END
I want it to be grey out when the report 1 is selected instead of show NULL on drop down list. Any idea???
You can't grey out the parameter. Unfortunately you can't hide the parameter either as the hidden property only takes True and False. What you are doing now might be the best you can do. However, you could try looking at cascading parameters, maybe you might be able to provide a slightly more user friendly value like "None" to the dropdown.

Postgres Set Select Value In If

I want to set the value of a column in a select statement but only in a certain condition.
Something like the following:
SELECT
IF (MIN(mode) <> MAX(mode))
THEN
"Start Mode" = 'Multiple'
ELSE
"Method" = MIN(mode)
ENDIF
FROM sessions
--Where condition here
The statement above produces errors. It is possible to use IF statements in selects like this?
EDIT
I want to determine what the start mode was. It can be WEB or CALL. It is possible to have multiple start modes. In that case (MIN doesnt equal MAX), then I want to set the Start Mode Column to multiple.
I could use "mode" as the column name, this select will be in a larger sql script which will generate a large table that we will export to a .csv. We want the column names to be clear for the people using the csv.
Samples:
Userid Mode
1 WEB
1 CALL
2 WEB
2 WEB
csv
User Id Start Mode
1 Multiple
2 WEB
SELECT
User_id,
CASE WHEN (MIN(mode) <> MAX(mode))
THEN
'Multiple'
ELSE
MIN(mode)
END
as "Start Mode"
FROM sessions
--Where condition here
GROUP BY user_id

Showing specific data without filtering out query data

I need to build a form where one field (Unplanned Amount) will only populate with data if another field (status) equals a certain value ("not in workflow"). If the status equals anything else, Unplanned amount field would be blank.
The data is coming from three different tables:
Table 1) AccountNum
Table 2) DocNum, DocAmount, DocStatus
Table 3) CommitAmount
The value in CommitAmount will always equal DocAmount, but the value of DocAmount doesn't have to equal the value of the CommitAmount if it's "unplanned."
I tried putting the data into a query and using the following code on my form to no avail:
If Me.DocStatus = "Not in workflow" Then
Me.DocAmount = Null
Else
Me.DocAmount = [forms]![form2]![DocAmount]
End If
Does anyone know how to go about making a query-based form or report that allows what I've described above to take place? Or maybe this should not be done via a query?
Thanks!!
Put the IF statement into to data source for me.docamount
OR use a case statement in the query itself
select case docstatus when 'Not in workflow' then null else docamount end