Error "Ambiguous column name SubscriberKey" - mysql

Select A.SubscriberKey, COUNT(DISTINCT EventDate) AS Count,B.CreatedDate
From _Open A
JOIN _ListSubscribers B
ON A.SubscriberKey = B.SubscriberKey
Where B.ListID = '10630'
Group By SubscriberKey
HAVING COUNT(DISTINCT EventDate) = 1

You need to specify the table that the column comes from as the same column name exists in both tables.
i.e. use Group By A.SubscriberKey as that is what you have in the SELECT list.
Also all RDBMSs except for MySQL will require you to also add B.CreatedDate to the GROUP BY list as that is in the SELECT list

You have more than one table that contains a column named after that name, so you need to specify the table containing the column referred to in your Group By SubscriberKey:
Group By A.SubscriberKey

Related

VĂ¡rios GROUP BY MySql

I'm trying to mount the sql that does the following query, in the TYPE field there are both type CUT and RELIANCE, but I need each of them to come in a column, but only I can now show it in each one in a line is possible to do so?
SELECT id_user, date_conclusion, type, COUNT (*)
FROM the
WHERE id_user = 8 and date_conclusion LIKE '2017-12%'
GROUP by date_conclusion, type
Assuming that only these two types exist. You do not need the extra type column then:
SELECT
id_user,
date_conclusion,
SUM(type = 'CORTE') AS 'CORTE',
SUM(type = 'RELIGACAO') AS 'RELIGACAO'
FROM
the
WHERE
id_user = 8 AND
date_conclusion LIKE '2017-12%'
GROUP BY
date_conclusion

Get Highest value from items with same IDs

I have a table like this:
ID Severity WorkItemSK
23636 3-Minor 695119
23636 3-Minor 697309
23647 2-Major 695081
23647 2-Major 694967
In here I have several WorkItems that share the same ID. How can I get unique IDs that have the highest WorkItem?
So it would like this:
ID Severity WorkItemSK
23636 3-Minor 697309
23647 2-Major 695081
Help the noob :) Mind giving a clue what SQL commands (again I am a noob) should I use? Or an example of a query?
Thank you in advance!
Assuming that Severity can change depending on the WorkItemSK, you'll want to use the following query:
Select T.ID, T.Severity, T.WorkItemSK
From Table T
Join
(
Select ID, Max(WorkItemSK) As WorkItemSK
From Table
Group By ID
) D On T.WorkItemSK = D.WorkItemSK And T.ID = D.ID
The last Join condition of T.ID = D.ID may or may not be needed, depending on whether WorkItemSK can appear multiple times in your table.
Otherwise, you can just use this:
Select ID, Severity, Max(WorkItemSK) As WorkItemSK
From Table
Group by ID, Severity
But if you have different Severity values per ID, you'll see duplicate IDs.
Use select with GROUP BY: SELECT id,MAX(WorkItemSK) FROM table GROUP BY id;

multiple query with sqlite 2nd query gives only first result

SELECT quantity, materialTypeId ,
(SELECT typeName
FROM invTypes
WHERE TypeID IN (SELECT materialTypeId
FROM invTypeMaterials
WHERE typeId= 12743
)
) AS material
FROM invTypeMaterials
WHERE TypeID=12743
so this query gives me nice results except the column material. only shows me the first entry instead of giving the name of each row.
if i run these sql seperate they work and i do see what i want. i just need them combined into 2 columns.
what i want to do is, i query one table for data, one of the column has a value wich i want to convert to a name, and that is in another table and its linked by a unique TypeID
Chilly
May be this will work :
SELECT tm.quantity, tm.materialTypeId , t.typeName
FROM invTypeMaterials tm
INNER JOIN invTypes t ON t.TypeID = tm.materialTypeId
WHERE tm.TypeID=12743
If you want to lookup the materialTypeID's name for the current record, you must not use a separate subquery but use the materialTypeID value from the outer query.
This is called a correlated subquery:
SELECT quantity, materialTypeId,
(SELECT typeName
FROM invTypes
WHERE TypeID = invTypeMaterials.materialTypeId
) AS material
FROM invTypeMaterials
WHERE TypeID=12743

DISTINCT with as clause

$query="SELECT a.pk_i_id,a.i_price,b.s_title,c.pk_i_id AS img_id,c.s_extension,d.s_city,d.s_city_area from zl_t_item a, zl_t_item_description b, zl_t_item_resource c, zl_t_item_location d where a.fk_i_category_id=$cat_id and a.pk_i_id=b.fk_i_item_id and a.pk_i_id=c.fk_i_item_id and a.pk_i_id=d.fk_i_item_id ORDER BY a.dt_pub_date DESC";
In this above query i need to add DISTINCT before this c.pk_i_id AS img_id ??
it shows error when i did like below
$query="SELECT a.pk_i_id,a.i_price,b.s_title,DISTINCT c.pk_i_id AS img_id,c.s_extension,d.s_city,d.s_city_area from zl_t_item a, zl_t_item_description b, zl_t_item_resource c, zl_t_item_location d where a.fk_i_category_id=$cat_id and a.pk_i_id=b.fk_i_item_id and a.pk_i_id=c.fk_i_item_id and a.pk_i_id=d.fk_i_item_id ORDER BY a.dt_pub_date DESC";
what is the problem on it?.
It is invalid use of DISTINCT keyword. You can only apply it on a set of columns and not for a specific column skipping other columns
DISTINCT should be applied right after SELECT for a column or set of columns you cannot use DISTINCT between the columns
SELECT DISTINCT c.pk_i_id AS img_id,
a.pk_i_id,a.i_price,b.s_title,c.s_extension,d.s_city,d.s_city_area
from zl_t_item a, zl_t_item_description b, zl_t_item_resource c,
zl_t_item_location d where a.fk_i_category_id=$cat_id
and a.pk_i_id=b.fk_i_item_id and a.pk_i_id=c.fk_i_item_id
and a.pk_i_id=d.fk_i_item_id ORDER BY a.dt_pub_date DESC
In general, using DISTINCT is performance kill.
DISTINCT is actually a filter to remove duplicates.
So, while selecting multiple columns the DISTINCT clause should be applied to the complete set rather than a single column.
Hence you are seeing an error.
The query can be rewritten based on the requirements. If you want filter out duplicates then either you can apply row rank, or group by and having clause to achieve the intended results.
DISTINCT always works on all columns, you might must put it directly after SELECT.
In MySQL there's an easy way to get only one row per img_id, add a GROUP BY img_id
SELECT
a.pk_i_id
,a.i_price
,b.s_title
,c.pk_i_id AS img_id
,c.s_extension
,d.s_city
,d.s_city_area
from
zl_t_item a
,zl_t_item_description b
,zl_t_item_resource c
,zl_t_item_location d
where
a.fk_i_category_id = $cat_id
and a.pk_i_id = b.fk_i_item_id
and a.pk_i_id = c.fk_i_item_id
and a.pk_i_id = d.fk_i_item_id
GROUP BY img_id
ORDER BY
a.dt_pub_date DESC
Of course this is a proprietary MySQL syntax which breaks all the rules of relational dabatabses and will not work with any other RDBMS.
You can have either SELECT DISTINCT <columns> or SELECT <columns> (which actually defaults to SELECT ALL <columns>.) You can't apply DISTINCT to a specific column.
So, the:
SELECT a.pk_i_id ,a.i_price, b.s_title, DISTINCT c.pk_i_id ...
is invalid SQL.

Unknown column 'wp_cons_users.id' in 'on clause'

I have three table one is for users and other one is for subject and third one contain user_id, subject_id foreign keys.
I get unknow coloumn when I run the following sql.
SELECT wp_cons_users.first_name, wp_cons_subject.subject, wp_cons_skilllist.skill_level
FROM `wp_cons_subject`
JOIN wp_cons_skilllist ON wp_cons_skilllist.user_id = wp_cons_users.id
JOIN wp_cons_users ON wp_cons_users.id = wp_cons_skilllist.user_id
WHERE wp_cons_subject.id = '1'
ORDER BY `wp_cons_skilllist`.`skill_level` DESC
I can't find the error with this query.
wp_cons_skilllist
column link to
id (primay)
user_id wp_cons_users -> id
subj_id wp_cons_subject -> id
skill_level
Here I try to get the username, skill level and subject for any given subject id.
Looks like your main problem is with the ordering of your JOINs. In your first join, you are matching with wp_cons_users.id, but you don't join that table until later in the query. If you re-order the joins it should work better. Also, based on your table description, it seems that you will also need to join on subject_id. This query should help:
SELECT wp_cons_users.first_name
, wp_cons_subject.subject
, wp_cons_skilllist.skill_level
FROM wp_cons_users
JOIN `wp_cons_subject`
ON wp_cons_users.id=`wp_cons_subject`.user_id
AND wp_cons_subject.id = '1'
JOIN
wp_cons_skilllist
ON wp_cons_skilllist.user_id = wp_cons_users.id
AND wp_cons_skilllist.subject_id = `wp_cons_subject`.id
ORDER BY `wp_cons_skilllist`.`skill_level` DESC
I am guessing about the field names that weren't in your original query, so you may have to make some changes if they're different from what I'm assuming.
Without information about your attributes in your table, I'm afraid we can only assume that there is no ID column in your wp_cons_users table.
when I corrected the query to following it started to work.
SELECT wp_cons_users.first_name, wp_cons_subject.subject, wp_cons_skilllist.skill_level
FROM `wp_cons_skilllist`
JOIN wp_cons_subject ON wp_cons_subject.id = wp_cons_skilllist.subject_id
JOIN wp_cons_users ON wp_cons_users.id = wp_cons_skilllist.user_id
WHERE wp_cons_skilllist.subject_id = '1'
ORDER BY `wp_cons_skilllist`.`skill_level` DESC
LIMIT 0 , 30