Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Better query to accomplish this?

$
0
0
I've run into something that's a bit more advanced than I am, and the only way I know how to accomplish the desired result is through a sort of "brute force" query. Can someone help me figure out how to do this more elegantly, and more importantly, with better performance?Here's the format of my table.[img]https://pfqmag.bl3302.livefilestore.com/y3mTZro1o9RPljqlzi0PUVGsdvpSDlMVmvk0AYctguBtc52iQKXJocIdyqGxfRmxtn4RQLjXH5WTU-ok8IiuBbBrE10xQzvI0np2jAkwjqYQWbeC-TR6F-mMkl2G0Qm1OYtUfLWE14ZzVCY8P8aHM__6rqa7FO6jPRph6J-gUEUBqg?width=274&height=380&cropmode=none[/img]Here's the format that I'd like to return for a report.[img]https://pfsljq.bl3302.livefilestore.com/y3mjI8Qxq1LZ2biFvSieYYrgO7vKD1tBAw913FBLe72R8FcGGbGkb8DpB0x3k5BgGqeIQrMvvfNfuooEPPUF_gKwOV4PExRsqnSQBstzvuIc9By9HWi6-yE7r0D7MgT42Xy4VAl8NpScE0GnrHlTOl3ucRLEfoRVpPH6QkM1kwNPFg?width=716&height=59&cropmode=none[/img]The query I'm using to accomplish this is below. It's ridiculously slow.[code="sql"]SELECT r1.record_date as [Date], r1.hour as [Hour], r1.kwh as [R1], r2.kwh as [R2], r3.kwh as [R3], w1.kwh as [W1], w2.kwh as [W2], w3.kwh as [W3], v1.kwh as [V1], v2.kwh as [V2], v3.kwh as [V3] FROM totals r1INNER JOIN totals r2 ON r1.record_date = r2.record_date and r1.hour = r2.hourINNER JOIN totals r3 ON r1.record_date = r3.record_date and r1.hour = r3.hourINNER JOIN totals w1 ON r1.record_date = w1.record_date and r1.hour = w1.hourINNER JOIN totals w2 ON r1.record_date = w2.record_date and r1.hour = w2.hourINNER JOIN totals w3 ON r1.record_date = w3.record_date and r1.hour = w3.hourINNER JOIN totals v1 ON r1.record_date = v1.record_date and r1.hour = v1.hourINNER JOIN totals v2 ON r1.record_date = v2.record_date and r1.hour = v2.hourINNER JOIN totals v3 ON r1.record_date = v3.record_date and r1.hour = v3.hourWHERE r1.feeder='r1' and r2.feeder='r2' and r3.feeder='r3' and w1.feeder='w1' and w2.feeder='w2' and w3.feeder='w3' and v1.feeder='v1' and v2.feeder='v2' and v3.feeder='v3'ORDER BY r1.record_date, r1.hour[/code]There must be a better way. Any thoughts?Thanks!

Viewing all articles
Browse latest Browse all 3145

Trending Articles