SQL Finding Multiple Values - mysql

I have a table with fields named SVCode and LocationCode. I want to be able to focus on one all of the SVCodes listed in the column and check to see if their LocationCode is different.
Example:
LocationCode:
107654403
107654403
107653802
107653802
107656502
126515001
128030852
126515001
SVCode:
STN10
STN10
STN10
STN10
STN10
STN10
GIN04
GIN04
GIN04
Each row matches up. LocationCode 107654403 with STN10 and so forth.
I want to basically create a new column called MultipleLocations and if a SVCode has more than one distinct Location code then set the MultipleLocation column = 1 else 0
Any help on doing so?

You can use something like this to identify those with multiple Locations:
SELECT SVCode
FROM (SELECT DISTINCT LocationCode, SVCode
FROM Table
)sub
GROUP BY SVCode
HAVING COUNT(*) >1

Taking some liberties with your column names, but
SELECT l.*, s.*, IF(COUNT(s.id) > 1,1,0) AS multipleLocation FROM LocationCode l
INNER JOIN SVCode s ON s.locationCodeId = l.id
GROUP BY l.id

select distinct SVCode
from Table1 t1
where exist
(select LocationCode from table1 where t1.SVCode = SVCode and
LocationCode != t1.LocationCode );
this will give you list of all SVcode with multiple locations.

This should work.
UPDATE t1
SET MultipleLocation = 1
WHERE SVCode IN (
SELECT DISTINCT SVCode FROM t1
WHERE COUNT(SVCode) > 1
GROUP BY SVCode
)
Good luck.

Related

Count non null values from a left joint table

Count non-null values directly from select statement (not using where) on a left joint table
count(*) as comments Need this to provide count of non-null values only. Also, inner join is not a solution because, that does not include content which have zero comments in count(distinct (t1.postId)) as no_of_content
select t1.tagId as tagId, count(distinct (t1.postId)) as no_of_content, count(*) as comments
from content_created as t1
left join comment_created as t2
on t1.postId=t2.postId
where
( (t1.tagId = "S2036623" )
or (t1.tagId = "S97422" )
)
group BY 1
Though Posting the sample data might help us more to answer this but you can update your count function to -
COUNT(CASE WHEN postId IS NULL THEN 1 END) as comments
Count only counts non-null values. What you need to do is reference the right hand side table's column explicitly. So instead of saying count(*) use count(right_joined_table.join_key).
Here's a full example using BigQuery:
with left_table as (
select num
from unnest(generate_array(1,10)) as num
), right_table as (
select num
from unnest(generate_array(2,10,2)) as num
)
select
count(*) as total_rows,
count(l.num) as left_table_counts,
count(r.num) as non_null_counts
from left_table as l
left outer join right_table as r
on l.num = r.num
This gives you the following results:

mySQL: Select and a count(*) from different table?

I have two tables: 1 Table wccrm_orders where all orders are stored, and a table wccrm_kunden where all the user data is stored. In wccrm_orders, I have a date-field "ordered_date" and in wccrm_kunden a date-field "Anprobe"
When I want to select the wccrm_orders I use that code:
Select Year(wccrm_orders.ordered_date) as Jahr,
Month(wccrm_orders.ordered_date) as Monat, round(sum(wccrm_orders.preis)) as Summe,
count(*) as Anzahl from wccrm_orders
GROUP BY YEAR(wccrm_orders.ordered_date), MONTH(wccrm_orders.ordered_date)
When I want to count(*) all the appointments from wccrm_kunden.anprobe I use this code:
Select Year(wccrm_kunden.anprobe) as Jahr, Month(wccrm_kunden.anprobe) as
Monat, count(*) as Anzahl from wccrm_kunden where wccrm_kunden.status = 1
GROUP BY YEAR(wccrm_kunden.anprobe), MONTH(wccrm_kunden.anprobe)
How can I combine these codes? How can I achieve it, to get the numbers of trials (appointments) into the first SELECT?
Thank you very much for your help!
BR,
Stefan
A couple of sub-queries may work:
SELECT A.Jahr, A.Monat, A.Summe, A.Anzahl, B.AnzahlB
FROM
(SELECT Year(wccrm_orders.ordered_date) as Jahr,
Month(wccrm_orders.ordered_date) as Monat,
round(sum(wccrm_orders.preis)) as Summe,
count(*) as Anzahl
FROM wccrm_orders
GROUP BY
YEAR(wccrm_orders.ordered_date),
MONTH(wccrm_orders.ordered_date)) AS A
LEFT OUTER JOIN
(SELECT Year(wccrm_kunden.anprobe) as Jahr,
Month(wccrm_kunden.anprobe) as Monat,
count(*) as AnzahlB
FROM wccrm_kunden
WHERE wccrm_kunden.status = 1
GROUP BY
YEAR(wccrm_kunden.anprobe),
MONTH(wccrm_kunden.anprobe)) AS B
ON A.Jahr = B.Jahr AND A.Monat = B.Monat
(Sorry, don't have the schema for this DB, so there may be a syntax error (or three!) in this code, but hopefully you get the idea.)

How to select first data of same id and sum column same id sql

I have data like
|id|id_barang|in|out|stock|
|1|1|2|0|2|
|2|1|2|0|4|
|3|1|0|1|3|
|4|2|2|0|2|
|5|2|1|0|3|
|6|2|0|1|2|
I want result is group by id_barang
|Id_barang|sum(in)|sum(out)|first(stock)|
|1|4|1|2|
|2|3|1|2|
You can use the MIN function for the first stock. It works similarly to SUM or COUNT.
You could try something like:
SELECT Id_barang, SUM(in), SUM(out), FIRST(stock)
FROM data
GROUP BY Id_barang
Unfortunately Mysql doesn't support Window functions. Try this
SELECT b.id_barang,
sum_in,
sum_out,
a.stock
FROM yourtable A
INNER JOIN (SELECT id_barang,
Sum(in) sum_in,
Sum(OUT) sum_out,
Min(id) AS min_id
FROM yourtable
GROUP BY id_barang) B
ON a.id_barang = b.id_barang
AND a.id = b.min_id
this query fetch all record of table group by Id_barang. its give first record of stock.
SELECT t1.Id_barang, sum(t1.in), sum(t1.out), (select min(t2.stock)
from table_name t2 where t2.Id_barang = t1.Id_barang)
FROM table_name t1
GROUP BY Id_barang
you can make query this way :
SELECT id_barang,SUM(in) as total_in,SUM(out) as total_out,stock
FROM stock
GROUP BY id_barang
if you found any error then you must change your column name, i think 'in' and 'out' is not valid. you change 'in' into 'in_change' and 'out' into 'out_change'. try with this column it works as you want. i did try....
SELECT id_barang,SUM(in_change) as total_in,SUM(out_change) as total_out,stock
FROM stock
GROUP BY id_barang

SQL subquery logic confusion

I'm writing an SQL query but I'm stuck at a point and can't figure out how to solve this issue. First have a look at the query below:
SELECT user_id, COUNT(*) as count
FROM hwc_attend
WHERE at_id IN
(SELECT evdet_id
FROM eve_detail
WHERE evdet_id IN (SELECT at_id FROM hwc_attend WHERE attendstate=1 )
AND location <> ''
AND evdet_id > 999
AND location NOT IN (SELECT ASIN FROM pReviews )
)
GROUP BY user_id
This query is working fine but giving lesser results than required because the part SELECT ASIN FROM pReviews should be like SELECT ASIN FROM pReviews where cID={place current value of "location" field from table eve_detail here.
For a better understanding, here's the errornous query:
SELECT user_id, COUNT(*) as count
FROM hwc_attend
WHERE at_id IN
(SELECT evdet_id **, location**
FROM eve_detail
WHERE evdet_id IN (SELECT at_id FROM hwc_attend WHERE attendstate=1)
AND location <> ''
AND evdet_id > 999
AND location NOT IN (SELECT ASIN FROM pReviews where cID=**location**)
)
GROUP BY user_id
It's hard to explain.. In short, I have to remove "location" values from the result fetched from table "eve_detail" that also exist in table "pReviews" in column cID.
Additionally, it would be nice if someone can covert it into joins. I would need both queries for learning.
Translating it to a join would use something like this. Using a LEFT OUTER JOIN and checking for NULL instead of NOT IN. I am assuming that hwc_attend has a unique column called id which is used in the count to get distinct rows.
SELECT ha1.user_id, COUNT(DISTINCT ha1.id) as count
FROM hwc_attend ha1
INNER JOIN eve_detail ed ON ha1.at_id = ed.evdet_id
INNER JOIN hwc_attend ha2 ON ed.evdet_id = ha2.at_id
LEFT OUTER JOIN pReviews pr ON ed.location = pr.ASIN AND cID = **location**
WHERE ha2.attendstate = 1
AND ed.location <> ''
AND ed.evdet_id > 999
AND pr.ASIN IS NULL
GROUP BY ha1.user_id
Change
AND location NOT IN (SELECT ASIN FROM pReviews )
To
AND NOT EXISTS (SELECT ASIN FROM pReviews WHERE eve_detail.location = ASIN.cID )

need some help from count query

I have a table called
tb_applicants with fields id, aic, name
app_interview with fields id, atic, atname
My problem is i want to count all (atic) from app_interview table where atic is equal to aic from table (tb_applicants) group by 1(aic) from tb_applicants
In my current query its not working can anyone help me find where is the problem it gives me 0 count all the time.
query:
SELECT count(t.atic)
FROM app_interview as t
INNER JOIN tb_applicants as t2
WHERE t.atic = t2.aic
GROUP BY t2.aic;
Remove the ; and use ON for JOINS:
SELECT count(*) FROM app_interview INNER JOIN tb_applicants ON tb_applicants.aic = app_interview.atic GROUP BY tb_applicants.aic;
Could probably done simpler, since you need only matching rows:
SELECT count(t.atic)
FROM app_interview as t, tb_applicants as t2
WHERE t.atic = t2.aic
GROUP BY t.atic;