Sum data from a table between dates in another table - php

I have these two tables that relate to each other with the column itemid.
I managed to make the sum of different itemid values as I show in the first example from data_table.
Now I want to relate this sum by specific dates in the other table but I can not. Please can anyone help me?
items_table
+--------+-----------+------------+
| itemid | published | publish_up |
+--------+-----------+------------+
| 1 | 1 | 02-01-2014 |
+--------+-----------+------------+
data_table
+--------+---------+------+
| itemid | fieldid | data |
+--------+---------+------+
| 1 | 27 | 5 |
| 1 | 28 | 10 |
| 1 | 29 | 5 |
+--------+---------+------+
This query is ok:
SELECT (SELECT SUM(data) FROM data_table WHERE fieldid='27')
+ (SELECT SUM(data) FROM data_table WHERE fieldid='28')
+ (SELECT SUM(data) FROM data_table WHERE fieldid='29')
But not between dates:
SELECT (SELECT SUM(data) FROM data_table WHERE fieldid='27')
+ (SELECT SUM(data) FROM data_table WHERE fieldid='28')
+ (SELECT SUM(data) FROM data_table WHERE fieldid='29')
WHERE (publish_up BETWEEN '2014-01-01 00:00:00' AND '2014-01-31 00:00:00')
FROM items_table

SELECT itemid, SUM(data)
FROM data_table JOIN items_table USING (itemid)
WHERE publish_up >= '2014-01-01'
AND publish_up < '2014-02-01'
GROUP BY itemid

Related

SQL - Get multiple values when limit 1

If I have a table like this:
ID | ident | product
1 | cucu1 | 99867 |
2 | kkju7 | 88987 |
3 | sjdu4 | 66754 |
4 | kjhu6 | 76654 |
5 | cucu1 | 98876 |
And use this query: SELECT ident,COUNT(*) FROM sales WHERE status=? AND team=? AND DATE(date) = DATE(NOW() - INTERVAL 1 DAY) GROUP BY ident order by COUNT(*) DESC LIMIT 1
I get the value: cucu1, since that has the most rows.
But if my table is like this:
ID | ident | product
1 | cucu1 | 99867 |
2 | kkju7 | 88987 |
3 | sjdu4 | 66754 |
4 | kkju7 | 76654 |
5 | cucu1 | 98876 |
It should return both cucu1 and kkju7, since they are the highest with same count, but still it gives me only cucu1. What am I doing wrong?
You can use rank():
SELECT ident, cnt
FROM (SELECT ident, COUNT(*) as cnt,
RANK() OVER (ORDER BY COUNT(*) DESC) as seqnum
FROM sales
WHERE status = ? AND team = ? AND
DATE(date) = DATE(NOW() - INTERVAL 1 DAY)
GROUP BY ident
) i
WHERE seqnum = 1;
LIMIT keyword simply limits the results to 1 row, no matter if there are equal values before or after the returned row.
A better solution would be to select the rows which have a count that's equal to max count in the query, which can be achieved by the following query:
SELECT ident,COUNT(*)
FROM sales
WHERE status=?
AND team=?
AND DATE(date) = DATE(NOW() - INTERVAL 1 DAY)
GROUP BY ident
HAVING COUNT(*) = MAX(COUNT(*))

Mysql union exclude duplicate entry by row

I have this query:
SELECT * FROM `date1` UNION SELECT * FROM `date2` ORDER BY (count1+count2) DESC
I need exclude duplicate entries by 'ID' column.
Can I do this with UNION?
Update:
table1
+---------+--------+
| id | count1 |
+---------+--------+
| 112337 | 567 |
+----+-------------+
table2
+---------+--------+
| id | count1 |
+---------+--------+
| 112337 | 565 |
+----+-------------+
I want to display only first 'id' data.
You can use NOT EXISTS:
SELECT * FROM date1
UNION
SELECT d2.* FROM date2 d2
WHERE NOT EXISTS (SELECT 1 FROM date1 d1 WHERE d1.id = d2.id)
Also change to UNION ALL, which is more efficient, if you don't want to exclude duplicate rows or if there re no duplicate ids in each table.
Using a union and wrapping it in a main query with aggregation may be what you want.
DROP TABLE IF EXISTS T,T1;
CREATE TABLE T(ID INT, COUNT1 INT);
CREATE TABLE T1(ID INT, COUNT1 INT);
INSERT INTO T VALUES (1,567),(2,20);
INSERT INTO T1 VALUES(1,565),(3,20);
select tid ,
max(case when tcount1 is not null then tcount1 else t1count1 end) cnt
from
(select id tid,count1 tcount1,null t1count1 from t
union
select id tid,null tcount1,count1 from t1
) s
group by tid;
+------+------+
| tid | cnt |
+------+------+
| 1 | 567 |
| 2 | 20 |
| 3 | 20 |
+------+------+
3 rows in set (0.001 sec)

MySQL get first existing row with value OR get another ordered

I need to write mysql query for table like this:
id | product_id | version | status
1 | 1 | 1 | 0
2 | 1 | 2 | 1
3 | 1 | 3 | 0
4 | 2 | 9 | 0
5 | 2 | 10 | 0
I need to get rows (unique for product_id - one for each product_id) but:
-if there is row for product_id with status=1 - grab it
-it there is no row as described get row with higher value or version
So for described table result should be
id | product_id | version | status
2 | 1 | 2 | 1
5 | 2 | 10 | 0
My only idea is to get rows with status 1, and then make second query using WHERE product_id NOT IN and then order by version DESC and GROUP BY product_id
Can join back to the table in this case
SELECT p1.id, p1.product_id, p1.version, p1.status FROM products p1
LEFT OUTER JOIN (
SELECT MAX(version) AS version FROM products p2
) p2 ON p1.version = p2.version OR p1.status = 1
GROUP BY p1.product_id
SQL Fiddle
You can achieve it with a UNION
select myTable.id, product_id, version, status from myTable
where id in(select id from myTable where status > 0)
union
select myTable.id, product_id, version, status from myTable
join (select max(id) as id from myTable group by product_id)
as m on m.id = myTable.id
and product_id not in(select product_id from myTable where status > 0 )

MySQL - Sum values width same ID in same table

I want sum values into my database by the same ID in the same table.
Table in database:
| ID | Value_o | Value_t | Value_tt |
| 1 | 40 | 20 | 10 |
query:
SELECT SUM(Value_o) AS Value_o, SUM(Value_t) AS Value_t, SUM(Value_tt) AS Value_TT
WHERE ID IN(1, 1)
And now the output id:
| Value_o | Value_t | Value_tt |
| 40 | 20 | 10 |
but I want:
| Value_o | Value_t | Value_tt |
| 80 | 40 | 20 |
I want get this output without JOIN.
Thanks!
PS. Sorry for my bad eng :/
Maybe this is what you are looking for:
SELECT
SUM(Value_o) AS Value_o,
SUM(Value_t) AS Value_t,
SUM(Value_tt) AS Value_TT
FROM
(
SELECT ID, Value_o, Value_t, Value_tt FROM Table1
UNION ALL
SELECT ID, Value_o, Value_t, Value_tt FROM Table1
) Table2
WHERE ID IN(1, 1);
Demo
The MySQL in operator doesn't work this way. Even if you have a value multiple times in the set, it doesn't duplicate the rows of your result.
If you want to have all the rows multiple times, you must use union all and sum over that
SELECT SUM(Value_o) AS Value_o, SUM(Value_t) AS Value_t, SUM(Value_tt) AS Value_TT
from (select * from mytable union all select * from mytable) t
WHERE ID IN (1)
Try this:
SELECT SUM(Value_o) AS Value_o, SUM(Value_t) AS Value_t, SUM(Value_tt) AS Value_TT
FROM TABLE
GROUP BY ID
HAVING ID = 1

MYSQL get row rank

I have a mysql table and I need to get random row and get the rank of total view
+--------+------------+---------+
| id | name |totalview|
+--------+------------+---------+
| 1 | ex1 | 20 |
| 2 | ex2 | 100 |
| 3 | ex3 | 30 |
| 4 | ex4 | 40 |
+--------+------------+---------+
for example :
SELECT * FROM `table` WHERE `id` = '$rand';
$rand may be 1 or 2 etc ..
I need to get rank of this row by totalview
thank's
SELECT *,
(SELECT COUNT(*) FROM table t2 WHERE totalview > t1.totalview ) + 1 cnt
FROM table t1
WHERE id = '$rand';
SELECT SUM(ref.totalview < t.totalview) FROM t1 CROSS JOIN t1 ref WHERE t1.id = '$rand'

Categories