I have 2 query which I want to make it as 1 query. Could anyone help me. Because the query is just the same. The difference is they have 2 condition which 1 query for same year and another query for different year.

If (bboard = "All") Then
            If (qter1 > "1" And qter2 > "1") Then
                strSQL = " SELECT para1, para2, para3, para_range1,para_range2,para_range3,para_range4, e.stock_code AS current, current_name,b.board_code, board_name, sector_name, sub_sector_name, e.year_id,e.period_id, e.alert_id, e.alert_value, h.shareholders_equity AS curse, i.shareholders_equity AS prevse, i.stock_code AS previousy,i.period_id AS q1,i.year_id AS y1, j.stock_code AS previousy1,j.period_id AS q2,j.period_id AS y2,  h.net_cash_operation AS cur_ocf, i.net_cash_operation AS prev_ocf, h.net_attributable AS cur_na, i.net_attributable AS prev_na, j.net_attributable AS prev_na1,h.paid_up_capital AS curpu, i.paid_up_capital AS prevpu  "
                strSQL = strSQL & " FROM qiddb.company_info a  "
                strSQL = strSQL & " LEFT JOIN ref_board b ON (b.board_code=a.board_code) "
                strSQL = strSQL & " LEFT JOIN ref_sector c ON (c.sector_code=a.sector_code) "
                strSQL = strSQL & " LEFT JOIN ref_sub_sector d ON (d.sub_sector_code=a.sub_sector_code) "
                strSQL = strSQL & " LEFT JOIN company_alert e ON (e.stock_code=a.stock_code) "
                strSQL = strSQL & " LEFT JOIN ref_financial_year f ON (f.year_id=e.year_id) "
                strSQL = strSQL & " LEFT JOIN ref_alert_parameter g ON (g.alert_id = e.alert_id) "
                strSQL = strSQL & " LEFT JOIN company_financial h ON (h.stock_code=e.stock_code AND h.year_id=e.year_id)  "
                strSQL = strSQL & " LEFT JOIN company_financial i ON (i.stock_code=h.stock_code AND i.period_id=h.period_id-1 AND i.year_id=h.year_id)  "
                strSQL = strSQL & " LEFT JOIN company_financial j ON (j.stock_code=i.stock_code AND j.period_id=i.period_id-1 AND j.year_id=i.year_id) "
                strSQL = strSQL & " e.year_id BETWEEN '" & byear1 & "' AND '" & byear2 & "' AND e.period_id BETWEEN '" & qter1 & "' AND '" & qter2 & "' "
                strSQL = strSQL & " ORDER BY e.year_id, a.stock_code "

            ElseIf (qter1 = "1" And qter2 = "1") Then

                strSQL = " SELECT para1, para2, para3, para_range1,para_range2,para_range3,para_range4, e.stock_code AS current, current_name,b.board_code, board_name, sector_name, sub_sector_name, e.year_id,e.period_id, e.alert_id, e.alert_value, h.shareholders_equity AS curse, i.shareholders_equity AS prevse, i.stock_code AS previousy,i.period_id AS q1,i.year_id AS y1, j.stock_code AS previousy1,j.period_id AS q2,j.period_id AS y2,  h.net_cash_operation AS cur_ocf, i.net_cash_operation AS prev_ocf, h.net_attributable AS cur_na, i.net_attributable AS prev_na, j.net_attributable AS prev_na1,h.paid_up_capital AS curpu, i.paid_up_capital AS prevpu  "
                strSQL = strSQL & " FROM qiddb.company_info a  "
                strSQL = strSQL & " LEFT JOIN ref_board b ON (b.board_code=a.board_code) "
                strSQL = strSQL & " LEFT JOIN ref_sector c ON (c.sector_code=a.sector_code) "
                strSQL = strSQL & " LEFT JOIN ref_sub_sector d ON (d.sub_sector_code=a.sub_sector_code) "
                strSQL = strSQL & " LEFT JOIN company_alert e ON (e.stock_code=a.stock_code) "
                strSQL = strSQL & " LEFT JOIN ref_financial_year f ON (f.year_id=e.year_id) "
                strSQL = strSQL & " LEFT JOIN ref_alert_parameter g ON (g.alert_id = e.alert_id) "
                strSQL = strSQL & " LEFT JOIN company_financial h ON (h.stock_code=e.stock_code AND h.year_id=e.year_id) "
                strSQL = strSQL & " LEFT JOIN company_financial i ON (i.stock_code=h.stock_code AND i.period_id=h.period_id-1 AND i.year_id=h.year_id-1)  "
                strSQL = strSQL & " LEFT JOIN company_financial j ON (j.stock_code=i.stock_code AND j.period_id=i.period_id AND j.year_id=i.year_id-1) "
                strSQL = strSQL & " e.year_id BETWEEN '" & byear1 & "' AND '" & byear2 & "' AND e.period_id BETWEEN '" & qter1 & "' AND '" & qter2 & "' "
                strSQL = strSQL & " ORDER BY e.year_id, a.stock_code "

            End If

            cmd = New MySqlCommand(strSQL, conn)
            cmd.CommandTimeout = 0

            dr = cmd.ExecuteReader()

            '*** BindData to Repeater ***'  
            repeaterAlert.DataSource = dr
            repeaterAlert.DataBind()

            dr.Close()
            dr = Nothing

        End If

is it possible for me to put in one query?

Recommended Answers

All 2 Replies

lol, I like going back to unanswered posts. by the way yes you can achieve it by using Union between your two queries. under one condition. both queries must return same number of columns.

SELECT ..... your query1
UNION
SELECT ..... your query2

Well good luck and hope this helps

That condition is easily achieved if you add NULL or '' for the missing columns in the query with the fewer columns

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.