Postgres Set Select Value In If - sql

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

Related

Checking existance through sqlite3 in xcode

I am using sqlite3 in swift files through the application xcode. I am currently working on a journal project where you log one journal entry a day, and I am storing the contents, date logged etc in a database. Right now I am trying to implement a way to check if a there is already a log existing for a given day. I am using the following query within sqlite3_prepare_v2
IF EXISTS
(
SELECT *
FROM journal
WHERE date = ?)
)
(I am using the ? to later bind a given date btw) How can I check if the evaluation of this query is true or false and set that value to a variable? Will sqlite3_step() return the return values of this query? Thanks!
You can do:
select exists (select 1 from journal where date = ?) as flag
This always gives you a scalar result (one row and one column, called flag), with a 0/1 value that indicates whether at least one record satisfies the condition.
Another option is:
select count(*) > 0 as res
from journal
where date = 1

How do I apply a COUNT() column (calculated for each row) without making my run in O(n^2)

I have a "clicks" table that records every time a user clicks on a link.
For the purpose of keeping this simple, let's just assume this table only has three columns "Email", "LinkId", and "ClickTime".
My issue is I need to add a calculated column to my SELECT query. This column will record a true or false value indicating whether the system considers the click valid or not.
An entry in the table will be considered invalid if there exist two or more entries in the table which share the same domain name (taken from the email address), "LinkId" and "ClickTime".
I am trying to extract every record in this table with an extra column titled "valid" that has a true or false value in it according to the specification above.
Below is my attempt but it's not working and I'm not sure where to go from here.
SELECT
RIGHT(cl1.Email, LEN(cl1.Email) - CHARINDEX('#', cl1.Email)) Domain,
IF (SELECT
COUNT (*)
FROM
clicks as cl2
WHERE
cl2.ClickTime = cl1.ClickTime AND
cl2.Email LIKE CONCAT(cl1.Domain,"#%") AND
cl2.LinkId = cl1.LinkId
) <= 3 (True) ELSE (False)
As Valid
FROM
clicks as cl1
WHERE
Would a simple GROUP BY provide what you want?
Here is a simple example to illustrate it:
with rte as
(
select email, linkid, clicktime, count(*) as num
from table1
group by email, linkid, clicktime
having count(*) > 2
)
select * from rte
Example code here: http://sqlfiddle.com/#!18/cdaab/2

A 228 row results query job writing to table with gives 0 rows when allow large results is True

I have a SQL query that, when I write the results to a table without 'allow large results' set, will write 228 rows.
When I set allow large results however, the destination table will contain 0 rows. Both attempts use write disposition WRITE_TRUNCATE.
I see this both using the API and the BigQuery console.
The working no-allow-large-results job:
eagTEiR0wSMK6b5WLSL04vB9RfTUb8bhvEi1YFWjuhfaF_W0zEeLogxUYwOrhGyOheS_CyyaB1dUeafGPdyR592xMcbeEmpJ85_CO29PSbBAnmEBGHJVHWjpH5DvGyVCEjarfJ5XUQ9UmVT_FSHmkcEZktbfln9E_E1jobM65IuQv2sP4_r7eqK60aPaqxD7taEc1bpM2kS6GAtkxqFsUUOv_JXQgTn3ebCodHFKsdquhy3e1mfbu4QhqnoO5QCi
The non-working allow-large-results job:
G40HW4Z5zGTgL1NSCBBy380kY7Gu7WOU7s_zB9F8Kdrtao2gbzRLptWSSi76MC2gHCHPG0srssaGejfCIN4j1upjyh9vQnA3kPmuJcgm5ZgdYd3YwsmGzvcBXiPy9bY0x0GRhJXimHqhKiYbKz7fa3LljOb4kxNvB8wPazqeYj3xAXwbV8G2Sl3L6gmutvvYPalhd1CCtUbLfiw520_I4zKDgn7LYosyFjA0h9TwR8GQ80Scd5n8yKAsIEou7XDG
Query:
SELECT t1.email, MIN(t1.min_created_time), GROUP_CONCAT(t1.id)
FROM (
SELECT email, MIN(created) as min_created_time, id
FROM TABLE_QUERY([xxxxx], 'table_id in ("yyyyyy_201601", "yyyyyy_201602", "yyyyyy _201603", "yyyyyy_201604")')
WHERE created >= "2016-01-11 00:00:00" AND created < "2016-04-01 00:00:00" AND id != "null" AND name LIKE "%trike%"
GROUP BY email, id
) t1
GROUP EACH BY t1.email
IGNORE CASE
Also note, a simpler SQL works for both cases such as:
select email from xxxx group by email limit 100
This looks like a problem due to IGNORE CASE. The fix is underway, but in the meantime could you wrap string comparisons with LOWER() calls, i.e.
LOWER(id) != "null"
LOWER(name) LIKE "%trike%"
etc.

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.

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