473,379 Members | 1,174 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Problem with a complex SQL Query for SQL Server

Hi,

I want to do something like this -

Get Wind_direction, Wind_speed, SiteCode, Latitude, Longitude side by side-

The problem is that (DataValue as wind_direction)Wind_direction and (DataValue as)wind_speed are under the same column DataValue and for Wind_direction variableID is 4 and for Wind_speed is 3

the schema can be found here (Fixed to show instead - NeoPa)



And here's the query I wrote-

Expand|Select|Wrap|Line Numbers
  1. SELECT TOP(61) DataValues.DataValue AS Wind_speed, DataValues.DataValue AS Wind_direction, Sites.SiteCode, Sites.Latitude, Sites.Longitude
  2. FROM DataValues INNER JOIN Sites ON DataValues.SiteID = Sites.SiteID
  3. WHERE DataValues.DataValue IN (Select DataValue FROM DataValues WHERE DataValues.VariableID=3)
  4. OR DataValues.DataValue IN (SELECT DataValue FROM DataValues WHERE DataValues.VariableID = 4) 
  5. ORDER BY LocalDateTime DESC;
Thanks!

Piyush
Nov 17 '11 #1
8 4923
Rabbit
12,516 Expert Mod 8TB
You can use either a pivot (to pivot the 3/4 across the top) or join 2 queries to each other (one for 3 and the other for 4).
Nov 17 '11 #2
Sorry, didn't get you, I don't know how to use pivot but tried to join two qierues but it didn't work,

Here's my failed query-

Select speed.DataValue AS 'Wind_speed', direction.DataValue AS 'Wind_direction', Sites.Latitude, Sites.Longitude, Sites.SiteCode
FROM DataValues AS speed
INNER JOIN DataValues AS direction ON speed.SiteID = direction.SiteID
INNER JOIN Sites ON speed.SiteID = Sites.SiteID
WHERE (speed.VariableID = 3 AND direction.VariableID = 4)
HAVING COUNT(*) = 61
ORDER BY speed.LocalDateTime DESC;

Error:
Msg 8120, Level 16, State 1, Line 2
Column 'DataValues.DataValue' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Nov 18 '11 #3
Rabbit
12,516 Expert Mod 8TB
Why did you change your query into an aggregate query when your original was not? I didn't mention using an aggregate query so I don't know why you did that.

Also, what you need to do it take your original query, duplicate it, make one filter for 3, the other filter for 4, and then join them together.
Nov 18 '11 #4
It's not an aggregate query... I was just trying random stuff...

my two queries are

SELECT TOP(61) DataValue as 'Wind_speed', Sites.SiteCode, Sites.Longitude, Sites.Latitude, DataValues.VariableID, DataValues.SiteID
FROM DataValues INNER JOIN Sites ON DataValues.SiteID = Sites.SiteID
WHERE DataValues.VariableID = 3

SELECT TOP(61) DataValue as 'Wind_direction', Sites.SiteCode, Sites.Longitude, Sites.Latitude, DataValues.VariableID, DataValues.SiteID
FROM DataValues INNER JOIN Sites ON DataValues.SiteID = Sites.SiteID
WHERE DataValues.VariableID = 4
Could you tell me how to join these two...? Remember that Wind_speed and Wind_direction come from data value and I want to keep these two separate..

Thanks,
Nov 18 '11 #5
Rabbit
12,516 Expert Mod 8TB
Now you join your two queries on whatever you're using as your id.
Nov 18 '11 #6
Sorry I forgot to mention that there are multiple records with silar data I want the unique data(With Latest LocalDataTime) for each site -

I just executed the following query - SELECT TOP(61) S.DataValue AS 'Wind_speed',
D.DataValue AS 'Wind_direction',
Sites.SiteCode,
Sites.Longitude,
Sites.Latitude
FROM DataValues S JOIN DataValues D ON S.SiteID = D.SiteID
INNER JOIN Sites ON S.SiteID = D.SiteID
WHERE S.VariableID = 3 and D.VariableId = 4
ORDER BY S.LocalDateTime;



And got the follwing result which is not what I want -

17.25 340 KCKP -95.556 42.732
17.25 340 KICL -95.03 40.72
17.25 340 KCAV -93.77 42.75
17.25 340 KCBF -95.77 41.27
17.25 340 KCSQ -94.37 41.02

It does give me the unique sites but does not give me the latest values for each.. and is giving me the same value for wind direction and wind speed...


Thanks for your patience..
Nov 18 '11 #7
Rabbit
12,516 Expert Mod 8TB
You need to join on the fields that uniquely identify each record, not the variable id.
Nov 18 '11 #8
ck9663
2,878 Expert 2GB
Can you post some sample record on just the columns you need and your desired ouput?

~~ CK
Nov 18 '11 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: awarsd | last post by:
------=_NextPart_000_0007_01C34C8B.2CF5D7A0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi, I'm looking at some search engine where we can have...
0
by: schan | last post by:
Hi there, I was wondering if someone could shed some light on a problem I have no idea on how to fix. I created an Excel Add-In that uses an ADO connection to an Access database on a file...
2
manoj9849967222
by: manoj9849967222 | last post by:
Hi All I have a problem with query. I have designed a simple query which sould give me itemwise total qty sales during a given period. the fields in the query are startdate,...
3
blyxx86
by: blyxx86 | last post by:
Hey there, I'm running into a slight problem today... I have a few things to show... I'm running into duplicate values being shown in my query, but I cannot use a "Select DISTINCT" as it...
5
by: binky | last post by:
Question for all you SQL gurus out there. I have a (seemingly) complex query to write, and since I'm just learning SQL server, am not sure how to go about it. Tables: t_trans t_account All...
1
by: Rudolf Bargholz | last post by:
Hi, We have created triggers to log modifications to tables in our application. The triggers work fine, just on one of the tables in our database the triggers fail with the error message...
0
crystal2005
by: crystal2005 | last post by:
Hi, I am having trouble with some complex SQL queries. I’ve got winestore database, taken from Web Database Application with PHP and MySQL book. And some question about queries as the following ...
2
BeemerBiker
by: BeemerBiker | last post by:
I put together a (what I consider) complex query using the Access wizard. It works fine in access but fails when I code it up. I can actually make it work in code by "simplifying" it, but then it...
3
by: william67 | last post by:
I'm having a hard time building a query to do what I need to do and was hoping some genius could help me out, I need to do a complex query and any and all help is much appreciated this is the...
0
by: Kozy | last post by:
Hello everyone, i have a big problem with imports. I have only see this problem on 9.5, i have never goten it on 9.1. 99% of time db2 works great ( version 9.5 fixpack 5 ) on windows 2008 (...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.