I have table with the following:
ID TYPE ProjectID Date Rev
1 A 1 30-1-2010 500
2 B 1 28-02-2011 580
2 B 2 30-04-2011 540
2 B 3 03-04-2019 440
Results:
ID TYPE ProjectID Date Rev
1 A 1 30-1-2010 500
1 A 2 01-01-2000 0
1 A 3 01-01-2000 0
2 B 1 28-02-2011 580
2 B 2 30-04-2011 540
2 B 3 03-04-2019 440
I want to write an SQL query in which, whenever there is Type “A”, two rows should automatically be inserted with project id 2 and 3 and with default Date and Rev data.
Currently, I am using UNION function to add this data manually, but I want to do it automatically.
I am not sure how to do this in SQL.
If I understand correctly:
select it.id, it.type, p.projectid,
coalesce(t.date, '2000-01-01') as date, coalesce(t.rev, 0) as rev
from (select distinct id, type from t) it cross join
(select distinct projectid from t) p left join
t
on t.id = it.id and t.type = it.type and t.projectid = p.projectid;
This fills in the missing values, so all id/type combinations have all projects.
Related
so I have 3 tables in my db, where all 3 tables contains a column which have similar data, but name of that column is different on all the 3 tables, below is an example.
Ban Table
user_id
ban_user_id
ban_date
reason
end_date
1300
1
xyz
xyz
xyz
32
1
xyz
xyz
xyz
43
2
xyz
xyz
xyz
Reports Table
user_id
last_modified_user_id
report_date
reason
end_date
1300
1
xyz
xyz
xyz
32
2
xyz
xyz
xyz
43
2
xyz
xyz
xyz
Warning Table
user_id
warning_user_id
warning_date
reason
end_date
1300
1
xyz
xyz
xyz
32
2
xyz
xyz
xyz
43
3
xyz
xyz
xyz
Now I want to fetch data by combining these 3 tables, where ban_user_id, last_modified_user_id, and warning_user_id contains the data of staff member who took the actions, so i want to group the data by the staff id.
The output i am looking for is as follows:
staff_id
total_reports
total_bans
total_warnings
1
1
2
1
2
2
1
1
3
0
0
1
where it is counting the data for each table by grouping the 2nd column, ban_user_id, last_modified_user_id, warning_user_id respectively. And than combining the data.
I tried things with UNION All and stuffs, but it didn't work out.
Thankyou in advance for your help
Use UNION ALL for all 3 tables and then aggregate:
SELECT staff_id,
COUNT(report) AS total_reports,
COUNT(ban) AS total_bans,
COUNT(warning) AS total_warnings
FROM (
SELECT last_modified_user_id AS staff_id, 1 AS report, null AS ban, null AS warning FROM Reports
UNION ALL
SELECT ban_user_id, null, 1, null FROM Ban
UNION ALL
SELECT warning_user_id, null, null, 1 FROM Warning
) t
GROUP BY staff_id;
Or:
SELECT staff_id,
SUM(report) AS total_reports,
SUM(ban) AS total_bans,
SUM(warning) AS total_warnings
FROM (
SELECT last_modified_user_id AS staff_id, 1 AS report, 0 AS ban, 0 AS warning FROM Reports
UNION ALL
SELECT ban_user_id, 0, 1, 0 FROM Ban
UNION ALL
SELECT warning_user_id, 0, 0, 1 FROM Warning
) t
GROUP BY staff_id;
See the demo.
You can use Table joins (Inner/outer/left/right) to get the data instead of union.
I'm assuming the staff_id is the equivalent of user_id column as you haven't mentioned anything about that, so your script will look something like this:
SELECT W.user_id AS staff_id,
B.ban_user_id,
R.last_modified_user_id,
W.warning_user_id
FROM Warning AS W
LEFT JOIN Reports AS R on R.user_id = W.user_id
LEFT JOIN Ban AS B on B.user_id = W.user_id
group by W.user_id
I am currently working on a project while trying to learn MySQL and I would like to join three tables and get the latest status for each related shipment. Here are the tables I'm working with (with example data):
shipments
id
consignee
tracking_number
shipper
weight
import_no
1
JOHN BROWN
TBA99900000121
AMAZON
1
101
2
HELEN SMITH
TBA99900000190
AMAZON
1
102
3
JACK BLACK
TBA99900000123
AMAZON
1
103
4
JOE BROWM
TBA99900000812
AMAZON
1
104
5
JULIA KERR
TBA99900000904
AMAZON
1
105
statuses
id
name
slug
1
At Warehouse
at_warehouse
2
Ready For Pickup
ready_for_pickup
3
Delivered
delivered
shipment_status (pivot table)
id
shipment_id
status_id
1
1
1
2
2
1
3
3
1
4
4
1
5
5
1
6
1
2
7
2
2
8
3
2
9
4
2
10
5
2
all tables do have created_at and updated_at timestamp columns
Example of the results I'm trying to achieve
slug
shipment_id
status_id
ready_for_pickup
1
2
ready_for_pickup
2
2
ready_for_pickup
3
2
ready_for_pickup
4
2
ready_for_pickup
5
2
Here's the query I wrote to try to achieve what I'm looking for based on examples and research I did during the past couple of days. I find that sometimes there is sometimes a mismatch with the latest status that relates to the shipment
SELECT
statuses.slug AS slug,
MAX(shipments.id) AS shipment_id,
statuses.id AS status_id,
FROM
`shipments`
INNER JOIN `shipment_status` ON `shipment_status`.`shipment_id` = `shipments`.`id`
INNER JOIN `statuses` ON `shipment_status`.`status_id` = `statuses`.`id`
GROUP BY
`shipment_id`
Because we need to reference other fields from the same record that evaluates from the MAX aggregation, you need to do it in two steps, there are other ways, but I find this syntax simpler:
SELECT
shipments.id AS id,
statuses.slug AS slug,
statuses.id AS status_id,
shipment_status.shipment_id as shipment_id
FROM
`shipments`
INNER JOIN `shipment_status` ON `shipment_status`.`shipment_id` = `shipments`.`id`
INNER JOIN `statuses` ON `shipment_status`.`status_id` = `statuses`.`id`
WHERE
shipment_status.id = (
SELECT MAX(shipment_status.id)
FROM `shipment_status`
WHERE shipment_status.shipment_id = shipments.id
)
try it out!
This query makes the assumption that the id field is an identity column, so the MAX(shipment_status.id) represents only the most recent status for the given shipment_id
You can use window functions:
SELECT s.id, st.slug, st.id
FROM shipments s JOIN
(SELECT ss.*,
ROW_NUMBER() OVER (PARTITION BY shipment_id ORDER BY ss.id DESC) as seqnum
FROM shipment_status ss
) ss
ON ss.shipment_id = s.id JOIN
statuses st
ON ss.status_id` = st.id
WHERE ss.seqnum = 1;
Also note the use of table aliases so the query is easier to write and to read.
I have two tables to join in SQL using the ID column in both. Table 1 has only unique values of ID as follows and I want to keep all columns of this table:
ID code 1 code 2
1 123 99
2 222 09
3 344 13
Table 2 has multiple rows of each ID as follows:
ID application_time Application Number
1 11jan2004 123
2 15oct2010 124
1 24nov2008 845
3 05sep2010 166
1 07feb2001 865
2 24aug2017 545
3 12mar2009 233
2 11dec2001 811
So, from table 2, I want to add the total count of each ID, and Min and Max of Application_time to table 1. I also need to count the number Application Numbers that start with 8. of I do not know where I should use group by (). So the outcome should look like:
ID code 1 code 2 count Min (application_time) Max (application_time)
1 123 99 3 07feb2001 24nov2008
2 222 09 3 11dec2001 24aug2017
3 344 13 2 12mar2009 05sep2010
Count of Application Number starting with 8
2
1
0
here is how you can do it:
select
t1.Id
,t1.code1
,t1.code2
, count(*) count
,min(application_time)
,max(application_time)
, sum( case when left( t2.application number, 1 ) = '8' then 1 else 0 end )
from table1 t1
join table2 t2
on t1.Id = t2.Id
group by
t1.Id
,t1.code1
,t1.code2
I have the following database tables.
my_left_table
left_id name
1 A
2 B
3 C
my_right_tabe
right_id thing left_id_fk status
1 D 1 new
2 E 1 new
3 F 2 old
4 G 3 old
5 H 3 new
6 I 3 new
7 J 1 old
8 K 2 old
9 L 2 new
10 M 3 old
11 N 3 old
12 O 1 new
My desired result is as follow.
my_left_table
left_id name
3 C
How do I select the left records which its right records have AT LEAST 2 status is new AND 2 status is old. For example, left_id 1 is not the target because three of its right records have the status new but only one record has the status old.
So far I have is.
SELECT *, COUNT(my_right_tabe.left_id_fk) AS count_left_id_fk
FROM my_left_table
INNER JOIN my_right_tabe
ON my_left_table.id = my_right_tabe.left_id_fk
GROUP BY my_right_tabe.left_id_fk
Use the HAVING clause in MySQL
Like the following
SELECT my_left_table.left_id, my_left_table.name
FROM my_left_table
INNER JOIN my_right_tabe
ON my_left_table.left_id = my_right_tabe.left_id_fk
GROUP BY my_right_tabe.left_id_fk
HAVING SUM(my_right_tabe.status="new") >= 2 AND
SUM(my_right_tabe.status="old") >= 2
You can achieve desired results by first grouping the values and then check its total. If its >= 2 pull that record.
Here is the query
SELECT z.*
FROM
(
SELECT a.left_id, name, status, IF(COUNT(*) >=2, 1, 0) AS status_calc
FROM my_left_table a JOIN my_right_table b
ON a.left_id = b.left_id_fk
GROUP BY left_id, status
) z
GROUP BY z.left_id
HAVING SUM(status_calc) = 2;
Working Demo
I have a table of data like this:
id user_id A B C
=====================
1 15 1 2 3
2 15 1 2 5
3 20 1 3 9
4 20 1 3 7
I need to remove duplicate user ids and keep the record that sorts lowest when sorting by A then B then C. So using the above table, I set up a temp query (qry_temp) that simply does the sort--first on user_id, then on A, then on B, then on C. It returns the following:
id user_id A B C
====================
1 15 1 2 3
2 15 1 2 5
4 20 1 3 7
3 20 1 3 9
Then I wrote a Totals Query based on qry_temp that just had user_id (Group By) and then id (First), and I assumed this would return the following:
user_id id
===========
15 1
20 4
But it doesn't seem to do that--instead it appears to be just returning the lowest id in a group of duplicate user ids (so I get 1 and 3 instead of 1 and 4). Shouldn't the Totals query use the order of the query it's based upon? Is there a property setting in the query that might impact this or another way to get what I need? If it helps, here is the SQL:
SELECT qry_temp.user_id, First(qry_temp.ID) AS FirstOfID
FROM qry_temp
GROUP BY qry_temp.user_id;
You need a different type of query, for example:
SELECT tmp.id,
tmp.user_id,
tmp.a,
tmp.b,
tmp.c
FROM tmp
WHERE (( ( tmp.id ) IN (SELECT TOP 1 id
FROM tmp t
WHERE t.user_id = tmp.user_id
ORDER BY t.a,
t.b,
t.c,
t.id) ));
Where tmp is the name of your table. First, Last, Min and Max are not dependent on a sort order. In relational databases, sort orders are quite ephemeral.