I'm making a query that JOIN to table, A and B.
A contains below fields:
TAP - Short Text (AAAA, BBBB, etc)
Operator - Short Text
Zone - Short Text (Zone 01, Zone 02..)
B contains below one:
TAP - Short Text
MCC - Number (20210, 20032, etc)
My query is:
SELECT A.TAP, A.Operator, SWITCH(B.MCC='10020', 'Own Network', B.MCC, A.Zone) FROM A LEFT JOIN B ON A.TAP=B.TAP
Query result shows #Error value for all Zone values. I think that this is due to MCC is in Number format, because, when I change MCC to Short Text (althought there is not text, only number), the query dumps the correct Zone.. but I cannot change, because I have to use MCC in Number format for other queries.
TAP | Operator | Zone | MCC
AAAA | ATT | Zone 01 | 120001
BBBB | Two | Own Network | 10020
Any suggestion? Many thanks
I'm not really getting that SWITCH statement. The third argument (B.MCC) should evaluate to a boolean, and it doesn't.
Use IIF(Nz(B.MCC)=10020, 'Own Network', A.Zone). I believe that's what you intend to do.
Other problems fixed:
MCC is a number, '10020' a string. Removed apostrophes to make 10020 a number
MCC contained empty (Null) values. Used Nz to cast these values to 0
As per Erik suggestion, issue was resolved removing apostrophes to the number and addiding Nz in order to cast empty values to Zero.
Related
This question already has answers here:
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed 2 years ago.
I'm really struggling to figure this query out:
names
zone
zones
zone_active
bob
1
1,2
yes
bill
0
3
yes
james
1
1,2
yes
fred
1
1,2
no
barry
1
4
yes
Im selecting zones '1,2' and zone_active='yes'
But it's returning all rows except Bill and Barry its seems to be ignoring the zone_active part
SELECT p.names, n.zone, n.zones, n.zone_active
FROM names as n
JOIN people as p ON p.names=n.names
WHERE zone IN ('1,2') AND zone_active='yes'
It should only return - bob, james
any ideas?
IN() does not treat a string as a list of discrete values just because the string contains commas. The value '1,2' is a single value, a string. So the operand will be compared to one value.
I assume zone is an integer (though you have not shown your table's data types, so that might be a wrong assumption). If you compare an integer to a string that has leading digits like '1,2', the initial digits are converted to an integer, and then compared.
So if zone is an integer column, then this:
WHERE zone IN ('1,2')
Is equivalent to this:
WHERE zone = 1
This does not match the row for bill because zone is 0 and the integer conversion for '1,2' is 1. So 0 = '1,2' is false.
The query should match the row for barry because 1 = '1,2' is true. I wonder if you made some mistake in your question.
Re your comment:
I want it to check if 1 or 2 is in the zone field
That would be:
WHERE zone IN (1,2) AND zone_active='yes'
But not:
WHERE zone IN ('1,2') AND zone_active='yes'
If you're trying to use query parameters, you need a separate parameter for each value:
WHERE zone IN (?, ?) AND zone_active='yes'
Because each parameter will be interpreted as one value. You can't use a single parameter for a list of values. If you try to use a parameter value of '1,2' that will become one string value, and that'll behave as I described above.
Some people try to fake this using FIND_IN_SET() but I would not recommend that. It will ruin any chance of optimizing the query with indexes.
I saw a post here in stack overflow with question and answer very similar to my problem.
its this post:
MySQL: turn decimal into percent within view
and as said in the post above, use:
CONCAT(columnname * 100, '%')
and thats what i actually did..
i have a table with a column for percentages for specific ranges of salary.
its a salary deduction schedule.
i stored the percent values as a decimal datatype in column "ee".
ee | decimal(4,4) | not null
and my problem is its giving me this result and its not what i wanted..
for example, an entry in the column is 0.0200, and i want to show it upon viewing as "2%", but this is what i get.
select s_b,rangeA,rangeB,concat(ee * 100,'%') as 'ee_percent' from htbl;
+___________________________________+
|s_b| rangeA| rangeB|ee_percent |
+---+--------+---------+------------+
| 1| 0.00| 1500.00| 1.0000% |
| 2| 1500.00|999999.99| 2.0000% |
+-----------------------------------+
I know that this is a duplicate and an elementary question, but i don't know why its not working,
if you see a mistake somewhere in my syntax, or in the data type please tell me.
any suggestions and corrections are highly appreciated. Thank you so much.
You can use the following solution using FORMAT:
SELECT s_b, rangeA, rangeB, CONCAT(FORMAT(ee * 100, 0), '%') AS 'ee_percent'
FROM htbl;
The result of the calculation is always DECIMAL(4,4). So MySQL show the result with four decimal places. You can use the FORMAT function to format the result or other functions which remove the decimal places.
demo: https://www.db-fiddle.com/f/ncjBpJRwdQVbT7PUBoXgeU/0
You can try with Floor function,
select s_b,rangeA,rangeB,concat(FLOOR(ee * 100),'%') as 'ee_percent' from htbl;
Use round, i.e:
select s_b,rangeA,rangeB,concat(round(ee * 100),'%') as 'ee_percent' from htbl;
I am a rookie to SSRS and am having difficulty obtaining a Sum.
I want the cell to Sum the distinct values of "UnitNumber" which is what the report is using to generate each row of my table. The reasoning behind this is that behind the complex report, Unit numbers are distinct and provide me with distinct SqFt Values. For example:
Unit # | Sqft|
Unit 001 | 472 |
Unit 002 | 600 |
Unit 004 | 1203|
The below does not work:
Sum(IIF(Fields!Unitnumber.Value,1,Fields!SqFt.Value)
I either get "Contains an error: cannot be converted to String" or, #Error in the cell. I cannot solely use Sum(SqFt) because it dumps an aggregate of the whole dataset query (every single row summed up) Any ideas?
It seems that your IIF missing main condition. It can be something like below or do as per your requirement.
Sum(IIF(Fields!Unitnumber.Value=1,1,Fields!SqFt.Value)
Also, I would suggest you to make your expression like below,
=SUM(IIF(Fields!Unitnumber.Value = 'putyourcondition', Fields!SqFt.Value, 0))
I have a weather-station that transmits data each hour. During that hour it makes four recordings (one every 15 minutes). In my current situation I am using attr_persistence=row to store data in my MySql database.
With row mode I get the default generated columns:
recvTimeTs | recvTime | entityId | entityType | attrName | attrType | attrValue | attrMd
But my weather station sends me the following data:
| attrName | attrValue
timeRecorded 14:30:0,22.5.2015
measurement1 18.799
measurement2 94.0
measurement3 1.19
These attrValue are represented in the database as string.
Is there a way to leave the three measurements in row mode and switch the timeRecorded to column mode? And if not, then what is my alternative?
The point of all this is to query the time recorded value, but I cannot query date as long as it is string.
As a side note: having the weather station send the data as soon as it is recorded (every 15 minutes) is out of the question, firstly because I need to conserve battery power and more importantly because in the case of a problem with the post, it will send all of the recordings at once.
So if an entire day went without sending any data, the weather station will send all 24*4 readings at once...
The proposed solution is to use the STR_TO_DATE function of MySQL in order to translate the stored string-based "timeRecorded" attribute into a real MySQL Timestamp type.
Nevertheless, "timeRecorded" attribute appears every 4 rows in the table due to the "row" attribute persistence mode of OrionMySQLSink. In this case I think you can use the ROWNUM keyword from MySQL in order to get only every 4 rows, something like (not an expert on MySQL):
SELECT STR_TO_DATE( attrValue, '%m/%d/%Y' ) FROM def_servpath_0004_weatherstation where (ROWNUM / 4 = 0);
The alternative is to move to "column" mode (in this case you have to provision de table by yourself). By using this mode you will have a single row with all the 4 attributes, being one of these attributes the "timeRecorded" one. In this case, you can provision the table by directly specifying the type of the "timeRecorded" column as Timestamp, instead of Text. That way, you will avoid the STR_TO-DATE part.
I have a query that outputs address order data:
SELECT ordernumber
, article_description
, article_size_description
, concat(NumberPerBox,' pieces') as contents
, NumberOrdered
FROM customerorder
WHERE customerorder.id = 1;
I would like the above line to be outputted NumberOrders (e.g. 50,000) divided by NumberPerBox e.g. 2,000 = 25 times.
Is there a SQL query that can do this, I'm not against using temporary tables to join against if that's what it takes.
I checked out the previous questions, however the nearest one:
is to be posible in mysql repeat the same result
Only gave answers that give a fixed number of rows, and I need it to be dynamic depending on the value of (NumberOrdered div NumberPerBox).
The result I want is:
Boxnr Ordernr as_description contents NumberOrdered
------+--------------+----------------+-----------+---------------
1 | CORDO1245 | Carrying bags | 2,000 pcs | 50,000
2 | CORDO1245 | Carrying bags | 2,000 pcs | 50,000
....
25 | CORDO1245 | Carrying bags | 2,000 pcs | 50,000
First, let me say that I am more familiar with SQL Server so my answer has a bit of a bias.
Second, I did not test my code sample and it should probably be used as a reference point to start from.
It would appear to me that this situation is a prime candidate for a numbers table. Simply put, it is a table (usually called "Numbers") that is nothing more than a single PK column of integers from 1 to n. Once you've used a Numbers table and aware of how it's used, you'll start finding many uses for it - such as querying for time intervals, string splitting, etc.
That said, here is my untested response to your question:
SELECT
IV.number as Boxnr
,ordernumber
,article_description
,article_size_description
,concat(NumberPerBox,' pieces') as contents
,NumberOrdered
FROM
customerorder
INNER JOIN (
SELECT
Numbers.number
,customerorder.ordernumber
,customerorder.NumberPerBox
FROM
Numbers
INNER JOIN customerorder
ON Numbers.number BETWEEN 1 AND customerorder.NumberOrdered / customerorder.NumberPerBox
WHERE
customerorder.id = 1
) AS IV
ON customerorder.ordernumber = IV.ordernumber
As I said, most of my experience is in SQL Server. I reference http://www.sqlservercentral.com/articles/Advanced+Querying/2547/ (registration required). However, there appears to be quite a few resources available when I search for "SQL numbers table".