sql joining as alias does not working - sql

i am new to sql joins.. i have project that is 5 year old . Now i have to deploy it some new server .so deploy it to other server there i am facing some sql problem. here is the qyery .
SELECT DISTINCT d.*,DATE_FORMAT(d.downloads_updated, '%c/%d/%Y') AS updated,DATE_FORMAT(d.downloads_created, '%c/%d/%Y') AS created, s2.strings_english as title, s2.strings_english as description
FROM strings s, downloads d,
products_has_downloads pd
inner JOIN strings s2 ON d.downloads_description = s.strings_id WHERE d.downloads_id = pd.downloads_id AND s.strings_id = d.downloads_title AND d.downloads_status = 'Live' AND d.downloads_level = 'Public'
ORDER BY d.downloads_updated DESC LIMIT 5
i am getting this error
1054 - Unknown column 'd.downloads_description' in 'on clause'
i have not written this sql query.it is working fine on old server .
I am new to join and database .please can any one help me .

You are mixing both implicit and explicit join syntax which will not work.
You have the following implicit syntax where the tables are joined by commas:
FROM strings s, downloads d, products_has_downloads pd
The JOIN syntax has a higher precedence to the comma syntax so the alias for downloads is not available in the ON clause.
Try using all of the same syntax. I changed your query to use only explicit JOIN syntax:
SELECT DISTINCT d.*,
DATE_FORMAT(d.downloads_updated, '%c/%d/%Y') AS updated,
DATE_FORMAT(d.downloads_created, '%c/%d/%Y') AS created,
s2.strings_english as title,
s2.strings_english as description
FROM strings s
INNER JOIN downloads d
ON d.downloads_description = s.strings_id
INNER JOIN products_has_downloads pd
on d.downloads_id = pd.downloads_id
inner JOIN strings s2
on s2.strings_id = d.downloads_title
WHERE d.downloads_status = 'Live'
AND d.downloads_level = 'Public'
ORDER BY d.downloads_updated DESC
LIMIT 5

Related

Join 3 tables using column common to all 3 tables

I am a total SQL novice, so please bear with me. I have three tables that are set up in the following fashion:
date|country|Test 1|Test 2|Test 3|etc.
The data in the date and country columns are identical across the three tables, and the differences are in the data in the Test columns. I'd like to use Join to query one date column and the three corresponding Test columns from the three tables.
I'm planning on just re-building the table so that the Test columns in the other tables are additional columns in the one table, but I'd still like to know how to use Join in this way. This is what I have at the moment, although it's throwing an error saying that there's an error in the syntax of the FROM clause. It's worth noting that I'm running this query in VBA using an Access DB.
SELECT r.CRDate, r.Test, p.Test, z.Test
FROM CountryRaw as r
INNER JOIN CountryPct as p ON p.CPctDate = r.CRDate
INNER JOIN CountryZ as z ON z.CZDate = p.CPctDate
WHERE r.Country = 'US' AND p.Country = 'US' AND z.Country = 'US'
I came across something using SELECT COALESCE(r.CRDate, p.CPctDate, z.CZDate) to start, but I didn't get anywhere with that.
MS Access requires extra parentheses. So try this:
SELECT r.CRDate, r.Test, p.Test, z.Test
FROM (CountryRaw as r INNER JOIN
CountryPct as p
ON p.CPctDate = r.CRDate
) INNER JOIN
CountryZ as z
ON z.CZDate = p.CPctDate
WHERE r.Country = 'US' AND p.Country = 'US' AND z.Country = 'US'

Group by concat in SQL Server

I’m using multiple joins for a specific logic , but encountered a problem . Some of the records has 1-2 relation in one of the tables which mess up my output. I want to concat all these string so it will appear it one record, but I don’t know how to do it in sql server . In oracle and MySQL it’s easy but I tried playing with online examples and failed miserably.
My query:
SELECT c.customerName,c.Guid,p.campaignTitle ,
(SELECT k.campaignTitle FROM [DEV_TEST2].[dbo].campaigns l JOIN [DEV_TEST2].[dbo].campaignstitle k on k.campaignname = l.campaignname where l.campaignid = t.referrerurl) as Referrertitle,
t.activitydate,t.type
FROM [DEV_TEST2].[dbo].campaignknowncustomers c
join [DEV_TEST2].[dbo].[CampaignCustomerMatch] t ON(c.guid = t.visitorexternalid)
join [DEV_TEST2].[dbo].campaigns s ON(t.url = s.campaignid)
join [DEV_TEST2].[dbo].campaignstitle p on(s.campaignname = p.campaignname)
order by customername,activitydate
My problem is with campaigntitle column and referrertitle correlated query. Both come from the same table. I need to concat it and show only 1 row per ‘customername, guid, activitydate’

How to get queries by a user group

I'd like to get all queries run by users in a specific user group for a Redshift database. Here's the query I'm running:
select
q.*,
u.usename,
swq.total_queue_time / 1000000 as queue_time
from stl_query q
inner join pg_user u on q.userid = u.usesysid
inner join pg_group g on u.usesysid = ANY (g.grolist)
inner join stl_wlm_query swq on q.query = swq.query
where q.userid <> 1
AND database = 'mydb'
AND g.groname = 'ops'
order by q.starttime desc;
However, I'm getting Column "g.grolist" has unsupported type "integer[]". I've tried other forms of the same query (e.g., putting the ANY condition in WHERE), but I keep getting the same error. How can I check whether a user ID occurs in the list of user IDs in pg_group?
I found this redshift guide having array types included in the unsupported list of PostgresSQL data types, and that's causing your SQL error of column has unsupported type.
Redshift is not exactly Postgres & thus doesn't have full feature support for all Postgres data types and functions. So refering redshift guide for any errors/differences in behaviour you encounter will be the best bet.
Find out more differences here: Amazon Redshift and PostgreSQL
For unsupported data types, it states:
If a query attempts to use an unsupported data type, including explicit or implicit casts, it will return an error.
However redshift does support some array & json-array function(s) which you can use to play with array column type. Like below query will output your desired result using array_to_string.
select
q.*,
u.usename,
swq.total_queue_time / 1000000 as queue_time
from stl_query q
inner join pg_user u on q.userid = u.usesysid
inner join pg_group g on array_to_string(g.grolist,',') like '%' || u.usesysid || '%'
inner join stl_wlm_query swq on q.query = swq.query
where q.userid <> 1
AND database = 'mydb'
AND g.groname = 'ops'
order by q.starttime desc;

MS Access INNER JOIN/LEFT JOIN problems

I have the following SQL string which tries to combine an INNER JOIN with a LEFT JOIN in the FROM section.
As you can see I use table VIP_APP_VIP_SCENARIO_DETAIL_LE to perform the query. When I use it against this table, Access give me an "Invalid Operation" error.
Interestingly, when I use the EXACT same query using the VIP_APP_VIP_SCENARIO_DETAIL_BUDGET or VIP_APP_VIP_SCENARIO_DETAIL_ACTUALS table, it performs flawlessly.
So why would it work on two tables but not the other? All fields are in all tables and the data types are correct.
As a side note: on the query with the error, if I change the LEFT JOIN to an INNER JOIN, it runs with no problem! I really need a LEFT JOIN though.
SELECT
D.MATERIAL_NUMBER,
D.MATERIAL_DESCRIPTION,
D.PRODUCTION_LOT_SIZE,
D.STANDARDS_NAME,
D.WORK_CENTER,
S.OP_SHORT_TEXT,
S.OPERATION_CODE,
D.LINE_SPEED_UPM,
D.PERCENT_STD,
D.EQUIPMENT_SU,
D.EQUIPMENT_CU,
D.OPERATOR_NUM,
V.COSTING_LOT_SIZE,
V.VOL_TOTAL_ADJ
FROM
([STDS_SCENARIO: TEST] AS D INNER JOIN MASTER_SUMMARY AS S ON
D.MATERIAL_NUMBER = S.MATERIAL_NUMBER AND D.WORK_CENTER = S.WORK_CENTER)
LEFT JOIN
(SELECT ITEM_CODE, COSTING_LOT_SIZE, VOL_TOTAL_ADJ
FROM
VIP_APP_VIP_SCENARIO_DETAIL_LE
WHERE SCENARIO_ID = 16968) AS V ON D.MATERIAL_NUMBER = V.ITEM_CODE
ORDER BY D.MATERIAL_NUMBER, D.STANDARDS_NAME, S.OPERATION_CODE;
tried to mock this up in SQL server with some tables of my own, but the structure seemed to work, this follows the pattern referenced above. (hopefully no syntax errors left here)
SELECT * FROM (
select
D.MATERIAL_NUMBER,
D.MATERIAL_DESCRIPTION,
D.PRODUCTION_LOT_SIZE,
D.STANDARDS_NAME,
D.WORK_CENTER,
S.OP_SHORT_TEXT,
S.OPERATION_CODE,
D.LINE_SPEED_UPM,
D.PERCENT_STD,
D.EQUIPMENT_SU,
D.EQUIPMENT_CU,
D.OPERATOR_NUM
FROM [STDS_SCENARIO: TEST] D
INNER JOIN MASTER_SUMMARY S
ON D.MATERIAL_NUMBER = S.MATERIAL_NUMBER AND D.WORK_CENTER = S.WORK_CENTER) AS J
LEFT JOIN
(SELECT ITEM_CODE, COSTING_LOT_SIZE, VOL_TOTAL_ADJ
FROM
VIP_APP_VIP_SCENARIO_DETAIL_LE
WHERE SCENARIO_ID = 16968) AS V ON J.MATERIAL_NUMBER = V.ITEM_CODE
ORDER BY J.MATERIAL_NUMBER, J.STANDARDS_NAME, J.OPERATION_CODE;
Had help from a friend and we discovered that it was a casting problem between a linked Oracle table and the Access table. To fix the problem we casted both sides of the linked fields to a string:
CSTR(D.[MATERIAL_NUMBER]) = CSTR(V.[ITEM_CODE])

SQL Reporting count of parameter in a column

I am working in SSRS 3.0 with a SQL table including the following fields:
ApptID BookedBy ConfirmedBy CancelledBy
I also have a parameter setup to select which users to filter by (matches data in the BookedBy, ConfirmedBy and CancelledBy columns) called #Scheduler (which is a multi vale parameter/array).
I need to get a count for booked, confirmed and scheduled for how many times any value in the Scheduler parameter shows up in that column.
Basically:
COUNT(BookedBy IN (#Scheduler)) AS BookedCount
Can anyone help me out with the syntax for doing this?
Try this
SELECT Count(BookedBy = #Scheduler) as [BookedCount],
Count(ConfirmedBy = #Scheduler) as [ConfirmedCount],
Count(CancelledBy = #Scheduler) as [CancelledCount]
FROM tablename
WHERE BookedBy = #Scheduler OR
ConfirmedBy = #Scheduler OR
CancelledBy = #Scheduler
NB - Not tested might contain typos
If your input is a list separated by commas you can convert that to a table. See a reference like this:
http://www.projectdmx.com/tsql/sqlarrays.aspx
For this use case I'd recommend one of the solutions that saves the result in a CTE (since you only need to convert your input once and this will be fastest)
Then you could use that table (called sTable with column name) like this:
SELECT Count(Bo.Name) as [BookedCount],
Count(Co.Name) as [ConfirmedCount],
Count(Ca.Name) as [CancelledCount]
FROM tablename
LEFT JOIN sTable Bo ON BookedBy = Bo.name
LEFT JOIN sTable Co ON ConfirmedBy = Co.name
LEFT JOIN sTable Ca ON CancelledBy = Ca.name
I guess this will work but it does not seem as nice as the others:
SELECT (SELECT COUNT(*) FROM table WHERE BookedBy in (#Scheduler)) AS [BookedCount],
(SELECT COUNT(*) FROM table WHERE ConfirmedBy in (#Scheduler)) as [ConfirmedCount],
(SELECT COUNT(*) FROM table WHERE CancelledBy in (#Scheduler)) as [CancelledCount]