+---------+------+------+
| Col1 | Col2 | Col3 |
+---------+------+------+
| 12,57 | 001 | P |
| 23,08 | 002 | P |
| -12,57 | 003 | R |
| -23,08 | 004 | R |
| 139,44 | 005 | P |
| 163,99 | 006 | P |
| -303,43 | 007 | P |
+---------+------+------+
So i need to SUM in my SSRS report records from Col1 when Col3 equal 'R', else all records with Col3 equal 'P'.
The expresion that i have at the moment:
=Sum(IIF(Fields!Col3.Value = "R",Fields!Col1.Value , 0))
Use Switch
=SUM(Switch(
Fields!Col3.Value = "R", Fields!Col1.Value,
Fields!Col3.Value = "P", Fields!Col1.Value
)
)
try this:
=IIf(
sum(IIf(Fields!Col3.Value Like "R",Sum(Fields!Col1.Value),0)),
sum(IIf(Fields!Col3.Value Like "P",Sum(Fields!Col1.Value),0))
)
if it doesn't work let me know so I could give another solutions :)
Related
I want to identify missing values in an alphanumeric sequence.
The table is defined as such:
CREATE TABLE `seqtest` (
`ID` int(11) NOT NULL,
`PoleNo` text,
`Pre` char(1) DEFAULT NULL,
`Num` int(3) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The data is as shown below and will always be one letter (A-Z) followed by three numbers from 000 to 999.
| PoleNo | Pre | Num |
|------------------------|
| A000 | A | 000 |
| A001 | A | 001 |
| A002 | A | 002 |
| A004 | A | 003 |
| **** | * | *** |
| A998 | A | 998 |
| A999 | A | 999 |
| B000 | B | 000 |
| B001 | B | 001 |
| B002 | B | 002 |
| **** | * | *** |
| B998 | B | 998 |
| B999 | B | 999 |
| C000 | C | 000 |
| C001 | C | 001 |
| C005 | C | 005 |
| C006 | C | 006 |
|------------------------|
I want the query to find that, for example, C002, C003 AND C004 are missing as shown below.
| Pre | start | stop |
| C | 2 | 4 |
|----------------------|
Im using the following:
SELECT l.Pre, l.Num + 1 as start, min(fr.Num) - 1 as stop
FROM seqtest as l
LEFT OUTER JOIN seqtest as r ON l.Num = r.Num - 1 AND l.Pre = r.Pre
LEFT OUTER JOIN seqtest as fr ON l.Num < fr.Num AND l.Pre = fr.Pre
WHERE r.Num is null AND l.Num < 999
GROUP BY l.Pre, l.Num, r.Num
which is based on this.
It gives me the range that is missing and works well except for one case...when 'Pre' changes from one letter to the next.
IE With the following data:
| PoleNo | Pre | Num |
|------------------------|
| B995 | B | 995 |
| B996 | B | 996 |
| B997 | B | 997 |
| C003 | C | 003 |
| C004 | C | 004 |
| C005 | C | 005 |
| C006 | C | 006 |
|------------------------|
Id like to be able to return this:
| Pre | start | stop |
| B | 998 | 999 |
| C | 0 | 2 |
|----------------------|
Is this possible? Im using the Pre and Num fields which are simply the PoleNo field broken up...but if anyone sees a way to do it just using the PoleNo field, that would work as well.
This is much easier in MySQL 8+, because you have lead(). But, you can do what you want as:
select st.pre,
(st.num + 1) as start,
(st.next_num - 1) as stop
from (select st.*,
(select st2.num
from seqtest st2
where st2.pre = st.pre and
st2.num > st.num
order by st2.num asc
limit 1
) as next_num
from seqtest st
) st
where next_num <> num + 1;
EDIT:
This gets the ranges at the beginning and end as well:
select st.pre,
(st.num + 1) as start,
(st.next_num - 1) as stop
from (select st.pre, num,
coalesce( (select st2.num
from seqtest st2
where st2.pre = st.pre and
st2.num > st.num
order by st2.num asc
limit 1), 1000
) as next_num
from seqtest st
union
select st.pre, 0 as num, min(st.num) as next_num
from seqtest st
group by st.pre
) st
where next_num <> num + 1
order by pre, start;
Here is a db<>fiddle.
I created a query in my magento database to find product the have a "New From" dat but no "New To".
SELECT
catalog_product_entity.entity_id,
catalog_product_entity.sku,
a.value as New_From,
b.value as New_To
FROM `catalog_product_entity`
INNER JOIN catalog_product_entity_datetime as a on a.entity_id = catalog_product_entity.entity_id and a.attribute_id = '93'
INNER JOIN catalog_product_entity_datetime as b on b.entity_id = catalog_product_entity.entity_id and b.attribute_id = '94'
WHERE a.value IS NOT NULL AND b.value IS NULL
Now what I would like to accomplish since it concerns 500+ items to run a UPDATE statement on this query closing "New To" 1 month after having been opened.
Any ideas how to run that?
So to clarify:
Table 1: (catalog_product_entity)
| entity_id | sku |
| --------- | --- |
| 1 | ABC |
| 2 | DEF |
Table 2: (catalog_product_entity_datetime)
| id | entity_id | attribute_id | value |
| --- | --------- | ------------ | ----- |
| 1 | 1 | 93 | 2013-06-12 00:00:00 |
| 2 | 1 | 94 | 2013-07-12 00:00:00 |
| 3 | 1 | 98 | Some other attribute |
| 4 | 2 | 93 | 2014-08-20 00:00:00 |
| 5 | 2 | 94 | NULL | <- This I want updating
| 6 | 2 | 98 | Some other attribute |
I want to update the NULL values in table 2 with the date from that same table + 1 month. My search returns:
| entity -id | sku | New_From | New_To |
| ----------- | --- | ------------------- | ------------------- |
| 2 | DEF | 2014-08-20 00:00:00 | NULL |
So I would like to write the query that updates that field to the New_From + 1 month.
| entity -id | sku | New_From | New_To |
| ----------- | --- | ------------------- | ------------------- |
| 2 | DEF | 2014-08-20 00:00:00 | 2014-09-20 00:00:00 |
In my mind I will end up with something like this: (but this one does not work)
UPDATE catalog_product_entity_datetime
SET value = TIMESTAMP(a.value+300000000)
INNER JOIN catalog_product_entity_datetime as a on a.entity_id = catalog_product_entity_datetime.entity_id and a.attribute_id = '93'
WHERE attribute_id=94
Try this:
UPDATE catalog_product_entity_datetime t1
JOIN catalog_product_entity t2
ON t1.entity_id = t2.entity_id
AND t1.attribute_id = 94
JOIN catalog_product_entity_datetime t3
ON t2.entity_id = t3.entity_id
AND t3.attribute_id = 93
SET t1.value = CASE WHEN t1.value IS NULL THEN date_add(t3.value, interval 1 month)
ELSE t1.value END
;
Demo Here
I think that this is best asked using an example.
say that my original table is called origin and is
+-------+-------+------+
| list | type | date |
+-------+-------+------+
| a1 | a | 1 |
| a2 | a | 2 |
| a3 | a | 3 |
| a4 | a | 4 |
| a5 | a | 5 |
| a6 | a | 6 |
| ... | ... | ... |
| a(n-1)| a | (n-1)|
| an | a | n |
| b1 | b | 1 |
| b2 | b | 2 |
| b3 | b | 3 |
| b4 | b | 4 |
| b5 | b | 5 |
| b6 | b | 6 |
| ... | ... | ... |
+-------+-------+------+
and I want to add a column called list_shifted(list_s) where
+-------+-------+-------+------+
|list_s | list | type | date |
+-------+-------+-------+------+
| NULL | a1 | a | 1 |
| a1 | a2 | a | 2 |
| a2 | a3 | a | 3 |
| a3 | a4 | a | 4 |
| a4 | a5 | a | 5 |
| a5 | a6 | a | 6 |
| ... | ... | ... | ... |
|a(n-2) | a(n-1)| a | (n-1)|
|a(n-1) | an | a | n |
| NULL | b1 | b | 1 |
| b1 | b2 | b | 2 |
| b2 | b3 | b | 3 |
| b3 | b4 | b | 4 |
| b4 | b5 | b | 5 |
| b5 | b6 | b | 6 |
| ... | ... | ... | ... |
+-------+-------+-------+------+
is there a simple way to do this
It looks like both the columns list and list_s are dependent on the other two.
I would question the need for either of these columns, you can always build the column you require when you query the data.
For list:
SELECT CONCAT(type,date) list
FROM origin
For list_s:
SELECT CASE
WHEN date - 1 > 0 THEN CONCAT(type,date - 1)
ELSE NULL
END CASE list_s
FROM origin
here is two query that could solve your problem.
First one if your date column is increased by 1 in every next row (for every type) like you post in you question example from 1 to 5 it's always 1, 2, 3, 4, 5 then you can simple make one RIGHT JOIN like this:
SELECT ts.list, tx.*
FROM (SELECT `list`, type, `date`
FROM t1) AS ts
RIGHT JOIN t1 AS tx
ON ts.type = tx.type AND ts.date = (tx.date - 1);
Second one is solution if date is not in the exact order (if some row is deleted or something like this) and numbers from 1 to 5 not necessary to have all of them like 1, 2, 5... Then you can use temporary variables (it can be used in first case of course but query is a little bit complicated) like this.
SELECT CASE WHEN type_s = type THEN list_s ELSE null END AS list_s, `list`, type, `date`
FROM (SELECT #prev AS list_s, #prev := tx.list AS `list`,
#prevTyp AS type_s, #prevTyp := tx.type AS type, tx.date
FROM (SELECT #prev := null) AS i, (SELECT #prevTyp := null) AS y, t1 AS tx
ORDER BY tx.type, tx.date) AS myTable;
Here is SQL Fiddle for both query to see how it's work.
NOTE that the one row when value is inserted is commented. I do that so you can see where is the 'problem' with the first query! (if you uncomment it will work as in your desired result).
GL!
P.S. if you have any question fill free to ask in comment below...
Actually my question is almost the same with MySQL: Select multiple rows containing values from one column, I want to find the car_id of the cars that have MAKE='FORD' AND COLOR='SILVER', so in this case here it will returns car_id 1 and 2.
PS: There could be multiple criteria at once, like I can search by MAKE + CARLINE + COLOR, MAKE + CARLINE, and etc.
table_cars
+----+--------+----------+-----------+
| id | car_id | name | value |
+----+--------+----------+-----------+
| 1 | 1 | MAKE | FORD |
| 2 | 1 | CARLINE | FIESTA |
| 3 | 1 | COLOR | SILVER |
| 4 | 1 | TOPSPEED | 210KM/H |
| 5 | 2 | MAKE | FORD |
| 6 | 2 | CARLINE | FOCUS |
| 7 | 2 | COLOR | SILVER |
| 8 | 2 | TOPSPEED | 200KM/H |
| 9 | 3 | MAKE | HOLDEN |
| 10 | 3 | CARLINE | ASTRA |
| 11 | 3 | COLOR | WHITE |
| 12 | 3 | TOPSPEED | 212KM/H |
+----+--------+----------+-----------+
Thank you!
select car_id
from your_table
group by car_id
having sum(name = 'MAKE' and value = 'FORD') > 0
and sum(name = 'COLOR' and value = 'SILVER') > 0
Try with self join as below:
SELECT distinct car_id
FROM mytable mt1 INNER JOIN mytable mt2
ON mt1.car_id = mt2.car_id
WHERE mt1.name = 'MAKE'
AND mt1.value = 'FORD'
AND mt2.name = 'COLOR'
AND mt2.value = 'SILVER'
By using mysql can i create new columns according the groups in a table?i only can get the grouping and group_concat.
Before
-------------------------
no | item | code |
-------------------------
1 | aa | x |
2 | cc | c |
3 | pfr | a |
4 | bog | |
5 | aa | x |
6 | pfr | x |
1 | aa | x |
6 | pfr | a |
-------------------------
After group and group_concat
-------------------------
no | item | code |
-------------------------
1 | aa | x-2 |
2 | cc | c-1 |
3 | pfr | a-1 |
4 | bog | |
5 | aa | x-1 |
6 | pfr | a-1,x-1 |
-------------------------
I want to make like below this. Is it possible?
-------------------------------------------------------------------------------------
no | aa_qty | aa_code | cc_qty | cc_code | pfr_qty | pfr_code | bog_qty | bog_code |
-------------------------------------------------------------------------------------
1 | 2 | x-2 | | | | | | |
2 | | | 1 | c-1 | | | | |
3 | | | | | 1 | a-1 | | |
4 | | | | | | | 1 | |
5 | 1 | x-1 | | | | | | |
6 | | | | | 2 | a-1,x-1 | | |
-------------------------------------------------------------------------------------
You can achieve this using Pivot Table mechanism. MySQL does not have direct support on this mechanism. You need to depend on various if .. else.. or case .. when ... statements to build the required result view.
On the sample data given, say in table item_codes with no, item, and code as columns, you can try following example.
select no,
case item when 'aa' then cnt else '' end as aa_qty,
case item when 'aa' then code else '' end as aa_code,
case item when 'cc' then cnt else '' end as cc_qty,
case item when 'cc' then code else '' end as cc_code,
case item when 'pfr' then cnt else '' end as pfr_qty,
case item when 'pfr' then code else '' end as pfr_code,
case item when 'bog' then cnt else '' end as bog_qty,
case item when 'bog' then code else '' end as bog_code
from
(
select no, item, sum(cnt) cnt, group_concat( code ) code
from
(
select
no, item, count(no) cnt,
if( code='', code, concat( code, '-', count(no) ) ) 'code'
from item_codes ic
group by ic.no, ic.item, ic.code
) ig group by no, item
) grouped_data;
You may require some changes based on the data differences and other occurrences like null and empty codes, etc.