Finding last "Non-Null" value in a Pivot query - php

I have the following query:
SELECT * FROM
(
SELECT unix_timestamp, input_raw, tag_id
from [200030].[dbo].inputs
WHERE inputs.date_time > dateadd(day,-1,getdate())
AND
(tag_id = 92164 or tag_id = 92149)
) src
pivot
(
max(input_raw)
FOR tag_id IN ([92164], [92149])
) piv
ORDER by unix_timestamp DESC
which is great and works. It gives me the results:
However, I would like the query to do one more thing for me.
Whenever there is a 'NULL' result, I would like the query to replace the 'NULL' with the last 'non-NULL' value in the column.
For example, the first NULL seen in column '92164' would be replaced with '211'.
Also, it is possible that there will be several 'NULL's i a row, so the query would need to keep going up the column until it finds a none NULL.
I have been able to accomplish this with php. Putting the results into a 2D associative array and running a function that finds nulls then loops through to find the last non NULL, but I really want to do this all in SQL if possible. I'd much rather use the
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)){
//code
}
method than assigning several arrays.
Any help?
Thank you
//EDIT
Forgot to add that this only applies if there is any non-nulls above the null value. For example it is acceptable if the first row is NULL.

You can put your pivot query in a CTE and reuse the CTE when you figure out what value to show. You data have null values only in the first column but here is a version that deals with the nulls in both columns.
WITH C AS
(
SELECT *
FROM (
SELECT unix_timestamp, input_raw, tag_id
FROM inputs
WHERE date_time > dateadd(day,-1,getdate()) AND
(tag_id = 92164 OR tag_id = 92149)
) src
PIVOT
(
MAX(input_raw)
FOR tag_id IN ([92164], [92149])
) piv
)
SELECT C1.unix_timestamp,
(
SELECT TOP(1) C2.[92164]
FROM C AS C2
WHERE C1.unix_timestamp <= C2.unix_timestamp AND
C2.[92164] IS NOT NULL
ORDER BY C2.unix_timestamp
) AS [92164],
(
SELECT TOP(1) C2.[92149]
FROM C AS C2
WHERE C1.unix_timestamp <= C2.unix_timestamp AND
C2.[92149] IS NOT NULL
ORDER BY C2.unix_timestamp
) AS [92149]
FROM C AS C1
ORDER by C1.unix_timestamp DESC;
If you have a performance problem it might be faster for you to store the result from the CTE in a temp table with a useful clustered key index.
CREATE TABLE #C
(
unix_timestamp int PRIMARY KEY,
[92164] int,
[92149] int
);
INSERT INTO #C
SELECT *
FROM (
SELECT unix_timestamp, input_raw, tag_id
FROM inputs
WHERE date_time > dateadd(day,-1,getdate()) AND
(tag_id = 92164 OR tag_id = 92149)
) src
PIVOT
(
MAX(input_raw)
FOR tag_id IN ([92164], [92149])
) piv;
SELECT C1.unix_timestamp,
(
SELECT TOP(1) C2.[92164]
FROM #C AS C2
WHERE C1.unix_timestamp <= C2.unix_timestamp AND
C2.[92164] IS NOT NULL
ORDER BY C2.unix_timestamp
) AS [92164],
(
SELECT TOP(1) C2.[92149]
FROM #C AS C2
WHERE C1.unix_timestamp <= C2.unix_timestamp AND
C2.[92149] IS NOT NULL
ORDER BY C2.unix_timestamp
) AS [92149]
FROM #C AS C1
ORDER by C1.unix_timestamp DESC;
DROP TABLE #C;

You can use an adaptation of the "running total" calculation method detailed here. Create a temp table or table variable to hold the results of your pivot query, with an extra column to hold the last not-null value of the 92164 column. I can't get SQLFiddle to do this, but it works on my machine (famous last words)
CREATE TABLE #pivot_results ([unix_timestamp] int, [92164] int, [92149] int)
INSERT INTO #pivot_results ([unix_timestamp], [92164], [92149])
VALUES
(1361893407, NULL, 294),
(1361893218, 207, 294),
(1361893108, NULL, 292),
(1361892807, 211, 292),
(1361892799, NULL, 292)
CREATE TABLE #update_me
([unix_timestamp] int, [92164] int, [92149] int, last_not_null_92164 int)
DECLARE #last_not_null_92164 INT = NULL;
INSERT INTO #update_me([unix_timestamp], [92164], [92149], last_not_null_92164)
SELECT unix_timestamp, [92164], [92149], NULL
FROM #pivot_results
ORDER BY unix_timestamp DESC
UPDATE #update_me
SET #last_not_null_92164 = last_not_null_92164 = ISNULL([92164],#last_not_null_92164)
FROM #update_me
SELECT
unix_timestamp
,last_not_null_92164 AS [92164]
,[92149]
FROM #update_me
ORDER BY unix_timestamp DESC

Related

Mysql limit results based on value from column

I'm trying to limit the number of results of a query based on another table column. For example, I have a table for products and a config table, like this:
tb_product
id | active | name | value | ...
tb_config
max_product | ...
What I'd like to do is something like this
SELECT
a.name, a.value
FROM
tb_product a,
tb_config b
WHERE a.active = 1
LIMIT b.max_product
But I'm getting errors like #1327 - Undeclared variable: b. Is there a way to achieve this result?
Because currently what I'm doing is doing another query to get just the max_product value and then use it as php variable to limit the results, like this:
$limit = "SELECT max_product FROM tb_config";
SELECT name, value FROM tb_product WHERE ativo = 1 LIMIT $limit
Maybe....
SELECT a.name
, a.value
FROM tb_product a
CROSS JOIN (SELECT #Limit:=(SELECT max_product from tb_config))
WHERE a.active = 1
LIMIT #Limit
With help from #ENargit's answer in Variable LIMIT Clause in MySQL, you can do it using a row count variable.
Assuming the following schema:
CREATE TABLE
tb_product
(
id INT PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(10),
`value` VARCHAR(10)
);
INSERT INTO
`tb_product`
(`name`, `value`)
VALUES
('Name1','Value1'),
('Name2','Value2'),
('Name3','Value3'),
('Name4','Value4'),
('Name5','Value5'),
('Name6','Value6'),
('Name7','Value7'),
('Name8','Value8'),
('Name9','Value9'),
('Name10','Value10');
CREATE TABLE
`tbl_config`
(
id INT PRIMARY KEY AUTO_INCREMENT,
`type` VARCHAR(10),
`value` INT
);
INSERT INTO
`tbl_config`
(`type`,`value`)
VALUES
('something',10),
('maxrows',7);
You can reference the config table with a subquery:
SELECT * FROM (
SELECT
tb_product.*,
#rownum := #rownum + 1 AS RowNum
FROM tb_product,
(SELECT #rownum := 0) AS CounterTbl
) AS DataTbl
WHERE
RowNum <= (
SELECT
`value`
FROM
`tbl_config`
WHERE
`type` = 'maxrows'
);
Gives you the first 7 rows (according to the config value). You can obviously extend this to do sorting etc.
SQLFiddle: http://www.sqlfiddle.com/#!9/f789e4/2

How to get 2 columns from one table and 2 rows as columns from other table in one row, in MySQL?

I know this is quite complicated, but I sincerely hope someone will check this out.
I made short version (to better understand the problem) and full version (with original SQL)
Short version:
[TABLE A] [TABLE B]
|1|a|b| |1|x
|2|c|d| |1|y
|3| | | |2|z
|5| | | |2|v
|4|w
How can I make MySQL query to get rows like that:
1|a|b|x|y
2|c|d|z|v
2 columns from A and 2 rows from B as columns, only with keys 1 and 2, no empty results
Subquery?
Full version:
I tried to get from Prestashop db in one row:
product id
ean13 code
upc code
feature with id 24
feature with id 25
It's easy to get id_product, ean13 and upc, as it's one row in ps_product table. To get features I used subqueries (JOIN didn't work out).
So, I selected id_product, ean13, upc, (subquery1) as code1, (subquery2) as code2.
Then I needed to throw out empty rows. But couldn't just put code1 or code2 in WHERE.
To make it work I had to put everything in subquery.
This code WORKS, but it is terribly ugly and I bet this should be done differently.
How can I make it BETTER?
SELECT * FROM(
SELECT
p.id_product as idp, p.ean13 as ean13, p.upc as upc, (
SELECT
fvl.value
FROM
`ps_feature_product` fp
LEFT JOIN
`ps_feature_value_lang` fvl ON (fp.id_feature_value = fvl.id_feature_value)
WHERE fp.id_feature = 24 AND fp.id_product = idp
) AS code1, (
SELECT
fvl.value
FROM
`ps_feature_product` fp
LEFT JOIN
`ps_feature_value_lang` fvl ON (fp.id_feature_value = fvl.id_feature_value)
WHERE fp.id_feature = 25 AND fp.id_product = idp
) AS code2,
m.name
FROM
`ps_product` p
LEFT JOIN
`ps_manufacturer` m ON (p.id_manufacturer = m.id_manufacturer)
) mainq
WHERE
ean13 != '' OR upc != '' OR code1 IS NOT NULL OR code2 IS NOT NULL
create table tablea
( id int,
col1 varchar(1),
col2 varchar(1));
create table tableb
( id int,
feature int,
cola varchar(1));
insert into tablea (id, col1, col2)
select 1,'a','b' union
select 2,'c','d' union
select 3,null,null union
select 5,null,null;
insert into tableb (id, feature, cola)
select 1,24,'x' union
select 1,25,'y' union
select 2,24,'z' union
select 2,25,'v' union
select 4,24,'w';
select a.id, a.col1, a.col2, b1.cola b1a, b2.cola b2a
from tablea a
inner join tableb b1 on (b1.id = a.id and b1.feature = 24)
inner join tableb b2 on (b2.id = a.id and b2.feature = 25);
SQLFiddle here.
What you want to do is called a Pivot Query. MySQL has no native support for pivot queries, though other RDBMSen do.
You can simulate a pivot query with derived columns, but you must specify each derived column. That is, it is impossible in MySQL itself to have the number of columns match rows of another table. This has to be known ahead of time.
It would be much easier to query the results as rows and then use PHP to do the aggregation into columns. For example:
while ($row = $result->fetch()) {
if (!isset($table[$row->id])) {
$table[$row->id] = array();
}
$table[$row->id][] = $row->feature;
This is not a simple question because it's not a standard query, by the way if you can make use of views you can do the following procedure. Assuming you're starting from this tables:
CREATE TABLE `A` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`firstA` char(1) NOT NULL DEFAULT '',
`secondA` char(1) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
);
CREATE TABLE `B` (
`id` int(11) unsigned NOT NULL,
`firstB` char(1) NOT NULL DEFAULT ''
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `A` (`id`, `firstA`, `secondA`)
VALUES (1, 'a', 'b'), (2, 'c', 'd');
INSERT INTO `B` (`id`, `firstB`)
VALUES (1, 'x'), (1, 'y'), (2, 'z'), (2, 'v'), (4, 'w');
First create a view that joins the two tables:
create or replace view C_join as
select A.firstA, A.secondA, B.firstB
from A
join B on B.id=A.id;
Create the view that groups the rows in table B:
create or replace view d_group_concat as
select firstA, secondA, group_concat(firstB) groupconcat
from c_join
group by firstA, secondA
Create the view that does what you need:
create or replace view e_result as
select firstA, secondA, SUBSTRING_INDEX(groupconcat,',',1) firstB, SUBSTRING_INDEX(SUBSTRING_INDEX(groupconcat,',',2),',',-1) secondB
from d_group_concat
And that's all. Hope this helps you.
If you can't create views, this could be the query:
select firstA, secondA, SUBSTRING_INDEX(groupconcat,',',1) firstB, SUBSTRING_INDEX(SUBSTRING_INDEX(groupconcat,',',2),',',-1) secondB
from (
select firstA, secondA, group_concat(firstB) groupconcat
from (
select A.firstA, A.secondA, B.firstB
from A
join B on B.id=A.id
) c_join
group by firstA, secondA
) d_group_concat
Big thanks to everyone for the answers. James's answer was first, simplest and works perfectly in my case. The query runs several times faster than mine, with subqueries. Thanks, James!
Just a few words why I needed that:
It's a part of integration component for Prestashop and wholesale exchange platform. There are 4 product code systems that wholesalers use on the platform (ean13, upc and 2 other systems). Those 2 other product codes are added as product feature in Prestashop. There are thousands of products on the shop and hundreds of thousands of products on the platform. Which is why speed is crucial.
Here is the code for full version of my question. Maybe someone will find this helpful.
Query to get Prestashop product codes and certain features in one row:
SELECT
p.id_product, p.ean13, p.upc, fvl1.value as code1, fvl2.value as code2
FROM `ps_product` p
LEFT JOIN
`ps_feature_product` fp1 ON (p.id_product = fp1.id_product and fp1.id_feature = 24)
LEFT JOIN
`ps_feature_value_lang` fvl1 ON (fvl1.id_feature_value = fp1.id_feature_value)
LEFT JOIN
`ps_feature_product` fp2 ON (p.id_product = fp2.id_product and fp2.id_feature = 25)
LEFT JOIN
`ps_feature_value_lang` fvl2 ON (fvl2.id_feature_value = fp2.id_feature_value)
WHERE
ean13 != '' OR upc != '' OR fvl1.value IS NOT NULL OR fvl2.value IS NOT NULL;

MySQL "ERROR 1046 (3D000): No database selected" on update query

I have an UPDATE query where I explicitely reference the database, but MySQL still complains with the message: ERROR 1046 (3D000): No database selected.
Other queries that are similar of structure, but use an INSERT work fine. Other queries that only perform SELECTs also run fine.
To repeat the problem in a test case, try running these queries:
create table test.object1 (
id_object1 int unsigned not null auto_increment,
total int,
weight int,
dt datetime,
primary key (id_object1)
) engine=InnoDB;
create table test.object2 (
id_object2 int unsigned not null auto_increment,
primary key (id_object2)
) engine=InnoDB;
create table test.score (
id_object1 int unsigned not null,
id_object2 int unsigned not null,
dt datetime,
score float,
primary key (id_object1, id_object2),
constraint fk_object1 foreign key (id_object1) references object1 (id_object1),
constraint fk_object2 foreign key (id_object2) references object2 (id_object2)
) engine=InnoDB;
insert into test.object1 (id_object1, total, weight, dt) values (1, 0, 0, '2012-01-01 00:00:00');
insert into test.object1 (id_object1, total, weight, dt) values (2, 0, 0, '2012-01-02 00:00:00');
insert into test.object2 (id_object2) values (1);
insert into test.score (id_object1, id_object2, dt, score) values (1, 1, '2012-01-03 00:00:00', 10);
insert into test.score (id_object1, id_object2, dt, score) values (2, 1, '2012-01-04 00:00:00', 8);
update test.object1 p
join (
select ur.id_object1, sum(ur.score * ur.weight) as total, count(*) as weight
from (
select lur.*
from (
select s.id_object1, s.id_object2, s.dt, s.score, 1 as weight
from test.score as s
join test.object1 as o1 using(id_object1)
where s.dt > o1.dt
order by s.id_object1, s.id_object2, s.dt desc
) as lur
group by lur.id_object2, lur.id_object1, date(lur.dt)
order by lur.id_object1, lur.id_object2
) as ur
group by ur.id_object1
) as r using(id_object1)
set
p.total = p.total + r.total,
p.weight = p.weight + r.weight,
p.dt = now();
Note: I'm running these queries from a PHP environment and I have NOT explicitely used mysql_select_db('test'), because I prefer not to and none of the other (many!) queries require it. I'm sure that using mysql_select_db would solve my issue, but I would like to know why exactly this particular query does not work.
For comparison sake: if you'd run this simpler query, also without using mysql_select_db, everything works fine:
update test.object1 set total=1, weight=1, dt=now() where id_object1=1;
I've searched to no avail. The only thing I found that came close, was this bug report: http://bugs.mysql.com/bug.php?id=28551 and especially that last (unanswered) message...
You have fields named incorrectly, but even if you correct them, this is a bug in MySQL that won't let you do it if you don't have default database.
update test.object1 p
join (
select ur.id_object1, sum(ur.score * ur.weight) as total, count(*) as weight
from (
select lur.*
from (
select s.id_object1, s.id_object2, s.dt, s.score, 1 as weight
from test.score as s
join test.object1 as o1
using (id_object1)
where s.dt > o1.dt
order by
s.id_object1, s.id_object2, s.dt desc
) as lur
group by
lur.id_object1, lur.id_object1, date(lur.dt)
order by
lur.id_object1, lur.id_object1
) as ur
group by ur.id_object1
) as r
USING (id_object1)
SET p.total = p.total + r.total,
p.weight = p.weight + r.weight,
p.dt = now();
The problem is specific to UPDATE with double-nested queries and no default database (SELECT or single-nested queries or default database work fine)
You have some wrong field names in the UPDATE statement -
What is s.object? Shouldn't it be s.id_object2?
What is lur.object1? Shouldn't it be lur.id_object1?
What is lur.object2? Shouldn't it be lur.id_object2?
What is ur.id_object at the end?
Fix all these issues and try to update again;-)
First time I ran this script I got that error. My output:
1 row inserted [0,184s]
1 row inserted [0,068s]
1 row inserted [0,066s]
1 row inserted [0,147s]
1 row inserted [0,060s]
Error (32,1): No database selected
When I set default database name the problem disappeared.
Remember that you cannot use foreign keys when the Engine is set to MyISAM. Not only does the table that you are creating a foreign key in need to be InnoDB, but the table you are getting the key from also needs to be InnoDB.
I was getting the same error as you and pulling my hair out for days before I thought of this. I went into each of my tables and made sure the Engines were set to InnoDB for each one, and now I have no issues setting up foreign keys.

mysql - php blob field?

I have a query that returns as a result what seems to be a BLOB
SELECT CAST(GROUP_CONCAT(CONCAT(value,'(',qty,')') SEPARATOR ', ') AS CHAR)a
FROM (
SELECT value,count(*) qty
FROM extra_field_values
WHERE fieldid = #fieldid
AND value != ''
AND itemid IN (7,8,10,12,15,16,17,18,19,20,21,22,23,24,25,26)
GROUP BY value
) x
the table is
CREATE TABLE `extra_field_values` (
`itemid` int(11) NOT NULL DEFAULT '0',
`fieldid` int(11) NOT NULL DEFAULT '0',
`value` text NOT NULL,
KEY `itemid` (`itemid`),
KEY `fieldid` (`fieldid`),
KEY `value` (`value`(1)),
KEY `inx` (`itemid`,`fieldid`,`value`(1))
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
The main idea is that i want a string per row returning value:::count separated by ###.
The problem is that php returns empty result. I have tried both mysql and mysqli as far as drivers is concerned.
Any ideas?
PHP
$result = $mysqli->query(" SELECT CAST(GROUP_CONCAT(CONCAT(value,'(',qty,')') SEPARATOR ', ') AS CHAR)a
FROM (
SELECT value,count(*) qty
FROM extra_field_values
WHERE fieldid = #fieldid
AND value != ''
AND itemid IN (7,8,10,12,15,16,17,18,19,20,21,22,23,24,25,26)
GROUP BY value
) x ") ;
while( $row = $result->fetch_assoc() ){
print_r($row);
}
Even if you execute it through phpmyadmin it returns null. Try it on EMS mysql it works like a charm.
full query
SELECT
extra_field_values.fieldid,
extra_fields.var_name,
extra_fields.field,
extra_fields.type,
#fieldid:=extra_field_values.fieldid,
(
SELECT CAST(GROUP_CONCAT(CONCAT(value,'(',qty,')') SEPARATOR ', ') AS CHAR) a
FROM ( SELECT value, #fieldid, count(*) qty FROM extra_field_values WHERE fieldid = #fieldid GROUP BY value ) x
) as v,
count(itemid) as total,
(SELECT groupid FROM extra_fields_groups_items WHERE itemid = #fieldid) as groupid
FROM extra_field_values INNER JOIN extra_fields
ON (extra_field_values.fieldid=extra_fields.fieldid)
WHERE module = 'listings'
AND itemid IN (7,8,10,12,15,16,17,18,19,20,21,22,23,24,25,26)
AND settings LIKE '%"search";s:1:"1"%'
GROUP BY fieldid
You are querying for fields matching the session variable #fieldid but I would guess you're not defining that variable in your mysqli session or in your phpmyadmin environment.
SET #fieldid := 1234;
SELECT ... WHERE fieldid = #fieldid ...
Or else you can use ? as a query parameter placeholder for the fieldid and bind it to a PHP variable.
See http://php.net/manual/en/mysqli-stmt.bind-param.php
Does the following query do what you want, without using the #fieldid session variable?
SELECT x.fieldid, x.var_name, x.field, x.type, x.groupid, COUNT(*) AS total,
CAST(GROUP_CONCAT(CONCAT(x.value,'(',x.qty,')') SEPARATOR ', ') AS CHAR) a
FROM (
SELECT v.value, v.fieldid, f.var_name, f.field, f.type, g.groupid, COUNT(*) AS qty
FROM extra_field_values AS v
INNER JOIN extra_fields AS f ON f.fieldid=v.fieldid
INNER JOIN extra_fields_groups_item AS g ON g.itemid=v.fieldid
WHERE f.module = 'listings'
AND f.itemid IN (7,8,10,12,15,16,17,18,19,20,21,22,23,24,25,26)
AND f.settings LIKE '%"search";s:1:"1"%'
GROUP BY v.value, v.fieldid
) as x
GROUP BY x.fieldid

SQL most popular

I have a mysql table with items in relation to their order.
CREATE DATABASE IF NOT EXISTS `sqltest`;
USE `sqltest`;
DROP TABLE IF EXISTS `testdata`;
CREATE TABLE `testdata` (
`orderID` varchar(10) DEFAULT NULL,
`itemID` varchar(10) DEFAULT NULL,
`qtyOrdered` int(10) DEFAULT NULL,
`sellingPrice` decimal(10,2) DEFAULT NULL
)
INSERT INTO `testdata`(`orderID`,`itemID`,`qtyOrdered`,`sellingPrice`)
values ('1','a',1,'7.00'),('1','b',2,'8.00'),('1','c',3,'3.00'),('2','a',1,'7.00'),('2','c',4,'3.00');
Intended Result:
A = (1+1)2
B = 2
C = (2+4)6 <- most popular
How do I add up all the qty's for each item and result the highest one?
It should be fairly strait forward but I'm new to SQL and I can't work this one out :S
Solution needs to be mysql and or php.
I guess there needs to be some sort of temporary tally variable for each item ID,
but that seems like it could get messy with too many items.
ANSWER:
(thanks nuqqsa)
SELECT itemID, SUM(qtyOrdered) AS total FROM testdata GROUP BY itemID ORDER BY total DESC LIMIT 1;
How about this:
SELECT itemID, SUM(qtyOrdered) AS total FROM testdata GROUP BY itemID ORDER BY total DESC;
SELECT itemID, SUM(qtyOrdered) as blah FROM sqltest GROUP BY itemID ORDER BY blah DESC should do it
SELECT *
FROM testdata
ORDER BY SUM(gtyOrdered) DESC
GROUP BY itemID
SELECT SUM( qtyOrdered ) AS sum_ordered, itemID
FROM testdata
GROUP BY itemID
ORDER BY sum_ordered
select count(qtyOrdered), qtyOrdered from testdata group by qtyOrdered

Categories