Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
In the past year or so, I have been spending most of time working with noSQL databases. That said, I have started a new job that works with a SQL database and SQL Server Management Studio (SSMS). Any suggestions that would improve readability and make the query more concise would be much appreciated.
SELECT DISTINCT
[db1].[id] as "Node ID",
[db2].[name] as "Node Name",
[db3].[name] as "ISP",
[db4].[name] as "City",
CASE
WHEN [db1].[object_type_id] = 17
THEN 'Client'
WHEN [db1].[synthetic_location].[object_type_id] = 5
THEN 'System'
WHEN [db1].[object_type_id] IS NULL
THEN 'System'
END AS Type
FROM
[db1].[synthetic_location]
JOIN
[db2].[machine] ON [db2].[synthetic_location_id] IS NULL
JOIN
[db3].[internet_service_provider] ON [db3].[id] = [db1].[internet_service_provider_id]
JOIN
[db4].[geography_city] ON [db4].[geography_city].[id] = [db1].[synthetic_location].[geography_city_id]
WHERE
[db2].[status_type_id] < 1
AND [db1].[flags] = 6
Your query looks fine and is perfectly readable with one exception;
You should explicitly state INNER JOIN instead of just JOIN as it makes the intention clearer.
You can use tools like SSMS Boost or SQL complete that will help you to format the queries and those tools have so many additional features that can save lot of your time
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
I have a bunch of Oracle SQL queries I'd like to prepare some visual model / diagrams for. For example, to show all of the tables, the joins, and the join conditions.
Does such a tool exist?
Yes, Oracle SQL Developer, and it's included with your license of Oracle Database...in other words, it's free.
Bonus, it's Java, so will run on Windows, OS X, and Linux.
Open a connection, this give you a SQL Worksheet.
Type your query, example:
select b.extra_column
,b.department_id
,b.department_name
,b.manager_id
,b.location_id
,c.employee_id
,c.first_name
,c.last_name
,c.email
,c.phone_number
,c.hire_date
,c.job_id
,c.salary
,c.commission_pct
,c.manager_id
,c.department_id
,a.location_id
,a.street_address
,a.postal_code
,a.city
,a.state_province
,a.country_id
from departments b
,locations a
,employees c
where a.location_id = b.location_id
and c.employee_id = b.manager_id
and b.department_id = c.department_id;
Click the Query Builder tab.
Voila.
Note there is a performance bug in current version, will be fixed for version 18.2. In other words, it will take a few moments to render the diagram for you today.
Also this, from the SQL text only with no need to create the tables: https://sqldep.com/
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have an Excel VBA workbook that generates a SQL statement in the form of a string. I pass this SQL into a recordset object and use the CopyFromRecordSet function to dump out the data into an Excel worksheet.
The above method gives me 67 records. If I take the exact SQL generated in VBA and paste into a new query within Access it provides 400 records.
SQL is the following:
SELECT tbJob.jobID,
tbTasks.tskName,
tbTaskCat.catName,
tbTasks.tskActivity,
tbJob.JobDueDate,
tbJob.jobCompletedDate,
tbJob.jobCreatedOn,
tbJobStatus.statusDes,
tbStaff.staffForename & ' ' & tbStaff.staffSurname AS Assignee
FROM tbJobStatus
INNER JOIN (tbStaff
INNER JOIN (tbTaskCat
INNER JOIN (tbTasks
INNER JOIN tbJob
ON tbTasks.tskID = tbJob.jobTaskID)
ON tbTaskCat.catID = tbTasks.tskCatID)
ON tbStaff.staffID = tbJob.jobAssignedToID)
ON tbJobStatus.statusID = tbJob.jobStatusID
WHERE tbJob.jobStatusID = 4
AND tbJobStatus.statusDes <> 'Deleted'
ORDER BY tbJob.jobID;
I am struggling to explain why the difference occurs.
Any help would be appreciated. I have searched around the web but couldn't find a solution, if you know of one please post the link.
Thanks in advance.
Stuart
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am trying to write an SQL code in MS Access 2010 as follows:-
select WOWPerformanceData_tbl.Style,
WOWPerformanceData_tbl.FY,
WOWPerformanceData_tbl.Month,
PrintPromotions.[Type of Offer],
PrintPromotions.Start,
PrintPromotions.End
from WOWPerformanceData_tbl
right join PrintPromtions
on WOWPerformanceData_tbl.Style=PrintPromotions.Style
where (WOWPerformanceData_tbl.Style=[Enter Style nr:]);
Upon running the code, Access returns an error in the join operation pointing to the fourth line and selects PrintPromotions
Any feedback will be appreciated..
Thank you.
Should that be PrintPromotions and not PrintPromtions ?
This is fine as the where clause is an evaluation on the entire set.
SELECT WOWPerformanceData_tbl.Style,
WOWPerformanceData_tbl.FY,
WOWPerformanceData_tbl.Month,
PrintPromotions.[Type of Offer],
PrintPromotions.Start,
PrintPromotions.End
from WOWPerformanceData_tbl
LEFT join PrintPromtions
on WOWPerformanceData_tbl.Style=PrintPromotions.Style
where (WOWPerformanceData_tbl.Style=[Enter Style nr:]);
This would exclude records from printPromotions that were not in wowPerformanceData_tbl, which negates the right join.:
SELECT WOWPerformanceData_tbl.Style,
WOWPerformanceData_tbl.FY,
WOWPerformanceData_tbl.Month,
PrintPromotions.[Type of Offer],
PrintPromotions.Start,
PrintPromotions.End
from WOWPerformanceData_tbl
right join PrintPromtions
on WOWPerformanceData_tbl.Style=PrintPromotions.Style
where (WOWPerformanceData_tbl.Style=[Enter Style nr:]);
This is how you would do it to keep all records from PrintPromotions and those that matched in wowperofrmanceData_tbl.
SELECT WOWPerformanceData_tbl.Style,
WOWPerformanceData_tbl.FY,
WOWPerformanceData_tbl.Month,
PrintPromotions.[Type of Offer],
PrintPromotions.Start,
PrintPromotions.End
from WOWPerformanceData_tbl
right join PrintPromtions
on WOWPerformanceData_tbl.Style=PrintPromotions.Style
AND (WOWPerformanceData_tbl.Style=[Enter Style nr:]);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I am new to databases. And our teacher gave us pretty hard assignment. There are two tables. First table nickname is abilities(of superhero's:) ) and second table name superheros.
We have to select nick of Superhero and his average(medial) range for those who has two abilities?
Image of both tables:
Original here: http://postimg.org/image/85pqbc47n/
I will not give you solution - after all, it's homework and you have to learn something :) But I can give you an advice - try to do one task at a time
first, find those superheroes who has only 2 abilities (actually, you can do this by quering only table with abilities)
second - try to find average range of abilities for all superheroes (here you'll need join)
combine your queries
take a look at join, group by, count and having
Don't feel bad if you can't write it at first attempt, your query is not super easy, but 'm sure you can do this.
You can use HAVING and AVG() for this:
SELECT s.NickName, AVG(a.Range)
FROM abilities a
JOIN superhero s
ON a.ID_SuperHero = s.ID_SuperHero
GROUP BY s.NickName
HAVING COUNT(DISTINCT a.Abilities > 1)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
I have this complicated SQL query for Oracle that I want to visualize in a diagram to make it understandable for my co-workers. I tried at http://snowflakejoins.com but it just chokes on it.
Has someone a better suggestion? I prefer a web-app on the internet and if not a desktop app for windows.
with
logs as (
select
l.job_id,
l.subjob,
sum(l.verwerkt) verwerkt,
sum(l.errors) errors,
max(l.datum) laatst
from
dinf_monitor_logs l,
dinf_monitor_jobs j
where
l.datum>sysdate-j.dagen
and j.job_id=l.job_id(+)
group by
l.job_id,
l.subjob
),
alllogs as (
select job_id, subjob, max(datum) laatst from dinf_monitor_logs group by job_id, subjob
)
select row_number() over(order by alllogs.job_id, alllogs.subjob) r,
alllogs.job_id,
alljobs.naam,
alllogs.subjob,
logs.verwerkt,
logs.errors,
alllogs.laatst datum,
alljobs.wikilink,
alljobs.loglink,
alljobs.contact,
case
when alllogs.laatst is null then 1
when round(sysdate-(alllogs.laatst+alljobs.dagen))<0 then 0
else round(sysdate-(alllogs.laatst+alljobs.dagen))
end overtijd,
case
when logs.errors-alljobs.max_errors>0 then 5
when logs.verwerkt-alljobs.min_verwerkt<0 then 7
when round(sysdate-(alllogs.laatst+alljobs.dagen))>0 then 3
else 11
end status
from logs, alllogs, (select job_id, naam, wikilink, loglink, contact, dagen, min_verwerkt, max_errors from dinf_monitor_jobs) alljobs
where
logs.job_id(+)=alllogs.job_id
and logs.subjob(+)=alllogs.subjob
and alllogs.job_id=alljobs.job_id
order by alllogs.job_id, alllogs.subjob
You can use the "Query Builder" tab of the Oracle's SQL Developer.
The result of your sample query will be:
Each of the sub queries are data sets, I would just make a plain English statement of what the query does, then describe the data sets and how they relate to one another in an entity-relationship manner, then show how the query satisfies the plain English statement. You can represent the E-R with any variety of tools.
Have found how to do it in Toad, which i prefer above Sql Developer.
Open the editorwindow, paste the sql, rightclick in the editorwindow and select "Send to queryviewer"
My sql above is too complicated to use this technique but it's nice to know i can use it in the future with more "normal" queries.
Points to Sergio.