Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 707593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:17:43+00:00 2026-05-14T04:17:43+00:00

I have this query: SELECT COUNT(articles.id) AS count FROM articles, xml_documents, streams WHERE articles.xml_document_id

  • 0

I have this query:

SELECT COUNT(articles.id) AS count 
FROM articles, xml_documents, streams 
WHERE articles.xml_document_id = xml_documents.id 
AND xml_documents.stream_id = streams.id
AND articles.published_at BETWEEN '2010-01-01' AND '2010-04-01'
AND streams.brand_id = 7

Which just uses the default equajoin by specifying three tables in csv format in the FROM clause.. What I need to do is group this by a value found within articles.source (raw xml).. so it could turn into this:

SELECT COUNT(articles.id) AS count, ExtractValue(articles.source, "/article/media_type") AS media_type
FROM articles, xml_documents, streams 
WHERE articles.xml_document_id = xml_documents.id 
AND xml_documents.stream_id = streams.id
AND articles.published_at BETWEEN '2010-01-01' AND '2010-04-01'
AND streams.brand_id = 7
GROUP BY media_type

which works fine, the problem is, I’m using rails, and using STI for the xml_documents table. The articles.source that is provided to the ExtractValue method will be of a couple different formats.. So what I need to be able to do is use “/article/media_type” IF xml_documents.type = ‘source one’ and use “/article/source” if xml_documents.type = ‘source two’

This is just because the two document types format their XML differently, but I don’t want to have to run multiple queries to retrieve this information..

It would be nice if one could use a ternary operator, but i don’t think this is possible..

EDIT
At this Point I am looking at making a temp table, or simply using UNION to place multiple result sets together..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:17:44+00:00Added an answer on May 14, 2026 at 4:17 am

    What about something like this:

    SELECT COUNT(articles.id) AS count,
           ExtractValue(articles.source,
               CASE xml_documents.type WHEN 'source one' then '/article/media_type'
                                       WHEN 'source two' then '/article/source'
                                       ELSE 'error'
               END),
    FROM ...
    

    EDIT
    Try this:

    SELECT COUNT(articles.id) AS count,
           CASE xml_documents.type WHEN 'source one' THEN ExtractValue(articles.source, '/article/media_type')
                                   WHEN 'source two' THEN ExtractValue(articles.source, '/article/source')
                                   ELSE NULL
           END as media_type,
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this simple query : SELECT YEAR(P.DateCreated) ,MONTH(P.DateCreated) ,COUNT(*) AS cnt FROM tbl1,
I have a mysql query like this: $topicreplycount = mysql_query('SELECT COUNT(DISTINCT post_msg_id) FROM phpbb_attachments
I have a query like this: SELECT COUNT(*) AS amount FROM daily_individual_tracking WHERE sales
I have an sql query like this: SELECT IF( (SELECT COUNT(*) FROM `test_table2` WHERE
I have a very simply query like this: SELECT users.id, COUNT(others.id) as c FROM
so suppose I have this query SELECT COUNT(*) FROM (SELECT * FROM k JOIN
I have this query: select count(id) AS num,date_id from table where FROM_UNIXTIME(date_id,'%d-%m-%Y')='17-08-2009' order by
I have this query: select count (convert(varchar(50), TmpDate, 103 )),convert(varchar(50), TmpDate, 103 ) from
I have this query: SELECT count(member_id) FROM member_favorite_business WHERE business_id=3912 AND member_id=413 When I
So far I have this query SELECT COUNT(f.code_id) as item_count, f.code_desc FROM foo f

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.