I have the following table
ID Name Progress Date
------------------------------
1 A1 First Stage 1/1/2013
2 A1 Second Stage 1/2/2013
3 A2 First Stage 1/1/2013
4 A2 Second stage 1/2/2013
5 A3 First Stage 1/2/2013
6 A1 Closed 1/5/2013
I would like to display the stage of each name except the one that is ultimately closed.
For example the output of this should be
ID Name Progress Date
------------------------------
3 A2 First Stage 1/1/2013
4 A2 Second stage 1/2/2013
5 A3 First Stage 1/2/2013
Not A1 as A1 is ultimately Closed.
My query display Select * from table where Progress not like 'Closed' obviously displays all the results except that row.
Thanks
Use a subquery to filter all the names that you want to exclude:
select distinct name from table where progress = 'Closed'
Now, use it in your query:
select *
from table
where name not in (select distinct name from table where progress = 'Closed');
Hope this helps you
SELECT x.*
FROM my_table x
LEFT
JOIN my_table y
ON y.name= x.name
AND y.progress = 'closed'
WHERE y.id IS NULL;
Related
I have a dataframe:
day id value
1 a1 right
2 a1 right
2 a1 right
2 a1 right
3 a1 right
4 a1 right
1 b2 right
1 b2 right
2 b2 right
3 b2 right
I want to count how many rows there were counted in sum at day 3 for each id group. So desired result is:
day id count
3 a1 5
3 b2 4
How to do that? I know that in my query I have to write GROUP BY id. But how to count rows in sum at certain day?
This might work
SELECT MAX(day) day, id, COUNT(*) counts
FROM dataframe
WHERE day <= 3
GROUP BY id
select '3' as "Day",id, count(value)
from dataset
where day
group by 2
I have this table :
std_id std_type std_dept target
1 type-1 ALL 15
2 type-1 HRD 10
3 type-2 ALL 1
4 type-2 ACCTG 5
5 type-3 ALL 5
6 type-4 ALL 25
std_dept with value ALL means std_target for each std_type are valid for all dept
std_dept with specific dept value, override the value form point (1).
So, my problem is lets say I am from HRD dept and I want to have the result set as follow:
std_id std_type std_dept target
2 type-1 HRD 10
3 type-2 ALL 1
5 type-3 ALL 5
6 type-4 ALL 25
I really like to show my work, but I've got no clue at all, I don't know how to distinctly select * from std_table (for each std_type) where std_dept='HRD' if exist else get from std_dept='ALL'.
I need pointers if not the code
You could use NOT EXISTS and correlated subquery:
SELECT *
FROM tab
WHERE std_dept = 'HRD'
UNION ALL
SELECT *
FROM tab t1
WHERE std_dept = 'ALL'
AND NOT EXISTS (SELECT *
FROM tab t2
WHERE t2.std_dept = 'HRD'
AND t1.std_type = t2.std_type);
DBFiddle Demo
It sounds like you want to weight it, so if you're from HRD then you want HRD or ALL, and if you want ACCTG then you want ACCTG or ALL, for types 1-4.
You could group by std_type to make them distinct, and order by that to make the order correct. For the weighting thing, you could create a column which is the result of a comparison against the same value you're using in your WHERE clause, e.g. something like:
SELECT std_id, DISTINCT(std_type) AS std_type,
std_dept, target,
IF (std_dept = 'HRD', 50, 10) AS weight
FROM mytable
WHERE std_dept IN ('ALL', 'HRD')
GROUP BY std_id, std_dept, target, weight
ORDER BY std_type
I have two table named as booked_flat and master_flat.
columns of booked_flats are
customer_name, customer_address, customer_phone, building_wing_no, building_name_no, flat_no
columns of master_flats are
building_wing_no, building_name_no, flat_no, status
when any flat get booked the status get change as sold else status remains unsold.
all entries with sold status are in booked_flats.
I have tried to merge this two table but it gives an error please help me
$sql = SELECT * FROM 'booked_flats'
UNION SELECT * FROM 'master_flats'
WHERE building_wing_no, building_name_no, flat_no NOT IN (
SELECT building_wing_no, building_name_no, flat_no from booked_flats);
example:
**master_flat:**
building_wing_no building_name_no flat_no status
-----------------------------------------------
a a2 104 unsold
-------------------------------------------------
b a3 105 sold
booked_flat:
custo_name custo_add custo_ph building_wing_no building_name_no flat_no
---------------------------------------------------------------------------
harish wardha 284632 b a3 105
I want result like
cust_name cust_add custo_ph building_wing_no building_name_no flat_no status
harish wardha 284632 b a3 105
--- --- --- a a2 104 unsold
I think what you really want to do is a join and not an union, but you should update your question and write what you want to display.
SELECT * FROM booked_flats b
JOIN master_flats m ON b.building_wing_no = m.building_wing_no
AND b.building_name_no = m.building_name_no AND b.flat_no = m.flat_no;
table1
att_id name tel
a1 Joe 123456
a2 Tom 456789
a3 mary 444444
a4 sue 333333
table 2
group_id desc att_id
5 red a1
10 blue a1
12 orange a1
6 green a2
5 red a2
10 blue a3
6 green a3
5 red a4
10 blue a4
6 green a4
i want 1 row per att_id, where group id <> 12. but here's the problem. if that attendee does have a group record that is 5 (red) then I want to see that.
ie
att_id a1 is not on list because it has an item in group 12, which is correct
Results:
a2 tom 456789 red
a3 mary 444444
a4 sue 333333 red
my query goes something like this:
SELECT DISTINCT Name, Phone
FROM table1 INNER JOIN table2 ON table1.Att_ID = table2.Att_ID
WHERE (table1.Att_ID Not In (select Att_ID from table2 where table2.Group_ID = 12))
this works to exclude the records from table 1 that have a group id 12 in table 2.I don't know how look at the records that get returned to determine if any of them have a 'red' group, yet still only have 1 record returned for that att_id, just with a col to indicate they had a red group record in table 1
sorry not sure how to articulate this well.
Using a DLookup() for the "red" column seems to work:
SELECT
att_id,
[name],
tel,
DLookup("[desc]", "table2", "att_id=""" & att_id & """ AND [desc]=""red""") AS red
FROM table1
WHERE att_id NOT IN (SELECT att_id FROM table2 WHERE group_id=12)
I tried finding answer to this question in SO , but could not find any. Any links will be of great help.
I have a parent table and a child table with one to many relationship between the parent and child table. The child table contains around 1 million records and I want to create a view with 1st 10 records in child table for each parent record.
Example-
Parent_Table - Fields -- ID, Name
ID Name
---- -----
1 A
2 B
3 C
Child_Table - Fields -- ID, ParentID, Date, Data
ID ParentID Date Data
--------------------------
1 1 04/10 A1
2 1 04/11 A2
3 1 04/11 A3
4 1 04/12 A4
5 1 04/12 A5
6 2 04/10 B1
7 2 04/11 B2
8 2 04/12 B3
9 2 04/12 B4
10 2 04/13 B5
11 2 04/13 B6
Now, I want to create a view with 1st 4 records for each parent record sorted by date.
Output Expected
ID ParentID Date Data
--------------------------
1 1 04/10 A1
2 1 04/11 A2
3 1 04/11 A3
4 1 04/12 A4
6 2 04/10 B1
7 2 04/11 B2
8 2 04/12 B3
9 2 04/12 B4
Any links or guide to the solution will be appreciated. Thanks in advance!
In case you need any clarification, please post a comment.
If you need to create a VIEW, you could use something like this:
CREATE VIEW First_Four AS
SELECT c1.*
FROM
Child_Table c1 LEFT JOIN Child_Table c2
ON c1.ParentID = c2.ParentID
AND (STR_TO_DATE(c1.`date`, '%m/%Y')>STR_TO_DATE(c2.`date`, '%m/%Y')
OR (STR_TO_DATE(c1.`date`, '%m/%Y')=STR_TO_DATE(c2.`date`, '%m/%Y')
AND c1.ID>c2.ID)
)
GROUP BY
c1.ID, c1.ParentID, c1.`Date`, c1.Data
HAVING
COUNT(c2.ID)<4
I'm considering the field data as a VARCHAR column, so we need to use STR_TO_DATE, if it is not we can just compare c1.date with c2.date directly.
Please see fiddle here.
I tried this one my computer and it displayed based on your requirements using your own data. I changed some field name though like ID of Child to ChildID, Date to ChildDate, Data to ChildData. Here it is:
SELECT * FROM
(SELECT ParentID, ChildID, ChildDate, ChildData, #ChildRank:= CASE WHEN #Parent <> ParentID THEN 1 ELSE #ChildRank+1 END as ChildRanking, #Parent := ParentID as Parent FROM
(SELECT #ChildRank:=0) CR, (SELECT #Parent:=1) P, (SELECT * FROM Child_Table ORDER BY
ParentID, ChildID) MainTable) AllTable WHERE ChildRanking <=4;
I use only the Child Table only but anyway you could INNER JOIN this with Parent_Table if you like.
A little explanation:
1) ChildRank will starts with Rank 0 (i.e. SELECT #ChildRank:0) but because of #ChildRank+1 it will start with Rank 1
2) When new ParentID (i.e. #Parent<> ParentID) then starts with Rank 1 right away.
3) AllTable is the alias for everything so that you could now reference the ChildRanking field.
If you don't want to display the ChildRanking field then you have to specify the fields you want to dispaly.