Displaying matching pairs & return - mysql

I am stumped on a question in my assignment.
On a single table (Condo_Unit), we have several columns - CondoID, UnitNum, SqrFt (Square Feet) etc.
I need to find a query that can display the UnitNum of any pair of Condos which have the same square footage. For example, Condos 305 & 409 both have square footage of 1500ft. The output must show both condos in a pair
At this stage, I can generate a list showing only one of the pair duplicated across two result columns (ie unit 305 is shown twice, not 305 | 409) using:
SELECT UnitNum, UnitNum
FROM condo_unit
GROUP BY SqrFt
HAVING Count(SqrFt) >1;
Sample data includes:
Condo ID | UnitNum | SqrFt
1 | 102 | 675
2 | 201 | 1030
3 | 305 | 1500
4 | 409 | 1500
5 | 104 | 1030
6 | 207 | 870
From this data, we can see units 201 & 104 are a matching pair, as well as 305 & 409
Results should show:
1st Unit | 2nd Unit
201 | 104
305 | 409
The current results I am getting are:
1st Unit | 2nd Unit
201 | 201
305 | 305
Is anyone able to assist, or need further clarification?

Query:
SELECT
DISTINCT least(t.c,t.d) as "1st Unit",
greatest(t.c,t.d) as "2nd Unit"
FROM
(SELECT a.UnitNum c,b.UnitNum d
FROM world.condo a JOIN world.condo b
WHERE a.SqrFt=b.SqrFt AND a.Condo_ID!=b.Condo_ID) t;
Output:

This code will help you
select GROUP_CONCAT(UnitNum,'&'),SqrFt from Condo_Unit group by SqrFt ORDER BY SqrFt

This should do.
select GROUP_CONCAT(UnitNum SEPARATOR '|') as UnitName from condo_unit
group by SqrFt HAVING Count(SqrFt) >1;
DEMO FOR ANSWER
OUTPUT :
+----------+
| UnitName |
+----------+
| 201|104 |
+----------+
| 305|409 |
+----------+
You can use whatever separator you want. I have given pipe symbol |.

Related

SQL Count Distinct Using Multiple Unique Identifiers

My company ran a series of TV ads and we're measuring the impact by changes in our website traffic. I would like to determine the cost per session we saw generated, based on the cost of each ad.
The trouble is, the table this is referencing has duplicate data, so my currently cost_per_session isn't counting right.
What I have so far:
client_net_cleared = cost of ad
ad_time, media_outlet, & program = combined are a unique identifier for each ad
diff = assumed sessions generated by ad
.
SELECT DISTINCT tadm.timestamp AS ad_time
, tadm.media_outlet AS media_outlet
, tadm.program AS program
, tadm.client_net_cleared AS client_net_cleared
, SUM(tadm.before_ad_sum) AS before_ad_sessions
, SUM(tadm.after_ad_sum) AS after_ad_sessions
, (SUM(tadm.after_ad_sum) - SUM(tadm.before_ad_sum)) AS diff
, CASE WHEN tadm.client_net_cleared = 0 THEN null
WHEN (SUM(tadm.after_ad_sum) - SUM(tadm.before_ad_sum)) <1 THEN null
ELSE (tadm.client_net_cleared/(SUM(tadm.after_ad_sum) - SUM(tadm.before_ad_sum)))
END AS cost_per_session
FROM tableau.km_tv_ad_data_merged tadm
GROUP BY ad_time,media_outlet,program,client_net_cleared
Sample data:
ad_time | media_outlet | program | client_net_cleared | before_ad_sessions | after_add_sessions | diff | cost_per_session
---------------------|---------------|----------------|--------------------|--------------------|--------------------|------|-----------------
2016-12-09 22:55:00 | DIY | | 970 | 55 | 72 | 17 | 57.05
2016-12-11 02:22:00 | E! | E! News | 388 | 25 | 31 | 6 | 64.66
2016-12-19 21:15:00 | Cooking | The Best Thing | 428 | 70 | 97 | 27 | 15.85
2016-12-22 14:01:00 | Oxygen | Next Top Model | 285 | 95 | 148 | 53 | 5.37
2016-12-09 22:55:00 | DIY | | 970 | 55 | 72 | 17 | 57.05
2016-12-04 16:13:00 | Headline News | United Shades | 1698 | 95 | 137 | 42 | 40.42
What I need:
Only count one instance of each ad when calculating cost_per_session.
EDIT: Fixed the query, had a half completed row where I was failing at doing this before asking the question. :)
Get rid of the DISTINCT in SELECT DISTINCT in the first line of your query. It makes no sense in a GROUP BY query.
If your rows are entirely duplicate, try deduplicating the table before you put it into the GROUP BY grinder by replacing
FROM tableau.km_tv_ad_data_merged tadm
with
FROM ( SELECT DISTINCT timestamp, media_outlet, program,
client_net_cleared,
before_ad_sum, after_ad_sum
FROM tableau.km_tv_ad_data_merged
) tadm

Get Number of A's in Result Table - MySQL

This is the case. In my school all classes prepare excel sheet for each class with marks for each subject in term end test. There are 17 classes. I combine them in to access table. Then again export all data in to excel. make csv file . And import to Mysql Database using phpmyadmin. now I have result table as follow.
| ID | Name | Religion | Sinhala | science | english | maths | History | Categery 1 | Categery 2 | Categery 3 | Total | Average | Rank | |
|---- |------- |---------- |--------- |--------- |--------- |------- |--------- |------------ |------------ |------------ |------- |--------- |------ |--- |
| 1 | manoj | 45 | 65 | 78 | 98 | 67 | 67 | 63 | 76 | 64 | 654 | 62 | 12 | |
Sectional Head Need to get number of students who got >75 for all Subject.
And Number of Student Who got >75 for 8 subject out of 9.
I need to retrieve number of A s, B s (marks >=75) from this table.
Ex. Student names and Number of A s
Total Number of A for all 9 subject - 45
Total Number of A for all 8 subject (any 8 subject ) - 45
Total Number of A for all 7 subject (any 7 subject ) - 45
I Tried following SQL Statement
SELECT COUNT(SELECT COUNT()
FROM result
WHERE religion >=75
AND Math >=75)
FROM result
I read about same scenario in stack overflow.
Access 2010
this one get some point. but I cant solve it for my scenario.
Use GROUP BY studentName and SUM(grade = 'A') AS numberOfAs.
[Quick answer bc question is quickly formatted]

MySQL, Determine value associated with MAX() of another value using GROUP BY [duplicate]

This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 6 years ago.
I have a MySQL database that contains the table, "message_route". This table tracks the the path between hubs a message from a device takes before it finds a modem and goes out to the internet.
"message_route" contains the following columns:
id, summary_id, device_id, hub_address, hop_count, event_time
Each row in the table represents a single "hop" between two hubs. The column "device_id" gives the id of the device the message originated from. The column "hub_address" gives the id of the hub the message hop was received by, and "hop_count" counts these hops incrementally. The full route of the message is bound together by the "summary_id" key. A snippet of the table to illustrate:
+-----+------------+-----------+-------------+-----------+---------------------+
| id | summary_id | device_id | hub_address | hop_count | event_time |
+-----+------------+-----------+-------------+-----------+---------------------+
| 180 | 158 | 1099 | 31527 | 1 | 2011-10-01 04:50:53 |
| 181 | 159 | 1676 | 51778 | 1 | 2011-10-01 00:12:04 |
| 182 | 159 | 1676 | 43567 | 2 | 2011-10-01 00:12:04 |
| 183 | 159 | 1676 | 33805 | 3 | 2011-10-01 00:12:04 |
| 184 | 160 | 2326 | 37575 | 1 | 2011-10-01 00:12:07 |
| 185 | 160 | 2326 | 48024 | 2 | 2011-10-01 00:12:07 |
| 186 | 160 | 2326 | 57652 | 3 | 2011-10-01 00:12:07 |
+-----+------------+-----------+-------------+-----------+---------------------+
There are three total messages here. The message with summary_id = 158 touched only one hub before finding a modem, so row with id = 180 is the entire record of that message. Summary_ids 159 and 160 each have 3 hops, each touching 3 different hubs. There is no upward limit of the number of hops a message can have.
I need to create a MySQL query that gives me a list of the unique "hub_address" values that constitute the last hop of a message. In other words, the hub_address associated with the maximum hop_count for each summary_id. With the database snippet above, the output should be "31527, 33805, 57652".
I have been unable to figure this out. In the meantime, I am using this code as a proxy, which only gives me the unique hub_address values for messages with a single hop, such as summary_id = 158.
SELECT DISTINCT(x.hub_address)
FROM (SELECT hub_address, COUNT(summary_id) AS freq
FROM message_route GROUP BY summary_id) AS x
WHERE x.freq = 1;
I would approach this as:
select distinct mr.hub_address
from message_route mr
where mr.event_time = (select max(mr2.event_time)
from message_route mr2
where mr2.summary_id = mr.summary_id
);

Select statement inside SSRS textbox

I am trying to assign values to textboxes in SSRS but am running into a bit of trouble.
My dataset "dsStepInfo" groups and sums a number of rows together based on "ID", query below.
SELECT ID, SUM(Target) AS Target, SUM(Actual) AS Actual
FROM tblStepComplete
WHERE (CompleteID = #ParamID)
GROUP BY ID
And returns:
ID Target Actual
-------------------------------
| 10 | 44418 | 44418 |
-------------------------------
| 12 | 13193 | 13123 |
-------------------------------
| 22 | 1411 | 1411 |
-------------------------------
| 50 | 160 | 80 |
-------------------------------
| 52 | 68 | 34 |
-------------------------------
| 101 | 12120 | 12119 |
-------------------------------
| 105 | 875 | 868 |
-------------------------------
| 140 | 40 | 40 |
-------------------------------
| 560 | 2985 | 3418 |
-------------------------------
I want to assign cells in the grid in the picture to a certain ID. The Cell for tank 110 would always be the Target of ID=50 and Actual of ID=50 for example. Many of the IDs returned aren't going to be populated in the table, just the 10 displayed in this table. Is there any way to perform a SELECT, or equivalent in the textbox as an expression to get these specific values from the dataset?
You can use the Lookup function.
This should get the ID #50 and output its Target:
=Lookup(50, Fields!ID.Value, Fields!Target.Value, "DSStepInfo")
I think if it were me I'd just use a Case statement, something like
SELECT
Case [ID]
WHEN 10 Then 'Tank 50'
WHEN 12 Then 'Tank 120'
When 22 Then 'Tank 130'
WHEN 50 Then 'Tank 140'
When 52 Then 'Tank 150'
End As TankNo
,SUM([Target]) As Target
,SUM([Actual]) As Actual
FROM tblStepComplete
WHERE (CompleteID = #ParamID)
Group By [ID]
And then build your report using the values presented. Better would be an additional table that had the Tank names linked to the IDs you are pulling, but if you don't have that then the Case statement is fairly easy to maintain.

sql - Finding cost between two graph points

I have a table that stores the time between two adjacent railway stations.
+------+------+------+
| s1id | s2id | tbtw |
+------+------+------+
| 234 | 235 | 20 |
| 235 | 133 | 8 |
| 133 | 108 | 15 |
| 234 | 236 | 10 |
| 108 | 500 | 2 |
| 235 | 108 | 21 |
+------+------+------+
I want to find the time between any two point, if they can be connected like finding the time from station 234 to 500 (234->235->108->500). I know this is like a graph. I have tried to find the cumulative distance with t2.s1id = t1.s2id as follows
select t1.* SUM(t2.tbtw) as sum
from t t1
join t t2 on t1.s2id = t2.s1id
group by t1.id, t1.tbtw
but this is not giving me cumulative time or doesn't properly link with the nodes
select t1.*
(select sum(tbtw)
from t t2
where t2.s1id = t1.s2.id
) as sum
from t t1;
I can easily do it in a programming language, but in sql, its really confusing. Do I have to use procedures? Can't I do in simple sql statements? The solution is preferred in simple sql statements, however other solutions are also okay. Please help me.
see article: http://hansolav.net/sql/graphs.html
i guess it is helpfull