Is there an easy way to do an IN statement or something similar in a SSRS formula for an iff statement comparison without having to repeat a switch 16 times?
This is what I am looking to accomplish without having to write a statement or a separate OR ie(Fields!FieldName.Value = "01" OR Fields!FieldName.Value = "02"...etc) for each value:
=iif((Fields!FieldName.Value IN
("01","02","03","06","08","09","10","12","15","19"),"TypeA",
iff((Fields!ProdCode.Value IN ("04","07","11","13","14","16"),
"TypeB","TypeC")
Or this:
=switch(
Instr( "," & "01,02,03,06,08,09,10,12,15,19" & ",", "," & Fields!FieldName.Value & ",") > 0 ,"TypeA",
Instr( "," & "04,07,11,13,14,16" & ",", "," & Fields!ProdCode.Value & ",") > 0 ,"TypeB",
True, "TypeC")
I have mocked up an example of joining to a table that is generated in the sql. This is more efficient than doing it in the report
SELECT a,b,c,d, etc
, Lookup.FieldName, Lookup.TypeLabel
FROM Tables INNER JOIN
(SELECT '01' AS FieldName, "TypeA" as TypeLabel
UNION ALL
SELECT '02' , "TypeA"
UNION ALL
SELECT '03' , "TypeA"
UNION ALL
SELECT '04' , "TypeB"
UNION ALL
...
SELECT '18' , "TypeC"
UNION ALL
SELECT '19' , "TypeC") LookUp
ON Tables.FieldName = Lookup.FieldName
Try this:
=Switch(
Array.IndexOf(Split("01,02,03,06,08,09,10,12,15,19",","), Fields!FieldName.Value)>-1,"TypeA",
Array.IndexOf(Split("04,07,11,13,14,16",","),Fields!FieldName.Value)>-1,"TypeB",
true,"TypeC"
)
Let me know if this helps.
Related
I have two tables and I would like to have an sql query to get the minimum value of a field of the union of the maximums of the tables.
Set tmpRS = CurrentDb.OpenRecordset(" SELECT MIN(s.TorqueMax) FROM ( " & _
" (SELECT MAX(ds.TorqueMax) FROM tblUIOpTorqueRangeDS ds)" & _
" UNION " & _
" (SELECT MAX(cv.TorqueMax) FROM tblUIOpTorqueRangeCV cv )) as s")
You can do something like this:
SELECT MIN(s.TorqueMax)
FROM (SELECT MAX(ds.TorqueMax) as TorqueMax
FROM tblUIOpTorqueRangeDS ds
UNION
SELECT MAX(cv.TorqueMax) as TorqueMax
FROM tblUIOpTorqueRangeCV cv
) as s;
I need to insert multiple rows and fields in Access 2016 with SQL VBA. I'm doing this thing below -with union -but it doesn't work, it says my sintax with union is wrong. I need to figure out what's wrong otherwise run queries one by one and I have 9 like this!
(EDIT: solution found!)
query = "INSERT INTO tblInvoice2 ( Quantity, Price, MMDCarrier) SELECT qryTotals.JRN AS SumOfJRN, qryTotals.[Rate JRN],"
query = query & "qryTotals.MMDCarrier from qryTotals where qryTotals.MMDCarrier like " & Me.Combo0 & " union "
query = query & "SELECT qryTotals.MG AS SumOfMG, qryTotals.[Rate MG],"
query = query & "qryTotals.MMDCarrier from qryTotals where qryTotals.MMDCarrier like " & Me.Combo0 & ""
Here is the answer of what worked, very similar from a code someone showed me below but a differences that changed everything (no error messages and it does insert in the table!). I had to put the selects into a select, still gave me an error and for some reason, when I added the -> as Price and as Quantity it worked. I'm still not sure why it works now, let me know if you have an explanation!
query = "INSERT INTO tblInvoice2 ( Quantity, Price, MMDCarrier ) SELECT * FROM (SELECT qryTotals.JRN as Quantity, qryTotals.[Rate JRN] as Price, qryTotals.MMDCarrier FROM qryTotals where qryTotals.MMDCarrier like '" & Me.Combo0 & "'"
query = query & " UNION SELECT qryTotals.MG , qryTotals.[Rate MG], qryTotals.MMDCarrier From qryTotals where qryTotals.MMDCarrier like '" & Me.Combo0 & "')"
If you're using INSERT INTO ... SELECT with unions, you need to put all unions in a subquery, e.g.:
INSERT INTO tblInvoice2 ( Quantity, Price, MMDCarrier)
SELECT * FROM (
SELECT qryTotals.JRN AS SumOfJRN, qryTotals.[Rate JRN], qryTotals.MMDCarrier from qryTotals
where qryTotals.MMDCarrier like 'something'
UNION
SELECT qryTotals.MG AS SumOfMG, qryTotals.[Rate MG],
qryTotals.MMDCarrier from qryTotals where qryTotals.MMDCarrier like 'somethingElse'
) As A
Or, in your VBA implementation:
query = "INSERT INTO tblInvoice2 ( Quantity, Price, MMDCarrier) SELECT * FROM (SELECT qryTotals.JRN AS SumOfJRN, qryTotals.[Rate JRN],"
query = query & "qryTotals.MMDCarrier from qryTotals where qryTotals.MMDCarrier like " & Me.Combo0 & " union "
query = query & "SELECT qryTotals.MG AS SumOfMG, qryTotals.[Rate MG],"
query = query & "qryTotals.MMDCarrier from qryTotals where qryTotals.MMDCarrier like " & Me.Combo0 & ") As A"
Note that your query also contains a trailing comma in the INSERT INTO part, which isn't a syntax error in Access SQL, but is one in most other forms of SQL, and still is a bad practice. Also, LONG was probably right, you want those single quotes (but really, you should use parameters instead)
How to select all strings the containing the substring "n'th\product_table" from a column, in SQL using LIKE?
Example:-
1)There is a table1 with following data
column1
_ _
"from n'th\product_table of period"
"from n'th\product$table of period"
"for of n'th\groduct$table of people"
"to n'th\product_table of classes"
"change n'th\producttable of record"
"correct n'thproduct_table of value"
Can anyone give an SQL query that uses LIKE operator gives the result of all strings containing the substring "n'th\product_table"
Expected result
column1
_ _
"from n'th\product_table of period"
"to n'th\product_table of classes"
Example:
SELECT * FROM (
SELECT 'from n\'th\\product_table of period' AS c
UNION
SELECT 'from n\'th\\product$table of period'
) t
WHERE c LIKE '%n\'th\\\\product\_table%'
Result is
"from n'th\product_table of period"
You need to use an escape character to make the underscore mean underscore:
where col like '%n''th\\product\_table%' escape '\'
Note that the escape character needs to be used before the actual '\' in the string as well as the '_':
where col like '%n''th\\product\_table%' escape '\'
^ ^
This method uses REGEXP_LIKE(). You just have to escape the special characters ' and \ to make them literal:
SQL> with tbl(str) as (
select 'from n''th\product_table of period' from dual union
select 'from n''th\product$table of period' from dual union
select 'for of n''th\groduct$table of people' from dual union
select 'to n''th\product_table of classes' from dual union
select 'change n''th\producttable of record' from dual union
select 'correct n''thproduct_table of value' from dual
)
select str
from tbl
where regexp_like(str, '.*n''th\\product_table.*');
STR
-----------------------------------
from n'th\product_table of period
to n'th\product_table of classes
SQL>
I am trying to Count the distinct Number of UserID's in a table for each Weekday (e.g. 545 UserID's on Weekday 1 = Monday, 120 UserID's on Weekday 2 = Tuesday etc.). I am doing this in Access Visual Basic, but the syntax should be universal to SQL. Here is my VB Code:
sSQL = " SELECT Weekday(" & tablename & ".[DATE]) AS WEEKDAY, Count(DISTINCT " & tablename & ".[UserID]) AS User_COUNT"
sSQL = sSQL & " FROM " & tablename
sSQL = sSQL & " GROUP BY Weekday(" & tablename & ".[DATE])"
qdf.SQL = sSQL
The plain SQL Syntax should look like this (edited based on comments & test):
SELECT Weekday(tbl.[Date]) AS WEEKDAY, Count(DISTINCT tbl.[UserID]) AS User_COUNT
FROM tbl
GROUP BY Weekday(tbl.[Date])
..whereas [Date] is a field in tbl formatted as Datetime and [UserID] is a field formatted as Long (with duplicates).
When I try to run the command it tells me "Missing Operator in Query-Syntax.."
Is this a problem of my VB Code or is the SQL Syntax wrong?
MS Access database engine does not support COUNT(DISTINCT ...).
To workaroud it, please see this thread: SQL : how can i count distinct record in MS ACCESS where author suggests to solve issue by using subquery:
SELECT
user_id
, COUNT(*) AS count_distinct_clients
FROM
( SELECT DISTINCT
user_id,
client_id
FROM tbl_sActivity
) AS tmp
GROUP BY
user_id ;
Change the query code to your needs.
[EDIT]
SELECT
wday, COUNT(UserId) AS count_distinct_users
FROM
( SELECT DISTINCT WEEKDAY([Date]) AS wday, UserId
FROM tblName
) AS tmp
GROUP BY
wday;
This sql is working but the second substring, is the order num. For these we need to add a '0' at the end, to make it 8 pos now. Not sure how to do this in SQL.
SELECT
ALL SUBSTR(UANOTL,1,4) AS CODE1, SUBSTR(UANOTL,5,7) AS ORDER#,
UAATHN, UANOTD
FROM ASTDTA.NOTEH1 T01
WHERE SUBSTR(UANOTL,1,4) = 'REM '
You need the concatenation operator:
SELECT ALL
SUBSTR(UANOTL,1,4) AS CODE1,
SUBSTR(UANOTL,5,7) || '0' AS ORDER#,
UAATHN, UANOTD
FROM ASTDTA.NOTEH1 T01
WHERE SUBSTR(UANOTL,1,4) = 'REM '
you just put an & operator before the string and the 0 you want to add
so 0 in front would be
select ('0' & ORDER#)
and at the end would be
select (ORDER# & '0')