id | order_id | tracking | status | update_time
Table Name : ndc
1 100204835 124124304 0 2017-06-29 00:00:00
2 100204874 124104482 0 2017-06-29 00:00:00
3 100204835 124124304 0 2017-06-29 00:00:00
I need to SELECT all values (id,order_id,tracking_no) from ndc where order_id should be unique as there might be duplicate values.
The result should output all values in the row as I need to use them further.
For Ex. In the above table order_id 100204835 is duplicate.
Try this : Select * from ndc group by order_id;
You need to use DISTINCT with a field name and specifying other fields.
SQL: SELECT DISTINCT order_id, id,tracking,status,update_time FROM table
For more continent answer please follow-
MySQL - SELECT all columns WHERE one column is DISTINCT
use this
Select * from table group by order_id;
SELECT DISTINCT order_id,id,tracking_no FROM ndc;
also you can use it for distinct on multiple column
SELECT DISTINCT order_id FROM ndc
UNION
SELECT DISTINCT id FROM ndc
UNION
SELECT DISTINCT tracking_no FROM ndc
QUERY:
SELECT id,order_id,tracking_no,status,update_time, DISTINCT order_id FROM ndc;
or You Can Use Simple group by order_id;
Syntax: select * from table_name group by field_name;
NOTE: A DISTINCT and GROUP BY usually generate the same query plan, so performance should be the same across both query constructs.
none of the answers worked for me so here is one that i got working. use group bu on col4 while taking max values of other columns
select max(col1) as col1,max(col2) as col2,max(col3) as col3
, col4
from
table1
group by col4
Related
I have a table. Table has structure of id, name, color, product_id.
And the table has multiple rows with the same product_id.
With SQL query from PHP file - I would like to choose only one, the oldest, row. (The first one that was added to the current table).
What query should I use or approach?
Thank you!
Just making up a bit of mockup data ... Note the notes I put in. And I trust it's a newer version of MySQL, as the older ones did not support ROW_NUMBER() OVER() .
Here goes:
WITH
-- input ... you *need* a timestamp to identify the oldest ---
indata(id, name, color, product_id,ts) AS (
SELECT 1,'Arthur','blue' ,42,TIMESTAMP'2021-01-31 17:45:00'
UNION ALL SELECT 1,'Arthur','blue' ,42,TIMESTAMP'2021-01-31 17:50:00'
UNION ALL SELECT 1,'Arthur','blue' ,42,TIMESTAMP'2021-01-31 17:55:00'
UNION ALL SELECT 1,'Arthur','blue' ,42,TIMESTAMP'2021-01-31 18:00:00'
UNION ALL SELECT 2,'Ford' ,'red' ,42,TIMESTAMP'2021-01-31 17:45:00'
UNION ALL SELECT 2,'Ford' ,'blue', 42,TIMESTAMP'2021-01-31 17:50:00'
UNION ALL SELECT 2,'Ford' ,'green',42,TIMESTAMP'2021-01-31 17:55:00'
UNION ALL SELECT 2,'Ford' ,'cyan' ,42,TIMESTAMP'2021-01-31 18:00:00'
)
,
-- select all, plus a rank, on which you will filter outside ..
with_rank AS (
SELECT
*
, ROW_NUMBER() OVER(PARTITION BY id ORDER BY ts) AS rnk
FROM indata
)
SELECT
id
, name
, color
, product_id
, ts
FROM with_rank
WHERE rnk = 1
id|name |color|product_id|ts
1|Arthur|blue |42 |2021-01-31 17:45:00
2|Ford |red |42 |2021-01-31 17:45:00
One method is a correlated subquery:
select t.*
from t
where t.id = (select min(t2.id)
from t t2
where t2.product_id = t.product_id
);
This assumes that id is incrementing with each insertion. If not, you have no way of knowing what the "oldest" row is. SQL tables represent unordered sets, so there is no "oldest" row unless a column contains that information.
SELECT * FROM TableName WHERE product_id = ProductID ORDER BY product_id LIMIT 1;
Let's Say I have 2 tables
In Table1 I have 3 columns named as
Table1
Id | Name | Date (Format: 12-01-2019)
1 ABC 22-08-2019
2 XYZ 23-07-2019
Now My Question is How to store the Month wise count in another Table(i.e, Table2)
Expected Result:
Table 2
Month | Count
08/Aug 1
07/July 1
I searched many queries but I didn't find a better one
Can anyone provide me this sql query?
OR
If can you provide SQL query which stores all these count in separate column in Table1 with an extra column
merge into table_2 tgt
using
(select trunc(dt, 'month') dt, count(*) cnt
from table_1
group by trunc(dt, 'month')
) src on (tgt.dt = src.dt)
when not matched then insert (tgt.dt, tgt.cnt)
values (src.dt, src.cnt);
INSERT INTO table2(month,count)
SELECT
MONTH(Date) AS m, COUNT(DISTINCT Id)
FROM
table1 GROUP BY m;
This can be done in one query
Insert into Table2 (Column1, Column2)
Select Count(*), DATEColumn from Table1 group by DATEColumn
I am not sure which database your are using this work on MSSQL.
I have the table:
id | date_submitted
1 | 01/01/2017
1 | 01/02/2017
2 | 01/03/2017
2 | 01/04/2017
I'm looking for the correct SQL to select each row, limited to one row per id that has the latest value in date_submitted.
So the SQL should return for the above table:
id | date_submitted
1 | 01/02/2017
2 | 01/04/2017
The query needs to select everything in the row, too.
Thanks for your help.
You can find max date for each id in subquery and join it with the original table to get all the rows with all the columns (assuming there are more columns apart from id and date_submitted) like this:
select t.*
from your_table t
inner join (
select id, max(date_submitted) date_submitted
from your_table
group by id
) t2 on t.id = t2.id
and t.date_submitted = t2.date_submitted;
Note that this query will return multiple rows for an id in case there are multiple rows with date_submitted equals to max date_submitted for that id. If you really want only one row per id, then the solution will be a bit different.
If you just need id and max date use:
select id, max(date_submitted) date_submitted
from your_table
group by id
I am struggling to get this MySQL query to work. I have two tables:
Table 1
TYPE | NAME | LAT | LON | ICON
Table 2
ID | UID | NAME | LAT | LON | ICON
I am trying to select all results from Table 1 and only select some results from Table 2. I am trying to apply a WHERE clause to Table 2 but it doesn't seem to work.
I read the documentation and it said for a UNION to work the number of columns have to be the same. How can I then only select the same number of columns from both tables to be returned but filter the second table by a column only found on that table?
My (pseudo)Query:
(SELECT name,lat,lon,icon
FROM Table1)
UNION
(SELECT name,lat,lon,icon
FROM Table2
WHERE uid ="1")
SELECT * FROM table1
UNION
SELECT
NULL AS `type`,
`name`,
`lat`,
`long`,
`icon`
FROM table2 WHERE uid = 1
http://www.sqlfiddle.com/#!9/0a942/8
This selects everything from table1, and only where uid = 1 from table2.
An UNION can only be performed if both row sets have the exact same columns. Since there is no type column in table2, we select a NULL and name it type so we can do the UNION.
I have a table which has bus_id and cus_id in mysql. I want to get B108 as a return value from the table because I want result whose value is not equal to C108 and C108 has B109 also. So I do not want row 3 value also. I want to get only row 2 as my result. Please suggest me the select query for these table.
My table is :
you can use group table seperately and detect duplicated values for each column and then filter table using those values.
select * from table
where cus_id not in
(select cus_id from table
group by cus_id
having count(*) > 1)
and
bus_id not in
(select bus_id from table
group by bus_id
having count(*) > 1)
I haven't tested, but something like this may work
SELECT bus_id FROM table WHERE bus_id NOT IN (SELECT bus_id FROM table WHERE cus_id = C108)
Try this:
select bus_id from table where bus_id not in (select bus_id from table where cus_id='c108')
You can use mysql DISTINCT function with your condition.
eg.
select distinct(column_name) from table_name WHERE cus_id <> 'C108' AND bus_id <> 'B109';
Try this query
CREATE TABLE BUS
(`cb_id` int, `bus_id` varchar(4), `cus_id` varchar(4))
;
INSERT INTO BUS
(`cb_id`, `bus_id`, `cus_id`)
VALUES
(1, 'B101', 'C108'),
(2, 'B108', 'C101'),
(3, 'B109', 'C102'),
(24, 'B109', 'C108')
;
SELECT
*
FROM BUS B
WHERE cus_id<>'C108'
AND NOT EXISTS(SELECT * FROM BUS B1
WHERE
B1.BUS_ID=B.BUS_ID
AND B1.cus_id='C108')
If you mean ignore record who have 2 or more data in column bus_id or cus_id, maybe you can use this query :
select * from buscus a where
a.bus_id not in (select b.bus_id from (select count(bus_id) as cb, bus_id FROM BusCus group by bus_id) b where b.cb > 1)
and
a.cus_id not in (select c.cus_id from (select count(cus_id) as cc, cus_id FROM BusCus group by cus_id) c where c.cc > 1)
.