How to join columns of same table under different query conditon - mysql

I am explaining here I have table name TB which contains three columns and data like this
Cl1. Cl2 Cl3
0. X. A
0. Y. B
0. Z. C
1. X. a
1. X. b
1. X. c
1. Z. d
And I output like this
Cl1. Cl2. Cl3. No
0. X. A. 3
0. Y. B. 0
0. Z. C. 1
Here no column shows repetition of Cl2. Value when Cl1.=1 with respective value of Cl2. When Cl1. = 0 means for Cl1. = 1 here we can see for Cl.=1 and Cl2.=X it's value is repeated twice for Cl1.=1 similar when no match found for Cl1.=1 and Cl2.=Y no gives 0
I hope i have described my problem
I did many attempts but I didn't get any solution. My problem goes out of mind.
My few attempts are like
Select Cl1. ,Cl2. ,Cl3. ,IF(Cl2.=(Select Cl2. From TB where Cl1.=1),Cl2. ,0) as no.
From TB
where Cl2.=0
I also try join type statement for the same table but it also didn't help me.
Please guys help me. I am new on stack overflow if I did any mistake in describing my question then sorry

Try this, #Rajesh
SELECT
t1.cl1,
t1.cl2,
t1.cl3,
IFNULL(cnt, 0) as No
FROM
(
SELECT cl1, cl2, cl3 FROM tb WHERE cl1=0
) AS t1
LEFT OUTER JOIN
(
SELECT cl2, COUNT(cl2) AS cnt FROM tb WHERE cl1 <> 0 GROUP BY cl2
) AS t2
ON t1.cl2=t2.cl2

Related

SQL unwanted results in NOT query

This looks like it should be really easy question, but I've been looking for an answer for the past two days and can't find it. Please help!
I have two tables along the lines of
texts.text_id, texts.other_stuff...
pairs.pair_id, pairs.textA, pairs.textB
The second table defines pairs of entries from the first table.
What I need is the reverse of an ordinary LEFT JOIN query like:
SELECT texts.text_id
FROM texts
LEFT JOIN text_pairs
ON texts.text_id = text_pairs.textA
WHERE text_pairs.textB = 123
ORDER BY texts.text_id
How do I get exclusively the texts that are not paired with A given textB? I've tried
WHERE text_pairs.textB != 123 OR WHERE text_pairs.textB IS NULL
However, this returns all the pairs where textB is not 123. So, in a situation like
textA TextB
1 3
1 4
2 4
if I ask for textB != 3, the query returns 1 and 2. I need something that will just give me 1.
The comparison on the second table goes in the ON clause. Then you add a condition to see if there is no match:
SELECT t.text_id
FROM texts t LEFT JOIN
text_pairs tp
ON t.text_id = tp.textA AND tp.textB = 123
WHERE tp.textB IS NULL
ORDER BY t.text_id ;
This logic is often expressed using NOT EXISTS or NOT IN:
select t.*
from texts t
where not exists (select 1
from text_pairs tp
where t.text_id = tp.textA AND tp.textB = 123
);

MySQL WHERE in IF Statement of View

I'm trying to create a view of 3 tables. There is one line that is messing up everything and I can't get it right. Its in the "IF" statement below. I know I can't use WHERE inside an if statement, but that is what I'm trying to achieve.
Table 'c' (The qms_dpus table) is a table that stores ERRORS for each job. What I'm trying to do is say
"for every job that is inside table 'a', check table 'c' to see if the JOBNUM columns match AND the IS_COMPLETE column is 0. If that is true then put a 1 in this new Column OPEN_DPUS. If it is false, then put a 0 in the column"
CREATE OR REPLACE VIEW `view_qms_linea` AS SELECT
a.ID AS `ID`,
a.JID AS `JID`,
b.HPL AS `HPL`,
b.LINE AS `LINE`,
a.PAREA AS `PRODAREA`,
a.JOBNUM AS `JOBNUM`,
b.MODELNUMBER AS `MODELNUMBER`,
b.CUSTOMER AS `CUSTOMER`,
b.PM_AE AS `PM_AE`,
b.PM_PE AS `PM_PE`,
b.PM_DE AS `PM_DE`,
b.PM_EE AS `PM_EE`,
b.PM_ED AS `PM_ED`,
b.PM_CE AS `PM_CE`,
IF(c.IS_COMPLETE=0 WHERE c.JOBNUM LIKE a.JOBNUM, 1, 0) AS `OPEN_DPUS`
FROM `gen_jobs_in_production` a, `gen_jobs_table` b, `qms_dpus` c
WHERE
a.JOBNUM LIKE b.WO
AND
b.LINE = 1
AND
a.PAREA != 4
AND
a.PAREA <= 7;
When i remove the IF statement (and the reference to table 'c') the code works great and provides me with what I need aside from the column OPEN_DPUS, but if I replace the IF statement with this:
IF(c.JOBNUM LIKE a.JOBNUM AND c.IS_COMPLETE=0,1,0) AS `OPEN_DPUS`
My view provides me with all of the data, but instead of being 12 records (currently), It repeats every record 12 times and repeats that pattern infinitely.
I would love to display my table structure, but I have many columns and would only make this more confusing...
Any help would be greatly appreciated!
Your query was missing join conditions. Hence it was returning you the cross joined results of the 3 tables in your query. Try the query below.
CREATE OR REPLACE VIEW `view_qms_linea` AS SELECT
a.ID AS `ID`,
a.JID AS `JID`,
b.HPL AS `HPL`,
b.LINE AS `LINE`,
a.PAREA AS `PRODAREA`,
a.JOBNUM AS `JOBNUM`,
b.MODELNUMBER AS `MODELNUMBER`,
b.CUSTOMER AS `CUSTOMER`,
b.PM_AE AS `PM_AE`,
b.PM_PE AS `PM_PE`,
b.PM_DE AS `PM_DE`,
b.PM_EE AS `PM_EE`,
b.PM_ED AS `PM_ED`,
b.PM_CE AS `PM_CE`,
--change from here
IF(c.IS_COMPLETE = 0, 1, 0) AS `OPEN_DPUS`
FROM `gen_jobs_in_production` a
JOIN `gen_jobs_table` b ON a.JOBNUM = b.WO
JOIN `qms_dpus` c ON c.jobnum = a.jobnum
WHERE b.LINE = 1
AND a.PAREA != 4
AND a.PAREA <= 7;

Join two queries to get particular table format

I have two tables X and Y
SELECT ser.entity_type,ser.param_name
FROM X as ser
where
ser.group_name like '%CRVJL%'
and ser.param_name like '%CBV%'
and ser.entity_type like '%SIGNON%'
SELECT reg.entity_type
FROM REGISTER_TB as reg
where
reg.group_name like '%CRVJL%' and reg.PROV_NAME like'%CBV%' and reg.PROV_NAME like'%-OD-%' and reg.ENTITY_TYPE like '%SIGNON%'
I get some values from table x lets say 4 rows,i have to check if they are present in y or not if not then as -1 and if present then +1
Table should look like this
entity type param_name status
SIGNON CBV6815_I-OD-I 1
SIGNON CBV7815_I-OD-I 1
SIGNON CBV8815_I-OD-I -1
SIGNON CBV9815_I-OD-I 1
we have equal columns
X Y
entity_type entity_type
param-value prov_name
I tried using left join union and right join but its not giving me output as i want.Can anybody help me?
Use this sort of query, you'll have to replace table/field name to match your schema
SELECT a.entity_type, a.param_name, IF(b.status IS NULL, -1, 1) AS 'status'
FROM
X AS a
LEFT OUTER JOIN Y AS b ON a.entity_type=b.entity_type AND a.group_name=b.group_name
WHERE
a.group_name LIKE '%CRVJL%'
AND a.param_name LIKE '%CBV%'
AND a.entity_type LIKE '%SIGNON%'
AND b.prov_name LIKE '%CBV-OD-%'

MySQL - return records for clients unless they have a specific value and only that value

Trying to wrap my head around how to do this query - I want to return a list of client records and need to exclude clients if they had only a specific value and no other values.
For example
c# value
1 X
1 Y
2 X
3 Y
I want all the records for clients 1 and 3, since they had a value other than X. I do not want client 2, because that client had ONLY X.
I for example want returned in this case:
1 X
1 Y
3 Y
Of course, I could have lots of other records with other client id's and values, but I want to eliminate only the ones that have a single "X" value and no other values.
Maybe using a sub-query?
Try this:
SELECT client, value FROM myTable where `client` in
(select distinct(client) from myTable where value !='X');
Returns:
Client Value
1 X
1 Y
3 Y
Something like this
SELECT ABB2.*
FROM
mytable AS ABB2
JOIN
(SELECT c
FROM mytable
WHERE value <> "X"
GROUP BY c) AS ABB1 ON ABB1.c = ABB2.c
GROUP BY ABB2.c, ABB2.value
It's faster than using a WHERE clause to identify the sub query results (as in Mike's answer)

how to search for a given sequence of rows within a table in SQL Server 2008

The problem:
We have a number of entries within a table but we are only interested in the ones that appear in a given sequence. For example we are looking for three specific "GFTitle" entries ('Pearson Grafton','Woolworths (P and O)','QRX - Brisbane'), however they have to appear in a particular order to be considered a valid route. (See image below)
RowNum GFTitle
------------------------------
1 Pearson Grafton
2 Woolworths (P and O)
3 QRX - Brisbane
4 Pearson Grafton
5 Woolworths (P and O)
6 Pearson Grafton
7 QRX - Brisbane
8 Pearson Grafton
9 Pearson Grafton
So rows (1,2,3) satisfy this rule but rows (4,5,6) don't even though the first two entries (4,5) do.
I am sure there is a way to do this via CTE's but some help would be great.
Cheers
This is very simple using even good old tools :-) Try this quick-and-dirty solution, assuming your table name is GFTitles and RowNumber values are sequential:
SELECT a.[RowNum]
,a.[GFTitle]
,b.[GFTitle]
,c.[GFTitle]
FROM [dbo].[GFTitles] as a
join [dbo].[GFTitles] as b on b.RowNumber = a.RowNumber + 1
join [dbo].[GFTitles] as c on c.RowNumber = a.RowNumber + 2
WHERE a.[GFTitle] = 'Pearson Grafton' and
b.[GFTitle] = 'Woolworths (P and O)' and
c.[GFTitle] = 'QRX - Brisbane'
Assuming RowNum has neither duplicates nor gaps, you could try the following method.
Assign row numbers to the sought sequence's items and join the row set to your table on GFTitle.
For every match, calculate the difference between your table's row number and that of the sequence. If there's a matching sequence in your table, the corresponding rows' RowNum differences will be identical.
Count the rows per difference and return only those where the count matches the number of sequence items.
Here's a query that implements the above logic:
WITH SoughtSequence AS (
SELECT *
FROM (
VALUES
(1, 'Pearson Grafton'),
(2, 'Woolworths (P and O)'),
(3, 'QRX - Brisbane')
) x (RowNum, GFTitle)
)
, joined AS (
SELECT
t.*,
SequenceLength = COUNT(*) OVER (PARTITION BY t.RowNum - ss.RowNum)
FROM atable t
INNER JOIN SoughtSequence ss
ON t.GFTitle = ss.GFTitle
)
SELECT
RowNum,
GFTitle
FROM joined
WHERE SequenceLength = (SELECT COUNT(*) FROM SoughtSequence)
;
You can try it at SQL Fiddle too.