# Any SQL nerds here?

**URL:** <https://forums.speedlife.net/t/any-sql-nerds-here/271130>\
**Category:** NYSpeed Off Topic\
**Created:** [November 20, 2014, 12:25pm UTC](https://forums.speedlife.net/t/any-sql-nerds-here/271130 "2014-11-20T12:25:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![boxxa](https://yyz2.discourse-cdn.com/flex034/user_avatar/forums.speedlife.net/boxxa/32/5045_2.png) [@boxxa](https://forums.speedlife.net/u/boxxa)\
**Post date:** [November 20, 2014, 12:25pm UTC](https://forums.speedlife.net/t/any-sql-nerds-here/271130/1 "2014-11-20T12:25:32Z")

</div>

I have a table with 4 columns all INTs

ID (auto increment)  
TIMESTAMP (the same for all 6 elementids)  
ELEMENTID (one of 6 values)  
COUNT (0-400)

To pull my data, I am running multiple queries but trying to clean it up. I want to combine values in my parent query and getting stuck.

SELECT timestamp,count FROM stats WHERE elementid=10 ORDER BY timestamp DESC LIMIT 12  
SELECT timestamp,count FROM stats WHERE elementid=11 ORDER BY timestamp DESC LIMIT 12

In this query, I would like to turn count which selects the value for the row into one query so essential my SELECT returns the timestamp and then the sum of two SELECTS as total\_count for element=10 and element=11

---

<div class="post-metadata">

**Author:** ![JustinH](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@JustinH](https://forums.speedlife.net/u/JustinH)\
**Post date:** [November 21, 2014, 4:56am UTC](https://forums.speedlife.net/t/any-sql-nerds-here/271130/2 "2014-11-21T04:56:52Z")

</div>

I will ask our DBA when I get to work.

---

<div class="post-metadata">

**Author:** ![boxxa](https://yyz2.discourse-cdn.com/flex034/user_avatar/forums.speedlife.net/boxxa/32/5045_2.png) [@boxxa](https://forums.speedlife.net/u/boxxa)\
**Post date:** [November 21, 2014, 5:48am UTC](https://forums.speedlife.net/t/any-sql-nerds-here/271130/3 "2014-11-21T05:48:28Z")

</div>

This has stumped a few people. I am throwing my head at it and my friend last night was trying to do it with JOIN but we ran out of time.

Here is a better example if that doesn’t make sense

ID TIMESTAMP ELEMENTID COUNT  
1 10000000 10 50  
2 10000000 11 100  
3 10000000 12 60  
4 10000000 13 90  
5 10000120 10 100  
6 10000120 11 100  
7 10000120 12 0  
8 10000120 13 50

My query I want to return is:

TIMESTAMP TOTAL\_COUNT (element ID 10 count + element ID 11 count grouped with the timestamp they match)  
10000000 150  
10000120 200

---

<div class="post-metadata">

**Author:** ![Matt\_Danger](https://yyz2.discourse-cdn.com/flex034/user_avatar/forums.speedlife.net/matt_danger/32/4981_2.png) [@Matt\_Danger](https://forums.speedlife.net/u/Matt_Danger)\
**Post date:** [November 21, 2014, 9:48am UTC](https://forums.speedlife.net/t/any-sql-nerds-here/271130/4 "2014-11-21T09:48:26Z")

</div>

Not 100% sure what you’re trying to do. This?

SELECT timestamp t, (  
SELECT sum(count) FROM stats WHERE elementid IN (10, 11) AND timestamp = t  
) as total\_count FROM stats ORDER BY timestamp DESC LIMIT 12

---

<div class="post-metadata">

**Author:** ![boxxa](https://yyz2.discourse-cdn.com/flex034/user_avatar/forums.speedlife.net/boxxa/32/5045_2.png) [@boxxa](https://forums.speedlife.net/u/boxxa)\
**Post date:** [November 21, 2014, 10:57am UTC](https://forums.speedlife.net/t/any-sql-nerds-here/271130/5 "2014-11-21T10:57:29Z")

</div>

Ah you put me right on the correct track. Thank you!

SELECT timestamp t, (  
SELECT sum(count) FROM stats WHERE elementid IN (10, 11) AND timestamp = t  
) as total\_count FROM stats WHERE trunk=10 OR trunk=11 GROUP BY timestamp ORDER BY timestamp DESC LIMIT 12

---

<div class="post-metadata">

**Author:** ![Matt\_Danger](https://yyz2.discourse-cdn.com/flex034/user_avatar/forums.speedlife.net/matt_danger/32/4981_2.png) [@Matt\_Danger](https://forums.speedlife.net/u/Matt_Danger)\
**Post date:** [November 21, 2014, 11:39am UTC](https://forums.speedlife.net/t/any-sql-nerds-here/271130/6 "2014-11-21T11:39:46Z")

</div>

:tup:

---

<div class="post-metadata">

**Author:** ![boxxa](https://yyz2.discourse-cdn.com/flex034/user_avatar/forums.speedlife.net/boxxa/32/5045_2.png) [@boxxa](https://forums.speedlife.net/u/boxxa)\
**Post date:** [November 24, 2014, 8:15am UTC](https://forums.speedlife.net/t/any-sql-nerds-here/271130/7 "2014-11-24T08:15:00Z")

</div>

The only thing this query is slow as hell on my server. Need to optimize it somehow or just beef up the query on the server.
