SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Display two fields of two UNRELATED tables WITHOUT crossjoin


Display two fields of two UNRELATED tables WITHOUT crossjoin

Author
Message
dwain.c
dwain.c
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17873 Visits: 6431
Lynn Pettis (2/5/2013)
Biggest unanswered question asked so far is why is this needed? Maybe I missed it but I haven't seen a real viable answer.


masoudk1990 (2/5/2013)
Im trying to write this query for my personal fun and training.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)SSC Guru (95K reputation)

Group: General Forum Members
Points: 95197 Visits: 38967
dwain.c (2/5/2013)
Lynn Pettis (2/5/2013)
Biggest unanswered question asked so far is why is this needed? Maybe I missed it but I haven't seen a real viable answer.


masoudk1990 (2/5/2013)
Im trying to write this query for my personal fun and training.


Like I said...

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
dgowrij
dgowrij
SSC Journeyman
SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)

Group: General Forum Members
Points: 82 Visits: 75
Hi Craig,

Thank you for the explanation. I understood (The dt* are alias name for the derived queries and the q* and n are the alias name for the columns) how it works now.

Thank You!!!
Evil Kraig F
Evil Kraig F
SSC-Insane
SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)

Group: General Forum Members
Points: 20681 Visits: 7660
dgowrij (2/5/2013)
Hi Craig,

Thank you for the explanation. I understood (The dt* are alias name for the derived queries and the q* and n are the alias name for the columns) how it works now.

Thank You!!!


My pleasure. Been a very busy two weeks here with a massive rollout so I'd lost track of some things and am digging through my post history.

I'm glad that was able to help.


- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
david.s.patterson
david.s.patterson
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 3
GilaMonster - Tuesday, February 5, 2013 12:41 PM
In this particular case, since you want the two counts...
SELECT q1, q2 FROM(SELECT Count(a.PK) as q1 FROM Table1 as a) t1, (SELECT Count(b.PK) as q2 FROM Table2 as b) t2

Only in this case though. In general the question can't be answered. How can you meaningfully join two completely unrelated tables and have the columns related to each other? If they are completely unrelated, then they probably shouldn't even be in the same query

Thank you. Actually this was exactly what I needed to do. In my particular case, I simply needed to return two column summations from Table 1, and the row count from Table 2. The values are all related in a business sense, but not in any direct way in the schema.

 SELECT q1, q2, q3 FROM (SELECT SUM(t.ext_reg_deny) as q1, SUM(t.ext_reg_err) as q2 FROM traffic t) t1, (SELECT COUNT(r.id) as q3 FROM registration r) t2; 

ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41609 Visits: 20005
david.s.patterson - Tuesday, March 28, 2017 6:31 AM
GilaMonster - Tuesday, February 5, 2013 12:41 PM
In this particular case, since you want the two counts...
SELECT q1, q2 FROM(SELECT Count(a.PK) as q1 FROM Table1 as a) t1, (SELECT Count(b.PK) as q2 FROM Table2 as b) t2

Only in this case though. In general the question can't be answered. How can you meaningfully join two completely unrelated tables and have the columns related to each other? If they are completely unrelated, then they probably shouldn't even be in the same query

Thank you. Actually this was exactly what I needed to do. In my particular case, I simply needed to return two column summations from Table 1, and the row count from Table 2. The values are all related in a business sense, but not in any direct way in the schema.

 SELECT q1, q2, q3 FROM (SELECT SUM(t.ext_reg_deny) as q1, SUM(t.ext_reg_err) as q2 FROM traffic t) t1, (SELECT COUNT(r.id) as q3 FROM registration r) t2; 

There are numerous options at your disposal:

SELECT

q1 = SUM(t.ext_reg_deny),

q2 = SUM(t.ext_reg_err),

q3 = MAX(SELECT COUNT(r.id) as q3 FROM registration r)

FROM traffic t;



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
david.s.patterson
david.s.patterson
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 3
ChrisM@Work - Tuesday, March 28, 2017 8:55 AM
david.s.patterson - Tuesday, March 28, 2017 6:31 AM
GilaMonster - Tuesday, February 5, 2013 12:41 PM
In this particular case, since you want the two counts...
SELECT q1, q2 FROM(SELECT Count(a.PK) as q1 FROM Table1 as a) t1, (SELECT Count(b.PK) as q2 FROM Table2 as b) t2

Only in this case though. In general the question can't be answered. How can you meaningfully join two completely unrelated tables and have the columns related to each other? If they are completely unrelated, then they probably shouldn't even be in the same query

Thank you. Actually this was exactly what I needed to do. In my particular case, I simply needed to return two column summations from Table 1, and the row count from Table 2. The values are all related in a business sense, but not in any direct way in the schema.

 SELECT q1, q2, q3 FROM (SELECT SUM(t.ext_reg_deny) as q1, SUM(t.ext_reg_err) as q2 FROM traffic t) t1, (SELECT COUNT(r.id) as q3 FROM registration r) t2; 

There are numerous options at your disposal:

SELECT

q1 = SUM(t.ext_reg_deny),

q2 = SUM(t.ext_reg_err),

q3 = MAX(SELECT COUNT(r.id) as q3 FROM registration r)

FROM traffic t;


Hmmm. I could not get this to work in MySQL. But the following tweaked version did:
SELECT  SUM(t.ext_reg_deny) as q1,  SUM(t.ext_reg_err) as q2,  (SELECT COUNT(r.id) FROM registration r) as q3 FROM traffic t;

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search