How to get the records containing minimum unique numbers - unique

Can anyone help?
What is the MySQL query to get
id | number | member | date
11 | 133 | John | 2020-02-05
9 | 450 | Jason | 2020-02-07
6 | 10 | Luna | 2020-02-08
from
id | number | member | date
1 | 133 | John | 2020-02-05
2 | 456 | Carl | 2020-02-07
3 | 450 | Jason | 2020-02-07
4 | 5 | Kaiser | 2020-02-08
5 | 5 | Liliana| 2020-02-08
6 | 10 | Luna | 2020-02-08
7 | 20 | Liliana| 2020-02-09
8 | 20 | Luna | 2020-02-09
where, for each date, it'll select the row with the minimum but the unique number, if it exists.

Related

MySQL get multiple rows into columns

I have a table called visits where concat(s_id, c_id) is unique and id is the primary key. s_id is the ID number of a website and c_id is a campaign ID number. I want to show all the hits each campaign is getting and group by the site. I want each site on a single row
+-----+------+------+------+
| id | s_id | c_id | hits |
+-----+------+------+------+
| 1 | 13 | 8 | 245 |
| 2 | 13 | 8 | 458 |
| 3 | 13 | 3 | 27 |
| 4 | 13 | 4 | 193 |
| 5 | 14 | 1 | 320 |
| 6 | 14 | 1 | 183 |
| 7 | 14 | 3 | 783 |
| 8 | 14 | 4 | 226 |
| 9 | 5 | 8 | 671 |
| 10 | 5 | 8 | 914 |
| 11 | 5 | 3 | 548 |
| 12 | 5 | 4 | 832 |
| 13 | 22 | 8 | 84 |
| 14 | 22 | 1 | 7 |
| 15 | 22 | 3 | 796 |
| 16 | 22 | 4 | 0 |
+----+------+------+-------+
I would like to have the following result set:
s_id | hits | hits | hits| hits
13 | 245 | 458 | 27 | 193
14 | 320 | 183 | 783 | 226
5 | 671 | 914 | 548 | 832
22 | 84 | 7 | 796 | 0
Here is what I have tried which does not pull all the hits columns back.
SELECT v.*, v2.* FROM visits v
INNER JOIN visits v2 on v.s_id = v2.s_id
GROUP BY s_id
How can I get multiple rows into columns?
If your'e data set is not crazy huge and you are just trying to get the multiple rows as a single row.... one way to do this...
SELECT
s_id,
GROUP_CONCAT(hits SEPARATOR ',') as hits_list
FROM
visits
GROUP BY s_id
Since it doesn't use any joins or subqueries etc, i find this way to be quite fast.
you can later split/explode the data based on the ',' separator in PHP or whatever language you are using.
$hits = explode($hits_list, ','); //get them in an array

MySQL query to meet specific needs

My table is as follow:
-------------------------------------------
| rec_id | A_id | B_id |Date(YYYY-MM-DD)|
-------------------------------------------
| 1 | 1 | 6 | 2014-01-01 |
| 2 | 5 | 1 | 2014-01-02 |
| 3 | 2 | 6 | 2015-01-03 |
| 4 | 6 | 1 | 2014-01-04 |
| 5 | 7 | 1 | 2014-01-05 |
| 6 | 3 | 6 | 2014-01-06 |
| 7 | 8 | 1 | 2014-01-07 |
| 8 | 4 | 6 | 2014-01-08 |
| 9 | 9 | 1 | 2014-01-09 |
| 10 | 10 | 21 | 2014-01-10 |
| 11 | 12 | 21 | 2014-01-11 |
| 12 | 11 | 2 | 2014-01-12 |
| 13 | 1 | 1 | 2014-12-31 |
| 14 | 2 | 2 | 2014-12-31 |
| 15 | 1 | 1 | 2015-01-31 |
| 16 | 10 | 21 | 2015-01-31 |
| 17 | 1 | 21 | 2014-10-31 |
This table represents the possession of various "A_id" to a specific "B_id" with a date when it is possessed. The possession of each "A_id" can be changed later on at any time. That means the only the latest possession is considered.
I want to find out all the "A_id" that are currently (possessed in latest date) in possession of a specific "B_id". For example, for "B_id" = 6 the possessed "A_id" at present are as follows:
---------------------------
| A_id | Date(YYYY-MM-DD) |
---------------------------
| 2 | 2015-01-03 |
| 3 | 2014-01-06 |
| 4 | 2014-01-08 |
Similarly, for "B_id" = 21 the possessed "A_id" at present are as follows:
---------------------------
| A_id | Date(YYYY-MM-DD) |
---------------------------
| 10 | 2015-01-31 |
| 12 | 2014-01-11 |
I would highly appreciate your kind help in this regard.
One way to accomplish this is to use a correlated not exists predicate that makes sure that there doesn't exists any later possession for each A_ID with another B_ID.
SELECT A_ID, MAX(PDATE) AS DATE
FROM YOUR_TABLE T
WHERE B_ID = 6
AND NOT EXISTS (
SELECT 1
FROM YOUR_TABLE
WHERE A_ID = T.A_ID
AND PDATE > T.PDATE
AND B_ID <> T.B_ID
)
GROUP BY A_ID

Date less than MySQL query not working

I've a table
| adsid | user_id | earned_points | redeem_points | dialer_point | app_point | date |
+-------+---------+---------------+---------------+--------------+-----------+---------------------+
| 1 | 1 | 25 | 15 | 23 | 2 | 2015-03-15 19:38:43 |
| 3 | 1 | 25 | 15 | 23 | 2 | 2015-03-15 19:38:43 |
| 2 | 1 | 25 | 15 | 23 | 2 | 2015-03-15 19:38:43 |
| 1 | 2 | 25 | 15 | 23 | 2 | 2015-03-15 19:38:43 |
| 3 | 2 | 25 | 15 | 23 | 2 | 2015-03-15 19:38:43 |
| 2 | 2 | 25 | 15 | 23 | 2 | 2015-03-15 19:38:43 |
| 1 | 3 | 25 | 15 | 23 | 2 | 2015-03-15 19:38:43 |
| 3 | 3 | 25 | 15 | 23 | 2 | 2015-03-15 19:38:43 |
| 2 | 3 | 25 | 15 | 23 | 2 | 2015-03-15 19:38:43 |
| 1 | 4 | 25 | 15 | 23 | 2 | 2015-03-15 19:38:43 |
| 3 | 4 | 25 | 15 | 23 | 2 | 2015-03-15 19:38:43 |
| 2 | 4 | 25 | 15 | 23 | 2 | 2015-03-15 19:38:43 |
| 1 | 5 | 25 | 15 | 23 | 2 | 2015-03-15 19:38:43 |
| 3 | 5 | 25 | 15 | 23 | 2 | 2015-03-15 19:38:43 |
| 2 | 5 | 25 | 15 | 23 | 2 | 2015-03-15 19:38:43 |
| 1 | 6 | 25 | 15 | 23 | 2 | 2015-03-15 19:38:43 |
| 3 | 6 | 25 | 15 | 23 | 2 | 2015-03-15 19:38:43 |
| 2 | 6 | 25 | 15 | 23 | 2 | 2015-03-15 19:38:43 |
| 1 | 7 | 25 | 15 | 23 | 2 | 2015-03-15 19:38:43 |
| 3 | 7 | 25 | 15 | 23 | 2 | 2015-03-15 19:38:43 |
| 2 | 7 | 25 | 15 | 23 | 2 | 2015-03-15 19:38:43 |
| 1 | 8 | 25 | 15 | 23 | 2 | 2015-03-15 19:38:43 |
| 3 | 8 | 25 | 15 | 23 | 2 | 2015-03-15 19:38:43 |
| 2 | 8 | 25 | 15 | 23 | 2 | 2015-03-15 19:38:43 |
+-------+---------+---------------+---------------+--------------+-----------+---------------------+
The following less than date query does not works
select * from USER_POINTS_MAPPING where 'date' < '2015-03-17';
But when I do
select * from USER_POINTS_MAPPING where 'date' > '2015-03-17';
It throws back all the rows. What is this happening?
Try without ' characters (single quotation mark) around date. In MySQL either don't use any quotation mark or use this one ` (backtick) around field names.
'date' means date as string. And 'date' is always greater than '2015-03-17' when they are compared as string
While
`date`
means date as a field name
So the correct query is:
select * from USER_POINTS_MAPPING where date < '2015-03-17';
Pro tip: Don't use date as a column name. It's a reserved word. If you must use it surround it with backticks, not quotes.
where `data` < '2015-03-17'

MySQL Query to track transaction from 5 tables

I have 3 tables in my db, the scenario is inventory is entered into the database with reference to invoice no/po number, then users request for inventory and admin assign the items from specific invoices_items/po_items.
I need query to get when an invoice number is entered into the database and what quantity of items is this invoice has. then when when admin issue items from this issue. in other words i have to tracked inventory transactions with reference to invoice/po number.
I have following structure of tables,
table - po_reference
+----------------+-------------------------+--------------------+--------+---------------------+
| po_refrence_id | po_reference_name | requested_quantity | cost | created_on |
+----------------+-------------------------+--------------------+--------+---------------------+
| 6 | Dell Computer 000001256 | 14 | 15000 | 2015-02-18 10:36:33 |
| 11 | Dell Computer 000001257 | 50 | 150000 | 2015-02-18 10:38:33 |
+----------------+-------------------------+--------------------+--------+---------------------+
table - po_reference_details
+-------------------------+----------------+--------------------+-------------------+----------------------+-----------------------+
| po_reference_details_id | po_refrence_id | quantity_requested | quantity_received | quantity_outstanding | remarks |
+-------------------------+----------------+--------------------+-------------------+----------------------+-----------------------+
| 6 | 6 | 20 | 14 | 6 | 6 items are short.... |
| 8 | 11 | 60 | 50 | 10 | 10 items are short... |
+-------------------------+----------------+--------------------+-------------------+----------------------+-----------------------+
table - stock
+----------+----------+---------------------+------------+---------------------+------------+-----------+---------+--------------+---------+-------------+----------------+------------------+
| stock_id | quantity | created_on | created_by | updated_on | updated_by | module_id | item_id | main_unit_id | unit_id | category_id | po_refrence_id | startup_quantity |
+----------+----------+---------------------+------------+---------------------+------------+-----------+---------+--------------+---------+-------------+----------------+------------------+
| 290 | 35 | 2015-02-18 02:15:00 | NULL | NULL | NULL | 1 | 286 | 94 | 24 | 47 | 6 | 50 |
| 291 | 110 | 2015-02-18 00:00:00 | NULL | 2015-02-18 00:00:00 | NULL | 2 | 286 | 94 | 24 | 47 | 6 | 10 |
+----------+----------+---------------------+------------+---------------------+------------+-----------+---------+--------------+---------+-------------+----------------+------------------+
and request_stock_bridge
+--------------------------+------------+----------------+------------------+---------------------+--------------------------------------------------------------------+-----------------+--------------+
| stock_requests_bridge_id | request_id | po_refrence_id | quantity_on_hand | issued_date | remarks | issued_quantity | main_unit_id |
+--------------------------+------------+----------------+------------------+---------------------+--------------------------------------------------------------------+-----------------+--------------+
| 8 | 78 | 6 | 44 | 2015-02-18 06:49:34 | items are short , giving you 2 less, request after a week again... | 6 | 94 |
| 9 | 79 | 6 | 42 | 2015-02-18 08:18:56 | test | 2 | 94 |
| 10 | 80 | 6 | 35 | 2015-02-18 08:56:39 | 2 shorts.... | 7 | 94 |
+--------------------------+------------+----------------+------------------+---------------------+--------------------------------------------------------------------+-----------------+--------------+
and finally table - request
+------------+---------------+--------------------+---------------------+-----------------+--------+---------------+-----------+---------+
| request_id | department_id | quantity_requested | requested_date | quantity_issued | status | employee_name | module_id | item_id |
+------------+---------------+--------------------+---------------------+-----------------+--------+---------------+-----------+---------+
| 76 | 54 | 8 | 2015-02-18 00:00:00 | 0 | 0 | MTaqi | 2 | 279 |
| 77 | 54 | 7 | 2015-02-18 00:00:00 | 0 | 0 | MTaqi | 2 | 279 |
| 78 | 54 | 8 | 2015-02-18 00:00:00 | 0 | 1 | MTaqi | 2 | 286 |
| 79 | 54 | 2 | 2015-02-18 00:00:00 | 0 | 1 | MTaqi | 2 | 286 |
| 80 | 54 | 9 | 2015-02-18 00:00:00 | 0 | 1 | MTaqi | 2 | 286 |
+------------+---------------+--------------------+---------------------+-----------------+--------+---------------+-----------+---------+
i have write this query but doesn't work,
SELECT s.created_on, po.po_reference_name, s.startup_quantity, su.issued_date, su.issued_quantity, su.quantity_on_hand, su.remarks,po.po_refrence_id
FROM stock s, po_reference po, request r, stock_requests_bridge su
WHERE po.po_refrence_id = s.po_refrence_id
AND su.po_refrence_id = s.po_refrence_id
AND s.item_id = 286
GROUP by po.po_refrence_id
it returns this,
+---------------------+-------------------------+------------------+---------------------+-----------------+------------------+--------------------------------------------------------------------+----------------+
| created_on | po_reference_name | startup_quantity | issued_date | issued_quantity | quantity_on_hand | remarks | po_refrence_id |
+---------------------+-------------------------+------------------+---------------------+-----------------+------------------+--------------------------------------------------------------------+----------------+
| 2015-02-18 02:15:00 | Dell Computer 000001256 | 50 | 2015-02-18 06:49:34 | 6 | 44 | items are short , giving you 2 less, request after a week again... | 6 |
+---------------------+-------------------------+------------------+---------------------+-----------------+------------------+--------------------------------------------------------------------+----------------+

Magento Join two tables

I am new in magento.
I am creating a custom megento report. I decide to join two tables rev_table and emp_table.
emp_id | datefield | emp_name | salary |cpf | other_benefits
1 | 2013-11-12 00:00:00 | abc | 18000 | 2000 | 3000
2 | 2013-11-20 00:00:00 | test1 | 20000 | 1000 | 1000
3 | 2013-11-21 00:00:00 | test | 18000 | 2000 | 2000
4 | 2013-12-11 00:00:00 | demo | 15000 | 3000 | 2000
5 | 2013-12-20 00:00:00 | ash | 8000 | 5000 | 3000
6 | 2013-12-24 00:00:00 | test1 | 18000 | 1000 | 500
revenue_id | datefield | revenue | rental | repair | recruitment |wages
1 | 2013-12-24 07:02:00 | 200 | 22 | 23 | 546 |
2 | 2013-11-19 01:01:00 | 456 | 56 | 56 | 565 |
3 | 2013-10-09 00:00:00 | 500 | 565 | 564 | 56 |
4 | 2013-11-13 00:00:00 | 900 | 435 | 345 | 5 |
i want to show total of employee table of 1 month into rev_table report in magento. for example employes total wages of november month is (67000). this will show as a new column in revenue report of november month.
then result should be
revenue_id | datefield | revenue | rental | repair | recruitment | wages <br/>
2 | 2013-11-19 01:01:00 | 456 | 56 | 56 | 565 | 67000 <br/>
4 | 2013-11-13 00:00:00 | 900 | 435 | 345 | 5 |
please help me to explain me how to add joins in magento collections and add coloume in report grid.
Thanks