Im a beginner in sql query and I would like to ask 1 question.
I have 2 table which is dbo.Group_Master and dbo.Group_Assign.
TABLE : GROUP MASTER
| GROUP_ID | GROUP_DESC |
|------------ |:---------------: |
| PMD | POWER MOTOR |
| ASSY | ASSEMBLY |
| FINANCE | FINANCE DEPT |
| COSTING | COSTING DEPT |
| IE | IMPORT EXPORT |
| BBC GROUP | BBC GROUP |
| PRODUCTION | PRODUCTION DEPT |
| PURCHASING | PURCHASING |
| SALE | SALE DEPT |
| MMASTER | MATERIAL MASTER |
TABLE : GROUP_ASSIGN
| JOB_ID | STEP_ID | LOG_NAME | GROUP_ID |
|-------- |:-------: |---------- |---------- |
| J1 | 1 | ACCOUNT | PMD |
| J1 | 2 | ACCOUNT | ASSY |
| J1 | 3 | ACCOUNT | SALE |
| J1 | 1 | FREIGHT | IE |
When I joined both table and put where clause, and run query like below
SELECT
a.GROUP_ID,
a.GROUP_DESC,
CASE WHEN b.GROUP_ID IS NULL THEN '0' ELSE '1' end as status
FROM dbo.GROUP_MASTER A
LEFT JOIN dbo.GROUP_ASSIGN B ON (B.GROUP_ID = a.GROUP_ID)
WHERE B.LOG_NAME LIKE 'ACCOUNT'
It display result like this :
What I want is the result manage to display all the first 2 column and only third column is affected by where clause like below image. Is there any way i can do it ?
Expected Output
Try below query ...
SELECT
A.GROUP_ID,
A.GROUP_DESC,
CASE WHEN B.GROUP_ID IS NULL THEN '0' ELSE '1' end as status
FROM dbo.GROUP_MASTER A
LEFT JOIN (SELECT * FROM dbo.GROUP_ASSIGN WHERE LOG_NAME LIKE 'ACCOUNT') B ON (A.GROUP_ID = B.GROUP_ID)
You could try using a subquery (to fetch what you want from group_assign first) , like this :
SELECT
a.GROUP_ID,
a.GROUP_DESC,
CASE WHEN b.GROUP_ID IS NULL THEN '0' ELSE '1' end as status
FROM dbo.GROUP_MASTER A
LEFT JOIN (select GROUP_ID, LOG_NAME from dbo.GROUP_ASSIGN where LOG_NAME LIKE 'ACCOUNT') B ON (B.GROUP_ID = a.GROUP_ID)
Related
I am trying to join two tables with respect to the max values for the values column. I would like to produce the expected results as shown below based on the max value while joining
select * from order
-------------------------
| ID | value | Name |
-------------------------
| 1 | 23 | REM |
| 2 | 0 | SER |
| 3 | 13 | MH |
| 4 | 3 | MH |
| 5 | 1 | MP |
-------------------------
select * from product
-------------------------
| ID | value | Name |
-------------------------
| 1 | 2 | ABC |
| 2 | 2 | DEG |
| 3 | 17 | XYZ |
-------------------------
Desired result:
-------------------------
| ID | Value | Name |
-------------------------
| 1 | 23 | REM |
| 2 | 2 | DEG |
| 3 | 17 | XYZ |
| 4 | 3 | MH |
| 5 | 1 | MP |
-------------------------
I have tried something like below but it's not fetching the value (NAME) from other table
SELECT
MAX(IF(a.value >b.value , a.value ,b.value )) AS Value
from order a left join product b on a.ID= b.ID
Please suggest how to get the expected result from these two tables.
Below is for BigQuery Standard SQL
#standardsql
select as value array_agg(struct(id, value, name) order by value desc limit 1)[offset(0)]
from
(
select * from `project.dataset.order`
union all
select * from `project.dataset.product`
)
group by id
with output
You can do this using a full join:
select id,
(case when p.val is null or p.val < o.val then o.val else p.val end),
(case when p.val is null or p.val < o.val then o.name else p.name end)
from product p full join
order o
using (id);
I just find this the simplest way to think about the problem.
I've three tables with name employee, employee_products, product_type
reference : http://sqlfiddle.com/#!9/00436/4
I'm trying to get data as
Let this table is Table_1
using this query:
select emp.name,
(select count(*) from employee_products where product_type_id = 1 and employee_id = emp.id) as Service,
(select count(*) from employee_products where product_type_id = 2 and employee_id = emp.id) as Product,
(select count(*) from employee_products where product_type_id = 3 and employee_id = emp.id) as Other
from employee as emp;
but, i think it is not efficient and from every new product_type_id i've to alter this query, can I do this dynamically.
+------------+---------+---------+-------+--+
| Name | Service | Product | Other | |
+------------+---------+---------+-------+--+
| Bezos | 1 | 0 | 0 | |
+------------+---------+---------+-------+--+
| Steve | 0 | 3 | 0 | |
+------------+---------+---------+-------+--+
| Bill gates | 1 | 0 | 0 | |
+------------+---------+---------+-------+--+
| Tim Cook | 0 | 0 | 1 | |
+------------+---------+---------+-------+--+
and
Let this table is Table_2
In this I can't figure out how this is even possible in mysql as there is no pivot feature in mysql.
+------------+---------+---------+---------+---------+-----------+-------+
| Name | Amazon | iPhone | iPad | iPod | Microsoft | IDK |
+------------+---------+---------+---------+---------+-----------+-------+
| Bezos | Service | NULL | NULL | NULL | NULL | NULL |
+------------+---------+---------+---------+---------+-----------+-------+
| Steve | NULL | Product | Product | Product | NULL | NULL |
+------------+---------+---------+---------+---------+-----------+-------+
| Bill gates | NULL | NULL | NULL | NULL | PRODUCT | NULL |
+------------+---------+---------+---------+---------+-----------+-------+
| Tim Cook | NULL | NULL | NULL | NULL | NULL | OTHER |
+------------+---------+---------+---------+---------+-----------+-------+
Please help.
Note : There can be more than 100 items in product_type, employee_products table.
Try this for Table_1
Select name, Max(Service) as Service, Max(Product) as Product, Max(Other) as Other
From (
select e.name,
count(case when ep.product_type_id = 1 then 1 else null end) as Service,
count(case when ep.product_type_id = 2 then 1 else null end) as Product,
count(case when ep.product_type_id = 3 then 1 else null end) as Other,
from employee e
inner join employee_products ep on (e.id = ep.employee_id)
)
Group by name;
Note: same way you can try for Table_2
There are several bits of code out there for dynamically building and running the SELECT with the columns dynamically deduced. Here is mine.
I need to get emtpy fields where data is repeated
For example an customer can have two or more contact persons, so query return (just shorted qyery resul):
CUSTOMER_NAME| CONTACT_PERSON|ETC..
dell | Ighor |etc..
dell | Dima |etc..
but I'm need :
CUSTOMER_NAME| CONTACT_PERSON|etc...
dell | Ighor |etc..
NULL | Dima |etc..
SELECT
`contact`.*,
`branch_has_equipment`.*,
`branch_has_contact`.*,
`equipment`.*,
`customer_has_branch`.*,
`branch`.*,
`customer`.*,
`ip`.*
FROM `customer`
INNER JOIN `customer_has_branch`
ON `customer`.`customer_id` = `customer_has_branch`.`customer_id`
INNER JOIN `branch`
ON `customer_has_branch`.`branch_id` = `branch`.`branch_id`
INNER JOIN `branch_has_equipment`
ON `branch`.`branch_id` = `branch_has_equipment`.`branch_id`
INNER JOIN `equipment`
ON `branch_has_equipment`.`equipment_id` = `equipment`.`equipment_id`
INNER JOIN `branch_has_contact`
ON `branch`.`branch_id` = `branch_has_contact`.`branch_id`
INNER JOIN `contact`
ON `branch_has_contact`.`contact_id` = `contact`.`contact_id`
INNER JOIN `equipment_has_ip`
ON `equipment`.`equipment_id` = `equipment_has_ip`.`equipment_id`
INNER JOIN `ip`
ON `equipment_has_ip`.`equipment_id` = `ip`.`ip_id`
WHERE `customer`.`inservice` = 'Yes'
ORDER BY `customer`.`customer_name`
in additional, tables ^
Customer
customer_id
customer_name
inservice
service_type
comment
Branch
branch_id
city
address
Equipment
equipment_id
brand
model
connection_param
connection_type
serial_number
id
release
Contact
contact_id
name
surname
phone_mobile
phone_work
phone_other
position
customer_has_branch_id
customer_id
branch_id
Since I have no idea how any of those tables relate to one another, my only answer to you is to use an OUTER JOIN, which will keep NULL results.
I'm not seriously advocating this solution because really i think this sort of thing should be handled in application level code (e.g. a bit of PHP), but anyway, consider the following:
SELECT * FROM my_table;
+------+--------+--------+
| game | points | player |
+------+--------+--------+
| 1 | 5 | Adam |
| 1 | 8 | Alan |
| 1 | 7 | Brian |
| 1 | 6 | John |
| 2 | 2 | Adam |
| 2 | 3 | Alan |
| 2 | 4 | Brian |
| 2 | 6 | John |
+------+--------+--------+
SELECT IF(game= #prev,'',game)
, #prev := game
FROM my_table ORDER BY game,player;
+-------------------------+---------------+
| IF(game= #prev,'',game) | #prev := game |
+-------------------------+---------------+
| 1 | 1 |
| | 1 |
| | 1 |
| | 1 |
| 2 | 2 |
| | 2 |
| | 2 |
| | 2 |
+-------------------------+---------------+
pardon my question title, I'm not sure what should I put it, I have these two tables as below.
products orders
+------+----------+ +--------+------+-------+
| id | name | | id | qty | pid |
+------+----------+ +--------+------+-------+
| 1 | mouse | | 10001 | 20 | 1 |
| 2 | keyboard | | 10002 | 15 | 3 |
| 3 | headset | | 10004 | 5 | 3 |
+------+----------+ | 10005 | 12 | 2 |
| 10006 | 18 | 1 |
+--------+------+-------+
This is the LEFT JOIN query I am using and the output
SELECT p.id AS No, p.name AS ProductName, o.qty AS Quantity
FROM products AS p
LEFT JOIN orders AS o ON p.id = o.pid
+------+-------------+----------+
| No | ProductName | Quantity |
+------+-------------+----------+
| 1 | mouse | 20 |
| 1 | mouse | 18 |
| 2 | keyboard | 12 |
| 3 | headset | 15 |
| 3 | headset | 5 |
+------+-------------+----------+
What I am trying to achieve is an output as below:
+------+-------------+----------+
| No | ProductName | Quantity |
+------+-------------+----------+
| 1 | mouse | 20 |
| | | 18 |
| 2 | keyboard | 12 |
| 3 | headset | 15 |
| | | 5 |
+------+-------------+----------+
My question is it possible to do so? Any reply and suggestions is greatly appreciate. Thanks.
P/S: I also have tried using the GROUP_CONCAT(qty SEPARATOR ",") but it returns the result in one row as I may have more additional column to add in the Orders table in the future and it will be difficult to read.
Sure, it's possible — and without needing to use variables:
SELECT IF(c.min_oid IS NOT NULL, a.id, NULL) AS No,
IF(c.min_oid IS NOT NULL, a.name, NULL) AS ProductName,
b.qty AS Quantity
FROM products a
JOIN orders b ON a.id = b.pid
LEFT JOIN (
SELECT MIN(id) AS min_oid
FROM orders
GROUP BY pid
) c ON b.id = c.min_oid
ORDER BY a.id,
b.id
Basically what it's doing is if the row is not the minimum order id of a particular product, display blank (NULL), otherwise display the information.
SQLFiddle Demo
In this case you can use MySQL variables. I store the previous product id in the variable #prev, and only if it changes we output the product name.
http://www.sqlfiddle.com/#!2/d5fd6/9
SET #prev := NULL;
SELECT
IF( #prev = p.id, NULL, p.id) AS No,
IF( #prev = p.id, NULL, p.name) AS ProductName,
o.qty AS Quantity
,#prev := p.id
FROM products AS p
LEFT JOIN orders AS o
ON p.id = o.pid
I have a table something like this:
----------------------------------------
|uID|responseDate|field_01|field_02|etc|
+---+------------+--------+--------+---+
| 1 | 2011-12-02 | yes | no | |
| 2 | 2011-11-25 | no | yes | |
| 1 | 2012-01-02 | no | yes | |
| 2 | 2012-12-01 | no | no | |
| 3 | 2010-01-02 | yes | no | |
+---+------------+--------+--------+---+
I would like to get uIDs and responseDates for whom the latest response to field_01 is 'yes' - so my query should return:
------------------
|uID|responseDate|
+---+------------+
| 3 | 2010-01-02 |
+---+------------+
I'm using an inner join, but incorrectly. Here is my query:
SELECT f.uID, f.responseDate
FROM form_05 as f
INNER JOIN (
SELECT uID, max(responseDate) AS latest_date
FROM form_05
WHERE field_01 = 'yes'
GROUP BY uID ) dmax
ON dmax.uID = f.uID and dmax.latest_date = f.responseDate
ORDER BY f.uID ASC;
What this returns, however, is the latest entries for each uID where field_01 is yes, i.e.:
------------------
|uID|responseDate|
+---+------------+
| 1 | 2011-12-02 |
| 3 | 2010-01-02 |
+---+------------+
But I don't want that. I'd like to make it so that only the latest entry for each uID is eligible for the test. How can I restructure the query? Any pointers are greatly appreciated.
Give this a try and let me know if it doesn't work:
select t2.uid, t2.responseDate from t2
left join (
select t1.uid, max(t1.responseDate) as MaxDate from t1
group by t1.uid
) as SubQuery on t2.uid = SubQuery.uid and t2.responseDate = SubQuery.MaxDate
where MaxDate is not null and field_01 = "yes"