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)
.
Related
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'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
I have a query that want to compute each user_id's rank in comparison to all all other user_id real time. Real-time means when query is executing:
My query is as below, but unfortunately computed rank field is not as desired:
SELECT user_id, SUM(COALESCE(`duration`, 0)) AS duration_total,
(SELECT COUNT(*)
FROM `forms`
GROUP BY `user_id`
HAVING SUM(`duration`) > duration_total
) AS rank
FROM `forms`
GROUP BY `user_id`
The problem is on Having condition in the internal select. Where I want to count user_id that have more duration than current user_id.
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
I have a table with named "user-recent-activity" which has following columns: id, userid, activity and datetime. Now, I want to delete the records if any unique userid has more than 50 items, deleting the oldest records. For example, if the user id(lets say 1234) has more than 50 records in this table, then I have to save latest 50 records of user id(1234) and delete the oldest one.
Before inserting, query for the last 50 records with that ID (ordering from newer to older). If there is a 50th, substitute it (via update) instead of inserting a new row.
Assuming you are using a RDBMS that supports standard SQL the following stored procedure should do it.
create procedure remove-old-activities
(
#userid int
)
as
delete from user-recent-activity where userid=#userid and id not in (select top 50 id from user-recent-activity where userid=#userid order by datetime desc)
If you're DB does not support stored procedures then you should be able to use SQL parameters to pass the userid value...
Hope that helps
You could use rank method to precisely defined the rows number and thus delete the rows you want.
delete from tblName where id=
(select id from (
select #i := CASE WHEN ( #userid <> userid ) THEN 1
ELSE #i+1
END AS rank , id,userid, datetime2 ,#userid:=userid AS clset
from tblName x,(SELECT #i:=0) a ,(SELECT #userid:= 0) s
order by x.userid, datetime2 desc) T
where T.rank='50') ;
Another option:
Use the select query to select the rank <=50 and insert into a new table. Delete the old table and rename the new table afterwards.
insert into newtable (userid,activity,datetime2)
select userid,datetime2 from (
select #i := CASE WHEN ( #userid <> userid ) THEN 1
ELSE
#i+1
END AS rank , userid, activity,datetime2 ,#userid:=userid AS clset
from tblName x,(SELECT #i:=0) a ,(SELECT #userid:= 0) s
order by x.userid, datetime2 desc) T
where t.rank <=50